Monday, 21 April 2014

SOCCER

// A Program On Soccer and score of the players
// A Program to understand OOPS and File Handling


#include<conio.h>
#include<fstream.h>
#include<string.h>

class footballer{

protected:

char id[10],name[30];
int per;

public:

footballer(){per =100;}
void getid()
{
 cout<<"\n\n\ninput footballer id\t ";
 cin>>id;
 cout<<"\n\n\ninput fottballer name\t";
 cin>>name;
};


};

class fileinfoot:public footballer{

    public:
    void fil()
    {
     fstream x("FOOTBALL.txt",ios::out|ios::app);
     x.width(30);
     x<<endl;
     x<<name;
     x.width(20);
     x<<id;
     x.close();

    }

} ;
class goalkeeper: public fileinfoot{
   int save;
   int loss;
   public:
  void performance()
  {
   getid();
   cout<<"\n\n\nHow many goal he saved?\t";
   cin>>save;
   save=save*25;
   cout<<"\n\n\nHow many goal he got\t";
   cin>>loss;
   loss=loss*75;
   per=per+save-loss;
   if(per<0)
   per=0;
   file();
}
void file();


};

      void  goalkeeper::file()
{
 char fil[20];
 strcpy(fil,id);
 strcat(fil,".txt");
 ofstream x(fil);
 x<<"NAME:-"<<name<<endl;
 x<<"ID:-"<<id<<endl;
 x<<"POSITION- GOALKEEPER\n";
 x<<"Performance point:-"<<per<<endl;
 x.close();
      fstream y("GOALKEEP.txt",ios::out|ios::app);
     y<<endl;
     y.width(30);
     y<<name;
     y.width(20);
     y<<id;
     y.close();


}

class defender: public fileinfoot{
   int defend;
   int loss;
   public:
  void performance()
  {
   getid();
   cout<<"\n\n\nHow many defend?\t";
   cin>>defend;
   defend=defend*25;
   cout<<"\n\n\nHow many missdefence\t";
   cin>>loss;
   loss=loss*5;
   per=per+defend-loss;
   if(per<0)
   per=0;

   file();
  }
  void file();

};
 void defender::file()
{
 char fil[20];
 strcpy(fil,id);
 strcat(fil,".txt");
 ofstream x(fil);
 x<<"NAME:-"<<name<<endl;
 x<<"ID:-"<<id<<endl;
 x<<"POSITION- DEFENDER\n";
 x<<"Performance point:-"<<per<<endl;
 x.close();
      fstream y("DEFENDER.txt",ios::out|ios::app);
     y<<endl;
     y.width(30);
     y<<name;
     y.width(20);
     y<<id;
     y.close();
}

class midfielder:public fileinfoot{
    int passing;
   int mispass;
   public:
  void performance()
  {getid();
   cout<<"\n\n\nHow many pass?\t";
   cin>>passing;
   passing=passing*25;
   cout<<"\n\n\nHow many miss pass\t";
   cin>>mispass;
   mispass=mispass*75;
   per=per+passing-mispass;
   if(per<0)
   per=0;
   file();
  }
   void file();

};
 void midfielder:: file()
{
 char fil[20];
 strcpy(fil,id);
 strcat(fil,".txt");
 ofstream x(fil);
 x<<"NAME:-"<<name<<endl;
 x<<"ID:-"<<id<<endl;
 x<<"POSITION- MIDFIELDER\n";
 x<<"Performance point:-"<<per<<endl;
 x.close();
  fstream y("MIDFIELD.txt",ios::out|ios::app);
     y<<endl;
     y.width(30);
     y<<name;
     y.width(20);
     y<<id;
     y.close();



}

class striker: public fileinfoot{
   int goal;
   int loss;
public:
  void performance()
  {getid();
   cout<<"\n\n\nHow many goal?\t";
   cin>>goal;
   goal=goal*25;
   cout<<"\n\n\nHow many goal he miss\t";
   cin>>loss;
   loss=loss*75;
   per=per+goal-loss;
   if(per<0)
   per=0;
   file();
  }
   void file();

};
 void striker:: file()
{
 char fil[20];
 strcpy(fil,id);
 strcat(fil,".txt");
 ofstream x(fil);
 x<<"NAME:-"<<name<<endl;
 x<<"ID:-"<<id<<endl;
 x<<"POSITION- STRIKER\n";
 x<<"Performance point:-"<<per<<endl;
 x.close();
 fstream y("STRIKER.txt",ios::out|ios::app);
     y<<endl;
     y.width(30);
     y<<name;
     y.width(20);
     y<<id;
     y.close();
}



class add{
public:
add();
};
     add::add(){
 goalkeeper g;
 defender d;
 striker s;
 midfielder m;
 int co;
 while(1)
 {clrscr();
 cout<<"\n\n\n1\tgoalkeeper\n\n\n2\tdefender\n\n\n"
     <<"3\tstriker\n\n\n4\tmidfielder\n\n\n5\texit\n\n";
 cin>>co;
 if(co==5)
 break;
 if(co==1)
 {
g.performance();
g.fil();
 }
  if(co==2)
 { d.performance();
  d.fil();
  }
  if(co==3)
  {
  s.performance();
  s.fil();
  }
  if(co==4)
  {
  m.performance();
  m.fil();
 }
 }
}




class search{

public:
void find();
} ;
void search::find(){
   int i;
   char id[20],c;
   while(1)
   {
    clrscr();
   cout<<"\n\n\n1\tALL FOOTBALLER\n\n\n2\tALL GOALKEEPER\n\n\n3\tALL DEFENDER";
   cout<<"\n\n\n4\tALL MIDFIELDER\n\n\n5\tALL STRIKER\n\n\n6\tSEARCH BY ID\n\n\n7\tExit\n\n";
   cin>>i;
   if(i==7)
     break;            
   if(i==1)
   {
   ifstream x("FOOTBALL.txt");
   cout<<"FOOTBALLERS NAME AND ID \n\n";
   while(x)
    { x.get(c);
      cout<<c;
     }
     getch();
     x.close();
 }
 if(i==2)
 {ifstream x("GOALKEEP.txt");
  cout<<"FOOTBALLERS NAME AND ID \n\n";
  while(x)
  { x.get(c);
    cout<<c;
  }
  getch();
  x.close();
 }
 if(i==3)
 {ifstream x("DEFENDER.txt");
  cout<<"DEFENERS NAME AND ID \n\n";
  while(x)
  { x.get(c);
    cout<<c;
  }
  getch();
  x.close();
 }
 if(i==4)
 {ifstream x("MIDFIELD.txt");
  cout<<"MIDFIELDERS NAME AND ID \n\n";
  while(x)
  { x.get(c);
    cout<<c;
  }
  getch();
  x.close();
 }
 if(i==5)
 {ifstream x("STRIKER.txt");
  cout<<"STRIKER NAME AND ID \n\n";
  while(x)
  { x.get(c);
    cout<<c;
  }
  getch();
  x.close();
 }
 if(i==6)
 {clrscr();
  cout<<"input the id";
  cin>>id;
  char fil[20];
 strcpy(fil,id);
  strcat(fil,".txt");
  ifstream x(fil);
   while(x)
  { x.get(c);
    cout<<c;
  }
  getch();
  x.close();
 }
 }

}


int main()
{
  int c;
 clrscr();

 while(1)
 {
  clrscr();
  cout<<"\n\n\n1\tadd\n\n"<<"\n\n\n2\tsearch\n\n"<<"\n\n\n3\texit\n\n";
  cin>>c;
  if(c==3)
     break;
  else if(c==1)
     {
      add a;
     }
  else if(c==2)
     {
     search f;
     f.find();
     }
  else
 { cout<<"wrong choice";
   getch();
   }
 }


 return 0;
}

             // OUTPUT SCREENS









Wednesday, 16 April 2014

MOBILE BILLING SYSTEM

// An Intermediate Program on mobile billing system

// A Program to understand the concept of Linked List(Stack and Queue)


# include <iostream.h>
# include <conio.h>
# include <stdio.h>
# include <alloc.h>
# include <string.h>
# include <stdlib.h>

struct node
{
int num;
char name[15];
struct node *next;
};


struct node *list; //global start pointer
FILE *fp; // Global file pointer

struct node *getrecord()
{
struct node *temp,e;
temp=(struct node*)malloc(sizeof(struct node));

printf("Reached till here...");
getch();

fflush(stdin);
// fscanf(fp,"%d %s",&temp->num,temp->name);
fread(&temp,sizeof(temp),1,fp);
printf("%d %s",temp->num,temp->name);

// temp->num=e.num;
// strcpy(temp->name,e.name);
getch();
temp->next = NULL;
return temp;
}

struct node *getnode()
{
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));

printf("Enter the person's phone number--->");
scanf("%d",&temp->num);

printf("Enter the person's Name--->");
scanf("%s",&temp->name);

temp->next = NULL;
return temp;
}

struct node *search(int id,int *flag)
{
struct node *cur,*prev;
*flag=0;

if
(list==NULL)
return NULL;

for(prev=NULL,cur=list; (cur); prev=cur,cur=cur->next)
if(cur->num == id)
{
*flag = 1;
break;
}


return prev;
}

int insert(struct node *new1)
{
struct node *prev;
int flag;

prev = search(new1->num,&flag);

if(list==NULL)
{
list = new1; //first node in list
return 0;
}

if(flag==1)
return -1;
else
{
new1->next = prev->next;
prev->next = new1; // insert in the middle or end
}

return 0;
}

void display()
{
struct node *cur;
auto int n=0;

if(list==NULL)
{
printf("List empty !");
return;
}

printf("The phone records are--->");
cur=list;
for(cur=list;(cur);cur=cur->next)
{
printf("Record no. %d--->",n+1);
printf("Record phone no.:%d",cur->num);
printf("person Name:%s",cur->name);
n++;
}

}

int delnode(int id)
{
struct node *prev,*temp;
int flag=0;

if(list==NULL)
return -1;

prev=search(id,&flag);

if(flag==0)
return -1;

if(prev==NULL)
{
temp=list;
list=list->next;
free(temp);
}

else
{
temp=prev->next;
prev->next=temp->next;
free(temp);
}
return 0;
}

struct node *query(int id,char *flag,int toggle)
{
struct node *cur,*prev;
int x=0;

if(list==NULL)
return NULL;

if(toggle)
{
for(cur=list; (cur); cur=cur->next)
if(cur->num == id)
{
x=1;
break;
}
}
else
{
for(cur=list; (cur); cur=cur->next)
if(!strcmp(cur->name,flag))
{
x=1;
break;
}
}

if(x==0)
{
printf("The number doesnt exist !");
return NULL;
}

return cur;
}

void CopyToFile()
{
FILE *fp;
struct node *cur;
fp=fopen("phone.txt","w");

cur=list;
if(cur==NULL)
{
printf("The list is empty, nothing to write back...");
return;
}
for(cur=list;(cur);cur=cur->next)
fprintf(fp,"%d %s",cur->num,cur->name); //writing back to file
fclose(fp);
}

void main()
{
int ch=0,ans=0;
int val=0;
char str[15];
struct node *new1,*new2;


clrscr();

/* initialization of list with the existing records */

fp=fopen("phone.txt","r");

if(fp==NULL)
{
printf("File cant be opened !");
getch();
exit(1);
}

while(!feof(fp))
{
new1=getrecord();

if(insert(new1)==-1)
printf("Cant insert record ! Error...");
else
printf("Record entered...");
}
getch();
fclose(fp);

do
{
clrscr();
printf("Menu");
printf("1. Add a New phone record.");
printf("2. Delete an existing Record.");
printf("3. show all Records.");
printf("4. Modify a particular Record.");
printf("5. Exit.");
printf("Enter your choice--->");
scanf("%d",&ch);

switch(ch)
{
case 1:

new1 = getnode();
val = insert(new1);
if(val==-1)
printf("Employee id already exists ! try again...");
else
printf("Employee details successfully stored");
break;

case 2:
printf("Enter the phone no.you wish to delete--->");
scanf("%d",&val);
ans=delnode(val);

if(ans==-1)
printf("Record doesnt exist ! Try again...");
if(ans==0)
printf("Record deleted !");
break;

case 3:
display();
break;

case 4:
printf("Search by phone no. or Name ? (1/2)--->");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter the phone no. you wish to search for--->");
scanf("%d",&val);
new2=query(val,str,1);
}
else
{
printf("Enter the name of the person you wish to search for--->");
scanf("%s",&str);
new2=query(val,str,0);
}
if(new2)
{
printf("Enter the New name of the person--->");
scanf("%s",&new2->name);
printf("Enter new phone no. of the person--->");
scanf("%d",&new2->num);
printf("Record modified successfully !");
}
break;
case 5:
printf("Leaving Database,writing back to file...");
CopyToFile();
getch();
free(list);
exit(1);
break;
}
getch();
}while(1);

}

                     //OUTPUT SCREENS











Thursday, 10 April 2014

TYPING SPEED

// A Program for the expert C++ programmers.

// A Program to check your typing speed.

#include  <stdio.h>
#include  <dos.h>
#include  <conio.h>
#define   ESC 0x1b
#define   BSPACE 0x08

const unsigned long far * const dosTime = (const unsigned long far * const)MK_FP( 0x40, 0x6C );

class Timer
{

public:

    Timer();
    void start();
    void stop();
    void reset();
    int status();
    double time();
    static double resolution();
private:

    static unsigned adjust;
    static unsigned calibrate();
    int running;
    struct TIME
         {
         unsigned long dosCount;
         unsigned timerCount;
         };
    TIME startTime;
    double time_;
};

inline double Timer::time()
{
    return time_/1.E6;
}

inline double Timer::resolution()
{
    return 839/1.E9;
}

unsigned Timer::adjust = calibrate();

Timer::Timer() : time_(0), running(0)
{
}

void Timer::start()
{
    if( !running )
         {
         outportb( 0x43, 0x34 );
         asm jmp __1;
    __1:
         outportb( 0x40, 0 );
         asm jmp __2;
    __2:
         outportb( 0x40, 0 );
         startTime.dosCount = *dosTime;
         startTime.timerCount = 0;
         running = 1;
         }
}

