#include <stdio.h>
#include <math.h>
int main()
{
char ch;//用來接受一個輸入字符
int stack[50];//存儲處理後的十六進制對應的十進制
int top=0;//棧裏面數據的個數
int decimal=0;//小數點在輸入字符串中的位置,從0號位置開始計數
float num=0;//計算最後的十進制數
printf("請輸入十六進制(0-9,a-f,A-F):");
while ((ch=getchar())!='/n')
{
if (ch>='0'&&ch<='9')
stack[top++]=int(ch-48);
else if (ch>='A'&&ch<='F')
stack[top++]=int(ch-55);
else if (ch>='a'&&ch<='f')
stack[top++]=int(ch-87);
else if (ch=='.')//如果是小數點
decimal=top;//表明從decimal個數據以後的數據都是小數部分
else {
printf("輸入了非法數據");
return 0;
}
}
while(top)
{
if (decimal!=0&&top>decimal)//計算小數部分
num+=stack[--top]/pow(16,top-decimal+1);
else
num+=stack[--top]*pow(16,top);//計算整數部分
}
printf("%.4f",num);
getchar();
return 1;
}
2009年03月17日,留作紀念!