C ++如何将“cout”的数量减少到6

时间:2014-10-16 07:01:15

标签: c++

我想减少这个程序中使用的“cout”的数量。到目前为止,我的程序中有9个“cout”... 首先我有11个“cout”,但我可以通过将它们放在一个方法中减少到9个。但仍然超过6 ..

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

static void Endline(){
     cout << endl;
}

void printHeader(int start, int end) {
     cout << setw(4) << " ";
     for (int firstrow = start; firstrow <= end; firstrow++) {
        cout << setw(3) << firstrow << " ";
     }
     // cout << endl;
     Endline();

     // Second line, lead space then proper number of dashes
     cout << setw(4) << " ";
     for (int secondrow = start; secondrow <= end; secondrow++) {
        cout << "****";
     }
     //cout << endl;
     Endline();
}

int main() {
     int start;
     int end;

     // Collect start and end of listing
     cout << "Enter a start index (integer): " << endl;
     cin >> start;

     cout << "Enter an end index (integer): " << endl;
     cin >> end;

     for (int i = start; i <= end; i++) {
        // Executes once to print header
        if (i == start) {
            printHeader(start, end);
        }

        // Now loop through columns
        for (int j = start; j <= end; j++) {
            // Once per row create first column heading
            if (j == start) {
                cout << setw(3)<<  i << "|";
            }

            // Multiply row and column
            cout << setw(3) << (i * j) << " ";
        }

        // End row
        //cout << endl;
        Endline();
      }

     return 0;
}

2 个答案:

答案 0 :(得分:0)

如果您将整个表格流式传输到结果std::stringstreamcout,则可以将cout的数量减少到3:

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

void printHeader(stringstream& result, int start, int end) {
    result<< setw(4) << " ";
    for (int firstrow = start; firstrow <= end; firstrow++) {
        result<< setw(3) << firstrow << " ";
    }
    result<< endl;

    // Second line, lead space then proper number of dashes
    result << setw(4) << " ";
    for (int secondrow = start; secondrow <= end; secondrow++) {
        result << "****";
    }
    result << endl;
}

int main() {
    int start;
    int end;

    // Collect start and end of listing
    cout << "Enter a start index (integer): " << endl;
    cin >> start;

    cout << "Enter an end index (integer): " << endl;
    cin >> end;

    stringstream result;

    for (int i = start; i <= end; i++) {
        // Executes once to print header
        if (i == start) {
            printHeader(result, start, end);
        }

        // Now loop through columns
        for (int j = start; j <= end; j++) {
            // Once per row create first column heading
            if (j == start) {
                result<< setw(3)<<  i << "|";
            }

            // Multiply row and column
            result << setw(3) << (i * j) << " ";
        }

        // End row
        result<< endl;
    }
    cout << result.str();
    return 0;
}

但是:为什么你真的想减少cout?根据{{​​3}} cout似乎运行良好。

答案 1 :(得分:-1)

您要做的是减少代码中的重复次数。这是你在编程时总想做的事情。

因此,每当您看到两个看似相似的代码块时(有时它们来自复制粘贴),您应该搜索将它们编写为一个函数的方法。

 cout << setw(3)<<  i << "|";

cout << setw(3) << (i * j) << " ";

cout << setw(3) << firstrow << " ";

几乎相同。您可以将它们编写为一个函数,将要打印的数字和结束字符作为参数。

类似地,

 cout << setw(4) << " ";

出现两次,

 cout << "Enter a start index (integer): " << endl;

也出现两次。你可以将它们包装在一个你将调用两次的函数中,就像你为endl字符所做的那样。

你可以删除4个couts,这就够了吗?