首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

困惑!该怎么处理

2012-02-27 
困惑!#include iostreamusingnamespacestdclassc1{public:voidreverse(char)voidreverse(int)voidrev

困惑!
#include <iostream>
using   namespace   std;
class   c1
{
    public:
    void   reverse(char);
    void   reverse(int);
    void   reverse(float);
    private:
    int   r;
    char   c;
    float   s;
};
void   c1::reverse(int   r)
{
int   n;
while(r!=0)
{
n=r%10;
        cout < <n;
                r=r/10;
}
cout < <endl;
}
void   c1::reverse(char   c)
{
int   i=1;
char   cm,a[20];
a[0]=c;
while((cm=getchar())!= '\n ')
{
                a[i]=cm;
i++;
}
i--;
for(i;i> =0;i--)
cout < <a[i];
cout < <endl;
}
void   c1::reverse(float   t)
{
int   p=0,n,m=0;
        long   tmp;
        while((long)t!=t)  
        {
          t*=10;
          p++;
        }    
        tmp=(long)t;      
        while(tmp!=0)
{
n=tmp%10;
if(m==p)  
                {
                                  cout < < ". ";
                                  goto   s1;
                }
else   cout < <n;
                tmp=tmp/10;
          s1:m++;
}
cout < <endl;
}
int   main()
{
int   i,m;
char   ch;
float   f;
c1   t;
        for(;;)
{
cout < < "Please   input   a   number   to   make   a   choice: " < <endl;
        cout < < "1.zhengshu " < <endl;
        cout < < "2.zifu " < <endl;
        cout < < "3.fudianshu " < <endl;
        cout < < "4.quit " < <endl;
cin> > i;
switch(i)
{
    case   1:
    {
    cout < < "Please   input   a   zhengshu: " < <endl;
    cin> > m;
    t.reverse(m);
    }break;
    case   2:
    {
    cout < < "Please   input   a   string   of   character: " < <endl;
    cin> > ch;
    t.reverse(ch);
    }break;
    case   3:
    {
    cout < < "Please   input   a   fudianshu: " < <endl;
    cin> > f;
    t.reverse(f);
    }break;


    case   4:exit(0);
}
}
return   0;
}

为什么在输入3后,再输入浮点数34.99时,输出的不是99.43呢?
请帮下忙!

[解决办法]
void c1::reverse(float t)
{
int p=0,n,m=0;
long tmp;
while((long)t!=t)
{
t*=10;
p++;
}
tmp=(long)t;
while(tmp!=0)
{
n=tmp%10;
if(m==p)
{
cout < < ". ";
goto s1;
}
else cout < <n;
tmp=tmp/10;
s1:m++;
}
cout < <endl;
}

float是不精确的,*10处理不妥,直接把float转成字符串然后倒序输出好了

热点排行