Saturday, 12 November 2016

Q)(Tic-Tac-Toe) Write a program that allows two players to play the tic-tac-toe game. Your program must contain the class ticTacToe to implement a ticTacToe object. Include a 3-by-3 two-dimensional array, as a private member variable, to create the board. If needed, include additional member variables. Some of the operations on a ticTacToe object are printing the current board, getting a move, checking if a move is valid, and determining the winner after each move. Add additional operations as needed.

#include <iostream>
#include <iomanip>
using namespace std;

class ticTacToe{
      char b[3][3]; //two dimenstional  array
public:
      ticTacToe(); //constructor
      void board(); //prints the game board
      void Input_p1(int); //takes the input from the user
      bool Checker(); //checks the winner
};
int main()
{
      ticTacToe game; //creating object
      game.board(); //display the game board
      for (int i = 1; i <= 9; i++) //takes the game input from the user
      {
            game.Input_p1(i);
            if (game.Checker())
                  break;
            if (i + 1 == 10)
            {
                  cout << "NO one WUNS\n";
            break;
            }
            i++;
            game.Input_p1(i);
            if (game.Checker())
                  break;
            if (i+1 == 10)
                  cout << "NO one WUNS\n";
      }
      system("pause");
}
ticTacToe::ticTacToe() //constructor to intialize the 3by3 array
{
      b[0][0] = '1';
      b[0][1] = '2';
      b[0][2] = '3';
      b[1][0] = '4';
      b[1][1] = '5';
      b[1][2] = '6';
      b[2][0] = '7';
      b[2][1] = '8';
      b[2][2] = '9';
}
void ticTacToe::board()//function to create  the game board
{
      for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
            {
                  cout << " "<<b[i][j];
                  if (j <= 1)
                        cout << " |";
                  else
                        cout << endl;
                  if (j == 2&&i<2)
                        cout << "-----------" << endl;
            }
}
void ticTacToe::Input_p1(int a) //function that takes input from the user
{
      int n, temp = 0;
      char c;
      if (a == 1 || a == 3 || a == 5 || a == 7 || a == 9)
      {
            cout << "Player 'X' Turn" << endl;
            c = 'X';
      }
      else
      {
            cout << "Player 'O' Turn" << endl;
            c = 'O';
      }
     
      cout << "Enter the Box no :";
      do{ // loop to take the box number AGAIN if user enters in correct number
            if (temp == 1)
                  cout << "  Again Enter the box no:";
            while (!(cin >> n))
            {
                  cin.clear();
                  cin.ignore(900, '\n');
                  cout << "  Invalid!\n  Again Enter the box no :";
            }
            if (n < 0 || n>9)
                  temp = 1;
            switch (n)
            {
            case 1:
                  if (b[0][0] == 'X'||b[0][0]=='O')
                        temp = 1;
                  else
                  {
                        b[0][0] = c;
                        temp = 0;
                  }
                  break;     
     
            case 2:
                  if (b[0][1] == 'X' || b[0][1] == 'O')
                        temp = 1;
                  else
                  {
                        b[0][1] = c;
                        temp = 0;
                  }
                  break;
           
            case 3: 
                  if (b[0][2] == 'X' || b[0][2] == 'O')
                        temp = 1;
                  else
                  {
                        b[0][2] = c;
                        temp = 0;
                  }
                  break;
            case 4:
                  if (b[1][0] == 'X' || b[1][0] == 'O')
                  temp = 1;
                  else
                  {
                        b[1][0] = c;
                        temp = 0;
                  }
                  break;
     
            case 5:
                  if (b[1][1] == 'X' || b[1][1] == 'O')
                        temp = 1;
                  else
                  {
                        b[1][1] = c;
                        temp = 0;
                  }
                  break;
            case 6:
                  if (b[1][2] == 'X' || b[1][2] == 'O')
                        temp = 1;
                  else
                  {
                        b[1][2] = c;
                        temp = 0;
                  }
                  break;
            case 7:
                  if (b[2][0] == 'X' || b[2][0] == 'O')
                        temp = 1;
                  else
                  {
                        b[2][0] = c;
                        temp = 0;
                  }
                  break;
           
            case 8:
                  if (b[2][1] == 'X' || b[2][1] == 'O')
                        temp = 1;
                  else
                  {
                        b[2][1] = c;
                      temp = 0;
                }
                  break;
            case 9:
                  if (b[2][2] == 'X' || b[2][2] == 'O')
                        temp = 1;
                  else
                  {
                        b[2][2] = c;
                        temp = 0;
                  }
                  break;
            }
      }while(temp != 0);
      board();
}

bool ticTacToe::Checker() //checks the winner
{
      if ((b[0][0] == 'X'&& b[0][1] == 'X'&&b[0][2] == 'X') ||
            (b[1][0] == 'X'&& b[1][1] == 'X'&&b[1][2] == 'X') ||
            (b[2][0] == 'X'&& b[2][1] == 'X'&&b[2][2] == 'X') ||
            (b[0][0] == 'X'&& b[1][0] == 'X'&&b[2][0] == 'X') ||
            (b[0][1] == 'X'&& b[1][1] == 'X'&&b[2][1] == 'X') ||
            (b[0][1] == 'X'&& b[1][1] == 'X'&&b[2][1] == 'X') ||
            (b[0][2] == 'X'&& b[1][1] == 'X'&&b[2][0] == 'X')||
            (b[0][0] == 'X'&& b[1][1] == 'X'&&b[2][2] == 'X'))
      {
            cout << "Player 'X' WINS" << endl;
            return 1;
      }
      else if ((b[0][0] == 'O'&& b[0][1] == 'O'&&b[0][2] == 'O') ||
            (b[1][0] == 'O'&& b[1][1] == 'O'&&b[1][2] == 'O') ||
            (b[2][0] == 'O'&& b[2][1] == 'O'&&b[2][2] == 'O') ||
            (b[0][0] == 'O'&& b[1][0] == 'O'&&b[2][0] == 'O') ||
            (b[0][1] == 'O'&& b[1][1] == 'O'&&b[2][1] == 'O') ||
            (b[0][1] == 'O'&& b[1][1] == 'O'&&b[2][1] == 'O') ||
            (b[0][2] == 'O'&& b[1][1] == 'O'&&b[2][0] == 'O') ||
            (b[0][0] == 'O'&& b[1][1] == 'O'&&b[2][2] == 'O'))
            cout << "Player 'O' WINS" << endl;
      else
            return 0;
}

Q)Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class, say, romanType. An object of type romanType should do the following:  Store the number as a Roman numeral.  Convert and store the number into decimal form.  Print the number as a Roman numeral or decimal number as requested by the user. The decimal values of the Roman numerals are:
 Test your program using the following Roman numerals: MCXIV, CCCLIX, MDCLXVI.

#include<iostream>
using namespace std;

class romanType{
      char R[8]; //intialized by the constructor
      int decimal; //stores the decimal number after converting
public:
      romanType(); //constructor
      void R2D(); //declaration of a function to convert roman into decimal
      int get_decimal();//declaration of a function to get decimal number
    bool operator>=(int);//operator overloading
     
};
int main()
{
      romanType ob; //creating the object
      ob.R2D();
      if (ob >= 0)
            cout << "Decimal : " << ob.get_decimal() << endl;
      else
            cout << "Invalid Format of a number" << endl;

}
void romanType::R2D()
{
      char a[1000];
      cout << "Enter the number in roman format(M-D-C-L-X-V-I) :";
      cin >> a;
      int dec,l,n=0;
      l = strlen(a); //cal the length of char string
      for (int i = l-1; i>=0; i--) //cal from roman to decimal format
      {
            a[i] = toupper(a[i]);
            if (a[i] == R[0])
                  dec = 1000;
            else if (a[i] == R[1])
                  dec = 500;
            else if (a[i] == R[2])
                  dec = 100;
            else if (a[i] == R[3])
                  dec = 50;
            else if (a[i] == R[4])
                  dec = 10;
            else if (a[i] == R[5])
                  dec = 5;
            else if (a[i] == R[6])
                  dec = 1;
            else
            {
                  decimal = -1;
                  break;     
            }
            if (n > dec)
                  decimal = (decimal-dec);
            else
                  decimal = decimal + dec;     
            n = dec;
      }
}
int romanType::get_decimal() //function to return the decmial number
{
      return decimal;
}
romanType::romanType()//Constructor
{
      R[0] = 'M';
      R[1] = 'D';
      R[2] = 'C';
      R[3] = 'L';
      R[4] = 'X';
      R[5] = 'V';
      R[6] = 'I';

      decimal = 0;
}
bool romanType::operator>=(int n = 0)
{
      if (decimal >= 0)
            return 1;
      else
            return 0;
}


Q)A common place to buy candy is from a machine. A new candy machine has been purchased for the gym, but it is not working properly. The machine sells candies, chips, gum, and cookies. You have been asked to write a program for this candy machine so that it can be put into operation.
 The program should do the following:  Show the customer the different products sold by the candy machine.  Let the customer make the selection.  Show the customer the cost of the item selected.  Accept money from the customer.  Release the item.

#include<iostream>
using namespace std;

class candy_M {
      string items[4];//intialized by constructor the name of the items
      double price[4]; //intilized by constructor the price of items
      int choice; //stores the choice of item to be selected
      double E_amount; //stores the Amount entered by the user
      double r_amount; //stores the reaming amount if the entered amount is greater than the item price
public:
      candy_M(); //constructor
      void menu(); //declaration of a function  display the menu of items
      void set_Choice(int);  //declaration of a function set the choice entered ny the user
      void set_amount(double); //declaration of a function  set the Amount entered ny the user
      void check_Amount()const; //declaration of a function  check wheather the amont entered is ==  or > or < then the items price
};
int main()
{
      int ch;
      double am;
      candy_M M1; //creating object
      M1.menu(); //display menu
      while (!(cin >> ch) || (ch>4 || ch <= 0)) //takes the choice and checking it if it is valid or not
      {
            cin.clear();
            cin.ignore(900, '\n');
            cout << "  Invalid choice!\nAgain Enter the Choice:";
      }
      M1.set_Choice(ch);
      cout << "Enter the Amount :";
      while (!(cin >> am) || (am <= 0)) //takes the Amount and checking it if it is valid or not
      {
            cin.clear();
            cin.ignore(900, '\n');
            cout << "  Invalid Amount!\nAgain Enter the Amount:";
      }
      M1.set_amount(am);
      M1.check_Amount();

      return 0;

}
void candy_M::menu()//function  display the menu of items
{
      cout << "**************CANDY MACHINE*****************\n";
      cout << "\t 1 for Chips          RS 50.\n";
      cout << "\t 2 for Candies        RS 20.\n";
      cout << "\t 3 for Gum            RS 10.\n";
      cout << "\t 4 for Cookies        RS 40.\n";
      cout << "Enter your Choice :";
}
void candy_M::set_Choice(int ch) //function set the choice entered ny the user
{
      choice = ch - 1;
}
void candy_M::set_amount(double p) //function set the Amount entered ny the user
{
      E_amount = p;
}
void candy_M::check_Amount()const //function  check wheather the amont entered is ==  or > or < then the items price
{
      double am = E_amount - price[choice];
      if (am > 0)
      {
            cout << "Take the Remaining Money\n";
            cout << "You the purchased the item\nTake the item\n";
      }
      else if (am == 0)
            cout << "You the purchased the item\nTake the item";
      else
            cout << "\nYou have entered the Amount less than the price of an item\n";
}
candy_M::candy_M() //Constructor
{
      items[0] = "Chips";
      items[1] = "Candies";
      items[2] = "Gum";
      items[3] = "Cookies";
      price[0] = 50;
      price[1] = 20;
      price[2] = 10;
      price[3] = 40;
}