[問題] 求救..中序轉後序計算程式...
小弟是C語言初學者
這個學期修資料結構...
老師給的第一個作業是要寫一個中序轉後序的計算程式
要用兩個堆疊一個放運算元..一個放運算子
還要有一個子作業是要寫出一個副程式
可以判斷多位數的運算
我已經完全沒辦法了= = ...
寫了一個大概的程式...
可是對於strut要怎麼寫入跟讀取
push,pop的值要怎麼設定...
第一個C就寫這個真的好困難阿= =
可是我又不想放棄...
這是我的第一個C...
所以可能會有很多忘了打;
或者離奇的錯誤...
可是作業迫在眉睫...
希望各位可以鞭小力一點...
冒著會被罵到臭頭的危險..
把程式碼放上來給各位大德看看...
希望各位可以幫助我QQ"
--------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#define Stacksize 80
#define TRUE 1
#define FALSE 0
int input_infix(char); // 中序轉後序
int priority(int); // 決定運算子優先順序
void evalute_subvalue(int); //計算程式
void empty(int);//檢查堆疊是否為empty
void Push(int, int);//放值進堆疊
int Pop(int);//拿值出堆疊
////////////////////////////////////
/* 運算元堆疊宣告 */
////////////////////////////////////
struct stack_operand {
int top;
int stack[Stacksize];
};
////////////////////////////////////
/* 運算子堆疊宣告 */
////////////////////////////////////
struct stack_operator {
int top;
int stack[Stacksize]
};
////////////////////////////////////
/* 主程式 */
////////////////////////////////////
int main(void) {
int input[Stacksize];
printf("輸入運算式:");
scanf("%s", input);
input_fix(input);
system("pause");
return 0;
}
////////////////////////////////////
/* 中序轉後序 */
////////////////////////////////////
int input_infix(int* infix) {
int i=0;
struct stack_operand ope_rand
struct stack_operator oper_ator
op_erand.top = -1
oper_ator.top = -1
int symb;
int op;
while(!'\0') {
symb = infix[i];
if (symb = '+'||'-'||'*'||'/')
if (!Overflow(oper_ator))
Push(*oper_ator, symb);
else
printf("stack is overflow");
break;
else{
if
(symb == ')'){
while((op = Pop(*oper_ator))!='('){
evalute_subvalue(op);
continue;
}
else
while (priority(symb) <= oper_ator->top){
op = Pop(*ope_rand);
evalute_subvalur(op);
}
Push(*oper_ator, int symb)
}
}
while(!Empty(oper_ator)){
op = int Pop(*ope_rand);
evalute_subvalue(op);
}
printf("ans:%c",*ope_rand->stack[0]);
}
////////////////////////////////////
/* 判斷運算元優先順序 */
////////////////////////////////////
int priority(int symb)
{
int p;
switch(op) {
case '+': case '-':
p = 1;
break;
case '*': case '/':
p = 2;
break;
default:
p = 0;
break;
}
return p;
}
////////////////////////////////////
/* 計算函式 */
////////////////////////////////////
void evalute_subvalue(int op)
{
int a,b;
int op, new_value;
b = int Pop(*ope_rand);
a = int Pop(*ope_rand);
switch(op) {
case '+':
return new_value = a + b;
case '-':
return new_value = a - b;
case '*':
return new_value = a * b;
case '/':
return new_value = a / b;
}
Push(*ope_rand , new_value);
}
////////////////////////////////////
/* empty 判斷 */
////////////////////////////////////
void empty(int ps)
{
if(ps.top == -1)
return(TRUE);
else
return(FALSE);
}
////////////////////////////////////
/* voerflow 判斷 */
////////////////////////////////////
void overflow(struct stack ps)
{
if(ps.top == Stacksize - 1)
return(TRUE);
return(FALSE);
}
////////////////////////////////////
/* PUSH */
////////////////////////////////////
void Push(struct stack *ps , int symb)
{
ps->stack[++(ps->top)] = symb;
return;
}
////////////////////////////////////
/* POP */
////////////////////////////////////
int Pop(int* ps)
{
return(ps->stack[ps->top --]);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.139.132.70
→
03/27 17:32, , 1F
03/27 17:32, 1F
→
03/27 17:48, , 2F
03/27 17:48, 2F
推
03/27 17:51, , 3F
03/27 17:51, 3F
→
03/27 20:24, , 4F
03/27 20:24, 4F
推
03/27 23:57, , 5F
03/27 23:57, 5F
→
03/27 23:57, , 6F
03/27 23:57, 6F
→
03/27 23:57, , 7F
03/27 23:57, 7F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章