C ++代码调整

时间:2016-09-28 08:42:24

标签: c++

我写了一些代码来显示结果作为附图。 enter image description here

我写的代码如下:

#include <iostream>
#include <iomanip>

using namespace std;

int main()

{

int col , lig;

const int i = 10;

cout<<"tab x!";

for  (col = 1 ; col <= i ; col = col + 1)
{
    cout<< col<< setw(6) ;
}
cout<<endl;

cout<<"_________________________________________________________________"<<endl;

for  (lig=1;lig<=i;lig=lig+1)  {

  { 

      cout<<setw(5)<<endl;


 for (col=2;col<=i+1;col=col+1) 
   { for (lig=2;lig<=i+1;lig=lig+1)
     cout<<(col-1) * (lig-1) <<setw(6);
cout<<setw(1);
 cout<<setw(5)<<endl;}}



return 0;
}}

但是结果目前通过运行上面的代码显示如下:

tab x!1     2     3     4     5     6     7     8     9    10
_________________________________________________________________

    1     2     3     4     5     6     7     8     9    10
    2     4     6     8    10    12    14    16    18    20
    3     6     9    12    15    18    21    24    27    30
    4     8    12    16    20    24    28    32    36    40
    5    10    15    20    25    30    35    40    45    50
    6    12    18    24    30    36    42    48    54    60
    7    14    21    28    35    42    49    56    63    70
    8    16    24    32    40    48    56    64    72    80
    9    18    27    36    45    54    63    72    81    90
   10    20    30    40    50    60    70    80    90   100

问题:

我需要将结果显示为附图,我无法找到问题所在......请帮我完成与图片相同的结果。

3 个答案:

答案 0 :(得分:0)

在特定宽度的字段中打印出值时,需要先将操纵符放入。因此:

        std::cout << setw(6) << col;

以前,您没有设置第一列的宽度。

当您打印出实际行时,您需要先打印出行标题和&#39; |&#39;性格(不,我认为,&#39;!&#39;)。 (再次,你需要先放宽度。)

暂且不说:

  • 不要使用i作为上限。给它一个更有意义的名字(比如max)。 i通常用作循环索引。
  • 一般情况下,将您的循环编写为for (int col = 0; col < max; col++),但是您需要打印col+1,因此在这种情况下,使用for (int col = 1; col < max+1; col++)可能更好(但您仍应声明for和中的循环变量,而不是写col=col+1)。
  • 最后,为了上帝的爱,整理你的缩进和间距,这样你就可以阅读你的代码了!

答案 1 :(得分:0)

没有触及你的代码逻辑

int main() {
    int col, lig;
    const int i = 10;
    cout << "tab x|";
    for (col = 1; col <= i; col = col + 1) {
        cout << setw(4) << col;
    }
    cout << endl;
    cout << "______________________________________________" << endl;
    cout << endl;

    for (lig = 1; lig <= i; lig = lig + 1) {
        for (col = 2; col <= i + 1; col = col + 1) {
            cout << setw(4) << (col - 1) << " |";
            for (lig = 2; lig <= i + 1; lig = lig + 1)
                cout << setw(4) << (col - 1) * (lig - 1);
            cout << endl;
        }
    }
    return 0;
}

虽然只有两个for循环可以获得相同的结果。

答案 2 :(得分:0)

int main() {
    int col, lig;
    const int i = 10;
    cout << "tab x|";
    for (col = 1; col <= i; col = col + 1) {
        cout << setw(4) << col;
    }
    cout << endl;
    cout << "______________________________________________" << endl;
    cout << endl;

    for (lig = 1; lig <= i; lig = lig + 1) {
        for (col = 2; col <= i + 1; col = col + 1) {
            cout << setw(4) << (col - 1) << " |";
            for (lig = 2; lig <= i + 1; lig = lig + 1)
                cout << setw(4) << (col - 1) * (lig - 1);
            cout << endl;
        }
    }
    return 0;
}
Same result can be obtain with only two for loops though.