CCCPaste

NCTU LAB 07/05 NUM 3

#include 
#include 
#include 
using namespace std;

#define MAX 50

int countDigit(long long n){ 
    return floor(log10(n)+1); 
} 

char* hexToDec(char hex[]){
	int r, total=0;
	int length = strlen(hex);
	for(int i=0;i='0' && hex[i] <='9'){
			r = hex[i] - 48; 
		}else if(hex[i]>='a' && hex[i]<='f'){
			r = hex[i] - 87;
		}else if(hex[i]>='A' && hex[i]<='F'){
			r = hex[i] - 55;
		}
		total += r*pow(16, length);
	}
	int l = countDigit(total);
	char *result = new char[l];
	for(int i=0;i=0;i--){
		cout << num[i];
	}
	cout << endl;
}

int main(){
	char num[MAX];
	cout << "Enter a hexadecimal number : ";
	while(cin >> num){
		char* res = hexToDec(num);
		print(res);
		free(res);
		cout << "Enter a hexadecimal number : ";
	}
    return 0;
}