#include "math.h" main() { char b[20],a[20],c[20]; int i,k,t,m,n,f=0,emark=0; int b_to_t(char array[]); printf("Input the branry:"); scanf("%s",b); for(i=0; b[i]!='\0';i++) { if(b[i]!='1'&&b[i]!='0'&&b[i]!='.') { printf("Invalid string"); break; } } for(i=0; i<20; i++) if(b[i]=='.') emark=1; if(emark==1){ for(k=0;b[k]!='.';k++) *(a+k)=*(b+k); *(a+k)='\0'; for(k=k+1; b[k]!='\0'; k++) { *(c+f)=*(b+k); f++; } *(c+f)='\0'; } else { for(k=0; b[k]!='\0'; k++) *(a+k)=*(b+k); *(a+k)='\0'; }
printf("What do you want?\n"); printf("1:Ten.\n"); printf("2:Octal.\n"); printf("3:Hexacdecimal.\n"); printf("0:Exit.\n"); x1: scanf("%d",&t); if(t>=0||t<=3) { if(t==1) { if(emark==1) { m=b_to_t(a); n=b_to_t(c); printf("%d.%d",m,n); } else { m=b_to_t(a);printf("%d",m);} } if(t==2) { if(emark==1) { m=b_to_t(a); n=b_to_t(c); printf("%o.%o",m,n); } else { m=b_to_t(a);printf("%o",m);} } if(t==3) { if(emark==1) { m=b_to_t(a); n=b_to_t(c); printf("%x.%x",m,n); } else { m=b_to_t(a);printf("%x",m);} } if(t==0) exit(); } else { printf("Your choice is wrong,please choice again!"); goto x1; } getch(); } int b_to_t(char array[]) { int num,s,i=0; if(array[0]=='-'||array[0]=='+') { s=array[0]=='-'?-1:1; i++;} for(num=0; array[i]!=0; i++) num=num*2+array[i]-'0'; num=s*num; return nu; }
我想做一个二进制与其他进制转化的程序,可不知道那里错了,谁能帮我该一下?谢谢了