2021-05-19 C语言

int calculate(double a, double b, char cal, double *result) {
int ret = 1;
switch (cal) {
case '+':
*result = a + b;
break;
case '-':
result = a - b;
break;
case '
':
*result = a * b;
break;
case '/':{
if (b == 0){
ret = 0;
break;
}
*result = a / b;
break;
}
default:
ret = 0;
break;
}
return ret;
}

int main() {
while (1) {
double a, b, result;
char cal;
printf("/******************************************************************/\n");
printf("请输入算式,例如1+1,按回车输出结果\n");
scanf("%lf%c%lf",&a,&cal,&b);
if (calculate(a, b, cal, &result)) {
printf("结果为:%f\n",result);
}else{
printf("非法字符,清重新输入\n");
}
fflush(stdin);
}
return 0;
}

方法1:

include

void main ()
{
int i,flag,n,z,w,d,h;
printf("*****ª∂”≠ π”√ºÚµ•º∆À„∆˜*******\n");

for(n=1;n<100;n++)
{
printf("\n1º”,2ºı,3≥À,4≥˝, 0ÕÀ≥ˆ,( ‰»Î ˝µƒ ±∫Ú÷–º‰”√ø’∏Ò∏Ùø™)\n");
printf("«Î—°‘Òƒ˙“™Ω¯––µƒ‘ÀÀ„:\n");
scanf("%d",&i);
if(i==0) break;
switch(i)
{
case 1:flag=1;break;
case 2:flag=2;break;
case 3:flag=3;break;
case 4:flag=4;break;
case 5:flag=5;break;
default:printf(" ‰»Î”–¥ÌŒÛ!\n");break;
}

if(flag==1)
{
for(z=1;z<100;z++)
{

double fa(double x,double y);/º”∑®‘ÀÀ„/
double x,y;
printf("ƒ˙’˝‘⁄Ω¯––º”∑®‘ÀÀ„:ÕÀ≥ˆ«Î ‰»Îx,yŒ™0º¥ø…\n");
printf("ƒ˙’˝‘⁄Ω¯––µ⁄%d∏ˆº”∑®‘ÀÀ„\n",z);
printf("«Î ‰»Îx,yµƒ÷µ:\n");
scanf("%lf%lf",&x,&y);
if(x==0&&y==0) break;
printf("%lf+%lf=%lf\n",x,y,x+y);

}
}
else if(flag==2)
{
for(w=1;w<100;w++)
{
double fb(double x,double y);/ºı∑®‘ÀÀ„/
double x,y;
printf("ƒ˙’˝‘⁄Ω¯––ºı∑®‘ÀÀ„:ÕÀ≥ˆ«Î ‰»Îx,yŒ™0º¥ø…\n");
printf("ƒ˙’˝‘⁄Ω¯––µ⁄%d∏ˆºı∑®‘ÀÀ„\n",w);
printf("«Î ‰»Îx,yµƒ÷µ:\n");
scanf("%lf %lf",&x,&y);
if(x==0&&y==0) break;
printf("%lf-%lf=%lf\n",x,y,x-y);
}
}
else if(flag==3)
{
for(d=1;d<100;d++)
{
double fc(double x,double y);/≥À∑®‘ÀÀ„/
double x,y;
printf("ƒ˙’˝‘⁄Ω¯≥À∑®‘ÀÀ„:ÕÀ≥ˆ«Î ‰»Îx,yŒ™0º¥ø…\n");
printf("ƒ˙’˝‘⁄Ω¯––µ⁄%d∏ˆ≥À∑®‘ÀÀ„\n",d);
printf("«Î ‰»Îx,yµƒ÷µ:\n");
scanf("%lf %lf",&x,&y);
if(x==0&&y==0) break;
printf("%lf%lf=%lf\n",x,y,xy);
}
}
else if(flag==4)
{
for(h=1;h<100;h++)
{
double fd(double x,double y);/≥˝∑®‘ÀÀ„/
double x,y;
printf("ƒ˙’˝‘⁄Ω¯≥˝∑®‘ÀÀ„:ÕÀ≥ˆ«Î ‰»Îx,yŒ™0º¥ø…\n");
printf("ƒ˙’˝‘⁄Ω¯––µ⁄%d∏ˆ≥˝∑®‘ÀÀ„\n",h);
printf("«Î ‰»Îx,yµƒ÷µ:\n");
scanf("%lf %lf",&x,&y);
if(x==0&&y==0) break;
printf("%lf/%lf=%lf\n",x,y,x/y);
}
}
}
}

方法2:

include

int add(int a,int b){
return a+b;
}

int minus(int a,int b){
return a-b;
}

int mutiple(int a,int b){
return a*b;
}

int devide(int a,int b){
return a/b;
}

int (*fn[])(int,int)={add,minus,mutiple,devide};

void menu()
{
printf("*************************************\n");
printf("***** 1.add 2.minus ******\n");
printf("***** 3.multiple 4.devide *****\n");
printf("********** 5.exit ********** \n");
printf("*************************************\n");
}

int main(){
int chose;
while(1){
menu();
scanf("%d",&chose);
if(chose<1||chose>=6){
printf("The Index Is Invalid! please input right index!\n\n");
}
switch(chose){
int op1,op2;
case 1:
printf("input two numbers:\n");
scanf("%d %d",&op1,&op2);
printf("result=%d\n",fn0);
break;
case 2:
printf("input two numbers:\n");
scanf("%d %d",&op1,&op2);
printf("result=%d\n",fn1);
break;
case 3:
printf("input two numbers:\n");
scanf("%d %d",&op1,&op2);
printf("result=%d\n",fn2);
break;
case 4:
printf("input two numbers:\n");
scanf("%d %d",&op1,&op2);
printf("result=%d\n",fn3);
break;
case 5:
printf("exit successful\n");
exit(0);
}
}
}

方法3:
int main()
{

int a=0;
int b=0;
char m;
int result=0;
printf("请输入数字:\r\n");
scanf("%d",&a);
getchar();
printf("请输入运算符:+ - * / \r\n");
scanf("%c",&m);
getchar();
printf("请输入数字:\r\n");
scanf("%d",&b);
getchar();
switch (m)
{
case '+':{
    result=a+b;
         }break;
case '-':{
    result= a-b;
         }break;
case '*':{
    result=a*b;
         }break;
case '/':{
    result=a/b;
         }break;
default:
    result=-1;//异常情况
    break;
}
printf("运算结果:%d",result);
printf("\r\n");
system("pause");

你可能感兴趣的:(2021-05-19 C语言)