📄 赋值语句翻译.cpp
字号:
#include<iostream.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#define max 64
FILE *fp;
char cr;
void main()
{
if ((fp=fopen("input.txt","r"))==NULL)
cout << "找不到文件input.txt,请创建文件并输入程序:" << endl;
char display_out(char out_ch[max], char ch[32]);
static int i=0;
static int j=0;
char ch[max],s[max],out[max];
while((cr=fgetc(fp))!=EOF)
{
if(cr==' '||cr=='\n'||cr=='\t')continue;//跳过空格或换行或制表符
for(int k=0;cr!='#';k++)
{ch[k]=cr;
cr=fgetc(fp);
}
ch[k]='#';
}
for(i=0;i<max;i++)
{
out[i]=ch[i];
}
cout<<"您输入的表达式: ";
while(out[j]!='#')
{
cout<<out[j];
j++;
}
cout<<'#'<<endl;
display_out(s,out);
}
char display_out(char out_ch[32],char ch[])
{
int top=-1;
int i=0,data[max],n;
int j=0;
char sta[20];
while(ch[i]!='#')
{
if(isalnum(ch[i]))
{
while(isalnum(ch[i]))
{
out_ch[j]=ch[i];
j++;
i++;
}out_ch[j]=' ';j++;
}
else{
switch(ch[i])
{
case '=':if(sta[top]=='+'||sta[top]=='-')
{
out_ch[j]=sta[top];
j++;
top--;
}
else
{
top++;
sta[top]=ch[i];
i++;
}
break ;
case '+':
case '-': if(sta[top]=='('||top==-1)
{
top++;
sta[top]=ch[i];
i++;
}
else
{
out_ch[j]=sta[top];
j++;
top--;
}
break;
case '*':
case '/':if(sta[top]=='*'&&sta[top]=='/')
{
out_ch[j]=sta[top];
j++;
top--;
}
else
{
top++;
sta[top]=ch[i];
i++;
}
break ;
case '(':top++;
sta[top]=ch[i];
i++;
break;
case ')':if(sta[top]=='(')
{
top--;
i++;
}
if(top==-1)
{
cout<<"错误: 第"<<j<<"个位置的“)”没有找到与之匹配的“(”";
ch[i]='#';j=0;
}
else
{
out_ch[j]=sta[top];
top--;
j++;
}
break;
default:
cout<<" your input is error"<<endl;
ch[i]='#';
j=0;
break;
}
}
}while(top!=-1)
{
out_ch[j]=sta[top];j++;
top--;
}
out_ch[j]='#';
n=0;
cout<<"逆波兰表达式为: ";
while(out_ch[n]!='#')
{
cout<<out_ch[n];
n++;
}
j=0;
cout<<'='<<endl;
return out_ch[max];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -