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

📄 infix.cpp

📁 C、C++语言实现的数据结构重要算法和程序 比较全。
💻 CPP
字号:
#include<string.h>
#include<ctype.h>
#include"MathOptr.h"

int isoptr(char ch)
{
	if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='(')
	    return(1);
	else
	    return(0);
}

void Infix(char *str)
{ 
	char ch,num[10];  
	int i,k,n=strlen(str);  
	double opnd;  
	MathOptr optr;
	Stack<double> OpndS(n);
	Stack<MathOptr> OptrS(n);  
	k=0; ch=str[k];
	while(ch!='=')
	if(isdigit(ch)||ch=='.')
	{
		for(i=0;isdigit(ch)||ch=='.';i++)
		{
			num[i]=ch; k++; ch=str[k];}
			num[i]='\0'; opnd=atof(num);
			OpndS.Push(opnd);
		}
	else if(isoptr(ch)) 
	{
		optr=MathOptr(ch);
		while(!OptrS.Empty()&&OptrS.Peek()>=optr)
        OptrS.Pop().Evaluate(OpndS);
		OptrS.Push(optr); k++; ch=str[k];
	}
	else if(ch==')')
	{
		optr=MathOptr(ch);
		while(!OptrS.Empty()&&OptrS.Peek()>=optr)
		OptrS.Pop().Evaluate(OpndS);
		OptrS.Pop();  k++; ch=str[k];
     }
	while(!OptrS.Empty())
		OptrS.Pop().Evaluate(OpndS);
	opnd=OpndS.Pop(); 
	cout<<opnd<<endl;
}

void main(void)
{
	char str[50];
	cin>>str;
	Infix(str);
}

⌨️ 快捷键说明

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