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

📄 calculate.cpp

📁 大二 数据结构 课程设计 很有用 很规范 计算表达式的值 问题描述:对于给定的一个表达式
💻 CPP
字号:
#include"caculate.h"
#include<iostream>
#include<stdio.h>
using namespace std;
Calculate::Calculate()
{}
Calculate::~Calculate()
{}
//输入表达式并判断表达式是否超出了范围
void Calculate::getline(int limit)	
{
	char c; 
	int i = 0; 
	while (i < limit - 1 && (c = getchar()) != EOF && c != '\n') 
		infix[i++] = c; 
	infix[i] = '\0'; 	
}
//对中缀表达式中的负数进行处理
void Calculate::operatefushu()
{
	float ascii=0;
	char temp='0';
	char temp1;
	int i=0;
	for(i=0; infix[i] != '\0'; i++)
	{
		ascii=infix[i+2]-'0';
		if (infix[i] == '(')
		{
			if (((infix[i+1] == '+') || (infix[i+1] == '-')) && (ascii >= 0 && ascii <= 9))
			{
				temp=infix[i+1];
				infix[i+1]='0';
				for(int j=(i+2); infix[j-1] != '\0'; j++)
				{
					temp1=infix[j];
					infix[j]=temp;
					temp = temp1;
				}
			}
		}
	}
}						

//建立空的栈 
PSeqStack Calculate::createEmptyStack_seq()
{
	PSeqStack pastack; 
	pastack = new SeqStack; 
	if (pastack == NULL) 
		cout<<"没有足够的空间!!"<<endl; 
	else 
		pastack->t = -1; 
	return pastack; 
}

//压栈操作
void Calculate::push_seq(PSeqStack pastack,DataType x)
{
	if (pastack->t >=MAXNUM-1)
		cout<<"栈满溢出"<<endl;
	else
	{
		pastack->t += 1;
		pastack->s[pastack->t]=x;
	}
}


//出栈操作
DataType Calculate::top_seq(PSeqStack pastack)
{
	return pastack->s[pastack->t];
}

//取栈顶元素操作
void Calculate::pop_seq(PSeqStack pastack)
{
	if (pastack->t == -1) 
		cout<<"下溢!"<<endl; 
	else 
		pastack->t = pastack->t - 1;
}

//判断是否为到栈底
int Calculate::isEmptyStack_seq(PSeqStack pastack)
{	return pastack->t == -1; }

