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

📄 main.cpp

📁 本程序是以实现基本的加减乘除的功能的c语言程序
💻 CPP
字号:
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

typedef int ElemType;
const int STACK_INIT_SIZE=100;
const int STACKINCREMENT=10;

typedef struct 
{
	ElemType *base;
	ElemType *top;
	int stacksize;
}Stack;


int IfEmptyStack(Stack S);
void InitStack(Stack &S);
void EmptyStack(Stack &S);
void Push(Stack &S, ElemType e);
void Pop(Stack &S, ElemType &e);
void ShowStack(Stack S);


int In(char ch);
char Precede(char a, char b);
int Operate(int a, char f, int b);

void EvaluateExpression();


int main()
{
	cout<<"请输入表达式,以 # 结束..."<<endl;
	EvaluateExpression();
	return 0;
}


int IfEmptyStack(Stack S)
{
	if(S.base==S.top)
		return 1;
	return 0;
}

void InitStack(Stack &S)
{
	S.base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
	S.top=S.base;
	S.stacksize=STACK_INIT_SIZE;
	return ;
}

void EmptyStack(Stack &S)
{
	S.top=S.base;
	return ;
}

void Push(Stack &S, ElemType e)
{
	if(S.top-S.base>=S.stacksize)
	{
		S.base=(ElemType *)realloc(S.base, (S.stacksize+STACKINCREMENT)*sizeof(ElemType));
		S.top=S.base+S.stacksize;
		S.stacksize+=STACKINCREMENT;
	}	
	*S.top++=e;
	return ;
}

void Pop(Stack &S, ElemType &e)
{
	if(S.top==S.base)
		return ;
	e=*--S.top;
	return ;
}

ElemType GetTop(Stack &S)
{
	if(S.top==S.base)
		return 0;
	return *(S.top-1);
}

void ShowStack(Stack S)
{
	ElemType *p=S.base;
	while(p!=S.top)
		printf("%d",*p++);
    return;
}

int In(char ch)
{
	int res;
	switch(ch)
	{
		case '+':
		case '-':
		case '*':
		case '/':
		case '(':
		case ')':
		case '#':	res=1;break;
		default:	res=0;break;
	}
	return res;
}

char Precede(char a, char b)
{
	int i,j;
	int form[7][7]=
	{
		{1,1,-1,-1,-1,1,1},
		{1,1,-1,-1,-1,1,1},
		{1,1,1,1,-1,1,1},
		{1,1,1,1,-1,1,1},
		{-1,-1,-1,-1,-1,0,2},
		{1,1,1,1,2,1,1},
		{-1,-1,-1,-1,-1,2,0}
	};
	switch(a)
	{
		case '+':i=0;break;
		case '-':i=1;break;
		case '*':i=2;break;
		case '/':i=3;break;
		case '(':i=4;break;
		case ')':i=5;break;
		case '#':i=6;break;
	}
	switch(b)
	{
		case '+':j=0;break;
		case '-':j=1;break;
		case '*':j=2;break;
		case '/':j=3;break;
		case '(':j=4;break;
		case ')':j=5;break;
		case '#':j=6;break;
	}
	if(form[i][j]==1)
		return '>';
	else if(form[i][j])
		return '<';
	else
		return '=';
}

int Operate(int a, char f, int b)
{
	switch(f)
	{
	case '+':return a+b;
	case '-':return a-b;
	case '*':return a*b;
	case '/':return a/b;
	}
	return 0;
}

void EvaluateExpression()
{
	char c, d[100];
	int i, f, num, tmpa, tmpb;
	Stack OPTR, OPND;
	InitStack(OPTR);InitStack(OPND);
	Push(OPTR, '#');
	c=getchar();
	while(c!='#'||GetTop(OPTR)!='#')
	{
		if(c>='0'&&c<='9')
		{
			i=0;
			do{
				d[i++]=c;
				c=getchar();
			}while(c>='0'&&c<='9');
			d[i]='\0';
			num=atoi(d);
			Push(OPND, num);
		}
		else if(In(c))
		{
			switch(Precede(GetTop(OPTR), c))
			{
				case '<':	Push(OPTR, (int)c);c=getchar();
							break;
				case '=':	Pop(OPTR, f);c=getchar();
							break;
				case '>':	Pop(OPTR, f);Pop(OPND, tmpb);Pop(OPND, tmpa);
							Push(OPND, Operate(tmpa, f, tmpb));
							break;
			}
		}
	}
	cout<<"所求表达式的值为:";
	ShowStack(OPND);
	cout<<endl<<endl;
}

⌨️ 快捷键说明

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