C语言,编写一个程序 用户输入运算符和四则运算符,输出计算结果

如题所述

#include<stdio.h>

int main() 

{ int a=1,b,c;

  char op;

  while(a||b)

  {

 printf("请输入一个算式(二数都为0时结束):");

 scanf("%d%c%d",&a,&op,&b);

 if(a==0&&b==0)break;

 if(op=='+')c=a+b;

 if(op=='-')c=a-b;

 if(op=='*')c=a*b;

 if(op=='/')c=a/b;

 if(op=='%')c=a%b;

 printf("%d%c%d=%d\n",a,op,b,c);

  }

getch();

return 0;

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-01-04
#include<stdio.h>
typedef float (*function)(float, float);
float add(float a, float b){
return a+b;
}
float sub(float a, float b){
return a-b;
}
float mul(float a, float b){
return a*b;
}
float div(float a, float b){
return a/b;
}
void show_menu(){
printf("1.add(+)\n");
printf("2.sub(-)\n");
printf("3.mul(*)\n");
printf("4.div(/)\n");
printf("5.quit(q)\n");
printf("PLS enter your choice:");
}
void prepare_numbers(float *num1, float *num2){
printf("PLS enter 2 float:");
while(scanf("%f%f", num1, num2) != 2){
printf("PLS enter 2 float:");
scanf("%*c");
}
}
function getfunc(){
char choice;
while(1){
show_menu();
choice = getchar();
scanf("%*c");
switch(choice){
case '1': case '+':
printf("your choice is add\n");
return add;
case '2': case '-':
printf("your choice is sub\n");
return sub;
case '3': case '*':
printf("your choice is mul\n");
return mul;
case '4': case '/':
printf("your choice is div\n");
return sub;
case '5': case 'q':
return NULL;
default:
;
}
}
}
int main(){
float result, num1, num2;
function func;
func = getfunc();
if(func){
prepare_numbers(&num1, &num2);
printf("func(num1, num2)=%f\n", func(num1, num2));
}
return 0;
}