/ * Function: 1. Any credited to decimal (other amount of progress is considered to be complement, that is, the left first bit is considered to be complement) 2. Decimal to arbitrary credits (negative numbers) 3. Get a decimal number of GRAY code 4. Get binary coding corresponding to the GRAY code
Time: 2002-7-3 Note: When other credits exceeds 10, use AZ these 26 letters instead of 10-35 these 26 numbers. So actually entered input at 35 (letter Z) the following.*/
#include
Using namespace std;
// Arbitrary Bool Other2Dec (INT & NDEC, INT NOT NBASE); BOOL Other2Dec (INT NDEC, String Strother, INT NBASE); // Decimal to any credit (negative number with complement form) BOOL DEC2HER string & strOther, int nDec, int nBase); // get a decimal number of Gray code bool GetGrayCode (string & strGrayCode, int nDec); // get the binary coding bool GetBinCodeByGrayCode (string & strBinCode, const string & strGrayCode) Gray codes corresponding to;
const int cnMinDecNum = static_cast
Void main () {// The following is the test code: int NDEC; int NBase; string strnher; string string string; string strbincode; cout << "Decimal / T Gray code / T Gray Code -> binary / t decimal -> binary / T binary -> decimal "<< endl; for (int i = -9; i <= 0; I ) {GETGRAYCODE (STRGRAYCODE, I); getBincodeByGraycode; Dec2her (Strothers, I, 2); Other2Dec (NDEC, STROTHER, 2); cout << I // decimal << "/ t" << strgraycode // Gray code << "/ t / t" << strbincode // Gray Code -> binary << "/ T "<< strother // decimal -> binary <<" / t / t "<< ndec // binary -> decimal << Endl;} char ch; cout <<" Continue (Y / N)? "; CIN >> CH; IF (ch! = 'y' && ch! = 'y') goto exitfunc;
COUT << "Decimal Conversion to other credits:" << Endl; cout << "Enter the number of decimal numbers to be converted (BASE):"; cin >> NDEC >> NBASE; DEC2HER Strother, NDEC, NBASE; COUT << "Decimal number" << NDEC << "transformation into" << nbase << "number is" << strother << Endl << endl; cout << "Continue (y / n)? "; cin >> CH; if (ch! = 'y' && ch! = 'y') goto exitfunc; cout <<" Other input numbers to the decimal number: "<< ENDL; COUT << "The number of other credits to be converted (BASE):"; CIN >> Strothers >> NBase; if (Other2Dec (NDEC, Strothers, NBase)) cout << nbase << " The number of inputs to the decimal number is "<< nDEC << Endl; Else Cout <<" conversion failed! "<< end1
EXITFUNC: COUT << "Press any key to exit ..." << endl; cin.get ();
// ----------------------------- // Function: Transform other credits to decimal // parameters : // NDEC: Return Decimal number // Nother: Other Entering Values // NBase: Other Entering // Return Value: // True: Enter legal, operation Success / False: Input is not Legal, operation failed BOOL Other2DEC (int "= 2) {ostringstream ostrother; ostrother << Nother; String Strother = ostrother.str ();
Return Other2Dec (NDEC, Strothers, NBase);
// ----------------------------- // Function: Transform other credits to decimal // parameters : // NDEC: Return Decimal number // Nother: Other Entering Values // NBase: Other Entering // Return Value: // True: Enter legal, operation Success / False: Input is not Legal, operation failed BOOL Other2DEC (int) {INT NBASE = 2) {INT NLEN = Strother.Length (); if (Nlen <= 0) Return False; IF (NBase <= 1 || NBase> = 36 ) {Cout << "The value of the wrong enrollment, please enter 2-35!" << Endl; return false;} //} 补 4 整 整 (Strother.Length ()% 4! = 0 ) {Strother = "0" strother;}
NDEC = 0; for (int i = 0; i
/ / When less than 0, the NDEC is indicated by the complement, // will become NDEC = POW (2, n) nDEC; // where N is the encoded dimension, that is, NDEC is indicated by symbols. The number of digits required for the number. IF (NDEC <0) {INT nnum = ndec * -1; int ndigit = 0; while (nnum> 0) {nnum = nnum / nbase; ndigit ;} int NDIV = NDIGIT / 4; ndigit = (NDIV 1) * 4;
NNUM = 1; for (int I = 0; i Ostringstream Ostrother; Int nresidual; // 余 DO {nresidual = ndec% NBase; NDEC = NDEC / NBASE; if (NBase> 10 && nresidual> = 10) // When the progress is greater than 10 and the remainder is greater than 10 {// Output AZ letters nresidual = nresidual - 10 cnminHexNum; char ch = static_cast Ostringstream OstrGrayCode; int NCur; // Left the first character is the same // minus the integer value of '0', to get the true represented digital int NFRO = Static_cast For (int i = 1; i