11.表达式计算对一串加减乘除带括号进行计算

算法流程图:

执行截图:

完整代码:

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <math.h>
  5 #include <string.h>
  6 //删除空格
  7 void deleteSpace(char *str);
  8 //判断是不是数字
  9 bool isnum(char ch);
 10 //获取括号间的内容
 11 char *kuohao(char *str, int *pindex);
 12 //获取数据
 13 double getnum(char *str, int *pindex);
 14 //分析乘除
 15 double comfenxi(char *str, int *pindex);
 16 //分析加减
 17 double fenxi(char *str);
 18 
 19 //字符串删除空格
 20 void deleteSpace(char *str)
 21 {
 22     //获取首地址
 23     char *tmp = str;
 24     //如果没到结尾
 25     while (*str != '')
 26     {
 27         //如果不等于空格则进行赋值
 28         if (*str != ' ')
 29         {
 30             *tmp = *str;
 31             tmp++;
 32         }
 33         //不断遍历
 34         str++;
 35     }
 36     //字符串结尾
 37     *tmp = 0;
 38 }
 39 
 40 //判断当前字符是不是数字
 41 bool isnum(char ch)
 42 {
 43     return ch >= '0' && ch <= '9';
 44 }
 45 
 46 //截取括号里面的内容
 47 char *kuohao(char *str, int *pindex)
 48 {
 49     char *pstr = NULL;//指向截取的字符串
 50     int num = 0;//记录多少括号的对数
 51     int leftindex = *pindex;//记录左边括号的位置
 52 
 53     //直到循环到匹配到的')'
 54     do
 55     {
 56         switch (str[*pindex])
 57         {
 58             //如果是左括号num就加1,一直循环到匹配第一个'('的')'
 59         case '(': 
 60             num++;
 61             break; 
 62         case ')':
 63             //若果等于0,则是与第一个'('匹配的')'
 64             if (num == 0)
 65             {
 66                 (*pindex)++;//移动到括号的后面
 67                 //分配内存,长度为*pindex-leftindex
 68                 pstr = (char *)calloc(*pindex - leftindex, sizeof(char));
 69                 //字符串拷贝,拷贝到')'之前
 70                 strncpy(pstr, str + leftindex, *pindex - leftindex-1);
 71                 return pstr;
 72             }
 73             else
 74             {
 75                 num--;
 76             }
 77             break;
 78         }
 79         //往后移一位,如果没到结尾则继续
 80         (*pindex)++;
 81     } while (str[*pindex] != '');//(str[(*pindex)++] != '');//判断字符串有没有到结尾
 82 }
 83 
 84 //获取数据
 85 double getnum(char *str, int *pindex)
 86 { 
 87     //获取当前位置
 88     int id = *pindex;
 89     //存放结果
 90     double res = 0.0;
 91     //小数
 92     double xiaoshu = 0;
 93 
 94     while (str[id] == '(')
 95     {
 96         char *psubstr = NULL;//取出字符串
 97         *pindex = ++id;//跳到括号后面
 98 
 99         //获取从当前括号开始到下一个匹配的括号之间的数据
100         psubstr = kuohao(str, pindex);
101         //从截取的字符串中计算出数据
102         res = fenxi(psubstr);
103 
104         //释放内存
105         free(psubstr);
106         psubstr == NULL;
107         //返回计算出的结果
108         return res;
109     }
110 
111     //如果不是左括号,是数字则获取数据
112     while (isnum(str[id]))
113     {
114         res = res * 10 + str[id] - '0';
115         id++;
116     }
117 
118     //小数处理
119     if (str[id] == '.')
120     {
121         id++;
122         int count = 0;
123         
124         while (isnum(str[id]))
125         {
126             count++;
127             xiaoshu = xiaoshu * 10 + str[id] - '0';
128             id++;
129         }
130         xiaoshu = xiaoshu / pow(10, count);
131     }
132     //获得结果
133     res += xiaoshu;
134     //把数字后一位的位置赋给*pindex
135     *pindex = id;
136     return res;
137 
138 }
139 
140 //乘除法
141 double comfenxi(char *str, int *pindex)
142 {
143     double value = 0.0;
144     value = getnum(str, pindex);//获取第一个数据
145     while (1)
146     {
147         if (str[*pindex] == '*')
148         {
149             (*pindex)++;//下标移动
150             value *= getnum(str, pindex);
151         }
152         else if (str[*pindex] == '/')
153         {
154             (*pindex)++;//下标移动
155             value /= getnum(str, pindex);
156         }
157         else
158         {
159             break;
160         }
161     }
162 
163     return value;
164 }
165 
166 //运算处理
167 double fenxi(char *str)
168 {
169     double value = 0.0;
170     int index = 0;
171     value = comfenxi(str, &index);//获取第一个数据
172     while (1)
173     {
174         char ch = *(str + index);//取出字符
175         index++;
176         switch (ch)
177         {
178         case '':
179             return value;
180         case '+':
181             value += comfenxi(str, &index);
182             break;
183         case '-':
184             value -= comfenxi(str, &index);
185             break;
186         default:
187             break;
188         }
189     }
190 }
191 
192 void main()
193 {
194     char str[1024] = { 0 };
195     scanf("%[^
]", str);
196     //printf("要计算的是:%s
", str);
197     deleteSpace(str);
198     /*printf("删除空格后:%s
", str);
199 
200     int index = 0;
201     double value = getnum(str, &index);
202     printf("第一个获取的数据%f", value);*/
203     printf("计算结果:%f
", fenxi(str));
204 
205     system("pause");
206 }
原文地址:https://www.cnblogs.com/xiaochi/p/8359469.html