Skip to main content

Creating Calculator Program Using Cpp


Implement a class Complex which represents the Complex Number data type. Implement the following operations:
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers.



We are using g++ compiler and terminal to compile the code and ubuntu OS.

 PROGRAM:


#include<iostream>
using namespace std;
int main()
{

 PROGRAM:
   int a,b,c;
   char op,d;
   do
   {
   cout<<"Enter the first number:";
   cin>>a;
   cout<<"Enter the second number:";
   cin>>b;
   cout<<"Enter operator +,-,*,/:";
   cin>>op;
      switch(op)
      {
       case'+':
         c=a+b;
         cout<<"Addition:"<<a<<"+"<<b<<"="<<c;
         break;
       case'-':
         c=a-b;
         cout<<"Substraction:"<<a<<"-"<<b<<"="<<c;
         break;
       case'*':
         c=a*b;
         cout<<"Multiplication:"<<a<<"*"<<b<<"="<<c;
         break;
       case'/':
         c=a/b;
         cout<<"Divition:"<<a<<"/"<<b<<"="<<c;
         break;
       default:
         cout<<"Enter Valid operator";
       }
         cout<<"\nDo you wan to continue(Y/N):";
         cin>>d;
     }while(d=='Y'||d=='Y');
return 0;
}


    
  
 Output:
prakash@prakash-Vostro-3478:~$ g++ calculator.cpp
prakash@prakash-Vostro-3478:~$ ./a.out
Enter the first number:12
Enter the second number:13
Enter operator +,-,*,/:+
Addition:12+13=25
Do you wan to continue(Y/N):Y
Enter the first number:14
Enter the second number:15
Enter operator +,-,*,/:-
Substraction:14-15=-1
Do you wan to continue(Y/N):N
prakash@prakash-Vostro-3478:~$  



 

Comments

Popular posts from this blog

PPL 1

NEW Unit III STRUCTURING OF PROGRAM MULTIPLE CHOICE QUESTIONS 1. Which of the following is the functionality of ‘Data Abstraction’? (a) Reduce Complexity (b) Binds together code and data (c) Parallelism (d) None of the mentioned Answer : a Explanation : An essential element of Object Oriented Programming is ‘Data Abstraction’ which means hiding things. Complexity is managed through abstraction. 2. Which of the following mechanisms is/are provided by Object Oriented Language to implement Object Oriented Model? (a) Encapsulation (b) Inheritance (c) Polymorphism (d) All of the mentioned Answer : d Explanation : None. 3. Which of these is the functionality of ‘Encapsulation’? (a) Binds together code and data (b) Using single interface for general class of actions. (c) Reduce Complexity (d) All of the mentioned Answer : a Explanation : ‘Encapsulation’ acts as protective wrapper that prevents code and data from being accessed by other code defined outside the...

EEE

EEE Unit-1 DC Machines MCQ's MULTIPLE CHOICE QUESTIONS ON UNIT 1: DC MACHINES Edit 1.The sole purpose of a Commutator in a dc generator is to------- ((A))increase output voltage ((B)) reduce sparking at brushes ((C)) provide smoother output ((D)) convert the induced ac into dc Hide ! Answer : D 2.In a dc generator, the generated emf is directly proportional to the------ ((A)) field current ((B)) pole flux ((C)) number of armature parallel paths ((D)) number of dummy coils Hide ! Answer:B 3.An ideal dc generator has .......... voltage regulation. ((A))low ((B))zero ((C))positive ((D))negative Hide ! Answer:B 4.Which generator may have poorest voltage regulation? ((A))series ((B))shunt((C))compound ((D))high Hide ! Answer:A 5.Voltage equation of a dc motor is----- ((A)) V = Eb + IaRa ((B)) Eb = V + IaRa ((C))V = Eb /IaRa ((D))V = Eb + Ia2Ra Hide ! Answer: A 6.Which of the following motor has the constant speed? ((A))Series motor ((B))Shunt motor ((C))Cumulatively compound motor ((D)...

13.interface

File: /home/student/saiesh/ADS/Assignment No 13/ads13_2A.java package pack; interface ip { public public public public } void void void void add(int sub(int mul(int div(int a, a, a, a, int int int int b); b); b); b); public class A implements ip { public void add(int a, int b) { System.out.println("Addition is\t\t: "+(a+b)); } public void sub(int a,int b) { System.out.println("Substraction is\t\t: "+(a-b)); } public void mul(int a, int b) { System.out.println("Multiplication is\t: "+(a*b)); } public void div(int a, int b) { System.out.println("Division is\t\t: "+(a/b)); } } Page 1 of 1 ....................... File: /home/student/saiesh/ADS/Assignment No 13/ads13_2B.java package mypack; import pack.*; import java.util.Scanner; public class B { public static void main(String arg[]) { A obj= new A(); Scanner sc=new Scanner(System.in); System.out.println("\nEnter first number\t: "); int a=sc.nextInt(); System.out.println("\nEnter ...