c++ - 用 C++ 制作图表

标签 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 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;
}

这是我的输出:

Farenheit equivalents of Celsius temperatures: 
Celsius Farenheit Celsius Farenheit Celsius Farenheit Celsius Farenheit Celsius Farenheit 
      0        32
      1        33
      2        35
      3        37
      4        39
      5        41
      6        42
      7        44
      8        46
      9        48
     10        50 
         ...


Celsius equivalents of Farenheit temperatures: 
Fahrenheit Celsius Fahrenheit Celsius Fahrenheit Celsius Fahrenheit Celsius Fahrenheit Celsius 
        32       0
        33       0
        34       1
        35       1
        36       2
        37       2
        38       3
        39       3
        40       4
        41       5
        42       5
            ...

这是我老师想要的: 从 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 Celsius Farenheit 
      0        32      25        77      50       122      75       167     100       212 
      1        33      26        78      51       123      76       168     101       213 
      2        35      27        80      52       125      77       170     102       215 
      3        37      28        82      53       127      78       172     103       217 
      4        39      29        84      54       129      79       174     104       219 
      5        41      30        86      55       131      80       176     105       221 
      6        42      31        87      56       132      81       177     106       222 
      7        44      32        89      57       134      82       179     107       224 
      8        46      33        91      58       136      83       181     108       226 
      9        48      34        93      59       138      84       183     109       228 
     10        50      35        95      60       140      85       185     110       230 
 ...

Celsius equivalents of Farenheit temperatures: 
Fahrenheit Celsius Fahrenheit Celsius Fahrenheit Celsius Fahrenheit Celsius Fahrenheit Celsius 
        32       0         77      25        122      50        167      75        212     100 
        33       0         78      25        123      50        168      75        213     100 
        34       1         79      26        124      51        169      76        214     101 
        35       1         80      26        125      51        170      76        215     101 
        36       2         81      27        126      52        171      77        216     102 
        37       2         82      27        127      52        172      77        217     102 
        38       3         83      28        128      53        173      78        218     103 
        39       3         84      28        129      53        174      78        219     103 
        40       4         85      29        130      54        175      79        220     104         
...

最佳答案

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

从打印开始

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
    ...  

并考虑您必须在程序中更改哪些内容才能打印您想要的图表。

关于c++ - 用 C++ 制作图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7458033/

相关文章:

c++ - 有没有办法在不在 header 中实现模板函数的情况下处理模板函数的 undefined reference 错误?

c++ - 给定日期范围生成工作日日期的算法

c# - 多个图表上的相同网格设置

ios - 使用 Coregraphics 一次绘制多个 Guage 图表

javascript - 在 v2 上的 chart.js 中的图表上绘制水平线

javascript - 使用 Highcharts.js 的可拖动日期范围

silverlight - 白银图表轴间隔

c++ - 具有不同原型(prototype)的函数指针 vector ,我可以构建一个吗?

c++ - 如何在 gtk 中设置包含宽字符的文本?

C++ 嵌套类 - 内部/外部关系