各位大大晚安
終於比較不冷了
事情是這樣的~
我接到一個任務是要把RUN出來的結果
想要長成的樣子
但是我怎麼搞都搞不出來~
可以給點提示嗎?
謝謝
範例:Ch3_2.h
#define MAXSTACK 100
int stack[MAXSTACK];
int top=-1;
extern int isStackEmpty();
extern int push(int d);
extern int pop();
範例:stack.c
#include "Ch3_2.h"
/* 函數: 檢查堆疊是否是空的 */
int isStackEmpty() {
   if ( top == -1 ) return 1;
   else             return 0;
}
/* 函數: 將資料存入堆疊 */
int push(int d) {
   if ( top >= MAXSTACK ) { /* 是否超過堆疊容量 */
      printf("堆疊內容全滿\n");
      return 0;
   }   
   else {
      stack[++top] = d;    /* 存入堆疊 */
      return 1;
   }
}
/* 函數: 從堆疊取出資料 */
int pop() {
   if ( isStackEmpty() )   /* 堆疊是否是空的 */
      return -1;
   else 
      return stack[top--]; /* 取出資料 */
}
範例:3-3-2
#include <stdio.h>
#include <stdlib.h>
#include "stack.c
int isOperator(char op){
	switch(op){
		case '+':
		case '-':
		case '*':
		case '+':return 1;
		default: return 0;
		
	}
}
int cal(int op,int operand1,int operand2){
	switch((char)op){
		case '*':return(operand2*operand1);
		case '/':return(operand2/operand1);
		case '+':return(operand2+operand1);
		case '-':return(operand2-operand1);
	}
}
int postfixEval(char *exp){
	int operand1=0;
	int operand2=0;
	int pos=0;
	
	while(exp[pos] != '\n' && exp[pos] != '\n'){
		if(isOperator(exp[pos])){
			operand1=pop();
			operand2=pop();
			push(cal(exp[pos],operand2,operand1));
		}
		else
		push(exp[pos]-48);
		pos++;
	}
	
	return pop();
}
int main(){
char exp[100];
printf("請輸入後序運算式==>");
gets(exp);
printf("運算式:%s = %d\n",exp,postfixEval(exp));
	
	return 0;
}
範例:3-3-3
#include <stdio.h>
#include <stdlib.h>
#include "stack.c"
int isOperator(char op){
	switch(op){
		case '(':
		case ')':
		case '+':
		case '-':
		case '*':
		case '/':return 1;
		default: return 0;
		
	}
}
int priority(char op){
	switch(op){
		
		case '*':
		case '/':return 3;
		case '+':
		case '-':return 2;
		case '(':return 1;
		default: return 0;
		
	}
}
void postfix(char*infix){
	int op,doit;
	int pos=0;
	while(infix[pos] !='\0' && infix[pos] !='\n'){
		if(isOperator(infix[pos])){
		
		if(isStackEmpty() || infix[pos]=='(')push(infix[pos]);
		else if ( infix[pos]=='('){
			doit=1;
			while(doit){
				op=pop();
				if(op !='(')
				printf("%c",op);
				else
				doit=0;
			}
		}
		else{
			doit=1;
			while (doit && !isStackEmpty()){
				op=pop();
				if(priority(infix[pos])<=priority(op))printf("%c",op);
				else{
					push(op);
					doit = 0;
				}
			}
			push(infix[pos]);
		}
	}else printf("%c",infix[pos]);
	pos++;
	}
	while (!isStackEmpty()) 
	printf("%c",pop());
	printf("\n");
}
int main(){
char exp[100];
printf("請輸入中序運算式==>");
gets(exp);
printf("後序運算式:");
postfix(exp);
printf("計算結果=");	
	return 0;
}
想要可以運算-1+3*-5
-(10+20)3+-1-10+(-(20+3)*2)
但是RUN出來都是





謝謝各位大大