小编Wol*_*964的帖子

使用 switch case 而不是 if else

我研究编译器设计在我的大学这个程序是用来识别每一个wordnumberoperationseparator在进入行用户,当你进入像int i = 0 ; (每个单词之间的空格是必要的), 它识别int为关键字,i如ID,=作为平等的,0因为数量和;作为输出中的分隔符,我的老师给了我一项任务,将本节中的“ if else”更改为switch

for(int j=0;j<=k;j++){
        if(token[j]=="int"|| token[j]=="string"||token[j]=="float")
        { 
        cout<<"keyword : "<<token[j]<<endl; 
        num_token++;
        }
        else if(token[j]=="*"||token[j]=="+"||token[j]=="-"||token[j]=="/")
        {
        cout<<"operation : "<<token[j]<<endl;
        num_token++;
        }
        else if(token[j]==","||token[j]==";")
        {
        cout<<"separator : "<<token[j]<<endl;
        num_token++;
        }
        else if(token[j]=="=")
        {
        cout<<"equal : "<<token[j]<<endl;
        num_token++;
        } 
        else if(token[j]>="0"&&token[j]<="9")
        {
        cout<<"Number : "<<token[j]<<endl;
        num_token++;
        }
        else if(token[j]>="a"&&token[j]<="z")
        {
        cout<<"ID : "<<token[j]<<endl; …
Run Code Online (Sandbox Code Playgroud)

c++ if-statement token switch-statement

2
推荐指数
1
解决办法
86
查看次数

标签 统计

c++ ×1

if-statement ×1

switch-statement ×1

token ×1