4 binary addition-c++ implementation-Classification discussion

Source: Internet
Author: User

Ideas:

1. Four types of discussion

2. Get the addition and subtraction calculation method

3. Leading 0 deletion and symbol deletion

#include <iostream> #include <string> #include <iomanip>using namespace std;//input 4 binary number format to determine BOOL Judge (    String a) {int i = 0;    if (a[i] = = '-' | | a[i] = = ' + ') i++;    if (a[i] = = ' 0 ' && a.size () -1>i) return false;            for (; I<a.size (), i++) {switch (A[i]) {case ' 0 ': Case ' 1 ': Case ' 2 ':            Case ' 3 ': break;        Default:return false; }} return true;    string rollover void reverse (string &a) {char x;    unsigned len = a.size ();    if (len = = 1) return;        for (int i=0;i<len/2;i++) {x = A[i];        A[i] = a[len-1-i];    A[len-1-i] = x;    }}//positive, add, flip to low address down operation, then flip back string Strplus (string a,string b) {reverse (a);    Reverse (b);    int Lena = A.size ();    int lenb = B.size (); int lmin = Lena < LenB?    Lena:lenb; int Lmax = Lena > LenB?     Lena:lenb;    string res = "";    int i; for (i=0;i<lmin;i++) {res + = A[i]-' 0 ' + b[i];        } for (; i<lmax;i++) {if (Lena > LenB) Res + = A[i];    else res + = B[i];    } int c = 0;        for (i=0;i<lmax;i++) {Res[i] = c + res[i];            if (res[i]> ' 3 ') {res[i]-= 4;        c = 1;    }else c = 0;    } if (c = = 1) Res + = ' 1 ';    Reverse (RES); return res;}    Delete the leading 0void Deletefrontzero (string &res) {string temp;    bool S = true;    int len = Res.size ();        for (int i=0;i<len;++i) if (res[i] = = ' 0 ' && s) continue;            else {s = false;        Temp + = Res[i]; } res = temp;}    Large number-decimals, unsigned string Strminus (string a,string b) {reverse (a);    Reverse (b);    bool ABIGB = true;    int Lena = A.size ();    int lenb = B.size ();    String res ("");    if (Lena < LenB) ABIGB = false;        if (Lena = = lenb) {int i = Lena-1;        while (i>=0 && a[i]==b[i]) i--; if (a[i]<b[i]) ABIGB = false;        if (i = =-1) {return "0";        }} if (!ABIGB) {reverse (a);        Reverse (b);    Return "-" +strminus (B,a);    } int i;    for (i = 0;i < lenb;i++) {res + = A[i]-b[i] + ' 0 ';    } for (; i < lena;i++) res + = A[i];    int c = 0;        for (int i = 0;i < lena;i++) {Res[i] = res[i]-C;            if (Res[i] < ' 0 ') {res[i] + = 4;        c = 1;    }else c = 0;    } reverse (res);    Deletefrontzero (RES); return res;}    Delete symbol string Deletesymbol (string &a) {string B; if (a[0] = = ' + ' | |        A[0] = = '-') {for (int i=0;i<a.size () -1;i++) B + = a[i+1];    return b;    }else{return A; }}string Myplus (string a,string b) {//positive if (a[0]! = '-' && b[0]! = '-') return Strplus (Deletesymbol (    A), Deletesymbol (b)); Positive and negative if (a[0]! = '-' && b[0] = = '-') return Strminus (Deletesymbol (a), DELETESYMBOL (b));    Negative if (a[0] = = '-' && b[0] = = '-') return "-" +strplus (Deletesymbol (a), Deletesymbol (b)); Negative positive if (a[0] = = '-' && b[0]! = '-') return Strminus (Deletesymbol (b), Deletesymbol (a));}    int main () {string A ("123"), B ("323");    String C (" -123"), D ("-23423");     String x1,x2;    COUT&LT;&LT;SETW (6) <<a<< "+" &LT;&LT;SETW (6) <<b<< "=";     COUT&LT;&LT;SETW (6) <<myplus (A, b) <<endl;    COUT&LT;&LT;SETW (6) <<a<< "+" &LT;&LT;SETW (6) <<c<< "=";     COUT&LT;&LT;SETW (6) <<myplus (a,c) <<endl;    COUT&LT;&LT;SETW (6) <<c<< "+" &LT;&LT;SETW (6) <<d<< "=";     COUT&LT;&LT;SETW (6) <<myplus (c,d) <<endl;    COUT&LT;&LT;SETW (6) <<d<< "+" &LT;&LT;SETW (6) <<b<< "=";     COUT&LT;&LT;SETW (6) <<myplus (d,b) <<endl;        do{cout<< "Enter the number of two correct four" <<endl;        cout<< "Number 1:";        cin>>x1; cout<< "Number 2:";    cin>>x2; }while (!judge (x1) | |!    Judge (x2));    COUT&LT;&LT;SETW (6) <<x1<< "+" &LT;&LT;SETW (6) <<x2<< "=";     COUT&LT;&LT;SETW (6) <<myplus (X1,X2) <<endl; return 0;}


The results of the operation are as follows:


4 binary addition-c++ implementation-Classification discussion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.