输入:
用逗号分隔的整数序列,及其运算符和等号
输出:
最大数 op 最小数=结果
说明:本题目应该可以不使用数组就可以完成,关键是如何处理负数和减法。
#include<stdio.h>
int main(){int min=100,max=-100,temp=1,calc;char c,d;do{c=getchar();if( c <= 57 && c >= 48 ){//扫描到一个数字temp *= (c-48); do{d=getchar();if(d >= 48 && d <= 57){//其后又一个数字temp = 10*temp + d-48;}else if(44 == d){break;}}while(1);if(temp<min){min=temp;}if(temp>max){max=temp;}temp=1;}else if(c==42||c==43||47==c||37==c){break;}else if(45==c){d=getchar();if(d >= 48 && d <= 57){//负号后有数字temp=0-d+48;do{d=getchar();//扫描到其后有数字if(d >= 48 && d <= 57){temp = 10*temp - d+48;}else if(44 == d){break;}}while(1); if(temp<min){min=temp;}if(temp>max){max=temp;}temp=1;}else{break;} }}while(1);if(max < 0){printf("(%d) %c (%d) = ",max,c,min);}if(min >= 0){printf("%d %c %d = ",max,c,min);}if(max >= 0 && min < 0){printf("%d %c (%d) = ",max,c,min);}switch(c){case 45:calc=max - min;break;case 47:calc=max / min;break;case 43:calc=max + min;break;case 42:calc=max * min;break;case 37:calc=max % min;break; }printf("%dn",calc);return 0;
}
本文发布于:2024-01-30 05:54:16,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170656525719699.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |