2014-08-31 33 views
-4

我需要用数字替换我的输入字母。
a = 00,b = 01,c = 02等等......
我觉得char enc出了点问题,当ch == 'j'或更高时,程序不起作用。使用C++替换带有数字的字母

#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; 
} 
+3

这不是C#,'00'或'01'不是单个字符。使enc成为int数组,并在printf中将句柄的左边加上零。 – brz 2014-08-31 18:56:14

+1

字符类型只能有一个字母 – 2014-08-31 19:02:28

+0

自从我上次看到'conio.h'已经很长时间了。你在哪里学习你的C/C++? – pqnet 2014-08-31 19:22:17

回答

1

这些不是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; 
} 
+0

我不明白为什么有人会低估这个答案,有些人真的不应该有一些自由,直到他们足够成熟。 – Amen 2014-08-31 19:56:16

1

根据C++标准

一个多字符的文字,或含有 单个c炭在执行字符集不能表示一个普通字符文字,是 有条件支持,类型为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; 
} 
+0

这很好,谢谢。有什么办法可以做到这一点与输入?也许是某种扫描? – hazmatsuit 2014-08-31 19:42:20

+0

@ user3446665在C++中,你可以使用std :: cin >> ch;在C中,您可以使用scanf(“%c”,&ch);。 – 2014-08-31 19:43:42

1

我会建议你使用一个std ::地图为您的要求。地图存储关键值对。在你的情况下,密钥将是单个字符,值将是需要替换的字符串。这里是一个例子

#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++的方法。希望这可以帮助。

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'等。
  1. 想要std::cout打印出字符的代码而不是字符。为此,您必须将字符转换为任何整数类型。
  2. 冲洗stdin
  3. stdin丢弃字符,直到满足输入