N-en-complement, Gray code

zhaozj2021-02-17  40

/ * 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 #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 ( '0'); const int cnMaxDecNum = static_cast ( '9'); const int cnMinHexNum = static_cast ( 'A'); const int cnMaxHexNum = static_cast ('Z');

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 (ch); if (NBase <10) {if (nch> = cnmindecnum && nch <= cnmaxdecnum) nch = nch - cnmindecnum 0; else {cout << "- illegal input! ---" << Else {IF (nch> = cnminHexnum && nch <= cnmaxhexnum) NCH = Nch - CNMINHEXNUM 10; ELSE IF (Nch> = cnmindecnum && nch <= cnmaxdecnum) NCH = nch - cout <<< illegal input! - - "<< Endl; returnif false;}} ndec = ndec * NBase NCH;} // If started at 1, it is a negative number, and is defined by complement, it is necessary to use NDEC = -1 * (POW (2, n) - NDEC); if (strother.at (0) == '1') {int nNum = 1; for (int J = 0; j = 36) { Cout << "The value of the wrong progress, please enter 2-35!" << end1; return false;}

/ / 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 (nresidual); ostrother << ch;} else {// The rest of the remainder ostrother << nresidual;}} while (ndec> 0); String strreverse = ostrother.str (); // reverse string to get strother strother = ""; for (int i = 0; i

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 (strbin.at (0)) - cnmindecnum Ostrgraycode << nfro;

For (int i = 1; i (strbin.at (I-1) )) - cnmindecnum; // minus the integer value corresponding to the '0', get the true representative of the digital ncur = static_cast (Strbin.at (I)) - cnmindecnum; ostrgraycode << ((NFRO NCUR)% 2); // Pointing or} strgraycode = ostrgraycode.str (); return true;}

转载请注明原文地址:https://www.9cbs.com/read-28786.html

New Post(0)