//将中缀表达式转化为后缀表达式
int Calculate::infixtoSuffix() //如果中缀表达式能够转化为后缀表达式则返回true,否则返回false
{
	//state_int记录状态,等于true表示刚读入的是数字字符,等于false表示刚读入的不是数字字符, 
	//设置这个变量是为了在每输出一个整数后输出一个空格,以免连续输出的两个整数混在一起.	
	int state_int = FALSE;
	char c, c2;
	PSeqStack ps = createEmptyStack_seq();		//运算符栈
	int i=0;
	int j = 0; 
	int ascii=0;
	//	int flag=1;
	if (infix[0] == '\0') 
	{
		cout<<"输入的表达式不能够为空!!"<<endl;
		return FALSE; //不允许出现空表达式 
	}
	for (i = 0; infix[i] != '\0'; i++) 
	{ 
		c = infix[i]; 
		ascii = c-'0';
		//	cout<<ascii<<endl;
		while(1)
		{
			if ( c == ' ' || c =='\t' || c=='\n')
			{
				if (state_int == TRUE) 
					suffix[j++] = ' ';		//状态从true转换为false时输出一个空格 
				state_int = FALSE;			//遇到空格或制表符忽略
				break; 
			}
			if ( (ascii >=0 && ascii <=9) || ( ascii >= 17 && ascii <= 42 ) || ( ascii >= 49 && ascii <= 74 ) || c == '.')
			{
				state_int = TRUE; 
				suffix[j++] = c; //遇到数字输出 
				break; 
			}
			if (c == '(')
			{
				if (state_int == TRUE) 
					suffix[j++] = ' ';//状态从true转换为false时输出一个空格 
				state_int = FALSE; 
				push_seq(ps, c); //遇到左括号,入栈
				break; 
			}
			if ( c == ')')
			{
				if (state_int == TRUE) 
					suffix[j++] = ' ';//状态从true转换为false时输出一个空格 
				state_int = FALSE; 
				c2 = ')'; 
				while (!isEmptyStack_seq(ps)) 
				{ 
					c2 = top_seq(ps);	//取栈顶 
					pop_seq(ps);		//出栈 
					if (c2 == '(') 
						break; 
					suffix[j++] = c2;				
				} 
				if (c2 != '(') 
				{ 
					delete(ps); 
					suffix[j++] = '\0';
					cout<<"表达式中括号不匹配!!"<<endl;
					return FALSE; 
				} 
				break; 
			}
			if( c == '+' || c == '-')
			{
				if (state_int == TRUE) 
					suffix[j++] = ' '; 
				state_int = FALSE; 
				while(!isEmptyStack_seq(ps)) 
				{ 
					c2 = top_seq(ps); 
					if (c2 == '+' || c2 == '-' || c2 == '*' || c2 == '/') 
					{ 
						pop_seq(ps); 
						suffix[j++] = c2; 
					} 
					else if(c2=='(') break; 
				} 
				push_seq(ps, c); 
				break; 
			}
			if( c == '*' || c == '/')
			{
				
				if (state_int == TRUE) 
					suffix[j++] = ' '; 
				state_int = FALSE; 
				while (!isEmptyStack_seq(ps)) 
				{ 
					c2 = top_seq(ps); 
					if (c2 == '*' || c2 == '/') 
					{ 
						pop_seq(ps); 
						suffix[j++] = c2; 
					} 
					else if(c2=='+'||c2=='-'||c2=='(') 
						break; 
				} 
				push_seq(ps, c); 
				break; 
			}
			else
			{
				delete(ps); 
				suffix[j++] = '\0';
				cout<<"表达式中含有非法符号!!"<<endl;
				return FALSE; 
			}
		} 
	} 
	//运算符表达式中的数字全部输出后将栈中的所有运算符输出
	if (state_int == TRUE) 
		suffix[j++] = ' '; 
	while (!isEmptyStack_seq(ps)) 
	{ 
		c2 = top_seq(ps); 
		pop_seq(ps); 
		if (c2 == '(') 
		{ 
			free(ps); 
			suffix[j++] = '\0'; 
			cout<<"表达式中括号不匹配!!"<<endl;
			return FALSE; 
		} 
		suffix[j++] = c2; 
	} 
	delete(ps); 
	suffix[j++] = '\0'; 
	return TRUE; 
}

