高手进,C语言,调用BOOL型函数算十六进制的,问出错在哪里

来源:百度知道 编辑:UC知道 时间:2024/06/05 23:05:52
#include<stdio.h>
#include<math.h>
typedef enum{false,true}bool;
bool flag;
bool convert(char s1[]);
int d;
void main(){
int s;
char st1[100];
printf("请输入此十六进制数:\n");
gets(st1);
if(convert(st1)){
s=d;
printf("转为十进制后:\n%d",s);
}
else
printf("您输入的不是一个十六进制数。");
}
bool convert(char s1[]){
int i,d,n;
char s2[100];
d=0;
for(i=0;s1[i]!="\0";i++);
n=i;
for(i=0;i<n;i++)
s2[n-i]=s1[i];
i=0;
flag=true;
while((s2[i]!="\0")&&true){
if((int)s2[i]>=48&&(int)s2[i]<=57){
d=d+(((int)s2-48)*(pow(16,i)));
flag=true;
}
if((int)s2[i]>=65&&(int)s1[i]<=70){
d=d+(((int)s2[i]-55)*(pow(16,i)));
flag=true;
}
else
flag=false;
i++;
}
return flag;
}

全局变量d 不能再在convert 函数里面定义,否则变成局部变量了。

改写一个能work的:

#include <stdio.h>
#include <math.h>

typedef enum
{
FALSE,
TURE
}bool;

bool flag;
int power(int x, int y);
bool convert(char s1[]);
int d;

int main(void){
int s;
char st1[100];

printf("Please input a hexadecimal number:\n");
scanf("%s", st1);

if(convert(st1)) {
s=d;
printf("converted to decimal number: %d\n",s);
} else {
printf("input error");
}
return 0;
}

int power(int x, int y)
{
int pow1= 1;
int i;

for (i=1; i<=y;i++ ){
pow1*=x;
}
return pow1;
}

bool convert(char s1[])
{
unsigned int i,n;
unsigned char s2[100]="\0&quo