Data Structure Guide: 实验案例 2-1: 简单计算器

题目:

模拟简单计算器,运算符无优先级,遇 ‘=’ 结束,非法输入符号报错!

实验代码:

#include
#include
#include
#include "Header.h"

#pragma warning(disable:4996)

void errorReport(char* string) {

	printf("%s", string);

}

int main() {

	char symbol;

	int sum;

	int cc;

	scanf("%d%c", &sum, &symbol);

	while (symbol != '=') {
		
		scanf("%d", &cc);

		switch (symbol) {

		case '+':
			sum += cc;
			break;

		case '-':
			sum -= cc;
			break;

		case '*':
			sum *= cc;
			break;

		case '/':
			if (cc == 0) {

				errorReport("Error: divider is zero!\n");
				break;

			} else {

				sum /= cc;
				break;

			}

		default:
			errorReport("Error: Input symbol error!");
			break;

		}

		scanf("%c", &symbol);

	}

	printf("%d", sum);

	return 0;

}

你可能感兴趣的:(DataStructure,Guide,C,算法)