C语言输入一个八进制数,输出相应的十进制数

在线等

晕了,这个问题可以用一个很简单的输出语句就可以解决啦

main()
{
int a=0210;//初始化a为八进制整数210
printf("%d\n",a);//再以十进制整数输出就可以了
}

楼上的太复杂,也可以是十六进制(d改为x就行)
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-05-31
// Practice20.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <math.h>

int result[100]={0};
int index=0;
#define MaxSize 100

//用栈的方法
typedef struct {
int data[MaxSize];
int top;
}Stack;//顺序栈,也可用链栈

void InitStack(Stack &S)//初始化栈
{
S.top=-1;
}

int push(Stack &S,char x)//入栈
{
if(S.top==MaxSize)
return 0; //栈满
else{
S.top++;
S.data[S.top]=x;
return 1;
}
}

int pop(Stack &S,char &x)//出栈
{
if(S.top==-1)
return 0; //栈空
else{
x=S.data[S.top];
S.top--;
return 1;
}
}

void display(Stack &S)
{
char x;
while(S.top!=-1)
{
pop(S,x);
printf("%c",x);
}
cout<<endl;
}

int main(int argc, char* argv[])
{
Stack S;
InitStack(S);
char c='a';
int j;
int k=0;
int num=0;
int n;

printf("输入数据为几进制?");
scanf("%d",&n);
getchar();
printf("输入数据\n");
while(c!='\n')
{
scanf("%c",&c);
if(c!='\n')
push(S,c);
}

printf("转换的十进制为:\n");
while(S.top!=-1)
{
pop(S,c);
if(c>='0'&&c<='9')
{
j=c-'0';
}
if(c>='a' && c<='z')
{
j = c-'a'+10;
}
num+=pow(n,k++)*j;
}
printf("%d\n",num);
return 0;
}
第2个回答  2010-05-31
//---------------------------------------------------------------------------

#include <stdio.h>

int main(void)
{
char oct[80];
int dec;
scanf("%[0-7]",oct);
sscanf(oct,"%o",&dec);
printf("%d",dec);
return 0;
}
//---------------------------------------------------------------------------本回答被网友采纳
相似回答