日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式

發布時間:2025/4/5 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數據結構實驗之棧與隊列二:一般算術表達式轉換成后綴式

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

對于一個基于二元運算符的算術表達式,轉換為對應的后綴式,并輸出之。

Input

輸入一個算術表達式,以‘#’字符作為結束標志。

Output

輸出該表達式轉換所得到的后綴式。

Sample Input

a*b+(c-d/e)*f#

Sample Output

ab*cde/-f*+

在做這道題之前首先要了解什么是后綴表達式
百度百科—后綴表達式
這是一種適合計算機計算的表達式,具體步驟:

  • 遇到操作數:直接輸出(添加到后綴表達式中)
  • 棧為空時,遇到運算符,直接入棧
  • 遇到左括號:將其入棧
  • 遇到右括號:執行出棧操作,并將出棧的元素輸出,直到彈出棧的是左括號,括號不輸出。
  • 遇到其他運算符:加減乘除:彈出所有優先級大于或者等于該運算符的棧頂元素,然后將該運算符入棧
  • 最終將棧中的元素依次出棧,輸出。
  • 引用自 Casionx 的CSDN 博客——原表達式轉換為后綴表達式

    附上代碼:
    非線性

    #include <stdio.h> #include <stdlib.h> #include <string.h>typedef struct node {char data;struct node *next; }Node;typedef struct stack {Node *base,*top; }Stack;Node *newnode() {Node *t;t = (Node *)malloc(sizeof(Node));t->next = NULL;return t; };Stack *Newstack() {Stack *t;t = (Stack *)malloc(sizeof(Stack));t->top = newnode();t->base = t->top;return t; }void push(Stack *t,char x) {Node *p = newnode();p->data = x;p->next = t->top->next;t->top->next = p;t->base = p; }char top(Stack *t) {return t->top->next->data; }void pop(Stack *t) {Node *p;p = t->top->next;t->top->next = t->top->next->next;free(p); }int empty(Stack *t) {if(t->top->next==NULL)return 1;return 0; }int judge(char a,char b) {if(b=='(')return 1;if(a=='*'||a=='/'){if(b=='+'||b=='-')return 1;}return 0; }int main() {Stack *t;char s[100050],s2[100050];int i,num = 0;scanf("%s",s);t = Newstack();for(i=0;s[i]!='#';i++){if(s[i]=='(')push(t,s[i]);else if(s[i]==')'){while(top(t)!='('){s2[num++] = top(t);pop(t);}pop(t);}else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'){if(empty(t))push(t,s[i]);else{while(!empty(t)&&!judge(s[i],top(t))){s2[num++] = top(t);pop(t);}push(t,s[i]);}}elses2[num++] = s[i];}while(!empty(t)){s2[num++] = top(t);pop(t);}s2[num] = '\0';printf("%s\n",s2);return 0; }

    線性

    #include <stdio.h> #include <stdlib.h> #include <string.h>typedef struct stack {char *top,*base;int len; }Stack;Stack newstack() {Stack t;t.top = (char *)malloc(100050*sizeof(char));t.base = t.top;t.len = 0;return t; }char top(Stack t) {return *(t.top-1); }void pop(Stack *t) {t->top--;t->len--; }void push(Stack *t,char x) {*(t->top) = x;t->top++;t->len++; }int judge(char a,char b) {if(b=='(')return 1;if(a=='*'||a=='/'){if(b=='+'||b=='-')return 1;}return 0; }int main() {Stack t;char s[100050],s2[100050];int num = 0,i;t = newstack();scanf("%s",s);for(i=0;s[i]!='#';i++){if(s[i]=='(')push(&t,s[i]);else if(s[i]==')'){while(top(t)!='('){s2[num++] = top(t);pop(&t);}pop(&t);}else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'){if(!t.len)push(&t,s[i]);else{while(t.len&&!judge(s[i],top(t))){s2[num++] = top(t);pop(&t);}push(&t,s[i]);}}elses2[num++] = s[i];}while(t.len){s2[num++] = top(t);pop(&t);}s2[num] = '\0';printf("%s\n",s2);return 0; }

    轉載于:https://www.cnblogs.com/luoxiaoyi/p/9747948.html

    總結

    以上是生活随笔為你收集整理的SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。