足迹 发表于 2017-9-29 22:45:55

关于out和input

用code blocks 编写的程序
#include <iostream>
using namespace std;

int main()
{
    cout<<"请【xx.x c】或者的形式输入温度"<<endl;
    double tempin,tempout;
    char typein,typeout;
    const double RADIO = 9.0/5.0;
    const unsigned short REST =32;
    cin>>tempin;
    cin>>typein;
    cin.ignore(100,'\n');
    switch(typein)
    {
    case 'c':
    case 'C':
      tempout=tempin*RADIO+REST;
      typeout='F';
      break;
    case 'f':
    case 'F':
      tempout=(tempin-REST)/RADIO;
      typeout='C';
      break;
    default:
      typeout='E';
      break;
    }
    if (typeout != 'E')
    {
      cout<<tempin<<typein
      <<'=='<<tempout<<typeout;
    }
    else
      cout<<"输入错误";
    return 0;
}


输入的是
32.4 c

调试时


不知道为什么

eglym 发表于 2017-9-29 23:09:34

你写的不就应该是这个答案吗?
你想写成什么??

我有些懵逼了

足迹 发表于 2017-9-30 07:34:56

eglym 发表于 2017-9-29 23:09
你写的不就应该是这个答案吗?
你想写成什么??



tempin应该等于32.4
typein应该等于c

eglym 发表于 2017-9-30 11:53:59

足迹 发表于 2017-9-30 07:34
tempin应该等于32.4
typein应该等于c

tempin=32.3999是因为浮点数有误差引起的,具体可以想象将十进制浮点数转换为二进制的过程

typein = 99'c',前面的99是c对应的ASCII码,后面又把ASCII码转换为了字符‘c’;

足迹 发表于 2017-9-30 13:25:02

eglym 发表于 2017-9-30 11:53
tempin=32.3999是因为浮点数有误差引起的,具体可以想象将十进制浮点数转换为二进制的过程

typein = 9 ...

我当然知道啦,我只是想问如何解决。

eglym 发表于 2017-9-30 13:48:30

足迹 发表于 2017-9-30 13:25
我当然知道啦,我只是想问如何解决。

你的程序里面
<<'=='<<tempout<<typeout
这个有问题。
应该改成
      <<"=="<<tempout<<typeout;
这个误差很小一般不会有影响,而且数据在机器里面就是这样存储的,当然如果硬要说改的话
那么也只能够在输出的时候进行四舍五入,
或者换个存储方式,不过看你的程序也没有必要的

足迹 发表于 2017-9-30 14:48:30

eglym 发表于 2017-9-30 13:48
你的程序里面

有影响的是 我输入的是c,存入的是99c
页: [1]
查看完整版本: 关于out和input