Thursday, October 13, 2016

CD player simulator using c++

0 comments

Problem

Design a simple CD player simulator. The CD player has the following buttons: "POWER", "EJECT", "PLAY" and "STOP", and a tray where a CD is loaded. User can load a CD only if the tray is open. The tray can be opened only if the power is turned on (regard it as something like a motor-driven tray installed on a PC). If the power is turned off with the tray open, the tray will keep open and the power is turned off.
Implement the following actions of the CD player as methods of class CDPlayer.
  • pushPower() -- simulates pushing POWER(on/off) button.
    • if the power is turned off, turn on the power, and display "Power turned on."
    • if the power is turned on,
      • if playing, stop it.
turn off the power, and display "Power turned off."
  • pushEject() -- simulates pushing EJECT(tray open/close) button
    • if the power is not turned on, display "Power not turned on."
    • if the power is turned on,
      • if the tray is not open, open the tray and display "Tray opened."
      • if the tray is open, close the tray and display "Tray closed."
  • pushPlay() -- simulates pushing PLAY button
    • if the power is turned on,
      • if the media is loaded and tray is closed, begin to play and display "Now playing..."
      • otherwise, display "No CD loaded or tray is open."
  • pushStop() -- simulates pushing STOP button
    • if the power is turned on,
      • if it's playing, stop playing and display "Playing stopped."
  • loadUnloadMedia() -- simulates loading/unloading media to/from the tray (tray must be open when loading/unloading)
    • if the tray is open,
      • if the media is loaded, unload the media and display "Media unloaded."
      • if the media is not loaded, load the media and display "Media loaded."
    • if the tray is not open,
      • display "Tray is not open."

Code

#include<iostream>
#include<conio.h>
using namespace std;


class CDplayer
{
      private:
              int power;
              int eject;
              int tray;
              int media;
              int play;
              
      public:
      CDplayer():power(0),eject(0),tray(0),media(0),play(0)
      {
      }
      
