请使用多种格式化输出方式编写程序输出下方语句: 时间:2020-02-27, 姓名:苏炳添 体重:75公斤, 跑步速度:11公里/小时, 跑步时间:20.0分钟, 运动距离:3.67公里, 燃烧卡路里:297.95千卡。
时间: 2023-04-12 13:01:49 浏览: 204
以下是使用不同的格式化输出方式编写的程序:
1. 使用printf函数输出:
#include <stdio.h>
int main() {
char name[] = "苏炳添";
int weight = 75;
float speed = 11.0;
float time = 20.0;
float distance = 3.67;
float calories = 297.95;
printf("时间:%s,姓名:%s,体重:%d公斤,跑步速度:%.1f公里/小时,跑步时间:%.1f分钟,运动距离:%.2f公里,燃烧卡路里:%.2f千卡。\n", "2020-02-27", name, weight, speed, time, distance, calories);
return 0;
}
2. 使用cout流输出:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
string name = "苏炳添";
int weight = 75;
float speed = 11.0;
float time = 20.0;
float distance = 3.67;
float calories = 297.95;
cout << "时间:" << "2020-02-27" << ",姓名:" << name << ",体重:" << weight << "公斤,跑步速度:" << fixed << setprecision(1) << speed << "公里/小时,跑步时间:" << fixed << setprecision(1) << time << "分钟,运动距离:" << fixed << setprecision(2) << distance << "公里,燃烧卡路里:" << fixed << setprecision(2) << calories << "千卡。" << endl;
return 0;
}
3. 使用stringstream流输出:
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
int main() {
string name = "苏炳添";
int weight = 75;
float speed = 11.0;
float time = 20.0;
float distance = 3.67;
float calories = 297.95;
stringstream ss;
ss << "时间:" << "2020-02-27" << ",姓名:" << name << ",体重:" << weight << "公斤,跑步速度:" << fixed << setprecision(1) << speed << "公里/小时,跑步时间:" << fixed << setprecision(1) << time << "分钟,运动距离:" << fixed << setprecision(2) << distance << "公里,燃烧卡路里:" << fixed << setprecision(2) << calories << "千卡。";
string output = ss.str();
cout << output << endl;
return 0;
}
阅读全文