使用 switch case 而不是 if else

Wol*_*964 2 c++ if-statement token switch-statement

我研究编译器设计在我的大学这个程序是用来识别每一个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;
        num_token++;
        }
}
Run Code Online (Sandbox Code Playgroud)

所以我尝试了大约一周的时间来解决这个问题,但无济于事。我被困在这里有人可以帮助我吗,谢谢。

这是其余的代码

#include<iostream>
using namespace std;
int main(){
    int num;
    string st,st1;
    string token[30];
    int k=0;
    int num_token=0;
    cout<<"Enter How Many Lines : "<<endl;
    cin>>num;
    cout<<"Please Enter The Line You Want To Process : "<<endl;
    for(int ii=0;ii<=num;ii++){
        getline(cin,st);
        for(int i=0;st[i]!='\0';i++){
            if(st[i]!= ' ')
            st1+=st[i];
            else{
                token[k]=st1;
                k++;
                st1="";
            }
        }
        token[k]=st1;
        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;
            num_token++;
            }
    }

token[30]="";
    k=0;
    if(ii<num && ii>0){
        cout<<" "<<endl;
        cout<<"Please Enter The Line Again"<<endl;
    }

}
 cout<<"total Number of Tokens is : "<<num_token;
 return 0; }```
Run Code Online (Sandbox Code Playgroud)

小智 5

由于switch 仅适用于整数类型,您可能希望使用std::map将字符串链接到整数类型。

最小工作示例:

#include <iostream>
#include <map>
#include <string>
int main()
{
    int num_token=0;
    std::string token[4]{"int","="};
    std::map<std::string,int> m{{"int",0},{"string",0},{"float",0},{"=",1}};
    for(int j=0;j<2;j++){
        switch(m[token[j]]){
            case(0):
                std::cout<<"keyword : "<<token[j]<<std::endl; 
                num_token++;
                break;
            case(1):
                std::cout<<"eq : "<<token[j]<<std::endl; 
                num_token++;
                break;
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)