我在互联网上搜索了这种特定的方法,但没有找到我想要的东西。我写了这个程序,它以整数输入并打印信息(如手机的数字小键盘)。我想用这个程序做的是将输入作为一行输入
Enter the code to crack : 454545479833165445
并打印相应的消息。 而不是
Enter the code to crack :55
Enter the code to crack : 666
并在我按下特定键时打印信息,在这种情况下为-1。
#include <iostream>
using namespace std;
int main()
{
int a;
string n;
do{
cout << "Enter the code to crack";
cin >>a;
switch (a){
case 0:
{
n=n+" ";}
break;
case 1:
{
n=n+".";}
break;
case 11:
{
n=n+",";}
break;
case 2:{
n=n+"a";}
break;
case 22:
n=n+"b";
break;
case 222:
n=n+"c";
break;
case 3:
n=n+"d";
break;
case 33:
n=n+"e";
break;
case 333:
n=n+"f";
break;
case 4:
n=n+"g";
break;
case 44:
n=n+"h";
break;
case 444:
n=n+"i";
break;
case 5:
n=n+"j";
break;
case 55:
n=n+"k";
break;
case 555:
n=n+"l";
break;
case 6:
n=n+"m";
break;
case 66:
n=n+"n";
break;
case 666:
n=n+"o";
break;
case 7:
n=n+"p";
break;
case 77:
n=n+"q";
break;
case 777:
n=n+"r";
break;
case 7777:
n=n+"s";
break;
case 8:
n=n+"t";
break;
case 88:
n=n+"u";
break;
case 888:
n=n+"v";
break;
case 9:
n=n+"w";
break;
case 99:
n=n+"x";
break;
case 999:
n=n+"y";
break;
case 9999:
n=n+"z";
break;}
} while(a!=-1);
cout <<"The decoded message is :" << n;
return 0;
}
答案 0 :(得分:2)
如果你想处理一个signle输入,假设一条消息可能包含的多个数字肯定会溢出甚至是lognest整数类型,你必须使用一个字符串输入,并迭代它的字符:
string a;
string n;
cout << "Enter the code to crack";
cin >>a; // you'll get everything, but each digit is a char
for (int i=0; i<a.size(); i++) {
//... process each separate digit a[i] here
}
cout <<"The decoded message is :" << n;
每个数字a [i]然后在'0'和'9'之间。你必须要处理两个困难: