本文实例为大家分享了C语言实现计算器的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*===============================================*\ ** 设计目的:简单计算器,计算形如10*(20.2-30.6)+5.0/2的表达式值 ** 简要原理:中缀式转逆波兰式(后缀式) ** IDE:Dev-Cpp 4.9.9.2 ** 注意事项:括号必须是英文状态的 ** 时间: 2014-6-17 \*===============================================*/ #include <stdio.h> #include <string.h> #include <stdlib.h> #define maxn 1000 char buf[maxn], str[maxn], signStack[maxn], ch[2]; int len, id, idSign, idAns, i, n; double ans[maxn]; void checkSign( char sign){ if (sign == '(' ) signStack[idSign++] = sign; else if (sign == '*' || sign == '/' ){ while (idSign && (signStack[idSign-1] == '*' || signStack[idSign-1] == '/' )) str[id++] = signStack[--idSign]; signStack[idSign++] = sign; } else if (sign == ')' ){ while (signStack[idSign-1] != '(' ) str[id++] = signStack[--idSign]; --idSign; } else { while (idSign && signStack[idSign-1] != '(' ) str[id++] = signStack[--idSign]; signStack[idSign++] = sign; } str[id++] = ' ' ; } int check( double a, char sign){ if (a == 0 && sign == '/' ){ printf ( "除数不能为0!,程序结束。\n" ); system ( "pause" ); exit (EXIT_FAILURE); } return 1; } double cal( double a, double b, char sign){ switch (sign){ case '+' : return a + b; case '-' : return a - b; case '*' : return a * b; case '/' : return a / b; } } int main(){ printf ( "\t\t\t\t简单计算器\n\n" ); printf ( "\t\t ======================================\n" ); printf ( "\t\t || 支持+ - * / ( ) 等符号和负数输入 ||\n" ); printf ( "\t\t ======================================\n" ); printf ( "\t\t\t\t\t\t原创作者:邱正钢\n" ); printf ( "\t\t\t\t\t\t完成时间:2014年6月17日\n" ); printf ( "\t\t\t\t\t\tEmail:2276479303@qq.com\n" ); printf ( "请输入一个表达式,如 -10*(20.2-30.6)+5.0/2\n以回车结束:\n" ); do { gets (buf); id = idSign = idAns = 0; if ((len = strlen (buf)) == 0) continue ; int flag = 0; //这个是用来判断'-'字符是负号还是减号的。1表示数字,0表示 +*/( /*判断原理:如果第一次读取就碰到-,那么一定是负号,若最近一次读取是+*-/(那么 也一定是负号,如果是右括号或数字那么是减号*/ for (i = 0; i < len; ++i){ if (buf[i] == ' ' ) continue ; if (buf[i] >= '0' && buf[i] <= '9' || buf[i] == '.' || buf[i] == '-' && !flag){ str[id++] = buf[i]; flag = 1; } else { str[id++] = ' ' ; checkSign(buf[i]); if (buf[i] != ')' ) flag = 0; else flag = 1; } } while (idSign) str[id++] = signStack[--idSign]; for (i = 0, n = 0; i < id; ++i){ if (str[i] == ' ' ) continue ; /*用增加空格的方式判断‘-'字符是负号还是减号,若‘-'后面挨着数字或小数点,那么一定是负号*/ if (str[i] >= '0' && str[i] <= '9' || str[i] == '.' || str[i] == '-' && (str[i+1] >= '0' && str[i+1] <= '9' || str[i+1] == '.' )){ sscanf (str + i, "%lf%n" , &ans[idAns++], &n); i += n - 1; continue ; } else if (check(ans[idAns-1], str[i])){ ans[idAns-2] = cal(ans[idAns-2], ans[idAns-1], str[i]); --idAns; } } printf ( "结果是 %.2lf\n" , ans[0]); printf ( "输入Y 继续, N 退出: " ); scanf ( "%s" , ch); } while (ch[0] == 'Y' || ch[0] == 'y' ); printf ( "感谢您的使用!再见\n" ); system ( "pause" ); return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/chang_mu/article/details/31821185