C语言中的怎样筛选int和float数据???(在线等)

zhu833 2008-04-28 01:15:41
程序如下:

#include<stdio.h>

int main(void)
{
int i;
while( scanf("%d",&i)==0)
{scanf("%d",&i);}

printf("i=%d"i);

return 0;
}

问题 如果输入 12.9 得到12
如何作好判断机制 完成 小数的排除

希望是::::输入 12.9 返回重新输入

谢谢!
...全文
1398 64 打赏 收藏 转发到动态 举报
写回复
用AI写文章
64 条回复
切换为时间正序
请发表友善的回复…
发表回复
No65 2008-05-04
  • 打赏
  • 举报
回复
这个程序,我在LCC-win32中运行OK,思路是用getchar()函数先判断小数点,然后判断紧跟在小数点后的是数据还是空格。
include <stdio.h>
#include <ctype.h>

int main(void)
{
int i;
while(((scanf("%d",&i))) !=EOF)
{
if(!isspace(getchar())) //judge the input data is or not float
{
printf("please input the data with integer type\n");
while(!isspace(getchar())) //throw off the data that followed the decimal
continue;
continue;
}


printf("the input is ok:%d\n",(int)i);
}
return 0;

}
iwknow 2008-05-03
  • 打赏
  • 举报
回复
学到东西了..
ChikoHu 2008-05-03
  • 打赏
  • 举报
回复
楼主定于的i是整型啊
怎么能输入12.9呢?
xiaoc10 2008-05-03
  • 打赏
  • 举报
回复
[Quote=引用 62 楼 iwknow 的回复:]
学到东西了..
[/Quote]
x308603129 2008-05-02
  • 打赏
  • 举报
回复
迷茫中
c_draw 2008-05-02
  • 打赏
  • 举报
回复
思路很好!
guzhilei1986 2008-05-01
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 Chiyer 的回复:]
C/C++ codeintmain() {charc=0;intn;while(c!='\n') {
fflush(stdin);
scanf("%d%c",&n,&c);
}

printf("%d\n", n);return0;

}------1.111aaa1.2123123请按任意键继续. . .
[/Quote]
学习了。
guzhilei1986 2008-05-01
  • 打赏
  • 举报
回复
楼主可以先用char数组来保存,然后进行判断,看里面有没有小数点,有就重新输入。没有就将char数组变成int类型的。
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
都是很好的建议! 值得学习
shtianhai 2008-05-01
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 Chiyer 的回复::]
C/C++ codeintmain() {charc=0;intn;while(c!='\n') {
fflush(stdin);
scanf("%d%c",&n,&c);
}

printf("%d\n", n);return0;

}------1.111aaa1.2123123请按任意键继续. . .

[/Quote]

小强啊,佩服!
phyert 2008-04-30
  • 打赏
  • 举报
回复
把输入的浮点数强制转换乘整型
星光伴月 2008-04-30
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 languagec 的回复:]
我怎么踩到哪里都像个隐形人。。 我答的很切题呀,怎么楼主就是没看见
[/Quote]
我看见了,也许楼主眼神不好吧!
lisheng053758 2008-04-30
  • 打赏
  • 举报
回复
楼主考虑下把输入的浮点数强制转换乘整型的不就行了么?
ww5010 2008-04-30
  • 打赏
  • 举报
回复
进来学习经验,正好用的上!发现有很多程序都再输入字符时出现死循环的情况!
郗富琦 2008-04-30
  • 打赏
  • 举报
回复
很好用,而且连例如11.00的float也排出了。学习!!
int main() {

char c = 0;
int n;

while (c != '\n') {
fflush(stdin);
scanf("%d%c", &n, &c);
}

printf("%d\n", n);

return 0;

}
freeagle_5 2008-04-30
  • 打赏
  • 举报
回复
应该属于编译时的字符串识别。建议使用字符串读入,然后处理。
vbanglev 2008-04-30
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 Chiyer 的回复:]
C/C++ codeintmain() {charc=0;intn;while(c!='\n') {
fflush(stdin);
scanf("%d%c",&n,&c);
}

printf("%d\n", n);return0;

}------1.111aaa1.2123123请按任意键继续. . .
[/Quote]
fengjian_net 2008-04-30
  • 打赏
  • 举报
回复
按照浮点数读入,在将该数取整和浮点数判断即可。如:

float f;
scanf("%f", &f)
if( fabs(f-(int)f)<1e-8)
printf("读入是整数");
else
pringf("读入是浮点数");
andy_cai 2008-04-30
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 Chiyer 的回复:]
C/C++ codeintmain() {charc=0;intn;while(c!='\n') {
fflush(stdin);
scanf("%d%c",&n,&c);
}

printf("%d\n", n);return0;

}------1.111aaa1.2123123请按任意键继续. . .
[/Quote]
高手到底是高手,一个小程序,用到了好几个基础知识
yanghao58686763 2008-04-30
  • 打赏
  • 举报
回复
是呀,真的超级棒~~!
加载更多回复(44)

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