如何使用 cout 格式化语句以正确的格式打印输入和输出?
How to use cout formatting statements to print the input and output in proper format?
我有一个二维数组,用于存储 X 组数学和化学的数字。下面是我如何为每个 class 获取输入并将它们存储在每个 class.
的二维数组中
Input for Maths class:
50 20 30 40 50
Input for Chemistry class:
90 70 20 10 40
- 现在根据上述每个输入 class 我需要计算“最佳”、“差”和“平均”数字。
- 此外,我还需要计算“数学”和“化学”每组(共 5 组)的平均值 class,然后在这里也得出总体平均值。
一旦我接受了上述数学和化学输入 class,我需要以下面的格式打印,其中将包含上面第 1 点和第 2 点的数据以及输入-
1 2 3 4 5
*****************************
Math 50.00 20.00 30.00 40.00 50.00
Chemistry 90.00 70.00 20.00 10.00 40.00
问题陈述
我能够完成上述所有事情并轻松计算第 1 点和第 2 点,但我无法弄清楚如何以上述格式打印输出,从而以正确格式的方式显示输入和输出。截至目前,我的程序在接受输入后将所有内容与不同行的输出分开打印 -
int main()
{
double val[2][5];
//.. val array being populated
return 0;
}
如何在上面的代码中使用 cout.setf(ios::fixed)
、cout.setf(ios::showpoint)
和 cout.precision(2)
以及 cout.width(4);
来获得我需要的格式?
这可能是一种方式:
auto w = std::setw(6); // for number like " 10.00" (6 chars)
auto wb = std::setw(8); // for the numbers with more space between them
auto sw = std::setw(11); // for titles (math, chemistry)
// print the numbers above
cout << " ";
for(int i=1; i<=5; ++i) std::cout << w << i;
std::cout << " BEST WORST AVERAGE\n";
// print a line of *
std::cout << sw << "" << std::string(56,'*') << '\n';
cout << std::setprecision(2) << std::fixed; // precision 2, fixed
cout << sw << std::left << "Math" << std::right;
for(auto ms : array[0]) std::cout << w << ms;
cout << wb << math_best << wb << math_worse << wb << math_average << '\n';
cout << sw << std::left << "Chemistry" << std::right;
for(auto cs : array[1]) std::cout << w << cs;
cout << wb << chemistry_best << wb << chemistry_worse << wb << chemistry_average << '\n';
输出
1 2 3 4 5 BEST WORST AVERAGE
********************************************************
Math 50.00 20.00 30.00 40.00 50.00 50.00 20.00 38.00
Chemistry 90.00 70.00 20.00 10.00 40.00 90.00 10.00 46.00
我有一个二维数组,用于存储 X 组数学和化学的数字。下面是我如何为每个 class 获取输入并将它们存储在每个 class.
的二维数组中Input for Maths class:
50 20 30 40 50
Input for Chemistry class:
90 70 20 10 40
- 现在根据上述每个输入 class 我需要计算“最佳”、“差”和“平均”数字。
- 此外,我还需要计算“数学”和“化学”每组(共 5 组)的平均值 class,然后在这里也得出总体平均值。
一旦我接受了上述数学和化学输入 class,我需要以下面的格式打印,其中将包含上面第 1 点和第 2 点的数据以及输入-
1 2 3 4 5
*****************************
Math 50.00 20.00 30.00 40.00 50.00
Chemistry 90.00 70.00 20.00 10.00 40.00
问题陈述
我能够完成上述所有事情并轻松计算第 1 点和第 2 点,但我无法弄清楚如何以上述格式打印输出,从而以正确格式的方式显示输入和输出。截至目前,我的程序在接受输入后将所有内容与不同行的输出分开打印 -
int main()
{
double val[2][5];
//.. val array being populated
return 0;
}
如何在上面的代码中使用 cout.setf(ios::fixed)
、cout.setf(ios::showpoint)
和 cout.precision(2)
以及 cout.width(4);
来获得我需要的格式?
这可能是一种方式:
auto w = std::setw(6); // for number like " 10.00" (6 chars)
auto wb = std::setw(8); // for the numbers with more space between them
auto sw = std::setw(11); // for titles (math, chemistry)
// print the numbers above
cout << " ";
for(int i=1; i<=5; ++i) std::cout << w << i;
std::cout << " BEST WORST AVERAGE\n";
// print a line of *
std::cout << sw << "" << std::string(56,'*') << '\n';
cout << std::setprecision(2) << std::fixed; // precision 2, fixed
cout << sw << std::left << "Math" << std::right;
for(auto ms : array[0]) std::cout << w << ms;
cout << wb << math_best << wb << math_worse << wb << math_average << '\n';
cout << sw << std::left << "Chemistry" << std::right;
for(auto cs : array[1]) std::cout << w << cs;
cout << wb << chemistry_best << wb << chemistry_worse << wb << chemistry_average << '\n';
输出
1 2 3 4 5 BEST WORST AVERAGE
********************************************************
Math 50.00 20.00 30.00 40.00 50.00 50.00 20.00 38.00
Chemistry 90.00 70.00 20.00 10.00 40.00 90.00 10.00 46.00