600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 数据结构--表达式括号匹配和运算

数据结构--表达式括号匹配和运算

时间:2024-07-27 04:12:54

相关推荐

数据结构--表达式括号匹配和运算

多项式括号匹配:

#include <stdio.h>#include <malloc.h>#define STACK_MAX_SIZE 10//创建栈结构体 typedef struct charStack{int top;int data[STACK_MAX_SIZE];}*stackPtr; //输出栈void outputStack(stackPtr paraStack){for(int i=0;i<=paraStack->top;i++){printf("%d ",paraStack->data[i]);}printf("\r\n");} //初始化栈stackPtr initStack(){stackPtr tempStack=(stackPtr)malloc(sizeof(stackPtr));tempStack->top=-1;return tempStack;} //入栈void push(stackPtr stack,int paraValue){//检查空间if (stack->top >= STACK_MAX_SIZE - 1) {printf("Cannot push element: stack full.\r\n");return;}//update topstack->top++;//添加元素stack->data[stack->top]=paraValue; } //出栈int pop(stackPtr stack){// 空间检查 if (stack->top < 0) {printf("Cannot pop element: stack empty.\r\n");return '\0';}//update topstack->top--;//返回栈顶 return stack->data[stack->top+1];} bool bracketMatch(char *paraString, char paraLength){//初始化创建栈底stackPtr tempStack= initStack();push(tempStack, '#');char tempChar,tempPopedChar;//startfor(int i=0;i<paraLength;i++){tempChar= paraString[i];switch(tempChar){case '(':case '[':case '{':push(tempStack,tempChar);break;case ')':tempPopedChar = pop(tempStack);if (tempPopedChar != '(') {return false;}break;case ']':tempPopedChar = pop(tempStack);if (tempPopedChar != '[') {return false;} break;case '}':tempPopedChar = pop(tempStack);if (tempPopedChar != '{') {return false;} break;default:break;}}tempPopedChar = pop(tempStack);if (tempPopedChar != '#') {return false;} return true;}void pushPopTest() {printf("---- pushPopTest begins. ----\r\n");// Initialize.stackPtr tempStack = initStack();printf("After initialization, the stack is: ");outputStack(tempStack);// Pop.for (int i = 1; i < 10; i++) {printf("Pushing %d.\r\n", i);push(tempStack, i);outputStack(tempStack);}//Of for i// Pop.int s;for (int j = 0; j< 3; j++) {s = pop(tempStack);printf("Pop %d.\r\n", s);outputStack(tempStack);}//Of for iprintf("---- pushPopTest ends. ----\r\n");}// Of pushPopTestvoid bracketMatchingTest() {char* tempExpression = "[2 + (1 - 3)] * 4";bool tempMatch = bracketMatch(tempExpression, 17);printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);tempExpression = "( () )";tempMatch = bracketMatch(tempExpression, 6);printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);tempExpression = "(()())";tempMatch = bracketMatch(tempExpression, 6);printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);tempExpression = "({}[])";tempMatch = bracketMatch(tempExpression, 6);printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);tempExpression = ")(";tempMatch = bracketMatch(tempExpression, 2);printf("Is the expression '%s' bracket matching? %d \r\n", tempExpression, tempMatch);}// Of bracketMatchingTest/**The entrance.*/int main() {//pushPopTest();bracketMatchingTest();}

运行结果:

多项式加法:

#include <iostream>#include <cstring>#include <algorithm>#include <stack>#include <unordered_map>using namespace std;stack<int> num;stack<char> op;void eval(){auto b = num.top();num.pop();auto a = num.top();num.pop();auto c = op.top();op.pop();int x;if (c == '+') x = a + b;else if (c == '-') x = a - b;else if (c == '*') x = a * b;else x = a / b;num.push(x);}int main(){unordered_map<char, int> pr{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};string str;cin >> str;for (int i = 0; i < str.size(); i ++ ){auto c = str[i];if (isdigit(c)){int x = 0, j = i;while (j < str.size() && isdigit(str[j]))x = x * 10 + str[j ++ ] - '0';i = j - 1;num.push(x);}else if (c == '(') op.push(c);else if (c == ')'){while (op.top() != '(') eval();op.pop();}else{while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) eval();op.push(c);}}while (op.size()) eval();cout << num.top() << endl;return 0;}

运行结果:

总结:

括号匹配还好,表达式运算确实难到我了,没办法,又分析了一下大佬的代码,总结如下:

当自己在写代码时,总是想到哪里就写到哪里,其实说白了,还是自己的思维逻辑不是很清晰很明了,自己在以后必须多加强自己的思维逻辑训练,不能让自己的思维总是“旋转跳跃”。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。