zstu4189 字尾表示式 邏輯運算

2022-05-03 02:45:09 字數 1873 閱讀 1383

還記得大學裡學過的模電麼,今天就讓我們將與或非變成一道題吧。

給你乙個與或非的表示式,求出這個表示式的值,表示式總共有八種字元。

三種邏輯運算子按照優先順序排列如下。

『!』:表示取反。

『&』:邏輯與。

『|』:邏輯或。

兩個字元『t』,『f『分別表示true和 false。

另外還有左右括號,空格三種字元。跟一般的表示式一樣,括號可以改變優先順序。

每組資料輸入一行字串,字串長度小於等於100.

輸出乙個數0或1,表示邏輯表示式的答案。

t

1
wuyiqi

大意:**能力要求很高

肉鴿的遞迴做法:

#include#include

#include

#include

using

namespace

std;

char s[123

];intn;

int work0(int l, int

r);

int find(intl)}

int work(int l, int

r)

if(s[i] == '!'

) else

if(s[j] == 'f'

) else

if(s[j] == '('

) }

}else

if(s[i] == '

f') return0;

else

if(s[i] == '

t') return1;

}return0;

} int work1(int l, int

r)

return

work(l, r);}

int work0(int l, int

r) }

return

work1(l, r);}

intmain()

return0;

}/*!(t|f)|((t|f)&t)

f&t&f|t|t|f|!f

*/

view code

銘神的模擬棧,字尾表示式做法:

#include#include

const

int mx = 3110

;char

s[mx];

struct

exp

int calc(char k,int a,int

b)

else

if( k == '|'

)

return a ||b;

}bool prior(char a,char

b)

return

false

; }

void

solve()

else

}top--;

}else

if(s[i] == '

t' || s[i] == 'f'

) num[++tot] = s[i] == '

t' ? 1 : 0

;

else

else

}op[++top] =s[i];

} }

printf(

"%d\n

",num[1

]); }

};exp e;

intmain()

return0;

}

view code 

字尾表示式 中綴到字尾表示式

輸入空格跳出迴圈 while k getchar n 字尾表示式 此 僅限於0 9內的加減乘除 include include include define long 10 using namespace std typedef struct stack qstack void init qstac...

字尾表示式

字尾表示式的計算和中綴表示式轉字尾表示式 此處的運算用的是鍊錶的表示方法 以下為三個會涉及到的標頭檔案 error.h 字尾表示式 created by kyle.yang on 14 12 2.ifndef error h define error h include using namespac...

字尾表示式

對於乙個算術表示式我們的一般寫法是這樣的 3 4 5 6 這中寫法是中序表示式 而後序表示式則是將運算子放在運算元的後面,如 3 4 5 6 可以看出後序表示式中沒有括號,只表達了計算的順序,而這個順序恰好就是計算器中的一般計算順序。建立乙個棧s 從左到右讀表示式,如果讀到運算元就將它壓入棧s中,如...