#include <iostream> #include <cstdlib> #include <ctime> using namespace std; class Complex { float re; float im; public: Complex(float r=0.0, float i=0.0) {re = r; im = i;} Complex(const Complex& c) {re = c.re; im = c.im;} void operator =(const Complex& c) {re = c.re; im = c.im; } Complex operator -()const {return {-re, -im};} Complex operator +(const Complex&) const; Complex operator -(const Complex&) const; Complex operator *(const Complex&) const; Complex operator /(const Complex&) const; friend ostream& operator << (ostream&, const Complex&); }; // TODO 1: Overloaded addition operator to be implemented /** * overloading the add operator * @param c another complex number * @return the sum complex number */ Complex Complex::operator+(const Complex &c) const { return {re+c.re, im+c.im}; } // TODO 2: Overloaded subtraction operator to be implemented /** * overloading the difference operator * @param c another complex number * @return the difference complex number */ Complex Complex::operator -(const Complex &c) const { return {re-c.re, im-c.im}; } // TODO 3: Overloaded multiplication operator to be implemented /** * overloading the multiplication operator * @param c another complex number * @return the product complex number */ Complex Complex::operator *(const Complex &c) const { float realVal = re*c.re - im*c.im; float imaginaryVal = re*c.im + im*c.re; return {realVal, imaginaryVal}; } // TODO 4: Overloaded division operator to be implemented /** * overloading the division operator * @param c another complex number * @return the fractional complex number after dividing */ Complex Complex::operator/(const Complex &c) const { float denominator = c.re*c.re + c.im*c.im; float realVal = (re*c.re + im*c.im)/denominator; float imaginaryVal = (im*c.re - re*c.im)/denominator; return {realVal, imaginaryVal}; } // TODO 5: Overloading stream output operator /** * overloading the ostream operator * @param out - the stream to display the complex number * @param c - the complex number * @return - the resultant stream after displaying the number */ ostream& operator << (ostream& out, const Complex& c) { if (c.im > 0) { out << c.re << " + " << c.im << "j"; } else if (c.im < 0) { out << c.re << " - " << -c.im << "j"; } else { out << c.re; } return out; } template<class T> class myStack { T* ptr; int size; int top; public: myStack(int); ~myStack() { free(ptr);} bool empty() {return top == -1;} bool full() {return top == size - 1;} int hold() {return top + 1;} void push(T v) {ptr[++top] = v;} T pop() {return ptr[--top];} void display(int); }; // TODO 6: create a constructor for the empty stack template <class T> myStack<T>::myStack(int s) { size = s; top = -1; ptr = new T[s]; } // TODO 7: display function to display k items per line /** * This function displays the items in the stack k items per line * @tparam T - the datatype of the item * @param itemsPerLine - no of items to be displayed each line (k) */ template <class T> void myStack<T>::display(int itemsPerLine) { for (int i = 0; i < size; ++i) { for (int j = i; j < i+itemsPerLine && j < size; ++j) { cout << ptr[j] << "\t"; } i = i + itemsPerLine - 1; cout << endl; } } int main(int argc, char** argv) { // TODO 8: get n, m and k from cmd and create a complex number stack n // getting arguments from the program if (argc == 4) { int n = stoi(argv[1]); int m = stoi(argv[2]); int k = stoi(argv[3]); myStack<Complex> complexNumberStack(n); // TODO 9: generate n random complex numbers and push them to the stack // seeding the random number generator srand(time(0)); for (int i = 0; i < n; ++i) { // pushing random complex number to the stack complexNumberStack.push(Complex(rand() % 100, rand() % 100)); } // TODO 10: display all the complex numbers to the stack, k items per line // displaying the complex number complexNumberStack.display(k); // TODO 11: create 2 complex numbers c1 and c2 and display them Complex c1(rand() % 100, rand() % 100); Complex c2(rand() % 100, rand() % 100); cout << "c1: " << c1 << endl; cout << "c2: " << c2 << endl; // TODO 12: display results of c1+c2, c1-c2, c1*c2 and c1/c2 cout << "c1+c2: " << c1+c2 << endl; cout << "c1-c2: " << c1-c2 << endl; cout << "c1*c2: " << c1*c2 << endl; cout << "c1/c2: " << c1/c2 << endl; } }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}