0% found this document useful (0 votes)
94 views28 pages

My Cs Notes

The document describes a program to implement a dynamically allocated stack containing country names. It defines a node structure with a country name and link, and a stack class with a top pointer. The stack class contains push(), pop(), and display() functions. The main() function creates a stack object and uses a while loop to display a menu and call the stack functions to add and remove countries and display the stack.

Uploaded by

Parth Singhania
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views28 pages

My Cs Notes

The document describes a program to implement a dynamically allocated stack containing country names. It defines a node structure with a country name and link, and a stack class with a top pointer. The stack class contains push(), pop(), and display() functions. The main() function creates a stack object and uses a while loop to display a menu and call the stack functions to add and remove countries and display the stack.

Uploaded by

Parth Singhania
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

38-Program to implement searching in a file that has records

maintained through structure.

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

struct stu{ int rollno;

char name[25];

char Class[4];

float marks;

char grade;

}s1;

void main ()

{ clrscr ();

int rn;

char found='n';

ifstream fin("stu.dat",ios::in);

cout<<"\n\nEnter rollno to be searched for->";

cin>>rn;

while(!fin.eof())

{ fin.read((char *)&s1,sizeof(s1));

if(s1.rollno==rn)

{ cout<<s1.rollno<<",rollno"

<<rn<<"has"<<s1.marks<<" % marks and '"

<<s1.grade<<"' grade."<<endl;

found='y';
break;

if(found =='n')

cout<<"Roll no not found";

fin.close();

getch ();

OUTPUT:-

39- Program to read and write a structure using write() and read()
function using a binary file.

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<string.h>

#include<stdlib.h>
struct customer { char name[50];

float balance;

};

int main ()

{ clrscr ();

customer savic;

strcpy(savic.name,"Tina marshall");

savic.balance=21310.75;

ofstream fout;

fout.open("saving",ios::out|ios::binary);

if(!fout)

{ cout<<"file cant be opened";

getch();

return 1;

fout.write((char*)&savic,sizeof(customer));

fout.close();

ifstream fin;

fin.open("saving",ios::in|ios::binary);

fin.read((char*)&savic,sizeof(customer));

cout<<"\n\n"<<savic.name;

cout<<" has the balance amount of


Rs."<<savic.balance<<"\n";

fin.close();

getch();
return 0;

Output:-
40-Program to delete a record form a data file.

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<stdio.h>

#include<string.h>

class student

{ int roll;

char name[20];

public:

void getdata();

void display();

int getroll()

{ return roll;

};

void student::getdata()

{ cout<<"enter roll no->";

cin>>roll;

cout<<"\n Enter name-> ";

gets(name);

void student::display()

{ cout<<"\n Roll no-> "<<roll;


cout<<"\n Name-> " ;

puts(name);

void main ()

clrscr ();

student s;

ifstream fin("stu.dat",ios::in);

ofstream out( "temp.dat",ios::out);

int rno;

cout<<"\n Enter the roll no whose data is to be deleted-> ";

cin>>rno;

while(fin)

{ fin.read((char*)&s,sizeof(s));

if(s.getroll()==rno)

{ cout<<"\n\n\t\tDATA OF ROLLNO " <<rno<<" HAS BEEN DELETED.";

else

out.write((char*)&s,sizeof(s));

fin.close();

out.close();

remove("stu.dat");

rename("temp.dat","stu.dat");

getch();
}

Output-
41- Program to store TOY data and to display all data stored in file.

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<stdio.h>

class toy

{ int id_no;

char toy_name[40];

float price;

public:

void getdata()

{ cout<<"\n enter id no-> ";

cin>>id_no;

cout<<"\n enter toy name-> ";

gets(toy_name);

cout<<" \n price->" ;

cin>>price;

void showdata()

{cout<<"\n id no=> "<<id_no;

cout<<"\ntoy name=> ";

puts(toy_name);

cout<<"\n price-> "<<price;

};
void main()

{ toy t;

clrscr();

t.getdata();

ofstream outfile ;

outfile.open("toy_info.dat",ios::out|ios::app|ios::binary);

outfile.write((char*)&t,sizeof(t));

outfile.close();

ifstream infile;

infile.open("toy_info.dat",ios::in|ios::binary);

clrscr(); char ch='y';

for(int i=0;!infile.eof();i++)

{ infile.read((char*)&t,sizeof(t));

t.showdata();

cout<<"want to see next file press y->";

cin>>ch;

if(ch!='y')

break;

infile.close();

getch();

}
OUTPUT:-
42- Program to count the no of spaces, uppercase, lowercase from a
file.

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<ctype.h>

void main()

{ clrscr();

ifstream fin("book.dat",ios::in);

char ch;

int countL=0,countU=0,space=0;

while(!fin.eof())

{ fin.get(ch);

if(islower(ch))

countL++;

else

if(isupper(ch))

countU++;

else

if(ch==' ')

space++;

cout<<"\n\tno of upper case letters are-> "<<countU;

cout<<"\n\tno of lower case letters are-> "<<countL;

cout<<"\n\t no of spaces-> "<<space;


fin.close();

}
43- Program for insertion in linked queue

#include<iostream.h>

#include<conio.h>

#include<process.h>

struct node { int info;

node *next;

} *front,*newptr,*save,*ptr,*rare;

node*create_new_node(int);

void insert_end(node*);

void display(node*);

void main()

{ clrscr();

front=rare=NULL;

int inf;

char ch='y';

while

(ch=='y'||ch=='Y')

{ cout<<"\n enter information for the new node-> ";

cin>>inf;

newptr=create_new_node(inf);

if(newptr==NULL)

{ cout<<"\n can not create new node!!";

exit(0);

}
insert_end(newptr);

cout<<"\n Now the queue( Front to rarae) is ->";

display(front);

cout<<"\n\nPress y to enter more nodes-> ";

cin>>ch;

getch();

node*create_new_node(int n)

{ ptr=new node;

ptr->info=n;

ptr->next=NULL;

return ptr;

void insert_end(node*np)

{ if(front==NULL)

front=rare=np;

else

{ rare->next=np;

rare=np;

void display(node*np)

{ while(np!=NULL)

{cout<<np->info<<"-> " ;
np=np->next;

}
OUTPUT:-
44 -Program to search for a given character inside a string and to print
the point of match using pointers.

#include<iostream.h>

#include<conio.h>

char *match(char,char*);

void main()

{ clrscr();

char string[80],*cp,ch;

cout<<"\n Enter a string-> ";

cin.getline(string,80);

cout<<"\n Enter a cheracter to be searched for-> ";

cin>>ch;

cp=NULL;

cp=match(ch,string);

if(*cp)

{ cout<<"\n";

for(cp;*cp!='\0';cp++)

cout<<*cp;

else

cout<<"\n no match found";

getch();

char*match(char c,char*s)
{ while(c!=*s && *s)

s++;

return s;

OUTPUT:-
45-Program for insertion in array queue;

#include<iostream.h>

#include<conio.h>

#include<process.h>

int insert_in_q(int [],int);

void display(int [],int,int);

const int size=50;

int queue[size],front=-1,rare=-1;

void main()

{ int item,res;

char ch='y';

clrscr();

while(ch=='y'||ch=='Y')

{ cout<<"\n Enter item for insertion-> ";

cin>>item;

res=insert_in_q(queue,item);

if(res==-1)

{ cout<<"\n !!!Overflow!!! ";

exit(0);

cout<<"\n Now the queue(front to rare is)-> ";

display(queue,front,rare);

cout<<"\n Want to insert more elements(press y)";

cin>>ch;
}

int insert_in_q(int queue[],int ele)

{ if(rare==size-1)

return -1;

else

if(rare==-1)

{ front=rare=0;

queue[rare]=ele;

else

{ rare++;

queue[rare]=ele;

return 0;

void display(int queue[],int front,int rare)

{ if (front==-1)

return;

for(int i=front;i<rare;i++)

cout<<queue[i]<<"<-";

cout<<queue[rare]<<endl;

}
OUTPUT:-
36-Program to implement dynamically allocated stack containing names
of countries.

#include<iostream.h>

#include<process.h>

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

{ char country[20];

node*link;

};

class stack

{ node *top;

public:

stack()

{ top=NULL;

void push();

void pop();

void display();

};

void stack::push()

{ node *temp =new node;

cout<<"\n enter country name-> " ;

gets(temp->country);
temp->link=top;

top=temp;

void stack ::pop()

{ if(top==NULL)

cout<<"\nUNDERFLOW";

else

{ node *ptr=top;

top=top->link;

delete ptr;

void stack::display()

{ node *temp=top;

while(temp!=NULL)

{ cout<<temp->country<<" -> " ;

temp=temp->link;

void main()

{ clrscr();

stack st;
int ch;

while(1)

{ cout<<"\n\n\t\t MENU";

cout<<"\n 1-Push\n 2-POP \n 3-display";

cout<<"\n 4-EXIT";

cout<<"\n Enter your choice->";

cin>>ch;

switch(ch)

{ case 1: st.push();

break;

case 2: st.pop();

break;

case 3: st.display();

break;

case 4: exit(0);

default: cout<<"\n INVALIDE CHOICE";

getch();

clrscr();

getch();

}
OUTPUT:-

You might also like