簡體   English   中英

如何輸入小於 100 的整數作為數字字符數組,output 將其單詞形式的數字作為 C 中的字符串輸入?

[英]How can I input a whole number less than 100 as a character array of digits and output the number in its word form as a string in C?

我想創建一個 C 程序,要求用戶輸入一個小於 100 的數字,然后輸入 output 它的單詞形式。 例如,輸入可能是“53”,我希望 output 是“53”。 如果輸入的數字等於或大於 100(字符串長度 >= 3),我希望 output 成為錯誤消息。 示例:輸入為“132”,output 為“錯誤:數字大於或等於 100”。 我使用了 geeksforgeeks ( https://www.geeksforgeeks.org/convert-number-to-words/ ) 文章作為指南,因為本教程說明的數字小於 10000,但我只想說明數字小於超過 100。到目前為止,這是我的代碼:

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

void convertnumtostring(char *num){
    int len = strlen(num);
    if(len==0){
        fprintf(stderr, "empty string\n");
        return;
    }
    if(len>2){
        fprintf(stderr, "number is is equal to or greater than 100\n");
        return;
    }

    char *singledig[]={"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    char *doubledig[]={"", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
    char *multiplesoften[]={"", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
        
    printf("\n%s: ", num);
    if(len==1){
        printf("%s\n", singledig[*num - '0']);
        return;
    }
        
    while(*num != '0'){
        if(*num == '1'){
            int sum = *num - '0' + *(num + 1)- '0';
            printf("%s\n", doubledig[sum]);
            return;
        } else if(*num == '2' && *(num + 1) == '0'){
            printf("twenty\n");
            return;
        } else {
            int i = *num - '0';
            printf("%s ", i? multiplesoften[i]: "");
            ++num;
            if(*num != '0')
                printf("%s ", singledig[*num - '0']);
        }
        ++num;
    }
}

int main(void){
    char input[10];
    printf("Enter a number: ");
    scanf("%s", input);
    convertnumtostring(input);
    return 0;   

}

對於大於 99 的數字/大於 3 的字符串長度(100、1313、13235 等),該代碼可以正常工作,因為它們會收到我想要的錯誤消息,並且對於數字 0-20,輸出正確的單詞形式(零、四、二十等),但對於所有其他數字 21-99,我收到一條消息說

Segmentation fault (core dumped)

任何幫助,將不勝感激。 我想知道分段錯誤是什么以及它發生在哪里/為什么以及如何解決它。

我不知道那是不是一個作業,反正你不應該管理你看不懂的代碼。 但我會試着理解你。

首先,您會發現分段錯誤問題通常與 arrays 相關。 It is caused by an illegal attemp to access some memory location, in this case the program tries to read a memory position beyond the some of the arrays.

現在,為什么它會發生在你的程序中? 讓我們檢查並評論它的沖突部分:

while (*num != '0')
{
    if (*num == '1')
    {
        int sum = *num - '0' + *(num + 1) - '0';
        printf("%s\n", doubledig[sum]);
        return;
    } else if (*num == '2' && *(num + 1) == '0')
    {
        printf("twenty\n");
        return;
    } else
    {
        int i = *num - '0';                         //index ascii offset
        printf("%s ", i ? multiplesoften[i] : "");  //again compares if i is not 0
        ++num;                                      //next digit
        if (*num != '0')                            //is not equal to zero
            printf("%s ", singledig[*num - '0']);   //get by index ascii offset
    }
    
    ++num;//next digit again??? change ++num to break !!!

}

如您所見,一旦打印了數字,它就會繼續循環。 這樣,新數字將是垃圾數據,該數據將用於從 arrays 中檢索字符串,並將其用作索引,這就是導致段錯誤的原因。

我希望我的解釋可以幫助您繼續您的 C 學習。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM