The example of this article for everyone to share the simple implementation of C + + complex calculator specific code, for your reference, the specific content as follows
1. Functions related to calculators
A, to implement the subtraction, size comparison of multiple complex numbers (including real numbers),
B. Implementation of decimal real-system conversion, can be converted to arbitrary
2. The principles of design
A. The part of the complex number calculation, using the overloaded operator, subtraction and size comparison of complex numbers
For input and output, (>> and <<), subtraction operations are overloaded.
An input overload that implements the input of a complex number in the form of A+bi.
The output of the overload, to achieve the output of real numbers (to achieve a special output of the real part of the imaginary part, etc.).
Subtraction to implement an operation on a complex number.
B. The part of the conversion, using the method of conversion, the implementation of the 10-in-system arbitrary conversion. Divide, record each remainder, stored in an array to record the converted numbers
3. The characteristics of the design
A. Use functions to encapsulate the implemented functions.
B. Overloading of operators to make complex numbers more convenient
C. Consider all the input and output conditions, select the operation, input the plural type, and so on to judge and corresponding processing
D. After the completion of the program, accept everyone's advice, standardize the code format, their own in the production process has been harvested to learn.
Here is the implementation of the Code:
#include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include < cstring> #include <fstream> #include <ctime> #define EPS 1e-5//namespace STD with defined precision constant using;
Use standard space to name STD namespace nameccom//define namespaces Nameccom {class ccom//define a ccom class {public:double real,image;//real and imaginary parts
Ccom (double real=0,double image=0)//constructor {real=real;
Image=image; Friend IStream & Operator>> (IStream &is,ccom &com); Overload input Friend Ostream & operator<< (ostream &os,ccom &com); Overload output ccom operator+ (ccom &com); Additive overload ccom operator-(ccom &com); Subtraction overload ccom operator* (ccom &com); Multiplication overload ccom operator/(ccom &com); Division overload ccom operator+= (ccom &com); Additive assignment overload ccom operator-= (ccom &com); Subtraction Assignment overload ccom operator*= (ccom &com); Multiplication Assignment overload ccom operator/= (ccom &com);
Division assignment overload}; struct User//definitionUser structure body type {char szname[20]; User name}user; Defines the global variable int ccom::operator> (ccom &com)//overloaded operator ">", the size of the comparison model {if (mod () >com.mod ()) return 1;//If large, returns 1 E LSE return 0; Otherwise, the 0} int ccom::operator< (ccom &com)//overloaded operator "<" is returned, the size of the comparison model {if (mod () <com.mod ()) return 1; If small, it returns 1 else return 0; Otherwise, the 0} int ccom::operator!= (ccom &com)//overloaded operator "!=" is returned, respectively, to determine the real and imaginary parts of the complex number {if (*this==com) return 0; If equal, returns 0 else return 1;
Otherwise, return 1} IStream & Operator>> (IStream &is,ccom &com)//overloaded input, you can enter the A+bi form {cout<< "Please enter plural:";
Char s[80]; is>>s; Accept the plural int len=strlen (s) in the form of a string; Find the length of the string int n=0,sign=1; n is the current number extracted from the string, initialized to 0;sign is a token symbol, initialized to the positive COM. Image=com.
real=0; for (int k=0;k<len;k++)//To determine if the accepted string is valid {if (s[k]< ' 0 ' | | | | s[k]> ' 9 ') && (s[k]!= ' + ' && s[k]!= '-')
&& s[k]!= ' i ') {cout<< "error" <<endl; return is; Error, output error message and return}} for (int k=0;k<len;)//OrderIdentify the characters in the string {if (n!=0 && s[k]== ' | | | s[k]== ' + '))//Whether the current character is a symbol bit {com. Real=sign*n; Sign is a sign bit, and n!=0, that is, N has been assigned value, indicating that the current reading is the imaginary part of the symbol n=0; The original n*sign value is assigned to the real part, the n is cleared, and the value of the imaginary part is ready to be accepted} if (s[k]== '-')//The current character is a minus {sign=-1;k++; Assign value to symbol flags} if (s[k]== ' + ')//current character is positive {sign=1;k++; Assign value to symbol flags} if (s[k]== ' I ')//The current character is ' I ' {if (k!=len-1)//judge whether the character ' I ' is a character in the string cout<< "error\n";//if not, show plural number According to the format error else com. Image=sign*n;
If it is the last character, the plural object has been accepted and the sign*n is assigned to the imaginary part.
while (s[k]>= ' 0 ' && s[k]<= ' 9 ')///If the current character is between 0~9, converts the numeric character to numeric value {n=n*10+s[k]-' 0 ';
k++; } if (s[len-1]!= ' i ' && n!=0)//If the last character is not ' I ', it means that the plural object has only a real part and no imaginary part {com.
Real=n*sign;
return is; } Ostream & operator<< (ostream &os,ccom &com)//Overload input {if (fabs com. Image)///If the imaginary part is 0 os<<com <eps. Real; Output only the real part else if (fabs com. Real) <eps)///If the actual part is 0 os<<com. image<< "I"; Output only the imaginary part else if (COM. image>0) os<<com. real<< "+" <<com.
image<< "I"; Else os<<com. Real<<com. image<< "I";
The imaginary part is positive return OS;
} ccom ccom::operator+ (ccom &com)//additive overload {ccom sum; Sum. Real=real+com. Real; The real part adds sum. Image=image+com. Image;
The imaginary part is added to return sum;
} ccom ccom::operator-(ccom &com)//subtraction overload {ccom sub; Sub. Real=real-com.real; The real part subtracts the sub. Image=image-com.image;
Imaginary part subtraction return sub;
} ccom ccom::operator* (ccom &com)//multiplication overload {ccom multi; Multi. Real=real*com. Real-image*com. Image; Real part product multi. Image=real*com. Image+image*com. Real;
The imaginary part product return multi;
} ccom ccom::operator/(ccom &com)//division overload {ccom div; Div. Real= (real*com. Real+image*com. Image)/(COM. Real*com. Real+com. Image*com. Image); The real part is to divide the product div. Image= (image*com. Real-real*com. Image)/(COM. Real*com. Real+com. Image*com. Image);
Imaginary part in addition to the product return div; } ccom ccom::operator+= (ccom &com)//Overloaded addition assignment {real=real+com. Real; The real part is added image=image+com. Image;
Imaginary part added return *this; } ccom ccom::operator-= (ccom &com)//HeavyLoad subtraction Assignment {real=real-com.real; Real part subtraction Image=image-com.image;
Imaginary part subtraction return *this; } ccom ccom::operator*= (ccom &com)//overloaded multiplication assignment {double nreal=real*com. Real-image*com. Image; Real part product double nimage=real*com. Image+image*com. Real;
Imaginary part product real=nreal;
Image=nimage;
return *this; } ccom ccom::operator/= (ccom &com)//Overloaded division assignment {Double nreal= (real*com. Real+image*com. Image)/(COM. Real*com. Real+com. Image*com. Image); The real part except the product double nimage= (image*com. Real-real*com. Image)/(COM. Real*com. Real+com. Image*com. Image);
The imaginary part is real=nreal in addition to the product;
Image=nimage;
return *this; The int ccom::operator== (ccom &com)//Overload equals {if (real==com). Real && image==com.
Image) return 1;
else return 0;
} void Add ()//complex addition operational function {ccom Num1,num2,sum,zero (0,0);
cout<< "add \ n" << "Enter at least two complex numbers and end with 0 \ n";
cout<< "first plural:";
cin>>num1;
cout<< "second plural:";
cin>>num2;
sum=num1+num2;
cout<< "third plural:";
cin>>num1;
int i=4; while (!) (
Num1==zero)){sum=sum+num1;
"cout<<" <<i<< "plural:";
cin>>num1;
i++;
} cout<< "addition result is:" <<sum<<endl;
cout<< "Please press any key to continue \ n";
Cin.get ();
Cin.get ();
} void Sub ()//complex subtraction operator {ccom Num1,num2,sub,zero (0,0);
cout<< "Enter at least two complex numbers and end with 0 \ n";
cout<< "1th plural:";
cin>>num1;
cout<< "2nd plural:";
cin>>num2;
sub=num1-num2;
cout<< "3rd plural:";
cin>>num1;
int i=4; while (!) (
Num1==zero)) {sub=sub-num1;
"cout<<" <<i<< "plural:";
cin>>num1;
i++;
} cout<< "The result of the subtraction is:" <<sub<<endl;
cout<< "Please press any key to continue \ n";
Cin.get ();
Cin.get ();
} void Mul ()//complex multiplication operation function {ccom Num1,num2,mul,zero (0,0);
cout<< "Multiply compute \ n" << "Enter at least two complex numbers and end with 0 \ n";
cout<< "first plural:";
cin>>num1;
cout<< "second plural:";
cin>>num2;
mul=num1*num2;
cout<< "third plural:";
cin>>num1;
int i=4; while (!) (
Num1==zero)) {mul*=num1; cout<< "First" <<i<< "plural:";
cin>>num1;
i++;
cout<< "The result of the multiplication is:" <<mul<<endl;
cout<< "Please press any key to continue \ n";
Cin.get ();
Cin.get ();
} void Div ()//complex Division operational function {ccom Num1,num2,div,zero (0,0);
cout<< "division calculates \ n" << "Enter at least two complex numbers and ends with 0 \ n";
cout<< "first plural:";
cin>>num1;
cout<< "second plural:";
cin>>num2;
div=num1/num2;
cout<< "third plural:";
cin>>num1;
int i=4; while (!) (
Num1==zero)) {div/=num1;
"cout<<" <<i<< "plural:";
cin>>num1;
i++;
cout<< "Division result is:" <<div<<endl;
cout<< "Please press any key to continue \ n";
Cin.get ();
Cin.get ();
} void Compare ()//two complex comparison function {ccom num1,num2;
cout<< "Please enter two plural \ n";
cout<< "first plural:";
cin>>num1;
cout<< "second plural \ n";
cin>>num2;
if (num1==num2) cout<< "These two plural equals \ n";
else if (num1>num2) cout<<num1<< "modulo" is greater than "<<num2<<"; else if (num1<num2) cout<<num2<< "The modulus is greater than" <<num1<< "
The model \ n ";
else cout<< "The modulo equals of these two complex numbers \ n";
cout<< "Please press any key to continue \ n";
Cin.get ();
Cin.get ();
} void Jinzhi ()//implements the transform {long n;
int p,c,m=0,s[100];
cout<< "Enter the number to convert: \ n";
cin>>n;
cout<< "Enter the system to convert: \ n";
cin>>p;
cout<< "(" <<n<< ") 10=" << "(";
while (n!=0)//conversion, the result is stored in the array s[m] {c=n%p;
n=n/p; M++;s[m]=c; The remainder is stored in order in array S[m]} for (int k=m;k>=1;k--)//output converted sequence {if (s[k]>=10)//If 16 is the output of the corresponding letter cout<< (char) (s[
K]+55);
else//Otherwise direct output digital cout<<s[k];
} cout<< ")" <<p<<endl;
cout<< "Please press any key to continue \ n";
Cin.get ();
Cin.get ();
} void Outpt () {char szname[20];
cout<< "Please enter your name:";
cin>>szname;
System ("CLS");
Do {System ("CLS");
cout<< "\ t This is a calculator that can achieve the following functions, please press the corresponding button (0-6) \n\n\n";
cout<< "\t*----------hpioneer Small Calculator menu---------*\n"; cout<< "\t|
1: Multiple complex addition, with 0 end |\n "; cout<< "\t|
2: Multiple complex subtraction, to 0 end |\n "; cout<< "\t|
3: Multiple plural multiplication, to 0 end |\n "; cout<< "\t|
4: Multiple plural division, to 0 end |\n "; cout<< "\t|
5: plural comparison |\n "; cout<< "\t|
6: Into the conversion |\n "; cout<< "\t|
0: Exit the program |\n "; cout<< "\t|
Please choose: |\n ";
cout<< "\t*---------------------------------------*\n";
The using namespace nameccom;
int main (void)//main function begins, void can not write {int h;
Output ();
cin>>h;//each step operation if (h==1)//user selected 1 calls Add () function add ();
else if (h==2)//the user chooses 2 to call the sub () function sub ();
else if (h==3)//user selected 3 calls the Mul () function Mul ();
else if (h==4)//user selected 4 calls the Di V () function Div ();
else if (h==5)//user selected 6 calls the Compare () function Compare ();
else if (h==6)//user Select 7 then call function Jinzhi () function Jinzhi ();
else if (h==0) cout<< "Thank you very much for using \ n";
else break;
return 0;
}
The above is the entire content of this article, I hope that you learn C + + program design help.