Write a program to perform the complex arithmetic #include<iostream.h> #include<stdio.h> #include<conio.h> #include<process.h> class complex { int real; float image; public: void getdata() { cout<<"\n enter the real part of the complex"; cin>>real; cout<<"\n enter the imaginary part of the complex"; cin>>image; } void operator + (complex); void operator - (complex); }; void complex :: operator + (complex c1) { complex temp; temp.real=real+c1.real; temp.image=image+c1.image; if (temp.image>=0); { cout<<"\n complex no. after addition:"; cout<<temp.real<<"+"<<temp.image<<"i"; } else { cout<<"\n complex no. after addition "; cout<<temp.real<<temp.image<<"i"; } } void complex ::operator-(complex c1) { complex temp; temp.real = real-c1.image; temp.image= image-c1.image; if (temp.image>=0) { cout<<"\n complex no. after subtraction"; cout<<"\n temp.real<<"+"<<temp.image<<"i"; } else { cout<<"\n complex no. after subtraction"; cout<<temp.real<<temp.image<<"i" } } void main() { clrscr(); comp.ex c1, c2; int n; do { cout<<"\n 1. Input data for complex no. "; cout<<"\n 2. Addition of complex no. "; cout<<"\n 3. Subtraction of complex no. "; cout<<"\n 4. Quit"; cout<<"\n Enter your choice"; cin>>n; switch(n) { case1: cout<<endl<<"\n Enter the data for First Complex No......"; cl.getdata(); cout<<endl<<"\n Enter the data for seconds Complex No....."; c2.getdata(); clrscr(); break; case 2; cl+c2; getch(); clrscr(); break; case 3: cl-c2; getch(); clrscr(); brak; case 4: exit91); break; } } while (n!=4); getch(); } write a program to perform the rational number arithmetic in c++ #include<stdio.h> #include<iostream.h> #include<conio.h> class rational { int numer; int denom; public: void getdata() { cout<<"\n enter the numerator part of the rational no."; cin>>numer; cout<<"\n enter the denominator part of the rational no."; cin>>denom; } void operator+(rational); void operator-(rational); void operator *(rational); void operator /(rational); }; void rational ::operator+(rational c1) { rational temp; temp.numer=(numer*c1.denom)+(c1.numer*denom); temp.denom=denom*c1.denom; cout<<"\nrational no. after addition"; cout<<"\n numerator="<<temp.numer<<"\n denominator ="<<temp.denom; } void raional ::operator -(rational c1) { rational temp; temp.numer=(numer*c1.denom)-(c1.numer*denom); temp.denom=denom*c1.denom; cout<<"\n rational no. after subtraction"; cout<<"\n numerator="<<temp.numer<,"\n denominator ="<<temp.denom; } void rational ::operator (rational c1) { rational temp; temp.numer=numer*c1.numer; temp.denom=denom*c1.denom; cout<<"\n rational no. after multiplication"; cout <<"\n numerator="<temp.numer<<"\n denominator ="<< temp.denom; } void rational :: operator /(rational c1) { rational temp; temp.numer= numer*c1.denom; temp.denom=c1.numer*denom; cout<<"\n rational no. after dividation"; cout <<"\n numerator="<<temp.numer<<"\n denominator ="<<temp.denom; } void main() { clrscr(); rational c1, c2; int n; do { cout<<"\n 1.Input data for rational no. "; cout<<"\n 2. Addition of rational no. "; cout<<"\n 3. Subtraction of rational no. "; cout<<"\n 4. Multiplication of rational no."; cout<<\n 5. Division of rational no. "; cout<<"\n 6. Quit"; cout<<"\n Enter your choice"; cin>>n; switch(n) { case 1: cout<<endl<<"\n enter the data for first rational no."; c1.getdata(); cout<<endl<<"\n enter the data for second rational no. "; c2.getdata (); clrscr(); break; case 2; c1+c2; getch(); clrscr(); break; case 3; c1-c2; getch(); clrscr(); case 4: c1*c2; getch(); clrscr(); break; case 5: c1/c2; getch(); clrscr(); break; case 6: exit(1); break; } } while (n!=6); getch(); } Code for Program to find matrix addition, subtraction, multiplication, transpose and symmetric operations in C++ Programming #include<iostream.h> #include<conio.h> template<class T> class matrix { T x[3][3]; public: void getmatrix(); void showmatrix(); void addition(matrix<T>); void subtraction(matrix<T>); void multiplication(matrix<T>,matrix<T>); }; template<class T> void matrix<T>::getmatrix() { int i,j; cout<<"\n\n\t enter values of matrix"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>x[i][j]; } } } template<class T> void matrix<T>::showmatrix() { int i,j; cout<<"\n\n\t matrix is:"; for(i=0;i<3;i++) { cout<<"\n\n"; for(j=0;j<3;j++) { cout<<"\t"<<x[i][j]; } } } template<class T> void matrix<T>::addition(matrix<T> b) { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { x[i][j]=x[i][j]+b.x[i][j]; } } } template<class T> void matrix<T>::subtraction(matrix<T> b) { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { x[i][j]=x[i][j]-b.x[i][j]; } } } template<class T> void matrix<T>::multiplication(matrix<T> b,matrix<T> a) { int i,j,k; for(i=0;i<3;i++) { for(j=0;j<3;j++) { x[i][j]=0; } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { for(k=0;k<3;k++) { x[i][j]=x[i][j]+(a.x[i][k]*b.x[k][j]); } } } } void main() { int ch; matrix<int> a1,b1,c1; matrix<float> a2,b2,c2; clrscr(); do { cout<<"\n\n\t 1> Addition (int)"; cout<<"\n\n\t 2> Subtraction (int)"; cout<<"\n\n\t 3> Multiplication (int)"; cout<<"\n\n\t 4> Addition (float)"; cout<<"\n\n\t 5> Subtraction (float)"; cout<<"\n\n\t 6> Multiplication (float)"; cout<<"\n\n\t enter your choice"; cin>>ch; switch(ch) { case 1:a1.getmatrix(); a1.showmatrix(); b1.getmatrix(); b1.showmatrix(); a1.addition(b1); a1.showmatrix(); break; case 2:a1.getmatrix(); a1.showmatrix(); b1.getmatrix(); b1.showmatrix(); a1.subtraction(b1); a1.showmatrix(); break; case 3:a1.getmatrix(); a1.showmatrix(); b1.getmatrix(); b1.showmatrix(); c1.multiplication(b1,a1); c1.showmatrix(); break; case 4:a2.getmatrix(); a2.showmatrix(); b2.getmatrix(); b2.showmatrix(); a2.addition(b2); a2.showmatrix(); break; case 5:a2.getmatrix(); a2.showmatrix(); b2.getmatrix(); b2.showmatrix(); a2.subtraction(b2); a2.showmatrix(); break; case 6:a2.getmatrix(); a2.showmatrix(); b2.getmatrix(); b2.showmatrix(); c2.multiplication(b2,a2); c2.showmatrix(); break; } }while(ch!=7); getch(); } /* OUTPUT 1> Addition (int) 2> Subtraction (int) 3> Multiplication (int) 4> Addition (float) 5> Subtraction (float) 6> Multiplication (float) enter your choice1 enter values of matrix 1 2 3 4 5 6 7 8 9 matrix is: 1 2 3 4 5 6 7 8 9 enter values of matrix 2 3 4 5 6 7 8 9 0 matrix is: 2 3 5 6 8 9 matrix is: 3 5 9 11 15 17 4 7 0 7 13 9 1> Addition (int) 2> Subtraction (int) 3> Multiplication (int) 4> Addition (float) 5> Subtraction (float) 6> Multiplication (float) enter your choice 2 enter values of matrix 0 9 8 7 6 5 4 3 2 matrix is: 0 7 4 9 6 3 8 5 2 enter values of matrix 1 2 3 4 5 6 7 8 9 matrix is: 1 2 3 4 5 6 7 8 9 matrix is: -1 7 3 1 -3 -5 5 -1 -7 1> Addition (int) 2> Subtraction (int) 3> Multiplication (int) 4> Addition (float) 5> Subtraction (float) 6> Multiplication (float) enter your choice 3 enter values of matrix 1 2 3 4 5 6 7 8 9 matrix is: 1 2 3 4 5 6 7 8 9 enter values of matrix 2 3 4 5 6 7 8 9 0 matrix is: 2 3 4 5 6 7 8 9 0 matrix is: 36 42 18 81 96 51 126 150 84 1> Addition (int) 2> Subtraction (int) 3> Multiplication (int) 4> Addition (float) 5> Subtraction (float) 6> Multiplication (float) enter your choice 4 enter values of matrix 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 matrix is: 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 enter values of matrix 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9 9.0 matrix is: 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9 9 matrix is: 2.7 4.8 6.9 9 11.1 13.2 15.3 17.4 18.5 1> Addition (int) 2> Subtraction (int) 3> Multiplication (int) 4> Addition (float) 5> Subtraction (float) 6> Multiplication (float) enter your choice 5 enter values of matrix 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9 9.0 matrix is: 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9 9 enter values of matrix 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 matrix is: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 matrix is: 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.09 -0.9 1> Addition (int) 2> Subtraction (int) 3> Multiplication (int) 4> Addition (float) 5> Subtraction (float) 6> Multiplication (float) enter your choice 6 enter values of matrix 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 matrix is: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 enter values of matrix 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9 9.0 matrix is: 1.2 2.3 3.4 4.5 5.6 6.7 7.8 8.9 9 matrix is: 36.959999 81.510002 126.059998 44.219997 99.659996 155.099991 1> Addition (int) 2> Subtraction (int) 48.18 111.20999 174.23999 3> Multiplication (int) 4> Addition (float) 5> Subtraction (float) 6> Multiplication (float) enter your choice 7 */ C++ morse code converter #include <iostream> #include <cstring> using namespace std; void engconvert (char[50]); int main() { char englstring[50]; cout <<"Enter the English text which you would like to be converted to Morse Code: \n"; cin >> englstring; engconvert(englstring); system ("pause"); return 0; } void engconvert (char english[]) { int englishstring2; englishstring2 = strlen (english); cout << englishstring2 << endl; cout << "Here is the morse code translation:\n"; for (int i = 0; i<englishstring2; i++) { if (english[i] == ' ') cout << endl; else if (english[i] == ',') cout << "--..--" << endl; else if (english[i] == '.') cout << ".-.-.-" << endl; else if (english[i] == '?') cout << "..--.." << endl; else if (english[i] == '0') cout << "-----" << endl; else if (english[i] == '1') cout << ".----" << endl; else if (english[i] == '2') cout << "..---" << endl; else if (english[i] == '3') cout << "...--" << endl; else if (english[i] == '4') cout << "....-" << endl; else if (english[i] == '5') cout << "....." << endl; else if (english[i] == '6') cout << "-...." << endl; else if (english[i] == '7') cout << "--..." << endl; else if (english[i] == '8') cout << "---.." << endl; else if (english[i] == '9') cout << "----." << endl; else if (english[i] == 'A' || english[i] == 'a') cout << ".-" << endl; else if (english[i] == 'B' || english[i] == 'b') cout << "-..." << endl; else if (english[i] == 'C' || english[i] == 'c') cout << "-.-." << endl; else if (english[i] == 'D' || english[i] == 'd') cout << "-.." << endl; else if (english[i] == 'E' || english[i] == 'e') cout << "." << endl; else if (english[i] == 'F' || english[i] == 'f') cout << "..-." << endl; else if (english[i] == 'G' || english[i] == 'g') cout << "--." << endl; else if (english[i] == 'H' || english[i] == 'h') cout << "...." << endl; else if (english[i] == 'I' || english[i] == 'i') cout << ".." << endl; else if (english[i] == 'J' || english[i] == 'j') cout << ".---" << endl; else if (english[i] == 'K' || english[i] == 'k') cout << "-.-" << endl; else if (english[i] == 'L' || english[i] == 'l') cout << ".-.." << endl; else if (english[i] == 'M' || english[i] == 'm') cout << "--" << endl; else if (english[i] == 'N' || english[i] == 'n') cout << "-." << endl; else if (english[i] == 'O' || english[i] == 'o') cout << "---" << endl; else if (english[i] == 'P' || english[i] == 'p') cout << ".--." << endl; else if (english[i] == 'Q' || english[i] == 'q') cout << "--.-" << endl; else if (english[i] == 'R' || english[i] == 'r') cout << ".-." << endl; else if (english[i] == 'S' || english[i] == 's') cout << "..." << endl; else if (english[i] == 'T' || english[i] == 't') cout << "-" << endl; else if (english[i] == 'U' || english[i] == 'u') cout << "..-" << endl; else if (english[i] == 'V' || english[i] == 'v') cout << "...-" << endl; else if (english[i] == 'W' || english[i] == 'w') cout << ".--" << endl; else if (english[i] == 'X' || english[i] == 'x') cout << "-..-" << endl; else if (english[i] == 'Y' || english[i] == 'y') cout << "-.--" << endl; else if (english[i] == 'Z' || english[i] == 'z') cout << "--.." << endl; else { cout << "You have not entered a character in the american"; cout << " alphabet. Please re-run the program and try again." << endl; } } cout <<endl; } C++ Program - Tower Of Hanoi Implementation of Tower Of Hanoi Problem using C++ #include<iostream.h> #include<conio.h> void towers(int,char,char,char); void main() { int n; //Declare the variables to be used clrscr(); //Get the input for number of disks cout<<"enter the no of disks : "; cin>>n; towers(n,'A','C','B'); //Call the function getch(); } void towers(int n,char from,char to,char aux) { if(n==1) // If there is only one disk { cout<<endl<<"move 1 from peg "<<from<<" to "<<to; return; } towers(n-1,from,aux,to); //Recursive Call cout<<endl<<"move "<<n<<" from peg "<<from<<" to "<<to; towers(n-1,aux,to,from); } C++ Program to find GCD and LCM of two Given Numbers #include<iostream.h> #include<conio.h> void main() { int x,y,a,b,t,gcd,lcm; clrscr(); cout<<"Enter value of a and b:\n"; cin>>a>>" ">>b; x=a; y=b; while(b!=0) { t=b; b=a%b; a=t; gcd=a; lcm=(x*y)/gcd; } cout<<"\ngcd is:"<<gcd<<"\nlcm is:"<<lcm; getch(); } Program in Java To implement spell checker using dictionary public class SpellChecker { public static void main(String[] args) { SET<String> dictionary = new SET<String>(); // read in dictionary of words In dict = new In(args[0]); while (!dict.isEmpty()) { String word = dict.readString(); dictionary.add(word); } System.out.println("Done reading dictionary"); // read strings from standard input and print out if not in dictionary System.out.println("Enter words, and I'll print out the misspelled ones"); while (!StdIn.isEmpty()) { String word = StdIn.readString(); if (!dictionary.contains(word)) System.out.println(word); } } } To implement a color selector from a given set of colors in java import java.awt*; import java.awt.event.*; import java.applet.*; public class fillcolor extends Applet implement Item Listener { checkbox red,yellow,black,blue.orange; CheckboxGroup cbg; String msg; String s1="red"; String s2="yellow"; String s3="black"; String s4="orange"; public void init() { cbg = new CheckboxGroup(); red = new Chechbox("red",cbg,true); yellow= new Checkbox("yellow",cbg,false); black = new checkbox("black",cbg,false); blue = new Checkbox("blue",cbg,false); orange = new checkbox("orange".cbg.false); add(red); add(yellow); add(black); add(blue); add(orange); red.addItemListener(this); yellow.addItemListener(this); black.addItemListener(this); blue.addItemListener(this); orange.addItemListener(this); } publice void itemStartechanged(ItemEvent ie) { repaint(); } publice void paint (Graphics g) { msg = cbg.getSelectedCheckbox().getLabel(); if(msg.compareTo(s1)==0) { setBackground(Color.red); } else if (msg.compareTo(s2)==0) { setBackground(Color.yellow); } else if(msg.compare To(s3)==0) { setBackground(color.black); } else if (msg.compareTo(s4)==o) { setBackground(Color.blue); } else { set Background(Color.orange); } } } To implement a shape selector from a given set of shapes in java import java.awt.*; import java.awt.event.*; import java.applet.*; public class shape extend Applet implements itemListener { Chekbox reet,circle,line; ChekboxGroup cbg; String msg; String s1="reet"; String s2="circle"; string s3="line"; publice void init() { cbg=new checkboxGroup(); rect = new checkbox("reet,cbg,tru); circle = new Checkbox("circle",cbg.false); line = new Checkbox("line",cbg,false); add(reet); add(circle); add(line); rect.addItemListener(this); circle.addItemLisener(this); line.addItemListener(this); } public void item StateChanged(ItemEvent ie) { repaint(); } publice void paint(Graphics g) { msg=cbg.getSlectedCheckbox().getLabel(); if(masg.compareTo(s1)==0) { g.drawRect(10,40,40,80); } else if(msg.compareTo(s2)==0) { g.drawOvel(10,40,80,80); } else { g.drawLine(0,0,100,100); } } } To implement a calculator with its functionality // calc-ui-model/Calc.java -- Fred Swartz // Level : Intermediate. // Structure : Three files: main, GUI (subclass of JFrame), logic. // Components: JButton, JTextField (right justified). // Containers: JFrame, several JPanels. // Layouts : BorderLayout to put the other panels together. // Two GridLayout panels for the buttons. // Listeners : One ActionListener which is shared by all // numeric key buttons. Similarly share // an ActionListener for all operator buttons. // ActionListener for Clear button. // Other : Use Font to enlarge font for components. // : try...catch for NumberFormatExceptions. // Possible enhancements: // Check for zero before division. // Additional operations: mod, square root, sign change, ... // Make this work with doubles, BigInteger, or ... // Format double results with DecimalFormat // Add keyboard listener. // Change to RPN (Reverse Polish Notation) /** calc-ui-model/CalcGUI.java - A GUI for the calculator. * @author Fred Swartz * @version 2004-04-20 Rodenbach, 2007-02-11 minor changes. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; ///////////////////////////////////////////////////////////////////// class Calc class Calc extends JFrame { //================================================================ constants private static final Font BIGGER_FONT = new Font("monspaced", Font.PLAIN, 20); //=================================================================== fields //... Component referenced during execution private JTextField _displayField; // display result / input. //... Variables representing state of the calculator private boolean _startNumber = true; // true: num key next private String _previousOp = "="; // previous operation private CalcLogic _logic = new CalcLogic(); // The internal calculator. //============================================================== method main public static void main(String[] args) { //... Set the Look and Feel to that of system we're running on. try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception unused) { ; // Ignore exception because we can't do anything. Will use default. } //... Create the window. Calc window = new Calc(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } //============================================================== constructor public Calc() { //... Set attributes of the display field _displayField = new JTextField("0", 12); _displayField.setHorizontalAlignment(JTextField.RIGHT); _displayField.setFont(BIGGER_FONT); //... Create and set attributes of clear button JButton clearButton = new JButton("Clear"); clearButton.setFont(BIGGER_FONT); clearButton.addActionListener(new ClearListener()); //... Use one listener for all numeric keys. ActionListener numListener = new NumListener(); //... Layout numeric keys in a grid. Generate the buttons // in a loop from the chars in a string. String buttonOrder = "789456123 0 "; JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(5, 3, 2, 2)); for (int i = 0; i < buttonOrder.length(); i++) { String keyTop = buttonOrder.substring(i, i+1); JButton b = new JButton(keyTop); if (keyTop.equals(" ")) { //... Put a dummy button in this position. b.setEnabled(false); } else { //... Put a digit button in the interface. b.addActionListener(numListener); b.setFont(BIGGER_FONT); } buttonPanel.add(b); } //... One ActionListener to use for all operator buttons. ActionListener opListener = new OpListener(); //... Create panel with gridlayout to hold operator buttons. // Use array of button names to create buttons in a loop. JPanel opPanel = new JPanel(); opPanel.setLayout(new GridLayout(5, 1, 2, 2)); String[] opOrder = {"+", "-", "*", "/", "="}; for (int i = 0; i < opOrder.length; i++) { JButton b = new JButton(opOrder[i]); b.addActionListener(opListener); b.setFont(BIGGER_FONT); opPanel.add(b); } //... Put Clear button in flow layout to keep from expanding. JPanel clearPanel = new JPanel(); clearPanel.setLayout(new FlowLayout()); clearPanel.add(clearButton); //... Layout the top-level content panel. JPanel content = new JPanel(); content.setLayout(new BorderLayout(5, 5)); content.add(_displayField, BorderLayout.NORTH ); content.add(buttonPanel , BorderLayout.CENTER); content.add(opPanel , BorderLayout.EAST ); content.add(clearPanel , BorderLayout.SOUTH ); content.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //... Finish building the window (JFrame) this.setContentPane(content); this.pack(); this.setTitle("Simple Calc"); this.setResizable(false); this.setLocationRelativeTo(null); }//end constructor //============================================================== actionClear /** Called by Clear btn action listener and elsewhere.*/ private void actionClear() { _startNumber = true; // Expecting number, not op. _displayField.setText("0"); _previousOp = "="; _logic.setTotal("0"); } //////////////////////////////////////////// inner listener class OpListener /** Listener for all op buttons. */ class OpListener implements ActionListener { public void actionPerformed(ActionEvent e) { // The calculator is always in one of two states. // 1. A number must be entered -- an operator is wrong. // 2. An operator must be entered. if (_startNumber) { // Error: needed number, not operator //... In this state we're expecting a number, but got an operator. actionClear(); _displayField.setText("ERROR - No operator"); } else { //... We're expecting an operator. _startNumber = true; // Next thing must be a number try { // Get value from display field, convert, do prev op // If this is the first op, _previousOp will be =. String displayText = _displayField.getText(); if (_previousOp.equals("=")) { _logic.setTotal(displayText); } else if (_previousOp.equals("+")) { _logic.add(displayText); } else if (_previousOp.equals("-")) { _logic.subtract(displayText); } else if (_previousOp.equals("*")) { _logic.multiply(displayText); } else if (_previousOp.equals("/")) { _logic.divide(displayText); } _displayField.setText("" + _logic.getTotalString()); } catch (NumberFormatException ex) { actionClear(); _displayField.setText("Error"); } //... set _previousOp for the next operator. _previousOp = e.getActionCommand(); }//endif _startNumber }//endmethod }//end class //////////////////////////////////// inner listener class ClearListener /** Action listener for numeric keys */ class NumListener implements ActionListener { public void actionPerformed(ActionEvent e) { String digit = e.getActionCommand(); // Get text from button if (_startNumber) { //... This is the first digit, clear field and set _displayField.setText(digit); _startNumber = false; } else { //... Add this digit to the end of the display field _displayField.setText(_displayField.getText() + digit); } } } //////////////////////////////////// inner listener class ClearListener class ClearListener implements ActionListener { public void actionPerformed(ActionEvent e) { actionClear(); } }