程序以字数打印数字的数字

时间:2015-05-27 18:32:56

标签: c codeblocks

我写了一个程序,打印出我在终端输入的数字的数字。例如,123将返回一两三。当我尝试运行程序时,在输入我的号码之后,它说该程序已停止工作。我使用Codeblocks。代码有什么问题吗?它正在编译,但它返回错误-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;
} 

6 个答案:

答案 0 :(得分:3)

这是错误的:

scanf ("%lli", n);

需要:

scanf ("%lli", &n);

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

答案 1 :(得分:1)

该行

scanf ("%lli", n);

需要

scanf ("%lli", &n);

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

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

答案 2 :(得分:1)

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

     digit = m%10;
     switch (m){

     case 0:
           printf ("zero ");
           break;

必须是

     digit = m%10;
     switch (digit){

     case 0:
           printf ("zero ");
           break;

答案 3 :(得分:0)

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

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. 

编辑 (回答评论中的问题)
在正确评估之前,您 n 的值更改为0。你想比较它。完成以下编辑后,输入似乎正确处理...
更改行

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

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

答案 4 :(得分:0)

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

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

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 :(得分:0)

对于反转模式不适合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
相关问题