这题目我也确实没有好办法
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
int calc(int a, int b, char op) {
switch(op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
case '%': return a % b;
}
return 0;
}
bool read_number(int *n) {
char ch = getchar();
switch(ch) {
case '+':
ungetc(ch, stdin);
return false;
case '-': {
char temp = getchar();
if(!isdigit(temp)) {
ungetc(temp, stdin);
ungetc(ch, stdin);
return false;
}
ungetc(temp, stdin);
scanf("%d", n);
*n = -*n;
return true;
}
}
ungetc(ch, stdin);
return scanf("%d", n) == 1;
}
int main(void) {
int max = 0, min = 0;
char op;
while(1) {
scanf(",");
int temp;
if(read_number(&temp)) {
if(max < temp) max = temp;
if(min > temp) min = temp;
} else {
scanf("%c,=", &op);
break;
}
}
const char *format = "%d %c %d = %d\n";
if(min < 0) format = "%d %c (%d) = %d\n";
printf(format, max, op, min, calc(max, min, op));
return 0;
}
|