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

case标号的值是不是不能用STRING类的常量?该怎么处理

2012-03-03 
case标号的值是不是不能用STRING类的常量?如题,程序如下:#includeiostream#includestringusingnamespa

case标号的值是不是不能用STRING类的常量?
如题,程序如下:
#include   <iostream>
#include   <string>
using   namespace   std;

int   pri(string   &s)
{
switch(s)
{
case   "# ":
return   1;
case   "&& ":
return   2;
case   "|| ":
return   2;
case   "> ":
return   3;
case   " < ":
return   3;
case   "> = ":
return   3;
case   " <= ":
return   3;
case   "!= ":
return   3;
case   "== ":
return   3;
case   "+ ":
return   4;
case   "- ":
return   4;
case   "* ":
return   5;
case   "/ ":
return   5;
case   "! ":
return   6;
}
}

bool   operator> (string   &s1,string   &s2)
{
int   i   =   pri(s1);
int   j   =   pri(s2);
        return   (i> j);
}

bool   operator <(string   &s1,string   &s2)
{
int   i   =   pri(s1);
int   j   =   pri(s2);
return   (i <j);
}

bool   operator==(string   &s1,string   &s2)
{
int   i   =   pri(s1);
int   j   =   pri(s2);
return   (i==j);
}

int   main   ()
{
string   a;
string   b;
cin   > >   a;
cout   < <   "b: ";
cin   > >   b;
if(a> b)
cout   < <   "a> b "   < <   endl   ;
else   if(a <b)
cout   < <   "a <b "   < <   endl;
else   cout   < <   "a==b "   < <     endl;
return   0;

}
错误有:
  error   C2051:   case   表达式不是常量
“std::string”类型的   switch   表达式是非法的
                没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符

[解决办法]
if else if

热点排行