是不是C语言都很忌讳goto语句!~我叫人家帮我看下这个程序.人家一看有goto理都不理!~
我是初学者,能不能跟我讲下goto语句为什么那个另人讨厌!~我看这个程序似乎不是很乱嘛!~
#include<stdio.h>
#include<math.h>
#include<string.h>
#define N 100
char str[N];
unsigned long int n;
change(n)
{int a[N],i,t,m;
if(n!=0)
{for(i=0;i<N&&n!=0;i++)
{a[i]=n%2;
n=n/2;}
m=i;
for(i=0;i<(m+1)/2;i++)
{t=a[i];a[i]=a[m-1-i];a[m-1-i]=t;}
printf("BIN:");
for(i=0;i<m;i++)
{printf("%d",a[i]);}
printf("\n");}
else
printf("BIN:0\n");
}
int getnum(char c)
{
int j;
char str0[16]="0123456789ABCDEF";
for(j=0;j<16;j++)
{
if(str0[j]==c)
break;
}
return(j);
}
unsigned long any_to_dec(char _num[N],int radix)
{int i;
unsigned int y;
unsigned long dec=0;
y=strlen(_num);
y--;
for(i=0;_num[i]!=NULL;i++,y--)
{
dec=dec+(getnum(_num[i])*pow(radix,y));
}
return(dec);
}
main()
{
int radix;
char ch;
loop:printf("Please input the radix of the number(2,8,10,16):\n");
scanf("%d",&radix);
switch(radix)
{
case 2:loop2:printf("BIN:");
scanf("%s",str);
n=any_to_dec(str,radix);
if(n>4294967295)
{printf("Data error!");
goto loop2;}
printf("DEC:%ld\n",n);
printf("OCT:%lo\n",n);
printf("HEX:%lX\n",n);
printf("1.Press ESC to return to the main\n");
printf("2.Press any key to exit.\n");
ch=getch();
if(ch==27)
{exit(0);}
else
{goto loop;}
case 8:loop8:printf("OCT:");
scanf("%s",str);
n=any_to_dec(str,radix);
if(n>4294967295)
{printf("Data error!");
goto loop8;}
change(n);
printf("DEC:%ld\n",n);
printf("HEX:%lX\n",n);
printf("1.Press ESC to return to the main\n");
printf("2.Press any key to exit.\n");
ch=getch();
if(ch==27)
{exit(0);}
else
{goto loop;};
case 10:loop10:printf("DEC:");
scanf("%s",str);
n=any_to_dec(str,radix);
if(n>4294967295)
{printf("Data error!");
goto loop10;}
change(n);
printf("OCT:%lo\n",n);
printf("HEX:%lX\n",n);
printf("1.Press ESC to return to the main\n");
printf("2.Press any key to exit.\n");
ch=getch();
if(ch==27)
{goto loop;};
case 16:loop16:printf("HEX:");
scanf("%s",str);
n=any_to_dec(str,radix);
if(n>4294967295)
{printf("Data error!");
goto loop16;}
change(n);
printf("OCT:%lo\n",n);
printf("DEX:%ld\n",n);
printf("1.Press ESC to exit.\n");
printf("2.Press any key to return to the main.\n");
ch=getch();
if(ch==27)
{exit(0);}
else
{goto loop;};
default:printf("Data error!\n");goto loop;
}
}