我需要用数字替换输入字母
a = 00
,b = 01
,c = 02
等等......
我认为char enc有问题,程序在ch == 'j'
或更高版本时不起作用。
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
char ch = 'g'; // this should be replaced with some kind of an input function
char alp[26] = {'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'};
char enc[26] = {'00', '01', '02', '03', '04', '05', '06', '07', '08', '09',
'10', '11', '12', '13', '14', '15', '16', '17', '18', '19',
'20', '21', '22', '23', '24', '25'};
for(int i = 0; i <= 25; i++)
if(ch == alp[i]){
printf("%c", enc[i]);
break;
}
while(getchar()!='\n');
return 0;
}
答案 0 :(得分:2)
这些不是C ++字符:'00','01',...因为它们实际上包含两个字符。 试着用这个:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
using namespace std;
int main()
{
char ch = 'g'; // this should be replaced with some kind of an input function
char alp[26] = {'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'};
string enc[26] = {"00","01","02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"};
for(int i = 0; i <= 25; i++){
if(ch == alp[i]){
cout<<enc[i];
}
}
system("PAUSE");
return 0;
}
答案 1 :(得分:2)
根据C ++标准
多字符文字或包含的普通字符文字 在执行字符集中无法表示的单个c-char是 有条件地支持,类型为int,并且有一个 实现定义的值。
因此,如果不是第二个chatracter数组,你会定义一个指向字符串文字的指针数组会更好。考虑到定义std :: string类型的对象数组没有任何意义。
当程序看起来像用C语言编写时,还不清楚为什么要说C ++。
C ++中的代码可以采用以下方式
#include <iostream>
#include <cstdlib>
int main()
{
size_t N = 26;
char ch = 'g'; // this should be replaced with some kind of an input function
const char alp[N] =
{
'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'
};
const char *enc[N] =
{
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"
};
size_t i = 0;
while ( i < N && ch != alp[i] ) i++;
if ( i != N ) std::cout << enc[i] << std::endl;
std::system( "PAUSE" );
return 0;
}
用C编写的相同代码可能看起来像
#include <stdio.h>
#include <stdlib.h>
#define N 26
int main( void )
{
char ch = 'g'; // this should be replaced with some kind of an input function
const char alp[N] =
{
'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'
};
const char *enc[N] =
{
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"
};
size_t i = 0;
while ( i < N && ch != alp[i] ) i++;
if ( i != N ) printf( "%s\n", enc[i] );
system( "PAUSE" );
return 0;
}
答案 2 :(得分:1)
我建议您根据需要使用std :: map。地图存储键值对。在您的情况下,键将是单个字符,值将是需要替换的字符串。这是一个例子
#include <iostream>
#include <map>
using namespace std;
int main ()
{
char ch = 'b'; // this should be replaced with some kind of an input function
char alp[26] = {'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'};
string enc[26] = {"00","01","02" /*and the rest*/};
// declare a map
map<char, string> valueDecoderMap;
// store the values in key,value form
for ( int i = 0; i < (sizeof(alp)); i++ )
{
valueDecoderMap [ alp [ i ] ] = enc [ i ];
}
// Now search for particular value
map<char, string>::iterator mapIterator;
mapIterator = valueDecoderMap.find ( ch );
if ( mapIterator != valueDecoderMap.end () )
{
cout << "Key = " << mapIterator->first << " Value = " << mapIterator->second << endl;
}
else
{
cout << "No encoding present for " << ch << endl;
}
return 0;
}
这是一种更加面向c ++的方法。希望这会有所帮助。
答案 3 :(得分:1)
我建议你完全重写你的代码:
#include<iostream>
#include<limits>
int main(){
char input=std::cin.get()-'a'; //1
if(input<9)
std::cout<<0;
std::cout<<int(input)<<"\n\nPress Enter to exit program. "; //2
std::cin.sync(); //3
std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');//4
}
说明:
std::cin.get()
返回从 stdin
中提取的第一个字符。char
是一个数字。 Here您可以检查每个角色的值。'a'-'a'
时,'\0'
获得0
(代码为'b'-'a'
的字符)'\1'
,您获得std::cout
等。stdin
打印出字符的代码,而不是字符。为此,您必须将字符强制转换为任何整数类型。stdin
。