使用integrer的意外结果

时间:2017-10-21 07:23:59

标签: c int

我的程序确实在一个litteral数字中转换了一个int。 它准确地使用大于(-9999)的数字或小于9999的数字但是当我输入12314115151作为用户输入时,程序打印一个"否定的#34;。我想我的代码中出错并需要你的帮助..

这是我的代码:

#include <stdio.h>
#include <conio.h>

void positive(int input, int t);
void negative(int input, int t);

void main(){

int input,t;

clrscr();

textcolor(YELLOW);

cprintf("ENTER YOUR NUMBER: ");

scanf("%i",&input);

    if (input < -9999 || input > 9999){

        textcolor(RED);

        cprintf("\nINVALID ENTRY, PROGRAM TERMINATED");


    }      


    else if(input == 0){

      cprintf("\nNUMBER IN WORD(S): ");

      printf("Zero");

     } 

    else if(input < 0 || input >-10000){

      cprintf("\nNUMBER IN WORD(S): ");

      negative(input,t);
    }
    else if(input > 0 || input <10000){

      cprintf("\nNUMBER IN WORD(S): ");

      positive(input,t);
    }

getch();
}

void positive(int input, int t){

      char* a[11]={"","One","Two","Three","Four","Five","Six","Seven","Eeight","Nine","Ten"};


      char* b[10]={"","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};


      char* c[10]={"","","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};

      textcolor(WHITE);

      if(input > 0 || input < 11){

        t=input/1;
        cprintf("Positive %s",a[t]);
      }

      if(input > 10 || input < 20){

        t=input%10;

        cprintf("Positive %s",b[t]);
      }

      if (input > 19 || input < 100){
        t = input/10;

        cprintf("Positive %s",c[t]);

        t=input%10;

        cprintf(" %s",a[t]);
      }

      if (input > 99 || input < 1000){
        t =input/100;

        cprintf("Positive %s Hundred",a[t]);

        t*=100;
        input-=t;

          if(input > -1 || input < 11){

           t=input/1;
           cprintf(" and %s",a[t]);
          }

          if(input > 10  || input < 20){

            t=input%10;

            cprintf(" and %s",b[t]);
          }

          if (input > 19 || input < 100)
          {
            t=input/10;

            cprintf(" and %s",c[t]);

            input=input%10;

            cprintf(" %s",a[input]);
          }

      }
          if(input >999 || input <10000){
          t=input/1000;

            cprintf("Positive %s Thousand",a[t]);

          t*=1000;
          input-=t;

               if(input > -1 || input < 11){

                t=input/1;
                cprintf(" and %s",a[t]);

              }

              if(input > 10  || input < 20){
                t=input%10;
                cprintf(" and %s",b[t]);
              }

              if (input > 19 || input < 100)
              {
                t=input/10;
                cprintf(" and %s",c[t]);
                input=input%10;
                cprintf(" %s",a[input]);
              }
             if(input > 99 || input < 1000){
                t=input/100;
                  cprintf(" %s Hundred",a[t]);

                 t*=100;
                 input-=t;

                if(input > -1 || input < 11){

                  t=input/1;
                  cprintf(" and %s",a[t]);
                }

                if(input > 10  || input < 20){
                  t=input%10;
                    cprintf(" and %s",b[t]);
                }

                if (input > 19 || input < 100)
                {
                 t=input/10;
                     cprintf(" and %s",c[t]);
                   input=input%10;
                   cprintf(" %s",a[input]);
                  }

             }
          }

    return;
}



void negative(int input, int t){

  char* a[11]={"","One","Two","Three","Four","Five","Six","Seven","Eeight","Nine","Ten"};


  char* b[10]={"","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};


  char* c[10]={"","","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};

  textcolor(WHITE);

  input*=-1; 

  if(input > 0 || input < 11){

    t=input/1;
    cprintf("Negative|| %s",a[t]);
  }

  if(input > 10 || input < 20){

  t=input%10;

  cprintf("Negative %s",b[t]);
  }

  if (input > 19 || input < 100){

  t = input/10;

  cprintf("Negative %s",c[t]);

  t=input%10;

  cprintf(" %s",a[t]);
  }

  if (input > 99 || input < 1000){

  t =input/100;

  cprintf("Negative %s Hundred",a[t]);

  t*=100;
  input-=t;

    if(input > -1 || input < 11){
       t=input/1;
       cprintf(" and %s",a[t]);
    }

    if(input > 10  || input < 20){
      t=input%10;
      cprintf(" and %s",b[t]);
    }

    if (input > 19 || input < 100)
    {
      t=input/10;
      cprintf(" and %s",c[t]);
      input=input%10;
      cprintf(" %s",a[input]);
    }

  }

  if(input >999 || input <10000){
  t=input/1000;
    cprintf("Negative %s Thousand",a[t]);
  t*=1000;
  input-=t;

       if(input > -1 || input < 11){

         t=input/1;
         cprintf(" and %s",a[t]);
      }

      if(input > 10  || input < 20){
        t=input%10;
        cprintf(" and %s",b[t]);
      }

      if (input > 19 || input < 100)
      {
        t=input/10;
        cprintf(" and %s",c[t]);
        input=input%10;
        cprintf(" %s",a[input]);
      }
     if(input > 99 || input < 1000){
      t=input/100;

      cprintf(" %s Hundred",a[t]);

      t*=100;
      input-=t;

        if(input > -1 || input < 11){

          t=input/1;
          cprintf(" and %s",a[t]);
        }

        if(input > 10  || input < 20){
          t=input%10;
          cprintf(" and %s",b[t]);
        }

        if (input > 19 || input < 100)
        {
         t=input/10;
           cprintf(" and %s",c[t]);
           input=input%10;
           cprintf(" %s",a[input]);
          }

     }
  }

  return;
}

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

else if(input == 0){

      cprintf("\nNUMBER IN WORD(S): ");

      printf("Zero");

     } 

    else if(input < 0 || input >-10000){ // There should be && instead of ||.

      cprintf("\nNUMBER IN WORD(S): ");

      negative(input,t);
    }
    else if(input > 0 || input <10000){ //There too.

      cprintf("\nNUMBER IN WORD(S): ");

      positive(input,t);
    }

如您所见,我已评论您应编辑哪些代码部分。现在您正在寻找大于-10000 的数字 - 您的数字 IS 更高。 num&lt; 0并不意味着什么,因为它现在是可选条件。如下所述,

  

12314115151不是有效的32位int值

答案 1 :(得分:0)

如果你改变你的情况,你会得到理想的结果:

else if(input < 0){

      cprintf("\nNUMBER IN WORD(S): ");

      negative(input,t);
    }
    else if(input > 0){

      cprintf("\nNUMBER IN WORD(S): ");

      positive(input,t);
    }