Chinaunix首页 | 论坛 | 博客
  • 博客访问: 857193
  • 博文数量: 158
  • 博客积分: 4380
  • 博客等级: 上校
  • 技术积分: 2367
  • 用 户 组: 普通用户
  • 注册时间: 2006-09-21 10:45
文章分类

全部博文(158)

文章存档

2012年(158)

我的朋友

分类: C/C++

2012-11-15 10:51:27

// 本来只是回答[xmq120]而已,但觉得还有一点点意义,留此做存根:)

#include
#include
#include
#include
#include
using namespace std;

void main()
{
    cout << "请输入数字:" << flush;
    double earnings = 0.0;
    { // 不是数字,或不是第一个小数点就不显示;而且必须有输入才可以继续
        string temp;
        bool dot = false;
        for( char c; c=(char)_getch(), c!='\r' || temp.empty(); ) // 这里面有个逗号表达式,不要把它当成分号
        {
            if( ( c>='0' && c<='9' ) || (c=='.' && !dot && (dot=true)) ) // "&& (dot=true)" 没有写错,我这是个取巧做法
            {
                putch( c );
                temp += c;
            }
        }
        istringstream is( temp );
        is >> earnings;
        assert( is.eof() );
        cout << endl;
    }
    cout << "你输入的是:" << earnings << endl;
}
阅读(781) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~