void Timer::stop()
{
    outportb( 0x43, 0 );
    unsigned char temp = inportb( 0x40 );

    TIME stopTime;
    stopTime.timerCount = (inportb( 0x40 ) << 8) + temp;
    stopTime.dosCount = *dosTime;

    TIME elapsedTime;
    elapsedTime.dosCount = stopTime.dosCount - startTime.dosCount;
    elapsedTime.timerCount = -( stopTime.timerCount - adjust );

    const double fudge = 83810.0/100000.0;
    time_ += ((elapsedTime.dosCount << 16) +
elapsedTime.timerCount)*fudge;

    running = 0;

}

void Timer::reset()
{
    time_ = 0;
    if( running )
         start();
}

unsigned Timer::calibrate()
{
    adjust = 0;
    unsigned long sum = 0;
    Timer w;
    for( int i = 0; i < 100; i++ )
         {
         w.start();
         w.stop();
         sum += w.time();
         w.reset();
         }
    return (unsigned)((sum+5)/100);
}


void main()
{
 clrscr();
 Timer t;
 char text[1000];
 int i = 0, space_count = 0, letter_count = 0;
 float duration;
 printf("PROGRAM TO TEST TYPING SPEED\n");
 printf("Hit any key to start timer..\n");
 if(getch())
   {
    printf("Your time has started. Start typing. Hit Esc when done.\n");
    t.start();
   }
 while(1)
 {
   text[i] = getche();
   letter_count++;
   if(text[i] == ' ')
      space_count++;
   if(text[i] == ' ')
      printf("");
   if(text[i] == BSPACE)
      printf(" "); // to erase previous character instead of cursoring over
   if(text[i] == ESC)
    {
      printf(" ");
      // to eliminate a special character that is printed for Esc
      // A Backspace followed by Space erases previous character.
      break;
    }
 }

 t.stop();
 duration = t.time();
 printf("\nYour typing speed is :");
 printf("\n%6.2f  characters per minute",60*letter_count/duration);
 printf("\n%6.2f  words per minute (Actual)",60*space_count/duration);
 printf("\n%6.2f  words per minute (Average)",60*letter_count/duration/5);

 getch();
}

          // OUTPUT SCREENS




Sunday, 6 April 2014

HANGMAN

// A Program On Guessing Alphabets which will form a Country's name(Hangman Game)


#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

const int MAXLENGTH=80;
const int MAX_TRIES=5;
const int MAXROW=7;

int letterFill (char, char[], char[]);                        // function declaration
void initUnknown (char[], char[]);

int main ()
{
char unknown [MAXLENGTH];
char letter;
int num_of_wrong_guesses=0;
char word[MAXLENGTH];
char words[][MAXLENGTH] =
{
"india",
"pakistan",
"nepal",
"malaysia",
"philippines",
"australia",
"iran",
"ethiopia",
"oman",
"indonesia"
};

//choose and copy a word from array of words randomly
randomize();
int n=random(10);
strcpy(word,words[n]);

// Initialize the secret word with the * character.
initUnknown(word, unknown);

// welcome the user
cout << "\n\nWelcome to hangman...Guess a country Name";
cout << "\n\nEach letter is represented by a star.";
cout << "\n\nYou have to type only one letter in one try";
cout << "\n\nYou have " << MAX_TRIES << " tries to try and guess the word.";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Loop until the guesses are used up
while (num_of_wrong_guesses < MAX_TRIES)
{
cout << "\n\n" << unknown;
cout << "\n\nGuess a letter: ";
cin >> letter;
// Fill secret word with letter if the guess is correct,
// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown)==0)
{
cout << endl << "Whoops! That letter isn't in there!" << endl;
num_of_wrong_guesses++;
}
else
{
cout << endl << "You found a letter! Isn't that exciting!" << endl;
}
// Tell user how many guesses has left.
cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
cout << " guesses left." << endl;
// Check if they guessed the word.
if (strcmp(word, unknown) == 0)
{
cout << word << endl;
cout << "Yeah! You got it!";
break;
}

}
if(num_of_wrong_guesses == MAX_TRIES)
{
cout << "\nSorry, you lose...you've been hanged." << endl;
cout << "The word was : " << word << endl;
}
getch();
return 0;
}

/* Take a one character guess and the secret word, and fill in the
 unfinished guessword. Returns number of characters matched.
 Also, returns zero if the character is already guessed. */

int letterFill (char guess, char secretword[], char guessword[])                      // function definition
{
int i;
int matches=0;
for (i = 0; secretword[i]!='\0'; i++)
{

// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;

// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}


// Initialize the unknown word

void initUnknown (char word[], char unknown[])                                 // function definition
{
int i;
int length = strlen(word);
for (i = 0; i < length; i++)
unknown[i]='*';
unknown[i]='\0';
}

                   // Output Screens