① 用c語言編寫函數實現順序棧的進棧、退棧、取棧頂的演算法。
#include<stdio.h>
#define stacksize 100 //假定預分配的棧空間最多為100 個元素
typedef char elementtype; //假定棧元素的數據類型為字元 ,在此處可以自行設置
typedef struct
{
elementtype data[stacksize];
int top;
}seqstack;
// 置空棧
void initstack(seqstack *s)
{
s->top=-1;
//解釋一下,s->top 指向的是當前棧頂元素的位置
//當要向棧中添加一個新元素時,要先將s->top增加1,
//此時s->top 指向的就是新元素要添加的位置了。
//所以當棧為空時,填加第一元素時,top加1 後
//s->top的值就變為0,也就是第一個元素的位置了。
}
//判棧空
int stackempty(seqstack *s)
{
if(s->top==-1)
return 1; //若相等就返回1,否則為0
else return 0;
}
//入棧
void push(seqstack *s,elementtype x)
{
if(s->top==stacksize -1 ) //進棧前判斷棧是否已經滿了
printf(" stack overflow\n");
else
{
s->top= s->top + 1;
s->data[s->top]=x;
}
}
//出棧
elementtype pop(seqstack *s)
{
if(stackempty(s)) //出棧前先判斷當前棧中是否有內容
printf("stack is empty\n");
else
{
return s->data[s->top--]; //出棧後s->top的值會自減1
}
}
//取棧頂元素(只是想知道棧頂的值,並沒有出棧)
elementtype gettop(seqstack *s)
{
if(stackempty(s))
{
printf("stack already empty.\n");
}
else return s->data[s->top];
}
int main()
{
elementtype x;
seqstack *s; //定義一個棧,用指針的方式定義的
initstack(s); //想初始化定義好的棧
//當棧為空時調用出棧操作
pop(s);
//向棧中添加一個元素a
push(s,'a');
//觀察此時的棧頂元素
x=gettop(s);
printf("%c\n",x);
//再添加一個元素b
push(s,'b');
//觀察此時的棧頂元素
x=gettop(s);
printf("%c\n",x);
//彈出棧頂的元素
x=pop(s);
printf("%c\n",x);
//觀察彈出後棧頂元素的變化情況
x=gettop(s);
printf("%c\n",x);
return 0;
}
② 那位C語言高手能解決用C語言和數據結構幫我編寫一個順序棧的,實現任意表達式.註:一定要用順序棧的!
#define stacksize 100//棧最大空間
typedef int elemtype;//棧中元素類型
typedef struct
{
int top;//棧頂指針
elemtype data[stacksize];
}stack;//順序棧類型
void initstack(stack &s)//建立空棧
{
s.top=-1;
}
void push(stack &s,elemtype e)//壓棧
{
if(s.top>=stacksize)
{
printf("full!");
return;
}
s.data[++s.top]=e;
}
void pop(stack &s,elemtype &e)//彈棧
{
if(s.top<=-1)
{
printf("empty stack!");
return;
}
e=s.data[s.top--];
}
bool stackempty(stack s)//判斷棧非空
{
if(s.top==-1)return true;
return false;
}
③ 關於C語言的順序棧的操作
這是我以前寫的,今天不想砍代碼,就發這個了
#include<stdio.h>
#include<stdlib.h>
#define N 100
//棧的結構定義
typedef struct
{
int elem[N];
int top;//棧頂
}Stack;
//循環隊列的結構定義
typedef struct
{
int elem[N];
int front;
int rear;
}Queue;
int Greatstack(Stack*);
int GreatQueue(Queue *);
int pop(Stack *s);
void show(Stack *s);
int push(Stack *s);
int popStack(Stack *s);
int enQueue(Queue *q);
int deQueue(Queue *q);
int printQueue(Queue *q);
void conversion();
void main()
{
char exit='N';
int ch;
int result,data;
int m,e;
Stack s;
Queue q;
s.top=0;//棧頂指向當前棧頂的下一位置
q.front=q.rear=0;
do
{
system("CLS");
printf("\t\t********************************************\n");
printf("\t\t* 1.創建一個順序棧 .................(1) *\n");
printf("\t\t* 2.出棧.............................(2) *\n");
printf("\t\t* 3.入棧.............................(3) *\n");
printf("\t\t* 4.順序棧中元素依次出棧,並顯示.....(4) *\n");
printf("\t\t* 5.創建一個循環隊列.................(5) *\n");
printf("\t\t* 6.進隊.............................(6) *\n");
printf("\t\t* 7.出隊.............................(7) *\n");
printf("\t\t* 8.循環隊列中元素依次出隊,並顯示...(8) *\n");
printf("\t\t* 9.十進制數轉換為其它進制數.........(9) *\n");
printf("\t\t* 10.退出 ..............(10) *\n");
printf("\t\t********************************************\n");
printf("\n請選擇操作代碼:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("創建一個順序棧\n");
result=Greatstack(&s);
if(result==0)printf("順序棧創建失敗,棧已滿!\n");
else printf("順序棧創建成功!\n");
system("pause");
break;
case 2:
printf("出棧:\n");
m=pop(&s);
if(m==0)printf("出棧失敗,棧已空!\n");
else
{
printf("順序棧出棧成功!\n");
printf("該棧剩餘元素為:");
show(&s);
}
//請加入出棧操作的代碼或函數調用
system("pause");
break;
case 3:
printf("進棧:\n");
printf("請輸入入棧的元素");
scanf("%d",&e);
result=push(&s,e);
if(result==0)printf("棧已滿!\n");
else
{
printf("順序棧入棧成功!\n");
printf("該棧剩餘元素為:");
show(&s);
}
//請加入進棧操作的代碼或函數調用
system("pause");
break;
case 4:
printf("順序棧中元素依次出棧,出棧序列為:\n");
popStack(&s);
//請加入依次出棧並顯示元素操作的代碼或函數調用
system("pause");
break;
case 5:
printf("創建一個循環隊列\n");
result=GreatQueue(&q);
if(result==0)printf("循環隊列創建失敗,循環隊列已滿!\n");
else printf("循環隊列創建成功!\n");
system("pause");
break;
case 6:
printf("入隊:\n");
data=enQueue(&q);
if(result=0) printf("隊已經滿了\n");
else printf("入隊成功\n");
//請加入入隊操作的代碼或函數調用
system("pause");
break;
case 7:
printf("出隊:\n");
data=deQueue(&q);
if(data=0) printf("隊已經空了\n");
else printf("出隊成功\n");
//請加入依次出隊操作的代碼或函數調用
system("pause");
break;
case 8:
printf("循環隊列中元素依次出隊,出隊序列為:\n");
//請加入依次出隊並顯示元素操作的代碼或函數調用
data=printQueue(&q);
system("pause");
break;
case 9:
printf("十進制數轉換為其它進制數:\n");
conversion();
//請加入十進制數轉換為其它進制數操作的代碼或函數調用
system("pause");
break;
case 10:
getchar();
printf("\n您是否真的要退出程序(Y/N):");
exit=getchar();getchar();
break;
default:
printf("\n無效輸入,請重新選擇...:");
}
}while(exit!='y'&&exit!='Y');
}
/*從鍵盤輸入一系列整數,當輸入值為0時,停止輸入,產生順序棧*/
/*返回0表示生成成功,1表示失敗*/
int Greatstack(Stack *s)
{
int e;
s->top=0;//棧頂指向當前棧頂的下一位置
printf("請依次輸入需要入棧的元素,以0表示輸入結束:");
do
{
scanf("%d",&e);
if(e!=0)
{ if(s->top>=N)return 0;//棧滿
s->elem[s->top++]=e;
}
}while(e!=0);
}
/*從鍵盤輸入一系列整數,當輸入值為0時,停止輸入,產生循環隊列*/
/*返回0表示生成成功,1表示失敗*/
int GreatQueue(Queue *q)
{
int e;
q->front=q->rear=0;//初始化隊列
printf("請依次輸入需要入棧的元素,以0表示輸入結束:");
do
{
scanf("%d",&e);
if(e!=0)
{ if((q->rear+1)%N==q->front)return 0;//隊滿
q->elem[q->rear]=e;
q->rear=(q->rear+1)%N;
}
}while(e!=0);
return 1;
}
int pop(Stack *s) //出棧
{
if(s->top==0)
return 0;
--s->top;
}
void show(Stack *s) //顯示棧中的元素
{
int i=s->top;
while(i!=0)
{
--i;
printf("%d ",s->elem[i]);
}
}
int push(Stack *s,int e) //入棧
{
if(s->top>=N)return 0;
s->elem[s->top++]=e;
}
int popStack(Stack *s) //順序棧中元素依次出棧,並顯示
{
while(s->top!=0)
{
--s->top;
printf("%3d",s->elem[s->top]);
}
return 0;
}
int enQueue(Queue *q) //入隊
{
int e;
printf("請輸入入棧的元素:");
scanf("%d",&e);
if((q->rear+1)%N == q->front) return 0;
q->elem[q->rear]=e;
q->rear=(q->rear+1)%N;
return 1;
}
int deQueue(Queue *q) //出隊
{
int *e;
if(q->front == q->rear) return 0;
e =q->elem[q->front];
q->front=(q->front+1)%N;
return 1;
}
int printQueue(Queue *q) //循環隊列中元素依次出隊,並顯示
{
if(q->front == q->rear) return 0;
for(;q->front!=q->rear;)
{
printf("%3d",q->elem[q->front]);
q->front++;
}
}
void conversion() //十進制數轉換為其它進制數
{
Stack p1;
int n,m;
puts("請輸入需要轉換的元素:");
scanf("%d",&n);
puts("請輸入需要轉換的進制:");
scanf("%d",&m);
p1.top=0;
while(n)
{
push(&p1,n%m);
n=n/m;
}
popStack(&p1);
}
④ c語言 棧的操作
#include
#include
#define Max 100
typedef char T;
typedef struct MyStack
{
T aa[Max];
unsigned int p;
} stack;
//創建空棧
stack* createEmptyStack()
{
stack* st = (stack *)malloc(sizeof(stack));
int i=0;
for(i=0;i<Max;i++)
st->aa[i]=0;
st->p=0;
return st;
};
//棧判空
int isEmpty(const stack* st)
{
if(st->p==0) return 1;
else return 0;
};
//求棧的大小
unsigned int size(const stack* st)
{
return st->p;
};
//push操作
void push(stack* st,const T a)
{
st->p=st->p+1;
if(st->p==Max)
{
printf("棧滿\n");
st->p--;
return;
}
st->aa[st->p]=a;
};
//pop操作
T pop(stack* st)
{
if(isEmpty(st))
{
printf("棧空");
return NULL;
}
char t=st->aa[st->p];
st->p=st->p-1;
printf("%c ",t);
return t;
};
//棧銷毀
void destroy(stack* st)
{
free(st);
};
int main()
{
stack* st = createEmptyStack();
if(isEmpty(st)) printf("MyStack is empty\n");
else printf("MyStack is not empty\n");
push(st,'a');
push(st,'b');
push(st,'c');
push(st,'d');
push(st,'e');
printf("%d\n",size(st));
while(!isEmpty(st)) pop(st);
destroy(st);
system("pause");
return 0;
}
⑤ 用c語言棧計算順序表達式
#include <stdio.h>
double readnumber(char a[],int *i)//將數字字元轉變成相應的數
{
double x=0.0;
int k=0;
while(a[*i]>='0'&&a[*i]<='9')
{
x=x*10+a[*i]-'0';
(*i)++;
}
if(a[*i]=='.')
{
(*i)++;
while(a[*i]>='0'&&a[*i]<='9')
{
x=x*10+a[*i]-'0';
(*i)++;
k++;
}
}
while(k!=0)
{
x=x/10.0;
k=k-1;
}
return x;
}
double yunsuan(char a[])//求一個後綴表達式的值
{
double obst[100],b,c;//操作數棧
int top=0,i=0;
while(a[i]!='\0')
{
if(a[i]>='0'&&a[i]<='9')
obst[top++]=readnumber(a,&i);
else if(a[i]==' ') i++;
else if(a[i]=='+')
{
b=obst[--top];
c=obst[--top];
obst[top++]=b+c;
i++;
}
else if(a[i]=='-')
{
b=obst[--top];
c=obst[--top];
obst[top++]=c-b;
i++;
}
else if(a[i]=='*')
{
b=obst[--top];
c=obst[--top];
obst[top++]=b*c;
i++;
}
else if(a[i]=='/')
{
b=obst[--top];
c=obst[--top];
obst[top++]=c/b;
i++;
}
}
return obst[0];
}
int pd(char op)//判斷一個字元是不是運算符
{
switch(op)
{
case '+':
case '-':
case '*':
case '/':return 1;
default :return 0;
}
}
int priority(char op)//求運算符的優先順序
{
switch(op)
{
case '\0':return -1;
case '(':return 0;
case '+':
case '-':return 1;
case '*':
case '/':return 2;
default:return -1;
}
}
void charge(char a[],char b[])//將中綴表達式轉換等價的後綴表達式
{
int i=0,j=0;
char opst[100];
int top,t;
top=0;
opst[top]='\0';
top++;
while(a[i]!='\0')
{
if(a[i]>='0'&&a[i]<='9'||a[i]=='.')
b[j++]=a[i];//遇到數字和小數點直接寫入後綴表達式
else if(a[i]=='(')//遇到左括弧進入操作符棧
{
opst[top]=a[i];
top++;
}
else if(a[i]==')')
{
t=top-1;
while(opst[t]!='(')
{//'('之前出棧
b[j++]=opst[--top];
t=top-1;
}
top--;
}
else if(pd(a[i]))//'+','-','*','/'
{
b[j++]=' ';//用空格分開兩個操作數
while(priority(opst[top-1])>=priority(a[i]))
b[j++]=opst[--top];
opst[top]=a[i];
top++;
}
i++;
}
while(top) b[j++]=opst[--top];
}
int main()
{
char a[100],b[100];
double jieguo;
printf("\n\t請輸入算術表達式:");
scanf("%s",a);
charge(a,b);
jieguo=yunsuan(b);
printf("\t表達式運算的結果為:%lf",jieguo);
return 0;
}
⑥ C語言用棧編寫求表達式的值
//表達式輸入完了之後直接回車,就出結果了,跟平時輸入字元串一樣。
/**********************************************
算術表達式求值的算符優先順序演算法
利用棧來實現括弧匹配和表達式求值
演算法的詳細說明,請查看清華大學出版社《數據結構》,嚴蔚敏&吳偉民著,3.3節
***********************************************/
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
//存儲空間初始分配量
#define STACK_INIT_SIZE 100
//存儲空間分配增量
#define STACKINCREMENT 10
#define OK 0
#define ERROR 127
//定義一個順序棧
typedef struct
{
int*base; //在棧構造之前和銷毀之後,base的值為NULL
int*top; //棧頂指針
int stacksize; //當前已分配的存儲空間,以元素為單位
}SqStack;
int InitStack(SqStack*S)
{
//構造一個空棧
S->base=(int*)malloc(STACK_INIT_SIZE*sizeof(SqStack));
if(NULL==S->base)
{ //內存分配失敗
return ERROR ;
}
S->top=S->base ;
S->stacksize=STACK_INIT_SIZE ;
return OK ;
}
char GetTop(SqStack*S,char*element)
{
//若棧不空,取棧頂元素,用element返回
if(S->base==S->top)
{
return ERROR ;
}
*element=*(S->top-1);
return*element ;
}
int Push(SqStack*S,int element)
{
//插入元素element為新的棧頂元素
if((S->top-S->base)>S->stacksize)
{
//棧滿,追加空間
S->base=(int*)realloc(S->base,(STACK_INIT_SIZE+STACKINCREMENT)*sizeof(SqStack));
if(NULL==S->base)
{
return ERROR ;
}
S->top=S->base+S->stacksize ;
S->stacksize+=STACKINCREMENT ;
}
*S->top++=element ;
return OK ;
}
int Pop(SqStack*S,int*element)
{
//若棧不為空,則刪除棧頂元素,用element返回其值
if(S->top==S->base)
{
return ERROR ;
}
*element=*(--S->top);
return OK ;
}
int PopOPTR(SqStack*S,char*element)
{
if(S->top==S->base)
{
return ERROR ;
}
*element=*(--S->top);
return OK ;
}
//判斷字元c是否在集合OP中
int InOP(char c,char OP[7])
{
for(int i=0;i<7;i++)
{
if(c==OP[i])
{
return OK ;
}
}
return ERROR ;
}
//判斷運算符的優先順序
int Compare(int a,int b)
{
if('+'==a)
{
switch(b)
{
case '+' :
return '>';
case '-' :
return '>' ;
case '*' :
return '<' ;
case '/' :
return '<' ;
case '(' :
return '<' ;
case ')' :
return '>' ;
case '\n' :
return '>' ;
}
}
if('-'==a)
{
switch(b)
{
case '+' :
return '>' ;
case '-' :
return '>' ;
case '*' :
return '<' ;
case '/' :
return '<' ;
case '(' :
return '<' ;
case ')' :
return '>' ;
case '\n' :
return '>' ;
}
}
if('*'==a)
{
switch(b)
{
case '+' :
return '>' ;
case '-' :
return '>' ;
case '*' :
return '>' ;
case '/' :
return '>' ;
case '(' :
return '<' ;
case ')' :
return '>' ;
case '\n' :
return '>' ;
}
}
if('/'==a)
{
switch(b)
{
case '+' :
return '>' ;
case '-' :
return '>' ;
case '*' :
return '>' ;
case '/' :
return '>' ;
case '(' :
return '<' ;
case ')' :
return '>' ;
case '\n' :
return '>' ;
}
}
if('('==a)
{
switch(b)
{
case '+' :
return '<' ;
case '-' :
return '<' ;
case '*' :
return '<' ;
case '/' :
return '<' ;
case '(' :
return '<' ;
case ')' :
return '=' ;
}
}
if(')'==a)
{
switch(b)
{
case '+' :
return '>' ;
case '-' :
return '>' ;
case '*' :
return '>' ;
case '/' :
return '>' ;
case ')' :
return '>' ;
case '\n' :
return '>' ;
}
}
if('\n'==a)
{
switch(b)
{
case '+' :
return '<' ;
case '-' :
return '<' ;
case '*' :
return '<' ;
case '/' :
return '<' ;
case '(' :
return '<' ;
case '\n' :
return '=' ;
}
}
return ERROR ;
}
//簡單計算
int Calculate(int left,char oper,int right)
{
int result=0 ;
switch(oper)
{
case '+' :
result=left+right ;
break ;
case '-' :
result=left-right ;
break ;
case '*' :
result=left*right ;
break ;
case '/' :
result=left/right ;
break ;
}
return result ;
}
/**********************************************
算術表達式求值的算符優先順序演算法,設OPTR和OPND分別為運算符棧和運算數棧
OP為運算符集合
**********************************************/
int main()
{
SqStack OPTR,OPND ;
int element=0 ;
char OPTR_element ;
int leftNum,rightNum ;
char input ;
//獲取輸入
char OP[7]={'+','-','*','/','(',')','\n'};
InitStack(&OPTR);
Push(&OPTR,'\n');
InitStack(&OPND);
printf("請輸入表達式\n");
input=getchar();
while('\n'!=input||'\n'!=GetTop(&OPTR,&OPTR_element))
{
int temp=0 ;
if(isdigit(input))
{
//如果是數字
ungetc(input,stdin);
//返回給輸入流
scanf("%d",&temp);
Push(&OPND,temp);
//數字就進OPND棧
input=getchar();
continue ;
}
if(OK==InOP(input,OP))
{
GetTop(&OPTR,&OPTR_element);
switch(Compare(OPTR_element,input))
{
case '<' :
//棧頂元素優先順序低
Push(&OPTR,input);
//運算符進OPTR棧
input=getchar();
break ;
case '=' :
//脫括弧
PopOPTR(&OPTR,&OPTR_element);
input=getchar();
break ;
case '>' :
//退棧,並將運算結果入棧
PopOPTR(&OPTR,&OPTR_element);
Pop(&OPND,&rightNum);
Pop(&OPND,&leftNum);
Push(&OPND,Calculate(leftNum,OPTR_element,rightNum));
break ;
default :
printf("表達式括弧不匹配\n");
return ERROR ;
}//switch
}//if
else
{
printf("表達式內有未知字元,即將退出\n");
return ERROR ;
}
}//while
int value ;
Pop(&OPND,&value);
printf("結果 = %d\n",value);
return OK ;
}//end
⑦ 怎樣用C語言寫出對棧進行的五種運算:push()、pop()、top()、empty()、makempty()
這是我用鏈表寫的:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int x;
struct node *next;
}Node;
typedef struct stack
{
Node *top;
}Stack;
void InitStack(Stack *s)
{
s->top=NULL;
}
int IsEmpty(Stack *s)
{
if(s->top==NULL)
return 1;
else
return 0;
}
void PushStack(Stack *s,int x)
{
Node *p;
p=(Node*)malloc(sizeof(Node));
p->x=x;
// p->next=NULL;
p->next=s->top;
s->top=p;
}
int PopStack(Stack *s)
{
int data;
Node *p;
p=(Node *)malloc(sizeof(Node));
if(IsEmpty(s))
{
printf("the stack is empty!\n");
free(p);
return -1;
}
else
{
p=s->top;
data=p->x;
s->top=p->next;
free(p);
return data;
}
}
int main (int argc,char **argv)
{
int i;
Stack s;
InitStack(&s);
for(i=0;i<1000;i++)
{
PushStack(&s,i);
}
for(i=0;i<1000;i++)
{
printf("%d\n",PopStack(&s));
}
}
⑧ c語言的順序表 棧 判斷表達式括弧是否匹配
int match_kuohao(char c[i])
{
int i=0;
sequence_stack s;
init(&s); //初始化棧, &這個符號是用來修改s這個實參的值的,不然不能修改。
while(c[i]!='#')
{
switch(c[i])
{
case'{':
case'[':
case'(':push (&s,c[i]);break;
case'}':if(!empty(s)&&read(s)=='{' )
{pop(&s);break;}
else return 0; //意思就是如果遇到了右大括弧的話,那麼棧S不是空且棧頂元素是左大括弧,
//那麼就讓這個棧頂元素{出棧。
//如果棧是空的,或者說棧頂的不是{,那麼就return 0,意思也就是表達式不合法。下面同理。
case']':if(!empty(s)&&read(s)=='[' )
{pop(&s);break;}
else return 0;
case')':if(!empty(s)&&read(s)=='(' )
{pop(&s);break;}
else return 0;
}
i++;
}
return (empty(s));
}
⑨ 《C語言編寫》用棧完成表達式求值(順序存儲)
可能有點繁瑣了,能力只有這樣了,希望可以有所幫助
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct _aline{
char expression[40];
char * top;
char * base;
}aline;
char fuhao[7][7]={
'>','>','<','<','<','>','>',
'>','>','<','<','<','>','>',
'>','>','>','>','<','>','>',
'>','>','>','>','<','>','>',
'<','<','<','<','<','=','$',
'>','>','>','>','$','>','>',
'<','<','<','<','<','$','='
};
char ch[] = {'+','-','*','/','(',')','#'};
typedef struct _optr{
char operatar[10];
char *base;
char * top;
}optr;
typedef struct _opnd{
int data[10];
int * top;
int *base;
}opnd;
int init_line(aline *L);
int get_line(aline *L);
int init_optr(optr * x);
void push_optr(optr *x,char ch);
void pop_optr(optr *x);
void push_opnd(opnd *x, int data);
int init_opnd(opnd *x);
void pop_opnd(opnd *x);
void get_base(aline *L, char *a);
int empty_optr( optr *x);
int get_data(optr *x);
int get_no(char c);
int compare(char row, char cal);
int evalua_experssion(aline *L);
int main()
{
aline express;
init_line(&express);
get_line(&express);
printf(" %d \n",evalua_experssion(&express));
return 0;
}
int evalua_experssion(aline *L)
{
optr OPTR, temp;
opnd OPND;
int i,data1,data2;
char a,b;
init_optr(&OPTR);
init_opnd(&OPND);
init_optr(&temp);
push_optr(&OPTR,'#');
get_base(L , &a);
while(a != '#' || *(OPTR.top-1) != '#')
{
if(a >= '0' && a <= '9')
{
push_optr(&temp ,a);
get_base(L, &a);
}
else
{
if(!empty_optr( &temp))
push_opnd( &OPND , get_data(&temp));
b = *(OPTR.top-1);
i = compare(b , a);
while(i== 1)
{
data1 = *OPND.top;
pop_opnd(&OPND);
data2 = *OPND.top;
pop_opnd(&OPND);
switch(get_no(b))
{
case 0:
push_opnd(&OPND,data2+data1);
pop_optr(&OPTR);
break;
case 1:
push_opnd(&OPND,data2-data1);
pop_optr(&OPTR);
break;
case 2:
push_opnd(&OPND,data2*data1);
pop_optr(&OPTR);
break;
case 3:
push_opnd(&OPND,data2/data1);
pop_optr(&OPTR);
break;
}
b = *(OPTR.top-1);
i = compare(b,a);
}
if(i == 2 && a != '#')
{
push_optr(&OPTR,a);
get_base(L,&a);
}
if(i ==3 && a != '#')
{
pop_optr(&OPTR);
get_base(L,&a);
}
}
}
return *OPND.top;
}
int compare(char row, char cal)
{
int i,j;
i= get_no(row);
j = get_no(cal);
if(fuhao[i][j] == '>')
return 1;
else
if(fuhao[i][j] == '<')
return 2;
else
if(fuhao[i][j] == '=')
return 3;
else
if(fuhao[i][j] == '$')
{
printf("表達式錯誤!");
exit(0);
}
}
int get_no(char c)
{
int i=0;
while(c != ch[i]) i++;
return i;
}
int get_data(optr *x)
{
int i;
i = atoi(x->operatar);
init_optr(x);
return i;
}
int empty_optr( optr *x)
{
if(x->base == x->top)
return 1;
else if(x->base != x->top )
return 0;
}
void get_base(aline *L, char *a)
{
*a = *L->base;
L->base++;
}
void push_opnd(opnd *x, int data)
{
x->top++;
*(x->top) = data;
}
void pop_opnd(opnd *x)
{
*(x->top) = 0;
x->top--;
}
void pop_optr(optr *x)
{
x->top--;
*(x->top) = '\0';
}
void push_optr(optr *x,char ch)
{
*(x->top) = ch;
x->top++;
*(x->top) = '\0';
}
int init_opnd(opnd *x)
{
x->base = x->top = x->data;
x->data[0] = 0;
return 0;
}
int init_optr(optr *x)
{
x->base = x->top = x->operatar;
x->operatar[0] = '\0';
return 0;
}
int get_line(aline *L)
{
int i;
gets(L->expression);
i = strlen(L->expression);
L->expression[i] = '#';
L->expression[i+1] = '\0';
L->top = &L->expression[i];
L->base = L->expression;
return 0;
}
int init_line(aline *L)
{
L->base=L->top = NULL;
L->expression[0] = '\0';
return 0;
}