雪佛兰创酷轮胎尺寸:c语言: 输入一个十进制正整数,将其转换成八进制数,并输出结果。

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/04 19:11:29

个人意见,仅供参考!
1.
//用转意字符
⑴这是输出有前缀“0”的八进制数
int main(void)
{
int a;
printf("\n");
scanf("%d",&a);
printf("%#o",a); /*"%#o"这里的“#”就是输出前缀的,“o”是八进制*/
getch();
return 0;
}
⑵输出无前缀的八进制数
main()
{
int a;
printf("\n");
scanf("%d",&a);
printf("%o",a); /*不要在o前加“#”*/
}

——————————————————————————————
2.
//用数据结构的知识解决
int *Init()
{
int *top,*base;
base=(int *)malloc(sizeof(int) * 50);
if(!base) {printf("Error!");exit();}
top=base;
return top;
}

int *push(int *top,int n)
{
*top=n;
top++;
return top;
}

int pop(int *top)
{
int e;
top--;
e=*top;
return e;
}

void convs()
{
int *top, *base;
int e,N;
int i;
top=Init();
base=top;
printf("Input the number:\n");
scanf("%d",&N);
while(N!=0)
{
top=push(top,N%8);
N=N/8;
}
printf("After change,the number is:\n");
while(top!=base)
{
e=pop(top);
top--;
printf("%d",e);
}
printf("\n");
}
main()
{
convs();
getch();
}

#include <stdio.h>
#include <math.h>
int convert10_to_8(int i)
{
int k,s=0;
int j[6]={0,0,0,0,0,0};
for(k=0;k<6;k++)

if(i/8)
{
j[k]=i%8;
i/=8;
}
else
{
j[k]=i;
break;
}
for(k=0;k<6;k++)
s+=j[k]*pow(10,k);
return s;
}

main()
{
int a;
while(printf("input a number:")&&scanf("%d",&a))
printf("the rezult is:%d\n\n",convert10_to_8(a));
return 0;
}