在之前版本中使用栈结构来实现,但由于51单片机不支持malloc函数,所以使用C语言又写了一个计算器版本。
通过数组存放值和操作符模拟栈操作。
实现代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <math.h> 4 #define OK 1 5 #define ERROR 0 6 int InputJudge(char c); //判断函数是否为数字 7 float Calc(char optr, float num1, float num2); 8 char PriorityJudge(char optr1, char optr2); 9 int main() 10 { 11 char arrayChar[20]; 12 float arrayFloat[20] = {0}; 13 char tempChar, optr, c; 14 float tempFloat, num1, num2; 15 int topChar = 0; 16 int topFloat = 0; 17 int i; 18 for (i = 0; i < 20; ++i) 19 { 20 arrayChar[i] = ‘0‘; 21 } 22 arrayChar[topChar] = ‘#‘; 23 topChar++; 24 c = getchar(); 25 while(c != ‘#‘ || arrayChar[topChar - 1] != ‘#‘) 26 { 27 if (InputJudge(c)) 28 { 29 arrayFloat[topFloat] = (float)(c - ‘0‘); 30 topFloat++; 31 c = getchar(); 32 /* 当连续输入数字时,计算十位数和百位数 */ 33 while(InputJudge(c)) 34 { 35 topFloat--; 36 tempFloat = arrayFloat[topFloat]; 37 arrayFloat[topFloat] = (float)(tempFloat * 10 + (c - ‘0‘)); 38 topFloat++; 39 c = getchar(); 40 } 41 if (c == ‘.‘) 42 { 43 i = 1; 44 c = getchar(); 45 do 46 { 47 topFloat--; 48 tempFloat = arrayFloat[topFloat]; 49 tempFloat = tempFloat + ((c - ‘0‘) / pow(10, i)); 50 i++; 51 arrayFloat[topFloat] = tempFloat; 52 topFloat++; 53 c = getchar(); 54 } 55 while(InputJudge(c)); 56 } 57 } 58 else 59 { 60 tempChar = arrayChar[topChar - 1]; 61 switch(PriorityJudge(tempChar, c)) 62 { 63 case ‘<‘: 64 arrayChar[topChar] = c; 65 topChar++; 66 c = getchar(); 67 break; 68 case ‘=‘: 69 topChar--; 70 c = arrayChar[topChar]; 71 c = getchar(); 72 break; 73 case ‘>‘: 74 topChar--; 75 optr = arrayChar[topChar]; 76 topFloat--; 77 num2 = arrayFloat[topFloat]; 78 topFloat--; 79 num1 = arrayFloat[topFloat]; 80 arrayFloat[topFloat] = Calc(optr, num1, num2); 81 topFloat++; 82 break; 83 } 84 } 85 } 86 while(topFloat != 0) 87 { 88 topFloat--; 89 tempFloat = arrayFloat[topFloat]; 90 printf("%f\n", tempFloat); 91 } 92 return OK; 93 } 94 float Calc(char optr, float num1, float num2) 95 { 96 switch(optr) 97 { 98 case ‘+‘: 99 return (num1 + num2); 100 break; 101 case ‘-‘: 102 return (num1 - num2); 103 break; 104 case ‘*‘: 105 return (num1 * num2); 106 break; 107 case ‘/‘: 108 return (num1 / num2); 109 break; 110 } 111 } 112 int InputJudge(char c) 113 { 114 switch(c) 115 { 116 case ‘0‘: 117 case ‘1‘: 118 case ‘2‘: 119 case ‘3‘: 120 case ‘4‘: 121 case ‘5‘: 122 case ‘6‘: 123 case ‘7‘: 124 case ‘8‘: 125 case ‘9‘: 126 return OK; 127 break; 128 case ‘.‘: 129 case ‘+‘: 130 case ‘-‘: 131 case ‘*‘: 132 case ‘/‘: 133 case ‘(‘: 134 case ‘)‘: 135 case ‘#‘: 136 return ERROR; 137 break; 138 default: 139 break; 140 } 141 } 142 char PriorityJudge(char optr1, char optr2) 143 { 144 int i, j; 145 char priorityTable[7][7] = 146 { 147 {‘>‘, ‘>‘, ‘<‘, ‘<‘, ‘<‘, ‘>‘, ‘>‘}, 148 {‘>‘, ‘>‘, ‘<‘, ‘<‘, ‘<‘, ‘>‘, ‘>‘}, 149 {‘>‘, ‘>‘, ‘>‘, ‘>‘, ‘<‘, ‘>‘, ‘>‘}, 150 {‘>‘, ‘>‘, ‘>‘, ‘>‘, ‘<‘, ‘>‘, ‘>‘}, 151 {‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘=‘, ‘0‘}, 152 {‘>‘, ‘>‘, ‘>‘, ‘>‘, ‘0‘, ‘>‘, ‘>‘}, 153 {‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘0‘, ‘=‘} 154 }; 155 switch(optr1) 156 { 157 case ‘+‘: 158 i = 0; 159 break; 160 case ‘-‘: 161 i = 1; 162 break; 163 case ‘*‘: 164 i = 2; 165 break; 166 case ‘/‘: 167 i = 3; 168 break; 169 case ‘(‘: 170 i = 4; 171 break; 172 case ‘)‘: 173 i = 5; 174 break; 175 case ‘#‘: 176 i = 6; 177 break; 178 } 179 switch(optr2) 180 { 181 case ‘+‘: 182 j = 0; 183 break; 184 case ‘-‘: 185 j = 1; 186 break; 187 case ‘*‘: 188 j = 2; 189 break; 190 case ‘/‘: 191 j = 3; 192 break; 193 case ‘(‘: 194 j = 4; 195 break; 196 case ‘)‘: 197 j = 5; 198 break; 199 case ‘#‘: 200 j = 6; 201 break; 202 } 203 return priorityTable[i][j]; 204 }
时间: 2024-10-28 20:59:30