c语言: 输入一个十进制正整数,将其转换成八进制数,并输出结果。

[复制链接]
查看11 | 回复1 | 2006-5-6 14:14:43 | 显示全部楼层 |阅读模式
个人意见,仅供参考!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();}
回复

使用道具 举报

千问 | 2006-5-6 14:14:43 | 显示全部楼层
#include #include 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;}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行