⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 1、猴子选大王 2、约瑟夫环 3、迷宫求解 4、回文游戏 5、地图四染色问题 6、八皇后问题 7、原四则表达式求值 8、k阶斐波那契序列 9、遍历二叉树 10、编写DFS算法的非递归
💻
字号:
#include <stdio.h>
#define StackSize 100
#define QueueSize 100


typedef struct{
	char data[100];
	int front,rear;
}SeqQueue;


void InitQueue(SeqQueue * Q)
{
	Q->front=0;
	Q->rear=0;
}

int QueueEmpty(SeqQueue * Q)
{
	return Q->rear==Q->front;
}

void EnQueue(SeqQueue * Q,char x)
{
	if((Q->rear+1)%QueueSize==Q->front)
		printf("Queue overflow!");
	else
	{
		Q->data[Q->rear]=x;
		Q->rear=(Q->rear+1)%QueueSize;
	}
}

char DeQueue(SeqQueue * Q)
{
	if(Q->front==Q->rear)printf("ERROR!");
	Q->front++;
	return Q->data[Q->front-1];
	
}



typedef struct{
	char data[100];
	int top;
}SeqStack;

void InitStack(SeqStack * S)
{
	S->top=-1;
}

void Push(SeqStack * S,char x)
{
	if(S->top==StackSize-1)
		printf("stack overflow!");
	else{
		S->top=S->top+1;
		S->data[S->top]=x;
	}
}


char Pop(SeqStack * S)
{
	if(S->top==-1)
		printf("stack underflow!");
	else
		return S->data[S->top--];
}

char GetTop(SeqStack * S)
{
	if(S->top==-1)
		printf("stack empty!");
	else
		return S->data[S->top];
}


void CTPostExp(SeqQueue * Q)
{
	int Priority(char);
	SeqStack OS,*S;
	char c,t;
	
	
	S=&OS;
	InitStack(S);
	Push(S,'#');

	do{
		c=getchar();
		switch(c)
		{
		case ' ':break;

		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':EnQueue(Q,c);break;


		case '(':Push(S,c);break;
		
		case ')':
		case '#':
			do{
				t=Pop(S);
				if(t!='('&&t!='#')EnQueue(Q,t);
			}while(t!='('&&S->top!=-1);break;
			
			
		case '+':
		case '-':
		case '*':
		case '/':
			while(Priority(c)<=Priority(GetTop(S)))
			{
				t=Pop(S);
				EnQueue(Q,t);
			}
			Push(S,c);
			break;
		}//EndSwitch

	}while(c!='#');//End with '#'

}


int Priority(char op)
{
	switch(op){
	case '(':
	case '#':return 0;
	case '-':
	case '+':return 1;
	case '*':
	case '/':return 2;
	}
}

void main()
{
	int k;
	SeqQueue * Q;
	SeqQueue PostQ;
	
	int CPostExp(SeqQueue *);
	Q=&PostQ;

	InitQueue(Q);
	CTPostExp(Q);
	
	k=Q->front;

	while(k!=Q->rear)
	{
		printf("%2c",Q->data[k]);
		k++;
	}
	printf("\n");
	printf("Result:%d\n",CPostExp(Q));
		
}


int CPostExp(SeqQueue * Q)
{
	SeqStack VS,*S;
	char ch;
	int x,y;
	S=&VS;
	InitStack(S);
	while(!QueueEmpty(Q))
	{
		ch=DeQueue(Q);
		if(ch>='0'&&ch<='9')
			Push(S,ch-'0');
		else{
			y=Pop(S);
			x=Pop(S);
			switch(ch)
			{
			case '+':Push(S,x+y);break;
			case '-':Push(S,x-y);break;
			case '*':Push(S,x*y);break;
			case '/':Push(S,x/y);break;
			}
		}
	}
	return GetTop(S);
}



			

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -