这个程序哪里错了?结果和我预期要的不一样

junlovehang1314520 2010-10-30 08:10:05

#include <stdio.h>
void main()
{
int a,b;
float c;
c=a/b;
scanf("%d,%d",&a,&b);
printf("%f\n",c);
}
这个程序哪里错了?结果和我预期要的不一样
...全文
83 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
junlovehang1314520 2010-10-30
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hnuqinhuan 的回复:]
[Quote=引用 3 楼 junlovehang1314520 的回复:]
#include <stdio.h>
int main()
{
int a,b;
float c;

scanf("%f,%f",&amp;a,&amp;b);
c=a%b;
printf("%f\n",c);
return……
[/Quote]
谢谢~~
無_1024 2010-10-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 junlovehang1314520 的回复:]
#include <stdio.h>
int main()
{
int a,b;
float c;

scanf("%f,%f",&a,&b);
c=a%b;
printf("%f\n",c);
return 0;
}
这个程序结果为什么不是2而是0?
[/Quote]
请问楼主的输入数据是什么 为什么定义的a b是int 型而输入时又是float
而且浮点数的取模运算不能用 % 而是用math库里面的fmod(x,y)函数

#include <stdio.h>
#include <math.h>
#include <conio.h>
main()
{
float a,b;
float c;
scanf("%f %f",&a,&b);
c=fmod(a,b);//求浮点数的余数 也就是对付点数取模运算
printf("%f\n",c);
getch();
}
junlovehang1314520 2010-10-30
  • 打赏
  • 举报
回复
我是输入5,3。
如果把a,b定义成float程序就会出错
flysnowhite 2010-10-30
  • 打赏
  • 举报
回复
你是初学者吧!程序问题很多。a,b是整型,scanf语句应该输入整型,即格式控制字符应该为%d.
完整程序如下:
int main()
{
int a,b;
float c;

scanf("%d,%d",&a,&b);
c=a%b;
printf("%f\n",c);
return 0;
}
这样就没问题了。
xiaoqiao_82 2010-10-30
  • 打赏
  • 举报
回复
是的。整数除以整数还是整数。
5/2 = 2
转换为浮点数是2.0
try325 2010-10-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 junlovehang1314520 的回复:]

#include <stdio.h>
int main()
{
int a,b;
float c;

scanf("%f,%f",&a,&b);
c=a%b;
printf("%f\n",c);
return 0;
}
这个程序结果为什么不是2而是0?
[/Quote]
这个你得告诉我们输入a,b是多少
cap77 2010-10-30
  • 打赏
  • 举报
回复
楼主是不是输入数据时,两数据间没加逗号啊?
dingshaofengbinbin 2010-10-30
  • 打赏
  • 举报
回复
这没什么吧??要干什么??
junlovehang1314520 2010-10-30
  • 打赏
  • 举报
回复
#include <stdio.h>
int main()
{
int a,b;
float c;

scanf("%f,%f",&a,&b);
c=a%b;
printf("%f\n",c);
return 0;
}
这个程序结果为什么不是2而是0?
xiaolomg 2010-10-30
  • 打赏
  • 举报
回复
你预期的结果是嘛?
某某9 2010-10-30
  • 打赏
  • 举报
回复
#include <stdio.h>
int main()
{
float a,b;
float c;

scanf("%f,%f",&a,&b);
c=a/b;
printf("%f\n",c);
return 0;
}

69,381

社区成员

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

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