首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

挺怪的程序异常

2012-03-01 
挺怪的程序错误`#includestdafx.h #include iostream#include cstring#include map#include stde

挺怪的程序错误`
#include   "stdafx.h "
#include <iostream>
#include <cstring>
#include <map>
#include <stdexcept>
using   namespace   std;

map <string,   double>   table;
enum   Token_value{
NAME,   NUMBER,   END,
PLUS= '+ ',   MINUS= '- ',   MUL= '* ',   DIV= '/ ',
PRINT= '; ',ASSIGN= '= ',LP= '( ',RP= ') ',
};
Token_value   curr_tok=PRINT;

struct   LineCount{
LineCount():line_(1){}
unsigned   long   current(){return   line_;}
void   new_line(){++line_;}
private:
unsigned   long   line_;

};
LineCount   line_count;

int   no_of_errors;
double   error(const   string   &msg)
{
no_of_errors++;
cerr < < "error   (line   " < <line_count.current() < < "): " < <msg < < '\n ';
return   1;
}
double   number_value;
string   string_value;

Token_value   get_token()
{
char   ch;
do{   //跳过空白,除了'\n'
if   (!cin.get(ch)   )   return   curr_tok=END;
}while(ch!= '\n '&&   isspace(ch)   );
switch(ch){
case   '; ':   line_count.new_line();
case '\n ':
return   curr_tok=PRINT;
case   '0 ':   case   '1 ':   case   '2 ':   case   '3 ':   case   '4 ':  
case   '5 ':   case   '6 ':   case   '7 ':   case   '8 ':   case   '9 ':
case   '. ':
cin.putback(ch);
cin> > number_value;
return   curr_tok=NUMBER;
default:
if(isalpha(ch)){
string_value=ch;
while(cin.get(ch)   &&   isalnum(ch))   string_value.push_back(ch);
cin.putback(ch);
return   curr_tok=NAME;
}
error( "bad   token ");
return   curr_tok=PRINT;
}
}

double   expr(bool);
double   prim(bool   get)
{
if(get)   get_token();
switch(curr_tok){
case   NUMBER:
{
double   v=number_value;
get_token();
return   v;
}
case   NAME:
{
double&   v=table[string_value];
if(get_token()==ASSIGN)   v=expr(true);
return   v;
}
case   MINUS:
{
return   -prim(true);
}
case   LP:
{
double   e=expr(true);
if(curr_tok!=RP)   return   error( ")expected ");
get_token();
return   e;
}
default:
return   error( "primary   expected ");
}
}


double   term(bool   get)
{
double   left=prim(get);
for(;;)
switch(curr_tok){
case   MUL:
left*=prim(true);
break;
case   DIV:
if(double   d=prim(true)){
left/=d;
break;
}
return   error( "divide   by   0 ");
default:
return   left;
}
}

double   expr(bool   get)
{
double   left=term(get);
for(;;)
switch   (curr_tok){
case   PLUS:  


left+=term(true);
break;
case   MINUS:
left-=term(true);
break;
default:
return   left;
}
}


int   _tmain(int   argc,   _TCHAR*   argv[])
{
table[ "pi "]=3.1415926535897332385;
table[ "e "]=2.7182818284590452354;
while(cin){
get_token();
if(curr_tok==END)   break;
if(curr_tok==PRINT)   continue;
cout < <expr(false) < < '\n ';
}
return   no_of_errors;
get_token();
return   0;
}

错误出在cerr < < "error   (line   " < <line_count.current() < < "): " < <msg < < '\n ';
binary   ' < < '   :   no   operator   found   which   takes   a   right-hand   operand   of   type   'const   std::string '   (or   there   is   no   acceptable   conversion)

[解决办法]
cstring是MFC里的头文件...

热点排行