请教:如何判断一个浮点型变量的值是不是整数?

TinkyWinky 2008-05-08 11:47:46
哪位大虾能告诉我判断一个浮点型变量的值是不是整数的最简单方法?
...全文
440 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
星羽 2008-05-09
  • 打赏
  • 举报
回复


#include "iostream"
using namespace std;

enum {efloat, eint, notnum };

int check(const char str[]) {

int dot = 0;

for (size_t i = 0; i < strlen(str); ++i) {

if (!isdigit(str[i])) {

if (str[i] == '.') {
++dot;
if (dot > 1)
return notnum;
} else {
return notnum;
}

}
}

if (dot == 1)
return efloat;

return eint;
}

int main()
{
double d = 0.0;
int n = 0;
char t[128];

bool bcontinue = true;

while (bcontinue) {

cin>>t;

switch (check(t)) {
case efloat :
d = atof(t); // 转成double
cout<<"is float "<<d<<endl;
break;
case eint :
n = atoi(t); // 转成int
cout<<"is int "<<n<<endl;
break;
default :
bcontinue = false;
break;

}



}

return 0;

}

TinkyWinky 2008-05-09
  • 打赏
  • 举报
回复
楼上两位高人都是用字符数组来获取输入的值,而我是想让一个浮点型变量获取输入的值。。。。
c_spark 2008-05-09
  • 打赏
  • 举报
回复
如含有非数字字符,则不是整数
#include <stdio.h>

int main()
{
char str[20];
int flag = 0;
int v;
flag = 0;
scanf("%s",str);
for(int i = 0; str[i] != '\0'; ++i)
{
if(!(str[i] >= '0'&&str[i] <= '9'))
{
flag = 1;
break;
}
}
if(flag == 0)
{
sscanf(str,"%d",&v);
printf("%d is integer\n",v);
}
else
printf("%s is not integer",str);
return 0;
}

1.0
1.0 is not integer
星羽 2008-05-09
  • 打赏
  • 举报
回复

随便写了下,你参考下吧

#include "iostream"
using namespace std;

enum {efloat, eint, notnum };

int check(const char str[]) {

int dot = 0;

for (size_t i = 0; i < strlen(str); ++i) {

if (!isdigit(str[i])) {

if (str[i] == '.') {
++dot;
if (dot > 1)
return notnum;
} else {
return notnum;
}

}
}

if (dot == 1)
return efloat;

return eint;
}

int main()
{
double d = 0.0;
char t[128];

bool bcontinue = true;

while (bcontinue) {

cin>>t;

switch (check(t)) {
case efloat :
cout<<"is float"<<endl;
break;
case eint :
cout<<"is int"<<endl;
break;
default :
bcontinue = false;
break;

}



}

return 0;

}


--------

测试

1
is int
123123
is int
1.123123
is float
1.00000000000000000000001
is float
1.0
is float
1
is int
a

TinkyWinky 2008-05-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Chiyer 的回复:]
C/C++ code
#include <stdio.h>


int main()
{
float x = 0.f;
scanf("%f", &x);

if (x == (int)x)
printf("is int\n");

return 0;

}
[/Quote]
可能是我说得不清楚。。
补充一下:
如果那个浮点型变量的值是从cin中输入的话应该怎么判断呢?
比如我输入1.0它就判断是浮点型,输入1它就判断为整型。
kojie_chen 2008-05-09
  • 打赏
  • 举报
回复
x==(int)x
或者 就是判断小数点了
svchost 2008-05-09
  • 打赏
  • 举报
回复
楼上用的什么编译器?
xiaoc10 2008-05-09
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
float result = 0;
int x;
char input[100],c = 'n';
while( scanf("%s", input) )
{
sscanf(input, "%d%c", &x, &c);
if(c == '.')
{
cout << input << " is not a int." << endl;
sscanf(input, "%f", &result);
cout << "The input is set to result: " << result << endl;
c = 'n';
}
else
cout << input << " is a int" << endl;
}
system("pause");
return 0;
}

星羽 2008-05-08
  • 打赏
  • 举报
回复

#include <stdio.h>


int main()
{
double x = 0.f;

while (scanf("%lf", &x) == 1) {
if (x == (int)x)
printf("is int\n");
else
printf("is not int\n");
}
return 0;

}


在double精度内有效


--------

测试

1
is int
2
is int
1.1
is not int
1.00000000001
is not int
1.0000123123000123
is not int
12323123.000
is int
a

星羽 2008-05-08
  • 打赏
  • 举报
回复

#include <stdio.h>


int main()
{
float x = 0.f;
scanf("%f", &x);

if (x == (int)x)
printf("is int\n");

return 0;

}

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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