c - 如何获取输入提示用户输入两个字符并使用它们进行交换?

标签 c sorting loops pointers

我正在 Ubuntu 中使用 GCC 在 ANSI C 中制作一个小型应用程序。该应用程序应该通过交换数组中的两个字母来解密消息。

我的主要类(class):

#include "CipherCode.h"

char cipherText[] = "PUCXHTULUL, XW AELW, JUAJ TXJ FOCTXWU EH XW OCCQLPOWCU RXAT ATU DUZ GXJA. RTUW ATU FUJJOBU XJ LUCUXIUP, TU JUAJ ATU DUZ RTUUGJ AQ ATU";

int main(void) {
    char ch = 0;
    char chLetter, chReplacement;

    char *pLetter = chLetter;
    char *pReplacement = chReplacement;

    int cipherStats[ALPHABET_SIZE] = { 0 };
    char chAlphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    int *pCipherStats = cipherStats; 
    char *pCipherText = cipherText;
    char *pAlphabet = chAlphabet;

    do {
        DisplayCipherText(pCipherText);
        GetFrequency(pCipherText, pCipherStats);
        DisplayCipherStats(pCipherStats, pAlphabet, 26);
        chLetter = GetLetter("Enter character to substitute:");
        chReplacement = GetLetter("Swap this with character:");
        Decipher(pCipherText, pReplacement);
        printf("%s", "\nPress 'n' to exit or any other key to continue...\n"); /* prompt to continue looping */
    } while ((ch = getchar()) != 'n'); /* loop unless user enters char 'n' */

    return EXIT_SUCCESS;
}

我的 CipherCode.c 文件:

#include "CipherCode.h"

char GetLetter(const char* prompt) {
    char ch = '\0';
    do {
        printf( "%s", prompt ); 
        fflush(stdout);
        ch = toupper(getchar());
        printf( "%c\n", ch );
    } while (!isalpha(ch));
    return ch;
}

int GetFrequency(const char *pCipherText, int *pCipherStats) {
    printf("Frequency analysis:\n");
    for (; *pCipherText != '\0'; pCipherText++) {
        if (isalpha(*pCipherText)) {
            int index = toupper(*pCipherText) - 'A';
            if ( index >= 0 && index < 26 ) {
                pCipherStats[index]++;
            }
            else {
                fprintf(stderr, "%c gives invalid index: %d\n", *pCipherText, index);
            }
        }
    }
    return EXIT_SUCCESS;
}

void DisplayCipherText(char *pCipherText) {
    for (; *pCipherText != '\0'; pCipherText++) {
        printf("%c", *pCipherText);
    }
    printf("\n\n");
}

void DisplayCipherStats(int *pCipherStats, char *pAlphabet, int size) {
    int i;
    for (i = 0; i < size; i++) { /*for each letter in the alphabet*/
        printf("%1c:%-4d", *pAlphabet++, *pCipherStats++); /*print frequency information*/
    }
    printf("\n\n");
}

void Decipher(char *pCipherText, char *pReplacement) {
    for (; *pCipherText != '\0'; pCipherText++) {
        if (*pCipherText == *pReplacement) {
            SortChar(pCipherText, pReplacement);
        }
    }
}

void BubbleSort(int *pInt, char *pCh) {
    int i, j;
    for (i = ALPHABET_SIZE-1; i >= 0; i--) {
        for (j = 0; j < i; j++) {
            if (*(pInt+j) < *(pInt+j+1)) {
                SortChar(pCh+j, pCh+j+1);
                SortInt(pInt+j, pInt+j+1);
            }
        }
    }
}

void SortInt(int *pIntA, int *pIntB) {
    int tempInt; /*temp variable*/
    tempInt = *pIntA; /*store old value before it is overwritten*/
    *pIntA = *pIntB; /*overwrite old value*/
    *pIntB = tempInt; /*complete the swap*/
}

void SortChar(char *pChA, char *pChB) {
    char tempCh; /*temp variable*/
    tempCh = *pChA; /*store old value before it is overwritten*/
    *pChA = *pChB; /*overwrite old value*/
    *pChB = tempCh; /*complete the swap*/
}

