1. 程式人生 > >11.表達式計算對一串加減乘除帶括號進行計算

11.表達式計算對一串加減乘除帶括號進行計算

pan fin gpo ++ 字符 第一個 std 取數 main

算法流程圖:

技術分享圖片

執行截圖:

技術分享圖片

完整代碼:

  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 != \0) 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] != \0);//(str[(*pindex)++] != ‘\0‘);//判斷字符串有沒有到結尾 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 \0: 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("%[^\n]", str); 196 //printf("要計算的是:%s\n", str); 197 deleteSpace(str); 198 /*printf("刪除空格後:%s\n", str); 199 200 int index = 0; 201 double value = getnum(str, &index); 202 printf("第一個獲取的數據%f", value);*/ 203 printf("計算結果:%f\n", fenxi(str)); 204 205 system("pause"); 206 }

11.表達式計算對一串加減乘除帶括號進行計算