我决定使用代码制作一个计算器,但我的程序无法正常工作。当我输入我的操作数和新数字时,它似乎不会扫描操作数和数字,也不会启动循环。谢谢您的帮助。
#include <stdio.h>
#include <math.h>
float add(float x,float y);
float sub(float x,float y);
float div(float x,float y);
float exp(float x,float y);
float mult(float x,float y);
int main(){
float y,x;
char op;
printf("Type in a number\n");
scanf("%f",&x);
printf("Type in your operand and desired number\n");
scanf("%c",&op);
scanf("%f",&y);
while (!(op=='q')){
if(op=='+'){
printf("Your result is %.1f\n",add(x,y));
scanf("%c",&op);
scanf("%f",&y);
}
else if(op=='-'){
printf("Your result is %.1f\n",sub(x,y));
scanf("%c",&op);
scanf("%f",&y);
}
else if(op=='*'){
printf("Your result is %.1f\n",mult(x,y));
scanf("%c",&op);
scanf("%f",&y);
}
else if(op=='/'){
printf("Your result is %.1f\n",div(x,y));
scanf("%c",&op);
scanf("%f",&y);
}
else if(op=='^'){
printf("Your result is %.1f\n",exp(x,y));
scanf("%c",&op);
scanf("%f",&y);
}
}
printf("Your final result is %.1f\n",x);
return(0);
}
float add(float x,float y){
return (x+y);
}
float sub(float x,float y){
return (x-y);
}
float div(float x,float y){
return (x/y);
}
float exp(float x,float y){
x=pow(x,y);
return(x);
}
float mult(float x,float y){
return (x*y);
}