C++ 用C+制作图表+; 问题

C++ 用C+制作图表+; 问题,c++,charts,C++,Charts,我正在做一个摄氏到华氏的转换器,反之亦然 我所有的代码都很完美,但我不知道如何制作老师想要的图表 输出温度为0至100摄氏度和32至212华氏度 这是我的密码: #include <iostream> #include <cstdlib> #include <iomanip> using namespace std; using std::setw; int i; int ftemp; int ctemp; int farToC(int a, int

我正在做一个摄氏到华氏的转换器,反之亦然

我所有的代码都很完美,但我不知道如何制作老师想要的图表

输出温度为0至100摄氏度和32至212华氏度

这是我的密码:

#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;
using std::setw;

int i;

int ftemp;
int ctemp;

int farToC(int a, int b);
int celToF(int a, int b);

int main (int argc, const char * argv[])
{
cout << "Farenheit equivalents of Celsius temperatures: " << endl;

for(i = 0; i < 5; i++)
{
    cout << "Celsius Farenheit ";
}
cout << endl;

for(i = 0; i < 101; i++)
{
    cout << setw(7) << i << setw(8) << " " << celToF(i, ftemp)<< endl;
}
cout << endl;

cout << "Celsius equivalents of Farenheit temperatures: " << endl;
for(i = 0; i < 5; i++)
{
    cout << "Fahrenheit Celsius ";
}
cout << endl;

for(i = 32; i < 213; i++)
{
    cout << setw(10) << i << setw(7) << " " << farToC(i, ctemp)<< endl;
}
cout << endl;
}

int farToC(int a, int b)
{
b = (a - 32) * 5 / 9;
return b;
}

int celToF(int a, int b)
{
b = (a * 9/5) + 32;
return b;
}
以下是我的老师想要的: 摄氏0到100度

Farenheit equivalents of Celsius temperatures: 
Celsius Farenheit Celsius Farenheit Celsius Farenheit Celsius Farenheit Celsius Farenheit 
      0        32      25        77      50       122
      1        33      26        78      51       123
      2        35      27        80      52       125
      3        37      28        82      53       127
      4        39      29        84      54       129
      5        41      30        86      55       131
      6        42      31        87      56       132
      7        44      32        89      57       134
      8        46      33        91      58       136
      9        48      34        93      59       138
     10        50      35        95      60       140
    ...
华氏温度也一样,但从32度到212度

我知道如何使用setw()将数字与单词对齐,但我不知道如何使用列

任何想法或建议将不胜感激


解决方案 谢谢大家的帮助。我想了一会儿才明白

#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;
using std::setw;

int i;
int k;

int ftemp;
int ctemp;

int farToC(int a, int b);
int celToF(int a, int b);

int main (int argc, const char * argv[])
{
cout << "Farenheit equivalents of Celsius temperatures: " << endl;

for(i = 0; i < 5; i++)
{
    cout << "Celsius Farenheit ";
}

cout << endl;

for(k = 0; k < 25; k++)
{
    for(i = 0; i < 101; i+= 25)
    {
        cout << setw(7) << i+k << setw(10) << celToF(i+k, ftemp) << " " << setw(10);
    }
    cout<< endl;
}

cout << endl;

cout << "Celsius equivalents of Farenheit temperatures: " << endl;

for(i = 0; i < 5; i++)
{
    cout << "Fahrenheit Celsius ";
}

cout << endl;

for(k = 0; k < 45; k++)
{
    for(i = 32; i < 213; i += 45)
    {
        cout << setw(10) << i+k << setw(8) << farToC(i+k, ctemp) << " "  << setw(12);
    }
    cout << endl;
}

cout << endl;
}

int farToC(int a, int b)
{
b = (a - 32) * 5 / 9;
return b;
}

int celToF(int a, int b)
{
b = (a * 9/5) + 32;
return b;
}

您已经在打印对齐的列,只是数量不够

从打印开始

Farenheit equivalents of Celsius temperatures:  
Celsius Farenheit Celsius Farenheit Celsius Farenheit Celsius Farenheit  
      0        32       0        32       0        32       0        32  
      1        33       1        33       1        33       1        33
    ...  

想想你需要在程序中修改什么才能打印出你想要的图表。

你已经在一行一行地打印出来了,打印了25行。现在想想每一列与每一行的前几列之间的关系:

baseC   toF(baseC)   baseC+numRows   toF(baseC+numRows)     baseC+(2*numRows) ...
baseC+1 toF(baseC+1) baseC+1+numRows toF(baseC+1+numRows) baseC+1+(2*numRows) ...
行数与用于计算循环次数的值相同

循环变量将处理
baseC+X
部分


注意:这是伪代码,所以不要期望它直接工作:)

考虑如何将
n
逻辑行排列成
k
列,以及在每个输出行上需要哪些数据。在某些地方会有一些除法和模运算。+1是关于堆栈溢出的少数几个好的家庭作业问题之一。@Blake,Kerrek:你不需要严格的除法/模运算。您可以继续为表格右侧的每个部分添加已知偏移量。由于循环常量已经硬编码,我看不出这会影响最终的解决方案。@Blake:在C2F循环中,Celsius列中的数字取决于它的列和行。你只是在计算这个列。建议:当您使用嵌套循环时,为索引指定有意义的名称,例如“row”和“col”,而不是“i”和“j”。如果对代码执行此操作,您可能会看到遗漏了什么。
Farenheit equivalents of Celsius temperatures:  
Celsius Farenheit Celsius Farenheit Celsius Farenheit Celsius Farenheit  
      0        32       0        32       0        32       0        32  
      1        33       1        33       1        33       1        33
    ...  
baseC   toF(baseC)   baseC+numRows   toF(baseC+numRows)     baseC+(2*numRows) ...
baseC+1 toF(baseC+1) baseC+1+numRows toF(baseC+1+numRows) baseC+1+(2*numRows) ...