2014-08-31 30 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丟棄字符,直到滿足輸入