算法竞赛入门经典 习题3-4简单计算器

#include 
#include 
#include 
void trim(char *dest, char *src){
	char *tmp = src;
	while (*tmp != '\0'){
		if (*tmp != ' ' && *tmp != '\t'){
			*dest++ = *tmp;
		}
		++tmp;
	}
	*dest = '\0';
}
int main(){
	char in[100];
	char buf[100];
	memset(in, '\0', sizeof(in));
	memset(buf, '\0', sizeof(buf));
	fgets(in, sizeof(in), stdin);
	trim(buf, in);

	char *tmp = buf;
	int a = 0;
	int b = 0;
	char op = '\0';
	bool first = true;
	while (*tmp != '\0'){
		if (isdigit(*tmp)){
			if (first){
				a = a * 10 + (*tmp - '0');
			}
			else{
				b = b * 10 + (*tmp - '0');
			}
		}
		else if (op == '\0'){
			op = *tmp;
			first = false;
		}
		++tmp;
	}
	int result;
	switch (op){
	case '+':
		result = a + b;
		break;
	case '-':
		result = a - b;
		break;
	case '*':
		result = a * b;
		break;
	case '/':
		result = a / b;
		break;
	default:
		break;
	}
	printf("%d\n", result);
	return 0;
}

你可能感兴趣的:(竞赛,入门,acm)