//输出后缀表达式
void Calculate::display_Suffix()
{
	cout<<"后缀表达式为:"<<suffix<<endl;
}		
//计算后缀表达式的值
int Calculate::calculateSuffix()
{
	int state_int = FALSE; 
	int state_float = FALSE;		//小数部分的标志
	PSeqStack ps = createEmptyStack_seq(); 
	float num = 0, num1=0, num2=0; 
	float sum=0;
	float sum1=1;	
	int count=0;
	int i=0; 
	char c='0';
	int ascii=0;
	for (i = 0; suffix[i] != '\0'; i++) 
	{ 
		c = suffix[i]; 
		ascii = c- '0';
		while(1) 
		{ 
			if ( (ascii >=0 && ascii <=9) || ( ascii >= 17 && ascii <= 42 ) || ( ascii >= 49 && ascii <= 74 ) || c == '.')
			{
				if( c == '.')
					state_float = TRUE;
				if (state_int == TRUE && state_float == TRUE && c != '.')
				{
					count=count+1;
					sum1=1;
					for(int j=0; j<count; j++)
					{
						sum1 = sum1*10;
					}
					sum = (c - '0');
					sum = sum / sum1;
					num = num + sum;    
				}
				if (state_int == TRUE && state_float == FALSE) 
					num = num * 10 + (c - '0'); 
				else if (state_int == FALSE)
					num = c - '0'; 
				state_int = TRUE; 
				break; 
			}
			if ( c == ' ' || c =='\t' || c=='\n')
			{
				if (state_int == TRUE || state_float == TRUE) 
				{ 
					push_seq(ps, num);	//将表达式的数字或者变量压入栈中
					state_int = FALSE; 
					state_float = FALSE; 
					count = 0;
				} 
				break; 
			}
			if (c == '+' || c == '-' || c == '*' || c == '/')
			{
				if (state_int == TRUE || state_float == TRUE) 
				{ 
					push_seq(ps, num); 
					state_int = FALSE;
					state_float = FALSE;
				} 
				if (isEmptyStack_seq(ps)) 
				{ 
					delete(ps); 
					//		cout<<"表达式中运算符有多余!!"<<endl;
					return FALSE; 
				} 
				num2 = top_seq(ps); 
				pop_seq(ps); 
				if (isEmptyStack_seq(ps)) 
				{ 
					delete(ps); 
					cout<<"表达式中运算符有多余!!"<<endl;
					return FALSE; 
				} 
				num1 = top_seq(ps); 
				pop_seq(ps); 
				if (c == '+') 
					push_seq(ps, num1 + num2); 
				if (c == '-') 
					push_seq(ps, num1 - num2); 
				if (c == '*') 
					push_seq(ps, num1 * num2); 
				if (c == '/') 
				{
					if(num2 == 0)
					{
						cout<<"0 不能作除数"<<endl;
						return FALSE;
					}
					push_seq(ps, num1 / num2); 
				}
				break;
			}
			else
			{
				delete(ps);
				cout<<"表达式中运算符有多余!!"<<endl;
				return FALSE; 
			}
		} 
	} 
	result = top_seq(ps); 
	pop_seq(ps); 
	if (!isEmptyStack_seq(ps)) 
	{ 
		delete(ps); 
		cout<<"表达式中缺少运算符!!"<<endl;
		return FALSE; 
	} 
	delete(ps); 
	return TRUE; 
}

//输出计算的后缀表达式的结果
void Calculate::Display_Result()
{
	cout<<"运算结果为:"<<result<<endl;
}

//验证表达式中是否含有字符变量,是返回真,否则,返回0	
int Calculate::validate()
{
	int ascii=0;
	for ( int i = 0; suffix[i] != '\0'; i++ )
	{
		ascii = suffix[i] - '0';
		if (( ascii >= 17 && ascii <= 42 ) || ( ascii >= 49 && ascii <= 74))
		{
			cout<<"表达式中有单个字符变量。"<<endl;
			cout<<"你所输入的表达式如下:"<<endl;
			for ( i = 0; infix[i] != '\0'; i++ )
			{
				cout<< infix[i];
			}
			cout<<endl;
			return TRUE;
		}
	}
	return FALSE;
}
	
//对单个字符变量付值
void Calculate::pay_number()
{
	int ascii=0;
	int count=0; //计数器计算输入变量对应的值的个数
	int count1 = 0;	//计录后缀表达式中的表达式个数
	int t=0;
	char temp[50];//存放临时变量值的数组
	char c; 
	
	for (int i = 0; suffix[i] != '\0'; i++ )
	{
		ascii = suffix[i] - '0';
		if (( ascii >= 17 && ascii <= 42 ) || ( ascii >= 49 && ascii <= 74))
		{
			count1 = 0;
			for ( int j = 0; suffix[j] != '\0'; j++ )	//
				count1 ++;
			cout<<suffix[i]<<":";
			count=0;
           	while ((c = getchar()) != EOF && c != '\n') 
			{			
				temp[count] = c;
				count++;
			}
			for ( j=count1; j!=i; j--)
				suffix[count+j-1]=suffix[j];
			suffix[count+j-1]=suffix[j];
			j=i;
			t=0;
			while( t < count )
			{
				suffix[j]=temp[t];
				t++;
				j++;
			}
		}
	}
}
																					
/**/

⌨️ 快捷键说明

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