#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #define inf float(0x3f3f3f3f) #define MAXSIZE 100 char priority[7] = {'+', '-', '*', '/', '(', ')', '#'}; char priority_relationship[7][7] = { {'>', '>', '<', '<', '<', '>', '>'}, {'>', '>', '<', '<', '<', '>', '>'}, {'>', '>', '>', '>', '<', '>', '>'}, {'>', '>', '>', '>', '<', '>', '>'}, {'<', '<', '<', '<', '<', '=', ' '}, {'>', '>', '>', '>', ' ', '>', '>'}, {'<', '<', '<', '<', '<', ' ', '='} }; typedef struct StackNode { char data[MAXSIZE]; // 壓入棧裏面的數據都是字符型,在進行運行時,記得將字符型數字轉換爲浮點型數字 struct StackNode *next; }StackNode, *LinkStack; void InitStack(LinkStack &S) {// 構造一個空棧S,棧頂指針置空 S = NULL; } void Push(LinkStack &S, char data[]) {// 在棧頂插入元素data StackNode *p; p = (StackNode *)malloc(sizeof(StackNode)); // 生成新的結點 strcpy(p->data, data); // 將新結點的數據域置爲data p->next = S; // 將新結點插入棧頂 S = p; // 修改棧頂指針爲p } char *Pop(LinkStack &S) {// 刪除S的棧頂元素, 用data返回其值 char data[MAXSIZE]; if(S == NULL) printf("錯誤!!!\n棧爲空, 沒法執行刪除命令..."); else { StackNode *p; strcpy(data, S->data); // 將棧頂元素賦給data p = S; // 用p臨時保存棧頂元素的空間,以備釋放 S = S->next; //修改棧頂指針 free(p); // 釋放原棧頂元素的空間 return data; } } char *GetTop(LinkStack &S) { if(S != NULL) return S->data; else { printf("錯誤!!!\n棧頂爲空"); return "0"; } } float str_to_float(char *str) { float num = 0; int state_1 = 0; int state_2 = 0; while(( *str != '\0' && *str >= '0' && *str <= '9') || *str == '.' || (*str == '-' && *(str + 1) != '\0')) {// 注意判斷小數點和負號 if(*str == '.') state_1 = 1; else if(*str == '-') state_2 = 1; else { if(state_1 == 0) num = num * 10 + (*str - '0'); else { num += (*str - '0') * pow(0.1, state_1); state_1++; } } str++; } if(*str != '\0') return inf; else if(state_2 == 1) { return num * -1; } else return num; } char *float_to_str(float num) { char str[MAXSIZE]; sprintf(str, "%.4f", num); // 保留小數點後4位 return str; } int get_index(char str[]) { for(int i = 0; i < 7; i++) { if(str[0] == priority[i]) return i; } printf("未找到匹配的字符\n"); } char Precede(char inside_data[], char input_data[]) { int inside_index = get_index(inside_data); int input_index = get_index(input_data); return priority_relationship[inside_index][input_index]; } float Operate(char a[], char theta[], char b[]) {//執行運算 float a_num = str_to_float(a); float b_num = str_to_float(b); if(theta[0] == '+') return a_num + b_num; else if(theta[0] == '-') return a_num - b_num; else if(theta[0] == '*') return a_num * b_num; else if(theta[0] == '/') return a_num / b_num; else printf("錯誤!!!\n無該運算符"); } float EvaluateExpression() { StackNode *OPND, *OPTR; char str[MAXSIZE]; char theta[MAXSIZE]; char a[MAXSIZE]; char b[MAXSIZE]; InitStack(OPND); InitStack(OPTR); Push(OPTR, "#"); scanf("%s", str); while(str[0] != '#' || GetTop(OPTR)[0] != '#') { if(str_to_float(str) != inf) { Push(OPND, str); scanf("%s", str); } else { switch (Precede(GetTop(OPTR), str)) { case '<': Push(OPTR, str); scanf("%s", str); break; case '>': strcpy(theta, Pop(OPTR)); strcpy(b, Pop(OPND)); strcpy(a, Pop(OPND)); char temp_str[MAXSIZE]; strcpy(temp_str, float_to_str(Operate(a, theta, b))); Push(OPND, temp_str); break; case '=': Pop(OPTR); scanf("%s", str); break; default: printf("錯誤!!!\n該優先級不存在!!!"); } } } return str_to_float(GetTop(OPND)); } int main(void) { float num; num = EvaluateExpression(); printf("%f\n", num); printf("\n"); system("pause"); }