[英]How do I populate an array with char values from the user in c?
我是C的新手和編碼,所以請耐心等待。
我正在嘗試創建一個“鍵盤測試”程序,用戶必須通過在每個元素中輸入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輸出完整的數組?
那么我如何將數組傳遞給將對元素中的內容進行排序的函數呢?
(就主要和函數聲明中的參數而言)。
你應該使用fgets()
來獲得整行。 或者如果必須使用scanf()
,請注意%s
表示字符串而不是%c
表示一個字符。
#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;
}
我已經包含了您的問題的答案,您應該可以使用提供的內容來查看詳細信息:
如何存儲所有鍵入的內容?
char buffer[MAX_ELEMENTS];
。 memset(buffer, 0, sizeof(char) * MAX_ELEMENTS);
。 最后,您需要使用字符串formatter和scanf將用戶數據讀入此緩沖區。
scanf("%s", buffer);
。
如何讓printf輸出完整的數組?
您需要做的就是將printf與字符串格式化程序和緩沖區一起使用,如下所示:
printf("Your sorted data is: %s\\n", buffer);
那么我如何將數組傳遞給將對元素中的內容進行排序的函數呢?
使用參數列表中的char
指針創建排序函數:
void sortData(char* data);
接下來,將數據緩沖區傳遞給sort函數,如下所示:
sortData(buffer);
你現在需要的只是為同一個角色的多個實例實現計數!
快樂編程!!
我很高興能幫忙! 我已經更新了我的帖子,計算了一個角色被按下的次數。 我還提供了你的后續問題的答案,我將其解釋為更加清晰。
計算按鍵的最佳方法是什么?
有很多方法可以做到,但對我來說最直觀的方法是你需要某種可以為你提供索引的查找表。
char charLookupTable[] = "abcdefghijklmnopqrstuvwxyz";
請記住,此表僅適用於小寫字符,如果您想要數字或大寫,則可以分別將它們添加到開頭和結尾。 現在,創建一個函數,為您提供此字符串的索引。
int getIndex(char data);
最后,只需創建另一個整數表來跟蹤計數。
int countPressArray[sizeof(charLookupTable) - 1];
只需使用索引來增加正確的計數值(即索引:0 ='a',索引:1 ='b',...,索引:25 ='z'等等...)。
countPressArray[countPressIndex]++;
我應該為此單獨制作一個功能嗎?
是的,無論何時編寫執行特定任務的代碼,最好將其包裝在函數中。 這使代碼更易讀,更容易調試。
如何在沒有重復的情況下打印數組中的每個元素?
我沒有在上面的代碼示例中提供這個,但它很容易實現。 現在我們知道按下每個鍵的次數,只需要檢查以確保在打印之前它不大於1。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.