#include<stdio.h> intCal_Decimal(int input) { int sum = 0; while (input!=0) { int temp = input; if (temp < 10)//十进制的数字除到了10以下,直接加上这个数字 { sum = sum + temp; break; } int capacity = 0;//数字的位数 int temp_capacity;//临时变量-数字的位数 int a = 10;//用于计算最高位的值 int i = 0; int j = 0; int x = 8;//从高位往低位得到各位的数字 while (temp != 0)//先计算位数 { temp = temp / 10; capacity++; } capacity = capacity - 1; temp_capacity = capacity; temp = input; while (temp_capacity != 0)//获得现在最高位的数字 { temp = temp / 10; temp_capacity--; } j = temp; temp_capacity = capacity; while (temp_capacity > 1)//计算乘以8的次方数 { x = x * 8; temp_capacity--; } while (capacity >1) //减去最大的一位 { a = a * 10; capacity--; } a = a * j; i = x * j; input = input - a; sum = sum + i; } return sum; } intmain() { int input; int output; printf("请输入一个数字:->"); scanf("%d",&input); output = Cal_Decimal(input); printf("%d",output); return0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<stdio.h> intmain(void) { char s[20] = "1507"; int i = 0; int n = 0; while(s[i]!='\0') { n = n * 8 + s[i] - '0'; i++; } printf("%d",n); return0; }