2016-12-04 74 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这意味着一个字符。