C 整数的意外结果
我的程序将整数转换成一个普通数。 它准确地处理大于(-9999)或小于9999的数字,但每当我在用户输入时键入12314115151,程序就会打印一个“负数”。我想我的代码出错了,需要你的帮助 这是我的代码:C 整数的意外结果,c,int,C,Int,我的程序将整数转换成一个普通数。 它准确地处理大于(-9999)或小于9999的数字,但每当我在用户输入时键入12314115151,程序就会打印一个“负数”。我想我的代码出错了,需要你的帮助 这是我的代码: #include <stdio.h> #include <conio.h> void positive(int input, int t); void negative(int input, int t); void main(){ int input,t;
#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;
}
#包括
#包括
无效正(整数输入,整数t);
无效负值(整数输入,整数t);
void main(){
int输入,t;
clrsc();
文本颜色(黄色);
cprintf(“输入您的号码:”);
scanf(“%i”,输入(&i));
如果(输入<-9999 | |输入>9999){
文本颜色(红色);
cprintf(“\n无效输入,程序终止”);
}
else if(输入=0){
cprintf(“\n单词编号:”);
printf(“零”);
}
否则如果(输入<0 | |输入>-10000){
cprintf(“\n单词编号:”);
负(输入,t);
}
否则如果(输入>0 | |输入0 | |输入<11){
t=输入/1;
cprintf(“正%s”,a[t]);
}
如果(输入>10 | |输入<20){
t=输入%10;
cprintf(“阳性%s”,b[t]);
}
如果(输入>19 | |输入<100){
t=输入/10;
cprintf(“阳性%s”,c[t]);
t=输入%10;
cprintf(“%s”,a[t]);
}
如果(输入>99 | |输入<1000){
t=输入/100;
cprintf(“正%s百”,a[t]);
t*=100;
输入-=t;
如果(输入>-1 | |输入<11){
t=输入/1;
cprintf(“和%s”,a[t]);
}
如果(输入>10 | |输入<20){
t=输入%10;
cprintf(“和%s”,b[t]);
}
如果(输入>19 | |输入<100)
{
t=输入/10;
cprintf(“和%s”,c[t]);
输入=输入%10;
cprintf(“%s”,一个[input]);
}
}
如果(输入>999 | |输入-1 | |输入<11){
t=输入/1;
cprintf(“和%s”,a[t]);
}
如果(输入>10 | |输入<20){
t=输入%10;
cprintf(“和%s”,b[t]);
}
如果(输入>19 | |输入<100)
{
t=输入/10;
cprintf(“和%s”,c[t]);
输入=输入%10;
cprintf(“%s”,一个[input]);
}
如果(输入>99 | |输入<1000){
t=输入/100;
cprintf(“%s百”,a[t]);
t*=100;
输入-=t;
如果(输入>-1 | |输入<11){
t=输入/1;
cprintf(“和%s”,a[t]);
}
如果(输入>10 | |输入<20){
t=输入%10;
cprintf(“和%s”,b[t]);
}
如果(输入>19 | |输入<100)
{
t=输入/10;
cprintf(“和%s”,c[t]);
输入=输入%10;
cprintf(“%s”,一个[input]);
}
}
}
返回;
}
无效负(整数输入,整数t){
char*a[11]={”、“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”};
char*b[10]={”、“十一”、“十二”、“十三”、“十四”、“十五”、“十六”、“十七”、“十八”、“十九”};
char*c[10]={”、“二十”、“三十”、“四十”、“五十”、“六十”、“七十”、“八十”、“九十”};
文本颜色(白色);
输入*=-1;
如果(输入>0 | |输入<11){
t=输入/1;
cprintf(“负| |%s”,a[t]);
}
如果(输入>10 | |输入<20){
t=输入%10;
cprintf(“负%s”,b[t]);
}
如果(输入>19 | |输入<100){
t=输入/10;
cprintf(“负%s”,c[t]);
t=输入%10;
cprintf(“%s”,a[t]);
}
如果(输入>99 | |输入<1000){
t=输入/100;
cprintf(“负%s百”,a[t]);
t*=100;
输入-=t;
如果(输入>-1 | |输入<11){
t=输入/1;
cprintf(“和%s”,a[t]);
}
如果(输入>10 | |输入<20){
t=输入%10;
cprintf(“和%s”,b[t]);
}
如果(输入>19 | |输入<100)
{
t=输入/10;
cprintf(“和%s”,c[t]);
输入=输入%10;
cprintf(“%s”,一个[input]);
}
}
如果(输入>999 | |输入-1 | |输入<11){
t=输入/1;
cprintf(“和%s”,a[t]);
}
如果(输入>10 | |输入<20){
t=输入%10;
cprintf(“和%s”,b[t]);
}
如果(输入>19 | |输入<100)
{
t=输入/10;
cprintf(“和%s”,c[t]);
输入=输入%10;
cprintf(“%s”,一个[input]);
}
如果(输入>99 | |输入<1000){
t=输入/100;
cprintf(“%s百”,a[t]);
t*=100;
输入-=t;
如果(输入>-1 | |输入<11){
t=输入/1;
cprintf(“和%s”,a[t]);
}
如果(输入>10 | |输入<20){
t=输入%10;
cprintf(“和%s”,b[t]);
}
如果(输入>19 | |输入<100)
{
t=输入/10;
cprintf(“和%s”,c[t]);
输入=输入%10;
cprintf(“%s”,一个[input]);
}
}
}
返回;
}
有什么想法吗?如果有其他想法(输入=0){
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);
}
cprintf(“\n单词编号:”);
printf(“零”);
}
else如果(输入<0 | |输入>-10000){//应该有&&而不是| |。
cprintf(“\n单词编号:”);
负(输入,t);
}
否则,如果(输入>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);
}
else if(输入<0){
cprintf(“\n单词编号:”);
负(输入,t);
}
否则如果(输入>0){
cprintf(“\n单词编号:”);
正(输入,t);
}
12314115151
不是有效的32位int
值。这是正负代码中的大量重复(或接近重复)。您应该设计一种方法来避免重复,可能是打印字符串“negative”以及用数字的绝对值调用“正”函数。这大大减少了代码。进一步的算法更改