1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#include <iostream>
#include <stdlib.h>
using namespace std;
//Class definition
class Complex
{
private:
int a, b;
public:
Complex();
Complex(int, int);
Complex operator + (Complex);
Complex operator - (Complex);
Complex operator * (int);
friend istream& operator >> (istream &Input, Complex &cmplx);
friend ostream& operator << (ostream &Output, Complex &cmplx);
};
//A function that prints a menu.
void printMenu();
//Main function
int main()
{
cout << "The program does operations for complex numbers." << endl
<< "PUT AS MANY SPACES AS YOU WANT! JUST DO NOT SEPERATE A NUMBER."
<< endl << endl;
int repeat = 1;
do{
int Operation;
printMenu();
cin >> Operation;
cin.ignore();
Complex c1, c2, c_ans;
if (Operation == 1)
{
cout << "Enter the first complex number: ";
cin >> c1;
cout << "Enter the second complex number: ";
cin >> c2;
c_ans = c1 + c2;
cout << "( " << c1 << " ) + ( " << c2 << " ) = " << c_ans << endl;
}
else if (Operation == 2)
{
cout << "Enter the first complex number: ";
cin >> c1;
cout << "Enter the second complex number: ";
cin >> c2;
c_ans = c1 - c2;
cout << "( " << c1 << " ) - ( " << c2 << " ) = " << c_ans << endl;
}
else if (Operation == 3)
{
int mul;
cout << "Enter the complex number: ";
cin >> c1;
cout << "Enter a multiplier: ";
cin >> mul;
c_ans = c1 * mul;
cout << "( " << c1 << " ) * ( " << mul << " ) = " << c_ans << endl;
}
cout << "Enter 0 to exit: ";
cin >> repeat;
cout << endl;
} while(repeat);
return 0;
}
void printMenu()
{
cout << "1.Add 2. Sub 3. Scalar Multiply" << endl
<< "Please enter the number to execute a operation: ";
}
Complex::Complex()
{
a = 0;
b = 0;
}
Complex::Complex(int input_a, int input_b)
{
a = input_a;
b = input_b;
}
Complex Complex::operator + (Complex cmplx)
{
Complex c_ans(a, b);
c_ans.a += cmplx.a;
c_ans.b += cmplx.b;
return c_ans;
}
Complex Complex::operator - (Complex cmplx)
{
Complex c_ans(a, b);
c_ans.a -= cmplx.a;
c_ans.b -= cmplx.b;
return c_ans;
}
Complex Complex::operator * (int mul)
{
Complex c_ans(a, b);
c_ans.a *= mul;
c_ans.b *= mul;
return c_ans;
}
istream& operator >> (istream &Input, Complex &cmplx)
{
char box[100];
char a[50], b[50]; "
int ix_box = 0, ix_ab = 0;
Input.getline(box,100);
for(ix_box; isspace(box[ix_box]); ix_box++)
{
}
a[ix_ab++] = box[ix_box++];
for(ix_box; isspace(box[ix_box]); ix_box++)
{
}
for(ix_box; isdigit(box[ix_box]); ix_box++)
{
a[ix_ab] = box[ix_box];
ix_ab++;
}
a[ix_ab] = '\0';
cmplx.a = atoi(a);
for(ix_box; !ispunct(box[ix_box]); ix_box++)
{
}
ix_ab = 0;
b[ix_ab] = box[ix_box];
ix_ab++;
ix_box++;
for(ix_box; isspace(box[ix_box]); ix_box++)
{
}
for(ix_box; isdigit(box[ix_box]); ix_box++)
{
b[ix_ab] = box[ix_box];
ix_ab++;
}
b[ix_ab] = '\0';
cmplx.b = atoi(b);
return Input;
}
ostream& operator << (ostream &Output, Complex &cmplx)
{
Output << cmplx.a;
if(cmplx.b >= 0)
Output << " + " << cmplx.b << "i";
else
Output << " - " << abs(cmplx.b) << "i";
return Output;
}
|