حبيبة الكل
14-03-2004, 01:02 AM
أتمنى أنكم تستفيدوا من أول مساهمة لي
للي فاتته المحاضرات الأولى
المحاضرة الأولي : كانت بس مراجعة..
file contain 10 students records
the program read , record students records according to thier grades and rewrite the records in another sequence file
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>
struct student
{
char name[10];
double grade;
};
void main()
{ int i;
student x[10];
ifstream f1("input.dat");
if(!f1)
{
cerr<<"error.....";
exit(1);
}
for(i=0;i<3;i++)
{
f1>>x[i].name>>x[i].grade;
cout <<x[i].name<<" " <<x[i].grade << endl;
getch();
}
student tmp;
for( i=0;i<2;i++)
for(int j=0;j<2;j++)
if(x[j].grade>x[j+1].grade)
{
tmp=x[j];
x[j]=x[j+1];
x[j+1]=tmp;
}
ofstream f2("output.dat",ios::out);
for (i=0;i<3;i++)
f2<<x[i].name<<" "<<x[i].grade<<endl;
}
exercise 2 suppose that you have struct employee write a program to read 10 employees's information from the user
and save this information in random access file
#include<fstream.h>
#include<stdlib.h>
struct employee
{
char name[10];
int salary;
};
void main()
{
employee x;
ofstream f1("randomf.dat",ios::binary);
if (!f1)
{
cerr<<"Errorrrrrrrrrrrr";
exit(1);
}
for(int i=1;i<=3;i++)
{
cout<<"Enter the employee name:";
cin>>x.name;
cout<<"Enter the salary:";
cin>>x.salary;
f1.seekp((i-1)*sizeof(employee));
f1.write(reinterpret_cast<const char* >(&x),sizeof(employee));
}
}
lab2
create a class employee with friend function ( annual_inc),
and member functions
setData () To modify data
dispaly() to dispaly information
#include<iostream.h>
class employee
{
friend void annual_inc(employee &);
public:
employee();
employee(char*,float,int);
void setData(char*,float,int);
void display();
private:
char *name;
float salary;
int experience;
};
void annual_inc(employee &e)
{
if (e.experience>10)
{
e.salary+=1000;
}
}
employee::employee()
{
name='\0';
salary=0.0;
experience=0;
}
employee::employee(char* Na,float Sa,int Ex)
{
name=Na;
salary=Sa;
experience=Ex;
}
void employee::setData(char * Nam,float Sal,int Exp)
{
name=Nam;
salary=Sal;
experience=Exp;
}
void employee::display()
{
if(experience>10)
{
cout<<endl<<"*-*-*-*-*-*-*-*-*-*-*-*-*";
cout<<endl<<"This Employees' Salary has been increased by QR 1000";
cout<<endl<<"*-*-*-*-*-*-*-*-*-*-*-*-*";
}
else
{
cout<<endl<<"*-*-*-*-*-*-*-*-*-*-*-*-*";
cout<<endl<<"This Employees dose not have any Change in the Salary";
cout<<endl<<"*-*-*-*-*-*-*-*-*-*-*-*-*";
}
cout<<endl<<"Employee Name : "<<name;
cout<<endl<<"Employee Salary : "<<salary;
cout<<endl<<"Employee Experience : "<<experience<<endl;
}
void main()
{
employee x;
x.setData("ali",12000,9);
annual_inc(x);
employee y("ahmed",20000,15);
annual_inc(y);
x.display();
y.display();
}
will be continued
أتمنى اني اكون افدت أي شخص
راسلوني أنا منتظرة رسايلكم...
للي فاتته المحاضرات الأولى
المحاضرة الأولي : كانت بس مراجعة..
file contain 10 students records
the program read , record students records according to thier grades and rewrite the records in another sequence file
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>
struct student
{
char name[10];
double grade;
};
void main()
{ int i;
student x[10];
ifstream f1("input.dat");
if(!f1)
{
cerr<<"error.....";
exit(1);
}
for(i=0;i<3;i++)
{
f1>>x[i].name>>x[i].grade;
cout <<x[i].name<<" " <<x[i].grade << endl;
getch();
}
student tmp;
for( i=0;i<2;i++)
for(int j=0;j<2;j++)
if(x[j].grade>x[j+1].grade)
{
tmp=x[j];
x[j]=x[j+1];
x[j+1]=tmp;
}
ofstream f2("output.dat",ios::out);
for (i=0;i<3;i++)
f2<<x[i].name<<" "<<x[i].grade<<endl;
}
exercise 2 suppose that you have struct employee write a program to read 10 employees's information from the user
and save this information in random access file
#include<fstream.h>
#include<stdlib.h>
struct employee
{
char name[10];
int salary;
};
void main()
{
employee x;
ofstream f1("randomf.dat",ios::binary);
if (!f1)
{
cerr<<"Errorrrrrrrrrrrr";
exit(1);
}
for(int i=1;i<=3;i++)
{
cout<<"Enter the employee name:";
cin>>x.name;
cout<<"Enter the salary:";
cin>>x.salary;
f1.seekp((i-1)*sizeof(employee));
f1.write(reinterpret_cast<const char* >(&x),sizeof(employee));
}
}
lab2
create a class employee with friend function ( annual_inc),
and member functions
setData () To modify data
dispaly() to dispaly information
#include<iostream.h>
class employee
{
friend void annual_inc(employee &);
public:
employee();
employee(char*,float,int);
void setData(char*,float,int);
void display();
private:
char *name;
float salary;
int experience;
};
void annual_inc(employee &e)
{
if (e.experience>10)
{
e.salary+=1000;
}
}
employee::employee()
{
name='\0';
salary=0.0;
experience=0;
}
employee::employee(char* Na,float Sa,int Ex)
{
name=Na;
salary=Sa;
experience=Ex;
}
void employee::setData(char * Nam,float Sal,int Exp)
{
name=Nam;
salary=Sal;
experience=Exp;
}
void employee::display()
{
if(experience>10)
{
cout<<endl<<"*-*-*-*-*-*-*-*-*-*-*-*-*";
cout<<endl<<"This Employees' Salary has been increased by QR 1000";
cout<<endl<<"*-*-*-*-*-*-*-*-*-*-*-*-*";
}
else
{
cout<<endl<<"*-*-*-*-*-*-*-*-*-*-*-*-*";
cout<<endl<<"This Employees dose not have any Change in the Salary";
cout<<endl<<"*-*-*-*-*-*-*-*-*-*-*-*-*";
}
cout<<endl<<"Employee Name : "<<name;
cout<<endl<<"Employee Salary : "<<salary;
cout<<endl<<"Employee Experience : "<<experience<<endl;
}
void main()
{
employee x;
x.setData("ali",12000,9);
annual_inc(x);
employee y("ahmed",20000,15);
annual_inc(y);
x.display();
y.display();
}
will be continued
أتمنى اني اكون افدت أي شخص
راسلوني أنا منتظرة رسايلكم...