C语言-栈的四则运算(带小数点和括号)
生活随笔
收集整理的這篇文章主要介紹了
C语言-栈的四则运算(带小数点和括号)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
思路:
整體過程是遍歷一次輸入的所需運算的字符串
每次對一個元素進行判斷和操作,一共兩個棧(一個數字棧,一個符號棧)
(1)如果是數字就入數字棧,然后繼續操作下一個字符串中的元素
下面是對括號的一些操作:
如果是左括號,就進符號棧,然后繼續操作下一個字符串中的元素
如果是右括號,就讓符號棧棧頂元素出棧,讓數字棧棧頂和次棧頂的兩個元素出棧進行運算,并將運算結果入數字棧,一直循環此操作直至遇見符號棧棧頂為左括號為止,遇見左括號后將其出符號棧,并繼續操作下一個字符串中的元素
(2)如果是符號:
如果符號棧為空,就直接入符號棧
如果符號棧棧頂的符號的優先級大于當前正在操作的字符串中的元素,就將符號棧棧頂符號出棧,將數字棧棧頂和次棧頂的兩個元素出棧,進行運算后將結果入數字棧
如果符號棧棧頂的符號的優先級小于當前正在操作的字符串中的元素,就入符號棧
(3)當字符串中的元素全部遍歷完后如果符號棧仍然有元素,就繼續從符號棧棧頂出一個元素,從數字棧棧頂出兩個元素,計算后將結果入數字棧,循環此操作直至符號棧為空為止
具體代碼如下:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define n 50//數字棧 typedef struct nodeFirst{double a[n];int size;// 表示棧中含有的元素數量 } stackFirst;//符號棧 typedef struct nodeSecond{char a[n];int size; } stackSecond;//數字棧出棧 double pop(stackFirst *p) {if (p -> size == 0)//printf("空棧");return 0;else {--(p -> size);return p -> a[p -> size];} }//符號棧出棧 char popSecond(stackSecond *p) {if (p -> size == 0)return 0;else {--(p -> size);return p -> a[p -> size];} }//返回數字棧棧頂元素 double top(stackFirst *p) {if (p -> size == 0)return 0;//輸出0表示空else {return p -> a[p -> size - 1];} }//返回符號棧棧頂元素 char topSecond(stackSecond *p) {if (p -> size == 0)return 0;//輸出0表示空else {return p -> a[p -> size - 1];} }//將數字棧置空 int empty(stackFirst *p) {return p -> size == 0; }//將符號棧置空 int emptySecond(stackSecond *p) {return p -> size == 0; }//數字棧入棧 void push(stackFirst *p, double b) {p -> a[p -> size] = b;++p -> size; }//符號棧入棧 void pushSecond(stackSecond *p, char b) {p -> a[p -> size] = b;++p -> size; }//比較符號優先級 int compare(char str) {if (str == '+' || str == '-') {return 1;} else if (str == '*' || str == '/') {return 2;} else {return 0;} }//計算 double counter(double x, double y, char str) {double ans = 0.0;if (str == '-') {ans = x - y;} else if (str == '+') {ans = x + y;} else if (str == '*') {ans = x * y;} else {ans = x / y;}return ans; }int main(void) {//數字棧stackFirst *first;first = (stackFirst *) malloc(sizeof(stackFirst));first -> size = 0;//符號棧stackSecond *second;second = (stackSecond *) malloc(sizeof(stackSecond));second -> size = 0;char a[100];printf("請輸入需要計算的算術表達式:\n");scanf("%s", a);int length = (int)strlen(a);//在用戶所輸入的式子后面加‘#’號a[length] = '#';length = length + 1;int i = 0;double x = 0;//出棧用于計算的符號char strtest;//出棧用于計算的數字double numFirst, numSecond;//用于保存當次計算結果double res;while (a[i] != '#') {x = 0;//如果是數字就入數字棧if (a[i] >= '0' && a[i] <= '9') {while (a[i] >= '0' && a[i] <= '9') {x *= 10;x += a[i++] - '0';}//計算是小數的情況if (a[i] == '.') {double d = 0.1;++i;while (a[i] >= '0' && a[i] <= '9') {x += ((a[i] - '0') * d);d *= 0.1;++i;}}//數字進棧push(first, x);continue;}//如果是符號,且符號棧為空,符號直接進符號棧if (second -> size == 0 && (a[i] == '+' || a[i] == '-' || a[i] == '*' || a[i] == '/' || a[i] == '(' || a[i] == ')')) {pushSecond(second, a[i]);++i;continue;}//如果是左括號,直接入棧if (a[i] == '(') {pushSecond(second, a[i]);++i;continue;}//如果是右括號,循環取出符號棧中的符號,跟數字棧中兩個數字參與運算后入數字棧,直到碰到左括號為止if (a[i] == ')') {while (topSecond(second) != '(') {strtest = popSecond(second);numFirst = pop(first);numSecond = pop(first);res = counter(numSecond, numFirst, strtest);//計算結果入數據棧push(first, res);}//左括號出棧popSecond(second);++i;continue;}//最后一種情況是要入符號棧while (compare(a[i]) <= compare(topSecond(second))) {strtest = popSecond(second);numFirst = pop(first);numSecond = pop(first);res = counter(numSecond, numFirst, strtest);//計算結果入數據棧push(first, res);}//入符號棧pushSecond(second, a[i]);++i;}//如果符號棧還不為空的情況while (second -> size > 0) {strtest = popSecond(second);numFirst = pop(first);numSecond = pop(first);res = counter(numSecond, numFirst, strtest);//計算結果入數據棧push(first, res);}//輸出最終計算結果printf("%lf\n", top(first));return 0; } 最終運行結果: 例如: 輸入:4.2+5*(2.1+(1+2)) 輸出:29.700000總結
以上是生活随笔為你收集整理的C语言-栈的四则运算(带小数点和括号)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (总结)密码破解之王:Ophcrack彩
- 下一篇: 设计师谈配色 配色方案 WEB