问题描述
// infix.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include<stdlib.h>
#include<ctype.h>
using namespace std;
class expression
{
private:
char infix[100];
char stack[200];
int top;
int r;
char postfix[100];
public:
void convert();
int input_p(char);
int stack_p(char);
int rank(char);
};
int expression::input_p(char c)
{
if( c== ''+'' || c==''-'' )
return 1;
else if( c==''*'' || c==''/'' )
return 3;
else if( c==''^'')
return 6;
else if( isalpha(c)!=0)
return 7;
else if( c==''('' )
return 9;
else if( c=='')'' )
return 0;
else
{
cout<<"Invalid expression ::input error\n";
exit(0);
}
}
int expression::stack_p(char c)
{
if(c==''+'' || c==''-'')
return 2;
else if(c==''*'' || c==''/'')
return 4;
else if(c==''^'' )
return 5;
else if(isalpha(c)!=0)
return 8;
else if(c== ''('' )
return 0;
else
{
cout<<"Invalid expression ::stack error\n";
exit(0);
}
}
int expression::rank(char c)
{
if(c==''+'' || c==''-'')
return -1;
else if(c==''*'' || c==''/'')
return -1;
else if(c==''^'')
return -1;
else if(isalpha(c)!=0)
return 1;
else
{
cout<<"Invalid expression ::in rank\n";
exit(0);
}
}
void expression::convert()
{
cout<<"\n*************************************************\n"
<<"This program converts the given infix expression\n"
<<"in to postfix form"
<<"\n*************************************************\n";
cout<<"Enter an infix expression ::\n";
cin>>infix;
int l=strlen(infix);
infix[l]='')'';
infix[l+1]='''';
//Convertion starts
top=1;
stack[top]=''('';
r=0;
int x=-1;
int i=0;
char next=infix[i];
while(next!='''')
{
//Pop all the elements to outputin stack which have higher precedence
while( input_p(next) < stack_p(stack[top]) )
{
if(top<1)
{
cout<<"invalid expression ::stack error\n";
exit(0);
}
postfix[++x]=stack[top];
top–-;
r=r+rank(postfix[x]);
if(r<1)
{
cout<<"Invalid expression ::r<1\n";
exit(0);
}
}
if(input_p( next ) != stack_p( stack[top]))
stack[++top]=next;
else
top–-;
i++;
next=infix[i];
}
postfix[++x]='''';
if(r!=1 || top!=0)
{
cout<<"Invalid expression ::error in rank or stack\n";
exit(0);
}
cout<<"\n\nThe corresponding postfix expression is ::\n";
cout<<postfix<<endl;
}
int main()
{
expression obj;
obj.convert();
return 0;
}
1> ------构建已开始:项目:infix,配置:Debug Win32 ------
1>正在编译...
1> infix.cpp
1> z:\ math \ arianfar \ mydocs \ Visual Studio 2008 \ projects \ infix \ infix \ infix.cpp(94):错误C2137:空字符常量
1> z:\ math \ arianfar \ mydocs \ Visual Studio 2008 \ projects \ infix \ infix \ infix.cpp(106):错误C2137:空字符常量
1> z:\ math \ arianfar \ mydocs \ visual studio 2008 \ projects \ infix \ infix \ infix.cpp(118):错误C2065:"top–":未声明的标识符
1> z:\ math \ arianfar \ mydocs \ Visual Studio 2008 \ projects \ infix \ infix \ infix.cpp(118):错误C2059:语法错误:'';''
1> z:\ math \ arianfar \ mydocs \ Visual Studio 2008 \ projects \ infix \ infix \ infix.cpp(132):错误C2065:``top–'':未声明的标识符
1> z:\ math \ arianfar \ mydocs \ Visual Studio 2008 \ projects \ infix \ infix \ infix.cpp(132):错误C2059:语法错误:'';''
1> z:\ math \ arianfar \ mydocs \ Visual Studio 2008 \ projects \ infix \ infix \ infix.cpp(137):错误C2137:空字符常量
1>生成日志保存在"file://\\ scidc01 \ bsprof $ \ math \ arianfar \ mydocs \ visual studio 2008 \ projects \ infix \ infix \ Debug \ BuildLog.htm"中.
1>中缀-7个错误,0个警告
========== Build:0成功,1失败,0最新,跳过0 ==========
它是后缀程序的前缀:confused:
1>------ Build started: Project: infix, Configuration: Debug Win32 ------
1>Compiling...
1>infix.cpp
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(94) : error C2137: empty character constant
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(106) : error C2137: empty character constant
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(118) : error C2065: ''top–'' : undeclared identifier
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(118) : error C2059: syntax error : '';''
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(132) : error C2065: ''top–'' : undeclared identifier
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(132) : error C2059: syntax error : '';''
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(137) : error C2137: empty character constant
1>Build log was saved at "file://\\scidc01\bsprof$\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\Debug\BuildLog.htm"
1>infix - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
it is an infix to postfix program:confused:
推荐答案
z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(94) : error C2137: empty character constant
从infix.cpp(94)中查看infix.cpp的第94行.
Look at line 94 of infix.cpp - from the "infix.cpp(94)"
infix[l+1]='''';
不允许同时使用两个引号字符:它们不是字符.
我怀疑您的意思是"\ 0"作为空终止符.同样适用于第106行.
只是通过列表来查看与该行本身相关的每个错误消息就可以了!
Two quote characters together are not allowed: they are not a character.
I suspect that you mean ''\0'' as a null terminator character instead. The same will apply to line 106.
Just work though the list looking at each error message in relation to the line itself!
这篇关于为我提供帮助:“空字符常量";错误.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!