【问题标题】:Program to print the digits of numbers, in words以文字形式打印数字的数字的程序
【发布时间】:2015-05-27 18:32:55
【问题描述】:

我编写了一个程序,以文字形式打印我在终端中输入的数字的数字。例如,123 将返回一二三。当我尝试运行该程序时,输入我的号码后,它说该程序已停止运行。我使用代码块。代码有什么问题吗?它正在编译,但它返回错误 -1073741510。

#include <stdio.h>

int main (void)
{
   long long int m = 0, n, digit;

   printf ("Whats your number? \n");
   scanf ("%lli", &n);

   if (n < 0){
      n = -n;
      printf ("negative ");
   }

   if (n = 0)
      printf ("zero ");

   else {
      while (n != 0){                     //this is to reverse the number
         m = m*10 + n%10;
         n = n/10;
      }

      while (m != 0){
         digit = m%10;
         switch (digit){

            case 0:
               printf ("zero ");
               break;
            case 1:
               printf ("one ");
               break;
            case 2:
               printf ("two ");
               break;
            case 3:
               printf ("three ");
               break;
            case 4:
               printf ("four ");
               break;
            case 5:
               printf ("five ");
               break;
            case 6:
               printf ("six ");
               break;
            case 7:
               printf ("seven ");
               break;
            case 8:
               printf ("eight ");
               break;
            case 9:
               printf ("nine ");
               break;
         }
         m = m / 10;
      }
   }
   return 0;
} 

【问题讨论】:

    标签: c codeblocks


    【解决方案1】:

    这是错误的:

    scanf ("%lli", n);
    

    必须是:

    scanf ("%lli", &n);
    

    scanf 的参数需要是一个变量的地址来放入结果。

    【讨论】:

      【解决方案2】:

      线

      scanf ("%lli", n);
      

      需要

      scanf ("%lli", &n);
      

      更好的是,检查函数的返回值以确保读取输入成功。

      if ( scanf("%lli", &n) != 1 )
      {
         // Error in reading the input.
         // Deal with the error
      }
      

      【讨论】:

      • 我不太明白最后一部分。
      • 如果用户输入的不是数字,比如l234 而不是1234,那么您将能够捕捉到错误并进行处理。
      • @RSahu - 只是一个观察:您评论中数字的 text 版本与数字版本几乎没有区别。尝试在数字周围使用双引号。 “1234”
      • @ryyker,这是故意的 :) 举一个人类很容易犯这种错误的例子。
      【解决方案3】:

      我认为你必须根据数字而不是 m 进行切换

           digit = m%10;
           switch (m){
      
           case 0:
                 printf ("zero ");
                 break;
      

      必须是

           digit = m%10;
           switch (digit){
      
           case 0:
                 printf ("zero ");
                 break;
      

      【讨论】:

        【解决方案4】:

        您应该将输入视为字符而不是数字。

        您也可以使用数组作为数字文本:

        const char number_as_text[] = "1234";
        const char * digit_names[] = 
        { "zero", "one", "two", "three", "four",
          "five", "six", "seven", "eight", "nine"};
        
        const unsigned int length = strlen(number_as_text);
        
        for (unsigned int i = 0; i < length; ++i)
        {
          unsigned int digit_value = number_as_text[i] - '0';
          puts(digit_names[i]);
          puts("\n");
        }
        

        这也应该更快,因为没有除法运算。大多数处理器不喜欢除法,除法会减慢它们的速度。

        【讨论】:

          【解决方案5】:

          反转模式不适合long long 的大数将失败。请改用递归。

          下面评论了各种改进。

          #include <stdio.h>
          
          static void int_text_helper(long long neg_x) {
            if (neg_x <= -10) {
              int_text_helper(neg_x / 10);
              fputc(' ', stdout);
            }
            int digit = -(neg_x % 10);
            static const char *text[] = { "zero", "one", "two", "three", "four", "five",
                "six", "seven", "eight", "nine" };
            fputs(text[digit], stdout);
          }
          
          int main(void) {
            long long int n; // m = 0, n, digit;
            // printf("Whats your number? \n");  Typo
            printf("What's your number? \n");
            // Note: this will read numbers like 0123 and an octal number.
            scanf("%lli", &n);
          
            // Let us work with negative numbers instead so code can handle LLONG_MIN.
            if (n < 0) {
              fputs("negative ", stdout);
            } else {
              n = -n;
            }
          
            // Use do loop (or recursion), so no special case with 0
            // if (n = 0) printf("zero ");
          
            // Let us use recursion rather than reversing the number.
            // Reverse fails for a number like 9223372036854775799 (Near LLONG_MAX)
            int_text_helper(n);
          
            return 0;
          }
          
          -9223372036854775808
          negative nine two two three three seven two zero three six eight five four seven seven five eight zero eight
          

          【讨论】:

            【解决方案6】:

            对于初学者,您可以更改此行:

            scanf ("%lli", n);  //passed variable
            

            到这里:

            scanf ("%lli", &n); //  
            //      ^       ^   // Always: When you need to change the value of an 
                                 // argument, you need to pass the address 
                                 // of the value, not the value itself. 
            

            编辑(在 cmets 中回答问题)
            您正在 n 的值更改为 0,然后才能对其进行正确评估。你想比较它。进行以下编辑后,输入似乎被正确处理...
            换行

            if (n = 0) //ASSIGNS value of 0 to value of n
            

            if (n == 0) //COMPARES value of 0 to value of n
            

            【讨论】:

            • 这种情况下真的需要空间吗?
            • @NatashaDutta - 不是必需,但格式字符串中的空格与输入中的任意数量的空格匹配,包括无空格。在这种情况下,\n 将在输入之前。
            • 是的,但它仍然无法正常工作,即使我根据数字切换了
            • @Rockstar5645 - 根据您设置环境的方式,我想知道“%lld”是否适合您?错误信息是什么?
            • @ryyker - 我修复了 & 号问题,并将 switch 语句的表达式更改为数字,但是在我的终端中输入一个数字后,它只是将进程返回为 0。没有甚至不再是错误消息了。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-27
            • 1970-01-01
            • 2019-01-20
            • 1970-01-01
            • 2011-10-03
            • 1970-01-01
            相关资源
            最近更新 更多