iT邦幫忙

0

C語言的計算.無法改成想要的樣子

#c
Tzu 2021-01-14 22:33:291282 瀏覽
  • 分享至 

  • xImage

各位大大晚安
終於比較不冷了
/images/emoticon/emoticon06.gif

事情是這樣的~
我接到一個任務是要把RUN出來的結果

想要長成的樣子

但是我怎麼搞都搞不出來~
/images/emoticon/emoticon19.gif
可以給點提示嗎?
謝謝
/images/emoticon/emoticon41.gif
範例: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出來都是




/images/emoticon/emoticon04.gif

謝謝各位大大

Tzu iT邦新手 1 級 ‧ 2021-01-15 20:41:20 檢舉
結果沒人理我....那我自問自答





中序求值
/* 程式範例: Ch5-3-3.c */
#include <stdio.h>
#include <stdlib.h>
#include "stack.c"

char exp1[100];/*全域變數.放轉換過的後序式*/
int i = 0;/*全域變數.讓後序式在存放時可以按照順序*/

/* 是否是運算子 */
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; /* 運算式字串的索引 */
char temp = ""; /* 擷取pop()的輸出 */

/* 剖析運算式字串迴圈 */
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); /* 顯示運算子 */
exp1[i]=op;/*顯示運算子同時擷取輸出*/
i++;/*往下擷取*/
}
else
doit = 0;
}
}
else { /* 比較優先順序 */
doit = 1;
while ( doit && /* 比較優先順序的迴圈 */
!isStackEmpty()) {
op = pop(); /* 取出運算子 */

if (priority(infix[pos])<=priority(op))
{
printf("%c", op); /* 顯示運算子 */
exp1[i]=op;
i++;
}
else
{
push(op); /* 存回運算子 */
doit = 0;
}
} /* 將運算子存入堆疊 */
push(infix[pos]);
}
} else
{
printf("%c", infix[pos]); /* 顯示運算元 */
exp1[i]=infix[pos];
i++;
}
pos++;
} /* 取出剩下的運算子 */
while ( !isStackEmpty() )
{
temp = pop();/*將pop出的子暫時放在temp中*/
printf("%c",temp);/*列印出pop的值*/
exp1[i]=temp;
i++;
}
printf("\n");
exp1[i]='\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; /* 第1個運算元變數 */
int operand2 = 0; /* 第2個運算元變數 */
int pos = 0; /* 運算式字串索引 */



/* 剖析運算式字串迴圈 */
while ( exp[pos] != '\0' && exp[pos] != '\n' ) {
if ( isOperator(exp1[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("後序運算式: ");
postfix(exp);/* 顯示結果 */
printf("運算式: %s = %d\n", exp1, postfixEval(exp1));/*直接將擷取道的後序運算式直接進行運算並存取輸出*/
system("PAUSE");
return 0;


}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答