8进制转换函数:
#include <iostream.h>
#include <stdio.h>
#include <math.h>
int main()
{
char *p;
char s[6];
int n;
p=s;
cout<<"enter you want num to change !"<<endl;
//printf("enter you want num to change!");
gets(p);
n=0;
while ((*(p)!='\0'))
{
n=n*8+*p-'0';
p++;
}
cout<<"the num is "<<n<<endl;
//printf("%d",n);
return 0;
}
里面的n=n*8+*p-'0';是什么意思?为什么用到'0'?
试改如下:
//正8进制转换10进制函数:
#include <iostream.h>
#include <stdio.h>
#include <math.h>
int main()
{
char *p;
char s[6];
int n;
p=s;
cout<<"enter you want num to change !(请输入需转换数(最多5位8进制正数,例如76512)"<<endl;
//printf("enter you want num to change!");
gets(p);
n=0;
s[5]=0;//强制截断5位;
while ((*(p)!='\0'))
{
if((*p)<'0')||(*p)>'7') //看是否8进制
{
cout<<"输入的不是8进制正数 ,程序结束"<<n<<endl;
return(1);
}
n=n*8+*p-'0';
p++;
}
cout<<"the num is "<<n<<endl;
//printf("%d",n);
return 0;
}