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

📄 nibolan_loca.cpp

📁 逆波兰
💻 CPP
字号:
#include<stdio.h>
#include<iostream>
#include<math.h>
#define max 100
char ex[max];       //存储后缀表达式
void trans(){        //将算术表达式转化为后缀表达式
	char str[max];   //存储原算术表达式
	char stack[max];       //作为栈使用
	char temp[max];
	char temp2[max];
	char temp3[max];
	char ch,ch2;
	int sum,i,i2,j,j2=0,j3=0,t,t2,top=0,top2=0,flag=0;
	printf("输入一个以#结束的表达式:\n");
	i=0;                      //获取用户输入的表达式
	do{
		i++;
		scanf("%c",&str[i]);
	}while(str[i]!='#' && i!=max);

    for (i2=0;i2<i+1;i2++)
	{
		temp[i2]=str[i2];
		temp2[i2]=str[i2];
		temp3[i2]=str[i2];

	}

    sum=i;
	t=1;i=1;
	ch=str[i];i++;
	while(ch!='#'){
		switch(ch){
		case '(':                 //判定为左括号
			top++;stack[top]=ch;
			     break;
        case ')':                 //判定为右括号
			while(stack[top]!='('){
                ex[t]=stack[top];top--;t++;
			}
			top--;
			break;
        case '+':                 //判定为加减号
		case '-':       
			while(top!=0&&stack[top]!='('){
				ex[t]=stack[top];top--;t++;
			}
			top++;stack[top]=ch;
			break;
		case '*':                  //判定为乘除号
        case '/':
			while(stack[top]=='*'||stack[top]=='/'){
				ex[t]=stack[top];top--;t++;
			}
			top++;stack[top]=ch;
			break;
		case ' ':break;
		default:while(ch>='0'&&ch<='z'){                 //判定为数字
			ex[t]=ch;t++;
			ch=str[i];i++;
				}
			i--;
			ex[t]='#';t++;                               //逆波兰式里的空格
		}
		ch=str[i];i++;
	}
	while(top!=0){
		ex[t]=stack[top];t++;top--;
	}
	ex[t]='#';
	printf("\n");
	for(j=1;j<sum;j++)
		printf("%c",str[j]);
    printf("\t→\t",ex);
	for(j=1;j<t;j++)
		printf("%c",ex[j]);
    printf("\n\n");




	for (int k=0;k<i2+1;k++)                         //输入数字程序段
	{
		if (temp[k]>='a'&&temp[k]<='z')
		{
			if (k==0)
			{
				std::cout<<temp[0]<<"→";
				std::cin>>temp[0];
				temp3[0]=temp[0];
				std::cout<<"\n";
			}
			else
			{
				for (int k2=0;k2<k;k2++)
				{
					if (temp[k]==temp2[k2])
					{
						temp[k]=temp3[k2];
					    flag=0;
					    break;
					}
					else 
						flag=1;
				}
			    if (flag==1)
				{
					std::cout<<temp[k]<<"→";
					std::cin>>temp[k];
					temp3[k]=temp[k];
					std::cout<<"\n";
				}
			}
				
		}
	}


    sum=i2;
	t2=1;i2=1;
	ch2=temp[i2];i2++;
	while(ch2!='#'){
		switch(ch2){
		case '(':                 //判定为左括号
			top2++;stack[top2]=ch2;
			     break;
        case ')':                 //判定为右括号
			while(stack[top2]!='('){
                ex[t2]=stack[top2];top2--;t2++;
			}
			top2--;
			break;
        case '+':                 //判定为加减号
		case '-':       
			while(top2!=0&&stack[top2]!='('){
				ex[t2]=stack[top2];top2--;t2++;
			}
			top2++;stack[top2]=ch2;
			break;
		case '*':                  //判定为乘除号
        case '/':
			while(stack[top2]=='*'||stack[top2]=='/'){
				ex[t2]=stack[top2];top2--;t2++;
			}
			top2++;stack[top2]=ch2;
			break;
		case ' ':break;
		default:while(ch2>='0'&&ch2<='9'){                 //判定为数字
			ex[t2]=ch2;t2++;
			ch2=temp[i2];i2++;
				}
			i2--;
			ex[t2]='#';t2++;
		}
		ch2=temp[i2];i2++;
	}
	while(top2!=0){
		ex[t2]=stack[top2];t2++;top2--;
	}
	ex[t2]='#';


}
void compvalue(){                                 //计算逆波兰表达式的值
	float stack[max],d;                           //作为栈使用
	char ch;
	int t=1,top=0;                                //t为ex下标,top为stack下标
	ch=ex[t];t++;
	stack[0]=0;
    while(ch!='#'){
		switch(ch){
		  case '+':
			  stack[top-1]=stack[top-1]+stack[top];
			  top--;
			  break;
          case '-':
			  stack[top-1]=stack[top-1]-stack[top];
			  top--;
			  break;
          case '*':
			  stack[top-1]=stack[top-1]*stack[top];
			  top--;
			  break;

          case '/':
	          if(stack[top]!=0)
			    stack[top-1]=stack[top-1]/stack[top];
	          else{
		          printf("\n error!\n");
		          break;                   //异常退出
			  }
			  top--;
			  break;
		  default:
			  d=0;
			  while(ch>='0'&&ch<='9'){
				  d=10*d+ch-'0';               //将数字字符转化为对应的数值   
				  ch=ex[t];t++;
			  }
			  top++;
			  stack[top]=d;
		}
		ch=ex[t];t++;
	}
	printf("\nresult = %g\n",stack[top]);
	printf("\n");
}

void main(){
	trans();
	compvalue();
}

⌨️ 快捷键说明

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