自制计算器问题
file:///C:\Users\zhao\AppData\Roaming\Tencent\Users\954260765\QQ\WinTemp\RichOle\@3C`{(_CD5LZ{CL`SJ]YC@R.jpgfile:///C:\Users\zhao\AppData\Roaming\Tencent\Users\954260765\QQ\WinTemp\RichOle\@3C`{(_CD5LZ{CL`SJ]YC@R.jpg#include<stdio.h>float fun(float x,float y,char c);
main()
{
float x,y,s;
char c;
printf("please enter two interge and a char :");
scanf("%f%f%c",&x,&y,&c);
s=fun(x,y,c);
printf("%.2f",s);
}
float fun(float x,float y,char c)
{
float s=0;
switch(c)
{
case '+':s=x+y;break;
case '-':s=x-y;break;
case '*':s=x*y;break;
case '/':s=x/y;break;
}
return(s);
} 输入以后就错了,难道又是因为是局部变量的关系吗
看注释#include<stdio.h>
float fun(float x,float y,char c);
int main()
{
float x,y,s;
char c;
printf("please enter two interge and a char :");
scanf("%f%*c%f%*c%c",&x,&y,&c); //%*c去除空格或回车
s=fun(x,y,c);
printf("%.2f",s);
return 0;
}
float fun(float x,float y,char c)
{
float s=0;
switch(c)
{
case '+':s=x+y;break;
case '-':s=x-y;break;
case '*':s=x*y;break;
case '/':s=x/y;break;
}
return(s);
} 本帖最后由 oggplay 于 2014-3-7 22:23 编辑
switch(i) {case1... ;case 2......; .....}
!!!!!!! 本帖最后由 oggplay 于 2014-3-7 23:32 编辑
#include<stdio.h>
float fun(float x,float y,char c);
int main()
{
float x,y,s;
char c;
printf("please enter two interge and a char :");
scanf("%f%f%c",&x,&y,&c);
s=fun(x,y,c);
printf("s=%.8f\n",s);return 0;
}
float fun(float x,float y,char c)
{
float b;
c=getchar();
switch(c){
case '+':b=x+y;break;
case '-':b=x-y;break;
case '*':b=x*y;break;
case '/':b=x/y;break;
default:printf("error result!\n");
}
return b;
}
这里改成这样
scanf("%f %f %c",&x,&y,&c);
输入的时候以空格分隔 #include<stdio.h>
void Input(float * n1,float * n2);
int main()
{
float n1,n2,sum;
char ch;
s: setbuf(stdin,NULL);
printf("-----------------\n");
printf("- please choose -\n");
printf("- 1 : + -\n");
printf("- 2 : - -\n");
printf("- 3 : * -\n");
printf("- 4 : / -\n");
printf("- 5 : exit-\n");
printf("-----------------\n");
ch = getchar();
if(ch == '5')
{
return 0;
}
else
{
switch(ch)
{
case '1':
case '+':
Input(&n1,&n2);
sum = n1 + n2;
break;
case '2':
case '-':
Input(&n1,&n2);
sum = n1 - n2;
break;
case '3':
case '*':
Input(&n1,&n2);
sum = n1 * n2;
break;
case '4':
case '/':
Input(&n1,&n2);
sum = n1 / n2;
break;
default:
printf("Input error!\n");
goto s;
}
}
printf("sum = %f\n",sum);
goto s;
}
void Input(float * n1,float * n2)
{
printf("Please putinto the fist number:");
scanf("%f",n1);
printf("Please input the second number:");
scanf("%f",n2);
}
oggplay 发表于 2014-3-7 22:46 static/image/common/back.gif
为什么要加getchar,前面输入的话,总不至于吸收enter吧 oggplay 发表于 2014-3-7 22:17 static/image/common/back.gif
switch(i) {case1... ;case 2......; .....}
!!!!!!!
switch这里有什么错吗 #include<stdio.h>
#include <stdlib.h>
float fun(float x,float y,char c);
int main(void)
{
float x,y,s;
char c;
printf("please enter two integer and a char :");
scanf("%f,%f,%c",&x,&y,&c);
s=fun(x,y,c);
printf("%.2f",s);
system("pause");
return 0;
}
float fun(float x,float y,char c)
{
float s=0;
switch(c)
{
case '+':s=x+y;break;
case '-':s=x-y;break;
case '*':s=x*y;break;
case '/':s=x/y;break;
}
return(s);
}
页:
[1]