Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 用文字打印数字的程序_C_Codeblocks - Fatal编程技术网

C 用文字打印数字的程序

C 用文字打印数字的程序,c,codeblocks,C,Codeblocks,我写了一个程序,用文字打印我在终端中输入的数字。123会返回一二三。当我试着运行程序时,在我输入号码后,它表示程序已经停止工作。我使用代码块。代码有什么问题吗?它正在编译,但返回了错误-1073741510 #include <stdio.h> int main (void) { long long int m = 0, n, digit; printf ("Whats your number? \n"); scanf ("%lli", &n);

我写了一个程序,用文字打印我在终端中输入的数字。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;
} 
#包括
内部主(空)
{
长整型m=0,n,数字;
printf(“您的号码是多少?\n”);
scanf(“%lli”&n);
if(n<0){
n=-n;
printf(“负片”);
}
如果(n=0)
printf(“零”);
否则{
而(n!=0){//这是为了反转数字
m=m*10+n%10;
n=n/10;
}
而(m!=0){
数字=m%10;
开关(数字){
案例0:
printf(“零”);
打破
案例1:
printf(“一”);
打破
案例2:
printf(“两个”);
打破
案例3:
printf(“三”);
打破
案例4:
printf(“四”);
打破
案例5:
printf(“五”);
打破
案例6:
printf(“六”);
打破
案例7:
printf(“七”);
打破
案例8:
printf(“八”);
打破
案例9:
printf(“九”);
打破
}
m=m/10;
}
}
返回0;
} 
这是错误的:

scanf ("%lli", n);
它需要:

scanf ("%lli", &n);
scanf的参数必须是一个变量的地址,才能将结果放入其中。

这是错误的:

scanf ("%lli", n);
它需要:

scanf ("%lli", &n);

scanf的参数必须是一个变量的地址,才能将结果放入其中。

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

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


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

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

线路

scanf ("%lli", n);
需要

scanf ("%lli", &n);
更好的是,检查函数的返回值以确保输入读取成功

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

scanf ("%lli", n);
需要

scanf ("%lli", &n);
更好的是,检查函数的返回值以确保输入读取成功

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

我想你必须根据数字而不是m来切换

     digit = m%10;
     switch (m){

     case 0:
           printf ("zero ");
           break;
一定是

     digit = m%10;
     switch (digit){

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

我想你必须根据数字而不是m来切换

     digit = m%10;
     switch (m){

     case 0:
           printf ("zero ");
           break;
一定是

     digit = m%10;
     switch (digit){

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

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

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

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");
}
const char number作为文本[]=“1234”;
常量字符*数字_名称[]=
{“零”、“一”、“二”、“三”、“四”,
“五”、“六”、“七”、“八”、“九”};
常量无符号整数长度=strlen(数字作为文本);
for(无符号整数i=0;i

这也应该更快,因为没有分区操作。大多数处理器不喜欢除法,除法会使它们变慢

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

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

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");
}
const char number作为文本[]=“1234”;
常量字符*数字_名称[]=
{“零”、“一”、“二”、“三”、“四”,
“五”、“六”、“七”、“八”、“九”};
常量无符号整数长度=strlen(数字作为文本);
for(无符号整数i=0;i

这也应该更快,因为没有分区操作。大多数处理器不喜欢除法,除法会使它们变慢

对于反向模式不适合
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
#包括
静态void int_text_helper(长负x){

如果(neg_x对于反向模式不适合
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
#包括
静态void int_text_helper(长负x){

如果(neg_x是本例中真正需要的空间吗?@NatashaDutta-不需要,但格式字符串中的空格与输入中的任意数量的空格匹配,包括无空格。在本例中,
\n
将位于输入之前。是的,但即使在基于digit@Rockstar5645-取决于你是怎么做的您的环境设置,我想知道“%lld”对你有用吗?错误消息是什么?@Ryker-我解决了“与”的问题,并将switch语句的表达式改为“数字”,但在终端中键入一个数字后,它只是将进程返回为0。甚至不再有错误消息。这种情况下真的需要空间吗?@NatashaDutta-没有必要,but格式字符串中的空白与输入中的任何空白量相匹配,包括无空白。在这种情况下,
\n
将位于输入之前。是的,但即使在基于digit@Rockstar5645-根据您的环境设置,我想知道“%lld”对你有用吗?错误信息是什么?@Ryker-我修好了符号