1. 程式人生 > >2133資料結構實驗之棧與佇列三:字尾式求值and字首式求值

2133資料結構實驗之棧與佇列三:字尾式求值and字首式求值

資料結構實驗之棧與佇列三:字尾式求值
Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description
對於一個基於二元運算子的字尾表示式(基本運算元都是一位正整數),求其代表的算術表示式的值。

Input
輸入一個算術表示式的字尾式字串,以‘#’作為結束標誌。

Output
求該字尾式所對應的算術表示式的值,並輸出之。

Sample Input
59*684/-3*+#
Sample Output
57
Hint
基本運算元都是一位正整數!

Source

THINK:
是數字就入棧,注意不是一次性全部入棧,而是邊入棧,邊操作;
字尾式運算的時候是給top-1賦值,賦的值是top-1對top進行加減乘除的運算,之後top再減減,使其落再top-1上即可;從左往右進行掃描,操作的時候是棧次元素對棧頂元素進行操作
字首式求值是:從右往左進行掃描,操作的時候是棧頂元素對棧次元素進行加減乘除的運算;最後輸出棧頂元素;

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char s[200000];
int a[200000];
int main()
{
    scanf("%s",s);
    int len=strlen(s);
    int top=0;
    for(int i=0;i<len;i++)
    {
        if(s[i]>='0'&&s[i]<='9')
        {
            a[++top]=s[i]-'0';
        }
        else
if(s[i]=='+') { a[top-1]=a[top-1]+a[top]; top--; } else if(s[i]=='-') { a[top-1]=a[top-1]-a[top]; top--; } else if(s[i]=='*') { a[top-1]=a[top-1]*a[top]; top--; } else
if(s[i]=='/') { a[top-1]=a[top-1]/a[top]; top--; } } printf("%d\n",a[top]); return 0; }

相關推薦

2133資料結構實驗佇列字尾and字首

資料結構實驗之棧與佇列三:字尾式求值 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 對於一個基於二元運算子的字尾表

2133-資料結構實驗佇列字尾

#include <bits/stdc++.h> using namespace std; typedef int ElemType; class Stack{ private:

資料結構實驗佇列字尾(SDUT 2133

題解:把每一步計算的答案再存在棧裡面,直到計算結束。           如果是運算元 那麼直接入棧;如果是運算子,那麼把棧裡面最頂部的兩個運算元拿出來進行運算,運算結果再放入到棧裡面,計算完所有的(#

資料結構實驗佇列字尾

資料結構實驗之棧與佇列三:字尾式求值 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem

資料結構實驗佇列一般算術表示式轉換成字尾(SDUT 2132)

題目連結 #include <bits/stdc++.h> using namespace std; typedef long long ll; int ok(char ch, char sh) { if(sh == '(')return 1; if((ch ==

資料結構實驗佇列下一較大(二)(SDUT 3333)

#include <bits/stdc++.h> using namespace std; int a[1000006]; int b[1000006]; int sta[100006]; int main() { int t,n,i,j,top; while(~sc

資料結構實驗佇列下一較大(一)(SDUT 3332)

#include <bits/stdc++.h> using namespace std; int a[1005]; int main() { int t,n,i,j; while(~scanf("%d",&t)) { while(t-

資料結構實驗佇列括號匹配(SDUT 2134)

#include <bits/stdc++.h> using namespace std; typedef long long ll; char s[100]; char a[100]; int main() { int i,j,k,f,top,len; while(

資料結構實驗佇列下一較大(一)

資料結構實驗之棧與佇列五:下一較大值(一) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 對於包含n(1<=n<=1000)個整數的序列,對於序列中的每一元素,

資料結構實驗佇列下一較大(一,二)

資料結構實驗之棧與佇列五:下一較大值(一,二) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 對於包含n(1<=n<=1000)個整數的序列,對於序

資料結構實驗佇列序列判定

Problem Description 給一個初始的入棧序列,其次序即為元素的入棧次序,棧頂元素可以隨時出棧,每個元素只能入棧依次。輸入一個入棧序列,後面依次輸入多個序列,請判斷這些序列是否為所給入棧序列合法的出棧序列。 例如序列1,2,3,4,5是某棧的壓入順序,序列4

資料結構實驗佇列下一較大(二)(因為資料量大所以用來操作)

資料結構實驗之棧與佇列六:下一較大值(二) Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 對於包含n(1<=n<=100000)個整數的序列,對於序列中的每一元素,在序列中查詢

資料結構實驗佇列的基本操作

Problem Description 堆疊是一種基本的資料結構。堆疊具有兩種基本操作方式,push 和 pop。push一個值會將其壓入棧頂,而 pop 則會將棧頂的值彈出。現在我們就來驗證一下堆疊的

資料結構實驗佇列行編輯器

Problem Description 一個簡單的行編輯程式的功能是:接受使用者從終端輸入的程式或資料,並存入使用者的資料區。 由於使用者在終端上進行輸入時,不能保證不出差錯,因此,若在編輯程式中,“每接受一個字元即存入使用者資料區”的做法顯然不是最恰當的。較好

資料結構實驗佇列走迷宮

Problem Description 一個由n * m 個格子組成的迷宮,起點是(1, 1), 終點是(n, m),每次可以向上下左右四個方向任意走一步,並且有些格子是不能走動,求從起點到終點經過每個

資料結構實驗佇列下一較大(二)

Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 對於包含n(1<=n<=100000)個整數的序列,對於序列中的每一元素,在序列中查詢其位置之後第一個大於它的值,如果找到,輸出所找到的

資料結構實驗佇列進位制轉換(SDUT 2131)

題目連結 題解: 特判一下n==0的時候。 #include <bits/stdc++.h> using namespace std; int a[1000]; int main() {

資料結構實驗佇列進位制轉換

Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 輸入一個十進位制非負整數,將其轉換成對應的 R (2 <= R <= 9) 進位制數,並

資料結構實驗佇列的基本操作(new)

#include <stdio.h> #include <stdlib.h> typedef struct node { int *base; int *top; int stacksize; } sqstack;

[OJ.2131]資料結構實驗佇列進位制轉換

                                   資料結構實驗之棧與佇列一:進位制轉換                                            Time Limit: 1000 ms