使用方法./change 23456 10, 按最后指定的进制显示
#include<stdio.h>
#include<stdlib.h>#include<unistd.h>
int mystrlen(char *s)
{
int len=0;
char *temp=s;
while(*(temp)!=' ')
{
++len;
++temp;
}
return len;
}
int str2int(char *s)
{
int sum=0,len,i;
len=mystrlen(s);
for(i=0;i<len;i++)
{
if((*(s+i)<'0') | (*(s+i)>'9'))
{
printf("Warning: can't convert it into int ");
exit(1);
}
int k=len-i;
int pow=1;
while(k>1)
{
pow*=10;
--k;
}
sum = sum + (*(s+i)-'0')*pow;
}
return sum;
}
int main(int argc, char *argv[])
{
int result,sw;
if(argc!=3)
{
printf("Usage: argv[0] number 8 | 10 | 16 ");
exit(1);
}
char *s=argv[1];
char *s2=argv[2];
// printf("%d,%d ",mystrlen(s),str2int(s));
sw=str2int(s2);
result=str2int(s);
switch(sw)
{
case 8:
printf("%o ",result);
break;
case 10:
printf("%d ",result);
break;
case 16:
printf("%0x ",result);
break;
default:
printf("Usage: argv[0] number 8 | 10 | 16 ");
exit(1);
}
exit(0);
}