我的头文件:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define ALPHABET_SIZE 26

/*
* global variables
*/
extern char cipherText[];

/*
* function prototypes
*/
char GetLetter(const char*);
int GetFrequency(const char*, int*);
void DisplayCipherText(char*);
void DisplayCipherStats(int*, char*, int);
void Decipher(char*, char*);
void BubbleSort(int*, char*);
void SortInt(int*, int*);
void SortChar(char*, char*);

我希望程序采用 chLetter 并用 chReplacement 替换 cipherText 中所有出现的 chLetter。对于 do 循环的每次迭代,我想显示 cipherText,然后分析 cipherText 中字母的频率,然后显示频率,然后请求 chLetter,然后请求 chReplacement,最后将 chLetter 与 chReplacement 交换。

程序必须使用指针来引用数组的内容。我还必须使用排序函数来破译文本。

非常感谢任何帮助。谢谢。

最佳答案

char GetLetter(char chLetter) {
    char *pLetter = scanf("Enter character to substitute: %c", &chLetter);
    return *pLetter;
}

^ 指定 scanf 格式字符串的方式错误,该格式字符串用于 匹配来自缓冲区的输入,而不是显示提示。 scanf 返回数字 从缓冲区中找到的参数不是 char*

而是做这样的事情:

char GetLetter() 
{
  printf( "%s", "Enter character to substitute:"); 
  fflush(stdout);
  return getchar();
}

或者为了使其更灵活,请在参数中指定提示:

char GetLetter(const char* prompt) 
{
  printf( "%s", prompt ); 
  fflush(stdout);
  return getchar();
}

然后你可以调用它

char chReplace = GetLetter("Enter character to substitute:");
char chWith = GetLetter("Swap this with character:");

有了这个函数

int GetFrequency(char *pCipherText, int *pCipherStats) {
printf("Frequency analysis:\n");
for (; *pCipherText != '\0'; pCipherText++) { /*check if at the end of the array*/
    char ch = *pCipherText; /*store current letter as a char*/
    if (isalpha(ch)) pCipherStats[ch - 'A']++; /*if character is a letter, store ascii value in array and increment array*/
}

^ 您似乎假设所有输入字母都是大写,也许您应该首先将字符转换为大写来确保这一点。编程时,安全总比后悔好。通常,将参数用作临时变量来修改参数并不是一种好的风格,当函数更复杂时,它会使代码变得困惑。相反,使用本地变量,例如

int GetFrequency(const char *pCipherText, int *pCipherStats) 
{
  char* p = pCiperText;
  printf("Frequency analysis:\n");
  for (; *p != '\0'; ++p) 
  {
    if (isalpha(*p)) 
    {
      int index = toupper(*p) - 'A';
      if ( index >= 0 && index < 26 )
      {
        pCipherStats[index]++;
      }
      else
      {
        fprintf(stderr, "%c gives invalid index: %d\n", *p, index );
      }
    }
  }
  ...

也就是说,您可以使 GetLetter 函数更加强大:

char GetLetter(const char* prompt) 
{
  char ch = '\0';
  do
  {
    printf( "%s", prompt ); 
    fflush(stdout);
    ch = toupper(getchar());
    printf( "%c\n", ch );
  }
  while ( !isalpha(ch) );
  return ch;
}

关于c - 如何获取输入提示用户输入两个字符并使用它们进行交换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22182882/

相关文章:

C++,在数组中找到 3 个最大值,其中这些值之间的差异是参数之一

javascript - 遍历对象数组?

c - 为什么不允许非特权递归取消共享(CLONE_NEWUSER)?

c - 获取目录中的文件列表

c++ - 错误服务 irc mysql

algorithm - 高级/非常见的高效排序算法

c - 函数参数作为具有声明大小的数组

c - 将所有非空元素向左移动

postgresql - 在平局的情况下,对每列使用不同的标准对 postgresql 进行排序

php - 为什么我的变量不能作为 PHP 函数中的参数