判断是否为整数

x1m2j3-hq 2011-03-08 12:57:30
判断一个输入的数是否为整数(包括正负),有哪些方法(代码)
...全文
369 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
masmaster 2011-03-08
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 x1m2j3 的回复:]
我的意思是我输入一串字符,要电脑判断出我输入的是不是一个整数,c代码
[/Quote]

我想是这个意思吧,本人菜鸟,代码粗糙仅供参考。
root@~ #cat isdigit.c
#include <stdio.h>
#include <stdbool.h>

int main (void) {

char str[81];
bool isdigit (char str[]);

scanf ("%s",str);

if(isdigit(str)) {
printf ("yes\n");
}else{
printf ("No!\n");
}

return 0;

}

bool isdigit (char str[]) {

int i=0;
bool isIntStr;

while(str[i]!='\0') {

if(str[i]>='0'&&str[i]<='9') {
i++;
}else{
return isIntStr=false;
}

}

return isIntStr=true;
}

root@~ #


编译并测试:

root@~ #gcc isdigit.c -o isdigit
root@~ #./isdigit
123456
yes
root@~ #./isdigit
21342.2134
No!
root@~ #./isdigit
sadf
No!
root@~ #./isdigit
23c
No!
root@~ #
MSOKD 2011-03-08
  • 打赏
  • 举报
回复
判断输入的数是否有小数点。。

c=getch();
if(c==".")
....
wbruce 2011-03-08
  • 打赏
  • 举报
回复

string numstr = "5665";
int len = strspn(numstr.c_str(),"1234567890");
int len2 = numstr.size();
if (len == len2)

mienfong 2011-03-08
  • 打赏
  • 举报
回复
isdigit() 來判斷吧,只有是非數字的字元就算不對(小數點也包含),至於負號若在第一位則讓它pass,這樣就可以很簡單的處理了。
x1m2j3-hq 2011-03-08
  • 打赏
  • 举报
回复
我的意思是我输入一串字符,要电脑判断出我输入的是不是一个整数,c代码
x1m2j3-hq 2011-03-08
  • 打赏
  • 举报
回复
七楼的有点意思
AnshuWang 2011-03-08
  • 打赏
  • 举报
回复
求对2余 再加起来
是否和原来相等
赵4老师 2011-03-08
  • 打赏
  • 举报
回复
仅供参考
//d.txt为下面一行内容:( 去掉前面的// )
//01.2,3.45;5.6789 -0.12345678901234 abc 1234567890.123456789123456789 1e300 0
#include <stdio.h>
int n,r;
double d;
FILE *f;
void main() {
f=fopen("d.txt","r");
n=0;
while (1) {
r=fscanf(f,"%lf",&d);
if (1==r) {
n++;
printf("[%d]==%.15lg\n",n,d);//可以试试注释掉这句以后的速度
} else if (0==r) {
fgetc(f);
} else break;
}
fclose(f);
}
//实际运行输出:( 去掉前面的// )
//[1]==1.2
//[2]==3.45
//[3]==5.6789
//[4]==-0.12345678901234
//[5]==1234567890.12346
//[6]==1e+300
//[7]==0

x1m2j3-hq 2011-03-08
  • 打赏
  • 举报
回复
还有这些数据:125*/-、78shhdh、等等,输入的必须是严格的0-9,除了第一位符号位。
x1m2j3-hq 2011-03-08
  • 打赏
  • 举报
回复
问题补充:(C 语言代码)假如我输入的是1258、-78426、69.15、ieoiei、+/【】,、其中要求能判断出后面三个不是整数,越多方法越好,请教!
赵4老师 2011-03-08
  • 打赏
  • 举报
回复
Sorry!偶又一稿多投了!(为什么要说‘又’) (^_^)
#include <stdio.h>
#include <conio.h>
int i = 0;
char c;
void main() {
while (1) {
printf("\nPlease input a number:");
rewind(stdin);
if (2==scanf("%d%c",&i,&c)) {
if ('\n'==c) {
printf("\nThe number is %d.\n", i);
break;
}
}
}
printf("Press any key to end ...");
getch();
}
//
//Please input a number:12a
//
//Please input a number:a
//
//Please input a number:12
//
//The number is 12.
//Press any key to end ...
「已注销」 2011-03-08
  • 打赏
  • 举报
回复
if (n == n/1)
{
//是不含小数部分的“整数”,不一定是整型数
}
loveanybody 2011-03-08
  • 打赏
  • 举报
回复
你的问题很不清楚 因为如果你不管输入的是小数也好还是什么 关键是看你存储数据的时候你用到的是什么类型的变量 如果你输入的是个小数 但是存储的时候是个int的整形 那么就算你输入的是小数 它也是整数 如果你是想由字符串转的话 先判断第一个标识是不是符号位 如果是就是负数 如果不是就是正数 然后再将和后面的数进行一个atof 转成浮点数 对1取余 如果为0 那么就是整数 如果不为0 就是小数
LinuxBirdMan 2011-03-08
  • 打赏
  • 举报
回复
把数字转换成字符看是否有小数点

69,372

社区成员

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

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