Sie sind auf Seite 1von 7

#include<fstream.

h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
fstream f;
class item
{
int itemno;
char iname[100];
int qty;
int price;
public:
void input();
void show();
int retprice()
{ return price;
}
}ob;
void item::input()
{
cout<<"enter itemno"<<endl;
cin>>itemno;
cout<<"enter item name"<<endl;
gets(iname);
cout<<"enter quantity"<<endl;
cin>>qty;
cout<<"enter price"<<endl;
cin>>price;
}
void item::show()
{
cout<<"item no="<<itemno<<endl;
cout<<"item name="<<iname<<endl;
cout<<"quantity="<<qty<<endl;
cout<<"price="<<price<<endl;
}
void create()
{
char ch='y';
f.open("item.dat",ios::out|ios::binary);
while(ch=='y'||ch=='Y')
{
ob.input();
f.write((char*)&ob,sizeof(ob));
cout<<"want to continue(y/n)"<<endl;
cin>>ch;
}
f.close();
}
void search()
{
f.open("item.dat",ios::in|ios::binary);
f.read((char*)&ob,sizeof(ob));
while(!f.eof())
{
if(ob.retprice()>1000)
ob.show();
if(ob.retprice()<1000)
cout<<"price is less than 1000"<<endl;
cout<<"press any key for next record"<<endl;
getch();
f.read((char*)&ob,sizeof(ob));
}
f.close();
}
void display()
{
f.open("item.dat",ios::in|ios::binary);
if(!f)
{
cout<<"file not found";
exit(0);
}
else
{ f.read((char*)&ob,sizeof(ob));
while(!f.eof())
{
ob.show();
cout<<"press any key for next record"<<endl;
getch();
f.read((char*)&ob,sizeof(ob));
}
f.close();
}
}
void main()
{
int ch;
while(1)
{
clrscr();
cout<<"1.create data file"<<endl;
cout<<"2.search items having value>1000 from file"<<endl;
cout<<"3.display all records"<<endl;
cout<<"4.exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>ch;
switch(ch)
{ case 1:create();
break;
case 2:search();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:cout<<"wrong choice"<<endl;
}
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct item
{
int itemno;
float price;
struct item *next;
};
class stack
{
struct item *top;
public:
stack() // constructor
{
top=NULL;
}
void push();
void pop();
void show();
};
// PUSH Operation
void stack::push()
{
struct item *ptr;
ptr=new item;
cout<<"\nPUSH Operationn";
cout<<"Enter a item no.to insert: ";
cin>>ptr->itemno;
cout<<"Enter a price to insert : ";
cin>>ptr->price;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";

// POP Operation
void stack::pop()
{
struct item *temp;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
}
temp=top;
top=top->next;
cout<<"\nPOP Operation........\nPoped itemno is "<<temp->itemno;
delete temp;
}

// Show stack
void stack::show()
{
struct item *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<<ptr1->itemno<<",";
cout<<ptr1->price<<"->";
ptr1=ptr1->next;
}
cout<<"NULL\n";
}

// Main function
int main()
{ clrscr();
stack s;
int choice;
while(1)
{
cout<<"\n---------------------------------------------------------";
cout<<"\n\t\tSTACK USING LINKED LIST\n\n";
cout<<"1:PUSH\n2:POP\n3:DISPLAY STACK\n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4: exit(0);
break;
default:
cout<<"\nPlease enter correct choice(1-4)!!";
break;
}
}
getch();
return(0);
}

Das könnte Ihnen auch gefallen