简体   繁体   English

C++ 将 printf 更改为 cout

[英]C++ Change printf to cout

I have this code below that works great.我在下面有这段代码,效果很好。 I just need to write the printf as cout.我只需要将 printf 写为 cout。 I've tried a few times but it errors on me.我已经尝试了几次,但它对我来说是错误的。 Any help would be appreciated.任何帮助,将不胜感激。

#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;
}

The iomanip library includes methods for setting decimal precision, namely setprecision and fixed . iomanip库包括设置小数精度的方法,即setprecisionfixed You can specify setprecision(2) and fixed to print two decimal places as part of each score.您可以指定setprecision(2)fixed以打印两个小数位作为每个分数的一部分。 The following produces the same output as the original code.以下产生与原始代码相同的 output。

#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;
}

The first line is pretty easy as it can all be done as one string:第一行非常简单,因为它都可以作为一个字符串完成:

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

however, you might want to to treat the seperator a bit more obviously:但是,您可能希望更清楚地处理分隔符:

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

The loop just needs to use the std::setprecision() modifier to set the precision to two places:循环只需要使用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;
}

A more familiar syntax can be achieved with the Boost Library .使用Boost Library可以实现更熟悉的语法。 It would look something like this:它看起来像这样:

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM