利用函数,写出一个简易的计算器

#include <stdio.h>
#include <stdlib.h>
float sum(float a, char c,float b,float sum) {
	switch (c) {
		case '+':sum=a+b;break; 
		case '-':sum=a-b;break;
		case '*':sum=a*b;break;
		case '/':sum=a/b;break;
	}
	return sum;			//返回计算后的结果。
}
void wh() {
	printf("请输入你要计算的两个数,仅支持+,-,*,/。回车获得结果\n输入格式:1+1;结束请输入alt+F4\n");
	while (1) {
		char c;
		float a, b, sum1 = 0;
		scanf("%f%c%f", &a, &c, &b);
		sum1 = sum(a, c, b, sum1);   //调用计算函数;
		printf("%.2f\n", sum1);
		system("cls");				//清屏
		printf("请输入你要计算的两个数,仅支持+,-,*,/。回车获得结果\n输入格式:1+1;结束请输入alt+F4\n");
		printf("%0.0f%c%0.0f=%.2f\n", a, c, b, sum1);
	}
}

int main(){

	
	wh();//调用函数输出函数。

	system("pause");//屏幕暂停。
	return 0;
}
posted @ 2020-10-31 17:45  onedust  阅读(870)  评论(0)    收藏  举报