程序的目標是將十進制轉換爲十六進制 1)我有問題將整數值賦值給char數組元素如何完成。我得到我的數組數值的符號。 2)如何初始化我的數組到空格?擺脫數組元素中的垃圾和舊數據。 3)有人可以解釋我的問題是什麼?整數和字符? 4)cout是我的問題的一部分嗎?將整數值賦值給char數組元素
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <math.h>
int main()
{
using namespace std;
// binary declarations
int x, i, z, n, remainder;
int result = 0;
bool loop_cond, remainder_found, octval_found;
// octal declarations
int octalval,octresult,octremainder,temp_octalval;
int octloop_cnt;
bool oct_loop;
octresult = 0;
// hexadecimal declarations
int hexval, hexremainder, temp_hexval;
int hexloop_cnt;
bool hex_loop,hexval_found;
char hexresult[5] = {" "};
cout << "decimal" << "\t" << "binary" << "\t"<< "octal"<<"\t"<<"hexadecimal"<< endl;
for (i = 1; i <= 256; i++){
hexloop_cnt = 0;
octremainder = 0;
hexval_found = false;
hex_loop = true;
hexloop_cnt = 0;
while (hex_loop != false){
if (hexval_found != true){
hexval = i/16;
hexremainder = i % 16;
}
else {
temp_hexval = hexval;
hexval = hexval/16;
hexremainder = temp_hexval % 16;
}
if (hexval == 0){
switch (hexremainder){
case 10:
hexresult[hexloop_cnt] = 'A';
break;
case 11:
hexresult[hexloop_cnt] = 'B';
break;
case 12:
hexresult[hexloop_cnt] = 'C';
break;
case 13:
hexresult[hexloop_cnt] = 'D';
break;
case 14:
hexresult[hexloop_cnt] = 'E';
break;
case 15:
hexresult[hexloop_cnt] = 'F';
break;
default:
hexresult[hexloop_cnt] = (char)hexremainder;
break;
}//end switch
hex_loop = false;
}//end if
if ((hexval < 16) && (hexval > 0)){
switch (hexremainder){
case 10:
hexresult[hexloop_cnt] = 'A';
break;
case 11:
hexresult[hexloop_cnt] = 'B';
break;
case 12:
hexresult[hexloop_cnt] = 'C';
break;
case 13:
hexresult[hexloop_cnt] = 'D';
break;
case 14:
hexresult[hexloop_cnt] = 'E';
break;
case 15:
hexresult[hexloop_cnt] = 'F';
break;
default:
hexresult[hexloop_cnt] = static_cast<char>(hexremainder);
break;
}//end switch
hexloop_cnt++;
switch (hexval){
case 10:
hexresult[hexloop_cnt] = 'A';
break;
case 11:
hexresult[hexloop_cnt] = 'B';
break;
case 12:
hexresult[hexloop_cnt] = 'C';
break;
case 13:
hexresult[hexloop_cnt] = 'D';
break;
case 14:
hexresult[hexloop_cnt] = 'E';
break;
case 15:
hexresult[hexloop_cnt] = 'F';
break;
default:
hexresult[hexloop_cnt] = static_cast<char>(hexval);
break;
}//end switch
hex_loop = false;
} //end if
if ((hexval >= 16)){
switch (hexremainder){
case 10:
hexresult[hexloop_cnt] = 'A';
break;
case 11:
hexresult[hexloop_cnt] = 'B';
break;
case 12:
hexresult[hexloop_cnt] = 'C';
break;
case 13:
hexresult[hexloop_cnt] = 'D';
break;
case 14:
hexresult[hexloop_cnt] = 'E';
break;
case 15:
hexresult[hexloop_cnt] = 'F';
break;
default:
hexresult[hexloop_cnt] = static_cast<char>(hexremainder);
break;
}//end switch
hexval_found = true;
}//end if
hexloop_cnt++;
} //endwhile
cout << i << "\t" << result <<"\t" << octresult<<"\t" << hexresult<< endl;
result = 0;
octresult = 0;
// hexresult[5] = { ' ', ' ', ' ', ' ', ' ' };
}
cin.clear();
cin.ignore(255, '/n');
cin.get();
return 0;
}
請縮小代碼的相關部分,或者更好創建[最小,完整的,並且可驗證示例](http://stackoverflow.com/help/ mcve)並向我們展示。 –
也縮進代碼。 +1給Joachim! – gsamaras