c++列挙と文字列の比較

1076 ワード

(1).文字列を読み出し,その文字を対応する列挙に変換する.
画面から「a」を入力すると、set列挙に対応するaに変換され、ソースコードは以下のようになります.
//     char2enum(str,temp);

#include  
using namespace std;
enum set {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
void char2enum(char ch , set &em)
{
     //        ,    em set     ,  ch       
     char start = 'a';
     em = (set)((int)ch - (int)start);
}
void main()
{    <pre name="code" class="cpp">   char str;
   set temp;
   cout >str; //            
   char2enum(str, temp); //    str     temp
   switch(temp) //       ,     ,,    !!!
    {
     case a:
     cout<<'a'<<endl;break;
     case b:
     cout<<'b'<<endl;break;
     case c:
     cout<<'c'<<endl;break;
     case d:
     cout<<'d'<<endl;break;
     case e:
     cout<<'e'<<endl;break;
     case f:
     cout<<'f'<<endl;break;
     // case g:...
     // case h:...
     }
}

テスト結果:please input a char:aリターンa//出力内容