      void pushPower()
      {
           cout<<"\n\nNOTE: Press one(1) to turn on and zero(0) to turn off.";
           cout<<"\nWant to turn on the power? ";
           cin>>power;
           
           if(power == 1)
                cout<<"Power is ON.";
           else if (power == 0)
                cout<<"Power is OFF.";
           else
               cout<<"Invalid key";
           
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void pushEject()
      {
           if(power == 1)
           {
                if(tray == 0)
                {
                         tray = 1;
                         cout<<"Tray opened.";
                }
                else if(tray == 1)
                {
                     tray = 0;
                     cout<<"Tray closed.";
                }
           }
           else if(power == 0)
                cout<<"Power not turned on.";
           
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void pushPlay() 
      {
           if(power == 1)
           {
                if(media == 1 && tray == 0)
                {
                    play = 1;
                    cout<<"Now playing...";
                }
                else
                    cout<<"No CD loaded or tray is open.";
           }
           else if(power == 0)
                cout<<"Power not turned on.";
                
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void pushStop() 
      {
           if(power == 1)
           {
                if(play == 1)
                {
                        play = 0;
                        cout<<"Playing stopped.";
                }
           }
           cout<<"\n\nPress any key to continue.";
           getch();
      }
      
      void loadUnloadMedia()
      {
           if(tray == 1)
           {
                   if(media == 1)
                   {
                            media = 0;
                            cout<<"Media unloaded.";
                   }
                   else if(media == 0)
                   {
                        media = 1;
                        cout<<"Media loaded.";
                   }
           }
           else if(tray == 0)
                cout<<"Tray is not open.";
           
           cout<<"\n\nPress any key to continue.";
           getch();
      }
};

int main()
{
    CDplayer c1;
    int s;
    while(1)
    {
            system ("cls");
            cout<<"\n\nPlease, Press one of the following buttons: ";
            cout<<"\n1. POWER(on/off)";
            cout<<"\n2. EJECT(tray open/close)";
            cout<<"\n3. PLAY";
            cout<<"\n4. STOP";
            cout<<"\n5. Load/Unload media to/from the tray(tray will be opened)";
            cout<<"\n6. Close CDplayer";
            
            cout<<"\n\nEnter your choice: ";
            cin>>s;
            
            switch(s)
            {
                     case 1:
                          system ("cls");
                          c1.pushPower();
                          break;
            
                     case 2:
                          system ("cls");
                          c1.pushEject();
                          break;
            
                     case 3:
                          system ("cls");
                          c1.pushPlay();
                          break;
            
                     case 4:
                          system ("cls");
                          c1.pushStop();
                          break;
            
                     case 5:
                          system ("cls");
                          c1.loadUnloadMedia();
                          break;
            
                     case 6:
                          exit(0);
            }
    }
    
    getch();
    return 0;
}

Output

CD player simulator using c++
CD player simulator using c++

Read More..

Tuesday, September 9, 2014

Library management using structure

8 comments

Problem

Write a menu driven program that depicts the working of a library. The menu options should be:
  1. Add book information
  2. Display book information
  3. List all books of given author
  4. List the title of specified book
  5. List the count of books in the library
  6. List the books in the order of accession number
  7. Exit
Create a structure called library to hold accession number, title of the book, author name, price of the book, and flag indicating whether book is issued or not.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct library
{
 char bk_name[30];
 char arthur[30];
 int pages;
 float price;
};

int main()
{
 struct library l[3];
 char ar_nm,bk_nm;
 int i,j;

 printf ("1. Add book information\n2. Display book information\n3. List all books of given author\n4. List the title of specified book\n5. List the count of books in the library\n6. Exit");

 printf ("\n\nEnter one of the above : ");
 scanf("%d",&j);

 switch (j)
 {
  case 1:
   for (i=0;i<3;i++)
   {

    printf ("Enter book name = ");
    scanf ("%s",&l[i].bk_name);

    printf ("Enter arthur name = ");
    scanf ("%s",&l[i].arthur);

    printf ("Enter pages = ");
    scanf ("%d",&l[i].pages);

    printf ("Enter price = ");
    scanf ("%f",&l[i].price);
   }
   break;
  case 2:
   printf("you have entered the following information\n");
   for(i=0;i<3;i++)
   {
    printf ("book name = %s",l[i].bk_name);

    printf ("arthur name = %s",l[i].arthur);

    printf ("pages = %d",l[i].pages);

    printf ("price = %f",l[i].price);
   }
   break;

  case 3:
   printf ("Enter arthur name : ");
   scanf ("%s",&ar_nm);
   for (i=0;i<3;i++)
   {
    if (ar_nm == l[i].arthur)
     printf ("%s %s %d %f",l[i].bk_name,l[i].arthur,l[i].pages,l[i].price);
   }
   break;

  case 4:
   printf ("Enter book name : ");
   scanf ("%s",&bk_nm);
   for (i=0;i<3;i++)
   {
    if (bk_nm == l[i].bk_name)
     printf ("%s %s %d %f",l[i].bk_name,l[i].arthur,l[i].pages,l[i].price);
   }
   break;

  case 5:
   for (i=0;i<3;i++)
   {

   }
   break;
  case 6:
   exit (0);


 }
 return 0;
}

Sample Output

library working using struct

Read More..

Thursday, September 4, 2014

create a menu driven program to add, subtract, multiply, and divide two number using functions and switch case statement

1 comments

Problem

Write a menu driven program using switch statement that has the following options:
  1. Add numbers
  2. Subtract numbers
  3. Multiply numbers
  4. Divide numbers
  5. Exit
The prototype of the function for add, subtract, multiply and divide is as follows:
int add(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int add (int, int);
int sub (int, int);
int mul (int, int);
int divi (int, int);

int main()
{
 int i,x,y,c;
 printf("Enter two numbers : ");
 scanf ("%d %d",&x,&y);
 printf("1. addition\n");
 printf("2. subtraction\n");
 printf("3. multiplication\n");
 printf("4. division\n");
 printf("5. exit\n\n");
 printf("Which action you want to perform : ");
 scanf ("%d",&i);
 switch(i)
 {
  case 1:
   c=add(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 2:
   c=sub(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 3:
   c=mul(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 4:
   c=divi(x,y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 5 :
   exit(0);
   break;
  default :
   printf("Enter valid number\n");
   system("pause");
 }
}
int add(int x, int y)
{
 int c;
 c = (x+y);
 return c ;
}
int sub(int x, int y)
{
 int c;
 c = (x-y);
 printf("Answer = %d\n",c);
}
int mul(int x, int y)
{
 int c;
 c = (x*y);
 printf("Answer = %d\n",c);
}
int divi(int x, int y)
{
 int c;
 c = (x/y);
 printf("Answer = %d\n",c);
}

Sample Output

switch case statement

Read More..

write a menu driven program to calculate factorial of a number, prime or not, and even or odd using functions

0 comments

Problem

Write the program using functions. There are three functions fact (), prime() and even_odd() that contains the logic for calculating factorial of a number, determine that entered number is prime or not and determine that entered number is even or odd, respectively. Prototypes of the three functions are given below:
void fact(void);
void prime(void);
void even_odd(void);

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void fact(void);
void prime(void);
void even_odd(void);
main()
{
 int c;

 printf("Select one :\n 1. factorial.\n2. prime nmber:\n3. even or odd:\n4. exit.\n Enter choice:");
 scanf("%d",&c);

 switch(c)
 {
  case 1:
   fact();
   break;
  case 2:
   prime();
   break;
  case 3:
   even_odd();
   break;
  case 4:
   exit(0);
  default:
      printf("enter correct choise");
 }
 getch();
}

void fact (void)
{
 int n,b,i;
 printf("enter the first no.:");
 scanf("%d",&n);
 b=1;
 for(i=n;i>0;i--)
  b=b*i;
 printf("factorial is %d",b);
}
void prime(void)
{
 int j;
 printf("enter the first no.:");
 scanf("%d",&j);
 if(j%2==0)
 {
  printf("it is not a prime numbr.");  }
 else
  printf("it is a prime number.");
}
void even_odd(void)
{
 int g;
 printf("enter the first no.:");
 scanf("%d",&g);
 if(g%2==0)
  printf("it is an even number.");

 else
  printf("not an even number");
}

Sample Output


Read More..

Write a menu driven program using switch case statement which calculates factorial of a number, prime or not, even or odd, and power

2 comments

Problem

Write a menu driven program using switch case statement (without functions) that has the following options:
  1. Factorial of a number
  2. Prime or not
  3. Odd or even
  4. Power ( x raise to power y)
  5. Exit
Input should be taken inside the particular case. For example if the user enters choice is 4. Then in case 4 your program can prompt the user to input value of x and then value of y. The switch case statement and the menu (mentioned above) should be in while(1) loop. The only way to end the program is that the user has to selection choice 5. In case 5 you can simply call a function exit (0);

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
 int c,n,b,i,j,g;
 printf("Select one :\n 1. factorial.\n2. prime nmber:\n3. even or odd:\n4. exit.\n Enter choice:");
 scanf("%d",&c);
 switch(c)
 {
  case 1:
   printf("enter no.:");
   scanf("%d",&n);
   b=1;
   for(i=n;i>0;i--)
    b=b*i;
   printf("factorial is %d",b);
   system("pause");
   break;
  case 2 :
   printf("Enter no.:");
   scanf("%d",&j);
   if(j%2==0)
   {
    printf("it is not a prime numbr.\n");
   }
   else
    printf("it is a prime number.\n");
   system("pause");
   break;
  case 3 :
   printf("enter the :");
   scanf("%d",&g);
   if(g%2==0)
    printf("it is an even number.\n");
   else
    printf("not an even number\n");
   system("pause");
   break;
  case 4 :
   exit(0);
 }
 system("pause");
}

Sample Output


Read More..

Write a menu driven program using switch statement which add, subtract, multiply, and divide

0 comments

Problem

Write a menu driven program using switch statement (without functions) that has the following options:
  1. Add numbers
  2. Subtract numbers
  3. Multiply numbers
  4. Divide numbers
  5. Exit
The switch case statement and the menu (mentioned above) should be in while(1) loop. The only way to end the program is that the user has to selection choice 5. In case 5 you can simply call a function exit (0);

Solution - Code

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
 int i,x,y,c;
 printf("Enter two numbers : ");
 scanf("%d %d",&x,&y);
 printf("1. addition\n");
 printf("2. subtraction\n");
 printf("3. multiplication\n");
 printf("4. division\n");
 printf("5. exit\n\n");
 printf("Which action you want to perform : ");
 scanf("%d",&i);
 switch(i)
 {
  case 1:
   c = (x+y);
   printf("Answer = %d\n",c);
   break;
  case 2:
   c = (x-y);
   printf("Answer = %d\n",c);
   break;
  case 3:
   c = (x*y);
   printf("Answer = %d\n",c);
   break;
  case 4:
   c = (x/y);
   printf("Answer = %d\n",c);
   break;
  case 5 :
   exit(0);
  default :
   printf("Enter valid number\n");
 }
 system("pause");
 return 0;
}

Sample Output

switch statement in c language

Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact