2016-12-04 75 views
1

我是C新手,一般編碼,請耐心等待。如何用c中的用戶的char值填充數組?

我試圖創建一個「鍵盤測試」程序,其中用戶必須通過在每個元件輸入1個字符的值填充的陣列。然後數組的內容將被排序,printf將顯示哪個按鍵被按下最多(根據每個字符使用了多少個按鍵)。我已經研究了兩天無果(大多數解決方案都是針對C++的,而不是C)。我在Visual Studios工作。以下是我迄今爲止:

#define _CRT_SECURE_NO_WARNINGS 
#define PAUSE system("pause") 
#define CLS system ("cls") 
#define FLUSH flush() 
#define MAXELEMENTS 25 
#include <stdio.h> 
#include <stdlib.h> 

// prototyped functions 
int pressKeys(char keys); 

main() { 
    char keys[MAXELEMENTS]; 
    int x; 

    pressKeys(&keys[MAXELEMENTS]); 
    PAUSE; 
} 

// functions 

int pressKeys(char keys) { 
    printf("Enter characters: \n"); 
    scanf("%c", &keys); FLUSH; 

    printf("You typed: %c\n", keys); 

    return(0); 
} 

這個程序的輸出似乎是第一個字符我輸入了,所以它的部分工作,但它只能說明是什麼鍵[0]。

如何獲得它來存儲所有類型的?
如何獲得printf輸出完整的數組?
然後,我會如何將數組傳遞給將對元素中的內容進行排序的函數?

(至於論點主,函數聲明去)。

+0

出於好奇,你有什麼不利用C++來做這件事嗎?你在學習特定的C語言程序課程,還是在學習續集之前試圖學習原版? – Goodies

+0

順便說一句'&keys [MAXELEMENTS]'是最後一個元素的地址。 (並且它不使用:-) – BLUEPIXY

+0

用戶是否在其他鍵之間鍵入了輸入鍵,在輸入結束還是根本不輸入? – chux

回答

0
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int getIndex(char data); 
void sortData(char* data); 
void flushConsole(void); 

#define MAX_ELEMENTS 25 

// This table is used for looking up an index 
char charLookupTable[] = "abcdefghijklmnopqrstuvwxyz";  

int main(void) 
{ 
    // Create a buffer to hold the user input & clear it out before using 
    char buffer[MAX_ELEMENTS]; 
    memset(buffer, 0, sizeof(char) * MAX_ELEMENTS); 

    // Create a table to hold the number of times each key has been pressed 
    // this table will be 1 less than the size of character lookup table since 
    // it doesn't have a NULL at the end 
    int countPressArray[sizeof(charLookupTable) - 1]; 
    memset(countPressArray, 0, sizeof(countPressArray)); 

    // Get the user data and read it into a buffer 
    printf("Enter characters: "); 
    scanf("%s", buffer); 

    // Sort the user input data 
    sortData(buffer); 

    // Now that the data is sorted let's see how many times a specific character was used 
    int index = 0; 
    while(buffer[index] != NULL) 
    { 
    // Get the index of the counter to increment 
    int countPressIndex = getIndex(buffer[index]); 

    // Now increment the count in the table for each key press 
    countPressArray[countPressIndex]++; 

    // Check the next character 
    index++; 
    } 

    // Clear the console buffer 
    flushConsole(); 

    // Print the sorted data 
    printf("Your sorted data is: %s\n", buffer); 

    // Hold the console window open 
    printf("\nPress any key to continue..."); 
    getchar(); 

    // Exit the program 
    return 0; 
} 


// This function will sort the data in the array using 
// a simple bubble sort algorithm. 
void sortData(char* data) 
{ 
    for (int i = 0; ; i++) 
    { 
    if (data[i] == NULL) 
    { 
     // Everything is now in order 
     break; 
    } 

    for (int j = i + 1; ; j++) 
    { 
     if (data[j] == NULL) 
     { 
     // All values have been compared look at the next value 
     break; 
     } 

     if (data[i] > data[j]) 
     { 
     // The values need to be swapped use a temporary value as a third hand 
     char temp = data[i]; 
     data[i] = data[j]; 
     data[j] = temp; 
     } 
    } 
    } 
} 

// This function flushes the console 
void flushConsole(void) 
{ 
    while (getchar() != '\n'); 
} 

// This function will return the index of the character that was pressed 
int getIndex(char data) 
{ 
    for (int i = 0; i < sizeof(charLookupTable)/sizeof(char); i++) 
    { 
    if (data == charLookupTable[i]) 
    { 
     // We have found a match return the index of the character 
     return i; 
    } 
    } 

    // Couldn't find this character in the table return an error 
    return 0xFF; 
} 

我已經包括了問題的答案,你應該能夠與提供什麼詳情見下面運行:

  • 如何獲得它來存儲所有類型的?

    1. 首先,你需要創建一個字符緩衝區char buffer[MAX_ELEMENTS];

    2. 接下來,您需要清除此緩衝區,因爲它位於堆棧上並已用垃圾進行了初始化。您可以使用memset(buffer, 0, sizeof(char) * MAX_ELEMENTS);來完成此操作。

    3. 最後,您需要使用字符串formatter和scanf將用戶數據讀入此緩衝區。

      scanf("%s", buffer);

  • 如何獲得printf的輸出完整的數組?

    所有你需要做的是用printf與字符串格式化你的緩衝區如下:

    printf("Your sorted data is: %s\n", buffer);

  • 我怎麼會那麼去數組傳遞給函數這將排序元素中的內容?

    1. char指針在參數列表中創建一個排序函數:

      void sortData(char* data);

    2. 接下來,通過數據緩衝到分類功能如下:

      sortData(buffer);

現在您只需要對同一個字符的多個實例進行計數!

快樂編程!


我很高興我可以幫忙!我已經通過計算角色被按下的次數更新了我的帖子。我也提供了你的後續問題的答案,我將其解釋爲使問題更清楚。

  • 什麼是計數按鍵的最佳途徑?

    有很多方法可以完成,但最直觀的方法是我需要某種查找表,可以爲您提供索引。

    char charLookupTable[] = "abcdefghijklmnopqrstuvwxyz";

    請記住此表僅供如果你想數字或大寫,你可以分別將它們添加到開始和結束小寫字符。現在,創建一個函數,它會給你一個這個字符串的索引。

    int getIndex(char data);

    最後,只需創建另一個整數表來跟蹤計數。

    int countPressArray[sizeof(charLookupTable) - 1];

    只需使用索引遞增正確的計數值(即指數:0 = 'A',索引:1 = 'B',指數:25 = 'Z',等...)。

    countPressArray[countPressIndex]++;

  • 我應該爲這個獨立的功能?

    是的,無論何時您編寫執行特定任務的代碼,都最好將其包裝在一個函數中。這使得代碼更易讀,更易於調試。

  • 如何在不重複的情況下打印陣列中的每個元素?

    我沒有在上面的代碼示例中提供這個,但它很容易實現。現在我們知道每個按鍵被按下多少次,這只是在打印之前檢查並確保它不大於1的問題。

+0

你是偉大的幫助,布蘭登!我試圖弄清楚如何計算代碼中的迭代次數,但似乎無法弄清楚。最近我得到了(我用for循環),「NULL發生少於」#「次」,所以嘗試搜索數組時出錯。對我來說,最簡單的方法是什麼?這一次,我需要找出如何打印數組中的每個元素,但沒有重複。順便說一句,我應該爲此做一個單獨的功能嗎? –

+0

我很高興能幫上忙!我更新了我的帖子,以解決您的後續問題!最好的祝福! – Brandon83

+0

再次感謝您的幫助,布蘭登! 如果沒關係,你會幫我理解一下代碼好一點嗎? (爲什麼你鍵入你輸入的內容) 例如,在這一行:memset(buffer,0,sizeof(char)* MAX_ELEMENTS); memset是指爲其角色留出的內存,對吧? 0表示什麼?另外,NULL的作用是什麼? –

0

您應該使用fgets()得到一整行。或者,如果你必須使用scanf(),注意%s意味着一個字符串,而不是%c這意味着一個字符。