草庐IT

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

coder 2024-02-24 原文

问题

我正在制作摄氏度到华氏度的转换器,反之亦然。

我的所有代码都能完美运行,但我不确定如何制作老师想要的图表。

输出范围为 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++ - 用 C++ 制作图表的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  3. Unity 3D 制作开关门动画,旋转门制作,推拉门制作,门把手动画制作 - 2

    Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u

  4. 动漫制作技巧如何制作动漫视频 - 2

    动漫制作技巧是很多新人想了解的问题,今天小编就来解答与大家分享一下动漫制作流程,为了帮助有兴趣的同学理解,大多数人会选择动漫培训机构,那么今天小编就带大家来看看动漫制作要掌握哪些技巧?一、动漫作品首先完成草图设计和原型制作。设计草图要有目的、有对象、有步骤、要形象、要简单、符合实际。设计图要一致性,以保证制作的顺利进行。二、原型制作是根据设计图纸和制作材料,可以是手绘也可以是3d软件创建。在此步骤中,要注意的问题是色彩和平面布局。三、动漫制作制作完成后,加工成型。完成不同的表现形式后,就要对设计稿进行加工处理,使加工的难易度降低,并得到一些基本准确的概念,以便于后续的大样、准确的尺寸制定。四、

  5. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

  6. arrays - Ruby 数组 += vs 推送 - 2

    我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“

  7. += 的 Ruby 方法 - 2

    有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=

  8. ruby - Sinatra + Heroku + Datamapper 使用 dm-sqlite-adapter 部署问题 - 2

    出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t

  9. ruby - Ruby 中字符串运算符 + 和 << 的区别 - 2

    我是Ruby和这个网站的新手。下面两个函数是不同的,一个在函数外修改变量,一个不修改。defm1(x)x我想确保我理解正确-当调用m1时,对str的引用被复制并传递给将其视为x的函数。运算符当调用m2时,对str的引用被复制并传递给将其视为x的函数。运算符+创建一个新字符串,赋值x=x+"4"只是将x重定向到新字符串,而原始str变量保持不变。对吧?谢谢 最佳答案 String#+::str+other_str→new_strConcatenation—ReturnsanewStringcontainingother_strconc

  10. ruby - rails 3.2.2(或 3.2.1)+ Postgresql 9.1.3 + Ubuntu 11.10 连接错误 - 2

    我正在使用PostgreSQL9.1.3(x86_64-pc-linux-gnu上的PostgreSQL9.1.3,由gcc-4.6.real(Ubuntu/Linaro4.6.1-9ubuntu3)4.6.1,64位编译)和在ubuntu11.10上运行3.2.2或3.2.1。现在,我可以使用以下命令连接PostgreSQLsupostgres输入密码我可以看到postgres=#我将以下详细信息放在我的config/database.yml中并执行“railsdb”,它工作正常。开发:adapter:postgresqlencoding:utf8reconnect:falsedat

随机推荐