C++ 将 printf 更改为 cout

C++ Change printf to cout

下面这段代码效果很好。我只需要将 printf 写成 cout。我已经尝试了几次,但它对我来说是错误的。任何帮助,将不胜感激。

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
    double mathScores[] = { 95, 87, 73, 82, 92, 84, 81, 76 };
    double chemScores[] = { 91, 85, 81, 90, 96, 89, 77, 79 };
    double aveScores[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    //calculate size of array
    int len = sizeof(mathScores) / sizeof(double);
    //use array
    for (int i = 0; i < len; ++i) {
        aveScores[i] = (mathScores[i] + chemScores[i]) / 2;
    }
    printf("%s\t%s\t%s\t%s\n", "ID", "math", "chem", "ave");
    for (int i = 0; i < len; ++i) {
        printf("%d\t%.2f\t%.2f\t%.2f\n", i, mathScores[i], chemScores[i],
                aveScores[i]);
    }
    return 0;
}

iomanip库包含设置小数精度的方法,即setprecisionfixed。您可以指定 setprecision(2)fixed 以打印两位小数作为每个分数的一部分。以下产生与原始代码相同的输出。

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    double mathScores[] = { 95, 87, 73, 82, 92, 84, 81, 76 };
    double chemScores[] = { 91, 85, 81, 90, 96, 89, 77, 79 };
    double aveScores[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    //calculate size of array
    int len = sizeof(mathScores) / sizeof(double);
    //use array
    for (int i = 0; i < len; ++i) {
        aveScores[i] = (mathScores[i] + chemScores[i]) / 2;
    }
    // Set decimal precision
    std::cout << std::setprecision(2);
    std::cout << std::fixed;
    std::cout << "ID\tmath\tchem\tave" << endl;
    for (int i = 0; i < len; ++i) {
        std::cout << i << "\t" << mathScores[i] << "\t" << chemScores[i] << "\t" << aveScores[i] << endl;
    }
    return 0;
}

第一行很简单,因为它可以作为一个字符串完成:

std::cout << "ID\tmath\tchem\tave" << std::endl;

但是,您可能希望更明显地处理分隔符:

const char TAB('\t');
std::cout << "ID" << TAB << "math" << TAB << "chem" << TAB "ave" << std::endl;

循环只需要使用std::setprecision()修饰符将精度设置到两个地方:

for (int i = 0; i < len; ++i) {
    std::cout << std::setprecision(2)
              << i << TAB
              << mathScores[i] << TAB
              << chemScores[i] << TAB
              << aveScores[i] << std::endl;
}

可以使用 Boost Library 实现更熟悉的语法。它看起来像这样:

#include <iostream>
#include <stdio.h>
using boost::format
using namespace std;

int main() {
    double mathScores[] = { 95, 87, 73, 82, 92, 84, 81, 76 };
    double chemScores[] = { 91, 85, 81, 90, 96, 89, 77, 79 };
    double aveScores[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    string s;
    char ss[30];
    //calculate size of array
    int len = sizeof(mathScores) / sizeof(double);
    //use array
    for (int i = 0; i < len; ++i) {
        aveScores[i] = (mathScores[i] + chemScores[i]) / 2;
    }

    cout<<format("%s\t%s\t%s\t%s\n") % "ID" % "math" % "chem" % "ave";
    //printf("%s\t%s\t%s\t%s\n", "ID", "math", "chem", "ave");
    for (int i = 0; i < len; ++i) {
        //printf("%d\t%.2f\t%.2f\t%.2f\n", i, mathScores[i], chemScores[i],
                aveScores[i]);
        cout<<format("%d\t%.2f\t%.2f\t%.2f\n") %i  % mathScores[i] % chemScores[i] % aveScores[i];
    }
    return 0;
}