① c語言 十進制數轉換八進制 演算法
- 方法一:直接使用控制字元串 %o 八進制%x 
- 方法二: - 求余來算,比如求十進制數 x(x>100) 的8進制,先通過 x%8 可以得到個位(末位)上的數,當十進制數等於8時,必然會進位,求余的結果正好是不能進位的部分,x=x/8(這就像位移,x的8進制數向右移了一位),這樣已經求出來的 個位 位移後沒有了,原來的十位變成了個位,繼續把得到的x按上面的方式求末位,就能求出來十位,按照這種方式得到的 8進制數 是反的(先得到個位,再十位。。。),這樣很適合放到棧中,取得時候又會反過來,偽代碼可以這樣寫: - while(x){ - printf("%d",x%n);//會列印出x轉換為 N進制數 從低位到高位上的每一位數 - x/=n; - } 
- 十進制轉換N進制: - #include<stdio.h> - #include<stdlib.h> - #include<string.h> - typedef int INT; - typedef struct dd - { - INT data; - struct dd *next; - }LNode,*LStack; - LStack pushstack(LStack top,int x) - { - LStack p; - p=(LStack)malloc(sizeof(LNode)); - if((x)!=-1) {p->data=(x); p->next=top; top=p;} - return top; - } - LStack outstack(LStack top,int *x) - { - LStack p=top; - *x=p->data; - top=p->next; - free(p); - return top; - } - main() - { - int x,n; - LStack top=NULL; - printf("請輸入原數及要轉換的進制:"); - do{ - scanf("%d%d",&x,&n); //輸入一個十進制數和要轉換的進制,比如3 2 得到1 }while(x>35||x<0||n<2); - while(x){ //這個循環把每一位放到棧中 - top=pushstack(top,x%n); - x/=n; - while(top!=NULL) - { - top=outstack(top,&x); - if(x<10) - printf("%c",x+'0'); - else - printf("%c",x+'A'-10); - } - return 0; } 
② c語言十進制轉換為八進制
#include<stdio.h>
void main()
{
char *p,s[6];int n;
p=s;
gets(p);
n=0;
while(*(p)!='
