PDA

عرض الاصدار بالكامل : لكل اللي ماخد data dtructure العملي مع lab instructor محمد حسن ... يدخل!!!


حبيبة الكل
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

أتمنى اني اكون افدت أي شخص
راسلوني أنا منتظرة رسايلكم...

حـ قطرة بر
14-03-2004, 01:30 AM
هلا والله حبيبتي

أكيد راح نستفيد من مشركاتج


وشيء جميل أنج تفيدينا بكل المحاضرات يا عسل


بس المعمل الأول لنزلته أنا

http://www.qataru.com/vb/showthread.php?threadid=2884


أم بقية المعامل فراح أتركها لج



يالله عاد .....


لا تتأخرين في تنزيل المعمل الثالث

لأنه كان الأسبوع ألي طاف


وأنا وحده راح علي المعمل

لأن الدكس تبعي خترب وما شتغل

يعني راح أعتمد عليج


وبالتوفيق يالغالية

حبيبة الكل
14-03-2004, 06:54 PM
من عيوني يا قطرة بحر .. أتمنى أنك تستفيدي



/*LAB 3 Singly Linked List

The Question
if you have the following classes:

class node{
friend class list;
private:
char info[10];
node * next;
public:

node(char s[10], node *ptr)
{
strcpy(info,s);
next=ptr;
}
};

class list{
private:
node *head, *tail;
public:
list()
{
head=tail=0;
}

void addToHead(char []);
void addToTail(char []);
void deleteFromHead();
void deleteFromTail();
void print();
};

1) write the missing function bodies
2) add the following functions to the list class
* ListLength() return an integer represents the number of the node in the list
* Find(char s[10]) return the number of the first node that contain string in its INFO field.
if there is no such node, it returns -1
*insert(char s[10], int i) insert the string s in the position i of the list, or print an error
if there is no such position

*/

And the answers is:

#include<string.h>
#include<iostream.h>
class node{
friend class list;
private:
char info[10];
node *next;
public:
node(char s[],node *ptr )
{
//info=s;
strcpy(info,s);
next=ptr;
}
};

class list{
private:
node *head,*tail;
public:
list()
{
head=tail=0;
}
void addToHead(char []);
void addToTail(char []);
void deletFromHead();
void deletFromTail();
int listlength();
int find(char []);
void insert(char s[],int );
void print();
};

void list::addToHead(char s[10])
{
node *tmp=new node(s,head);
head=tmp;
if(tail==0)
tail=head;
}

void list::addToTail(char s[10])
{
node *tmp=new node(s,0);
if(tail==0)
{

head=tail=tmp;
}
else
{
tail->next=tmp;
tail=tmp;
}

}
void list::print()
{
if(head==0)
{
cout<<" ------------------------"<<endl;
cout<<" The list is empty"<<endl;
cout<<" -------------------------"<<endl;
}
else
{
cout<<"__________________________________________________ ______"<<endl;
for(node *i=head;i!=0;i=i->next)
cout<<i->info<<"->";
cout<<endl;
cout<<"NO of node: "<<listlength()<<endl;
cout<<"__________________________________________________ _______"<<endl;

}

}
void list::deletFromHead()
{
node *tmp=head;
if(head==tail)
head=tail=0;
else
head=head->next;
delete tmp;
}
void list::deletFromTail()
{
node * tmp;
if(head==tail)
{
tmp=head;
delete tmp;
head=tail=0;
}
else
for(tmp=head;tmp->next!=tail;tmp=tmp->next);
delete tail;
tail=tmp;
tail->next=0;
}
int list::listlength()
{
int i=0;
for(node *tmp=head;tmp!=0;i++,tmp=tmp->next);
return i;
}

int list::find(char s[10])
{
int i=1;
node *tmp=head;
while( tmp!=0)
{
if (strcmp(tmp->info,s)==0)
return i;
else
{
i++; tmp=tmp->next;
}

}
return -1;

}
void list::insert(char s[10],int p)
{
//int i;
node *tmp;
if(p==1)
{
tmp=new node(s,head);
head=tmp;
}
else
{
tmp=head;
int i=1;
while(i<p &&tmp!=0)
{
tmp=tmp->next;
i++ ;
}
if(tmp==0)
cout<<" No such position";
else
{
node *tmp2=new node(s,tmp->next);
tmp->next=tmp2;
}
}
}

void main()
{


list l;
int i=0;
char ss[10];

while(i!=6)
{
cout<<" Choose :"<<endl;
cout<<"1- To add element to the head of the list"<<endl;
cout<<"2- To add element to the tail of the list"<<endl;
cout<<"3- To delete an element from the head of the list"<<endl;
cout<<"4- To delete an element from the tail of the list"<<endl;
cout<<"5- To print the elements of the list"<<endl;
cout<<"6- To exit"<<endl;
cin>>i;
switch (i){
case 1:
cout<<"Enter a string";
cin>>ss;
l.addToHead(ss);
break;
case 2:
cout<<"Enter a string";
cin>>ss;
l.addToTail(ss);
break;
case 3:
l.deletFromHead();
break;
case 4:
l.deletFromTail();
break;
case 5:
l.print();
break;


}
}
/* l.addToHead("saleh");
l.addToHead("ali");
l.insert("ahmed",2);
// cout<<l.find("saleha");
l.print();*/



}


Lab 4 Doubly linked list
/* Exercise 1:
This is a modified class of simple Linked list classes(we had last lab) to implement doubly linked list:

class node{
friend class Doubly LinkedList;
private:
int info;
node *next, *prev;

public:
node(int s, node *n, *p)
{
info=s;
next=n;
prev=p;
}
};

class DoublyLinkedList{
private:
node *head,*tail;

public:
DoublyLinkedList()
{
head=tail=0;
}
void addToHead(int);
void addToTail(int);
void deleteFromHead();
void deleteFromTail();
void printFromTail();
};

1) write the missing function bodies
2) Assume we have ordered doubly Linked List of integers, write the function
insert(int) to insert integer number in the appropriate position
*/

//The answer is
#include<string.h>
#include<iostream.h>
class node{
friend class DoublyLinkedList;
private:
int info;
node *next,*prev;
public:
node(int i,node *n,node *p )
{
info=i;
next=n;
prev=p;
}
};

class DoublyLinkedList{
private:
node *head,*tail;
public:
DoublyLinkedList()
{
head=tail=0;
}
void addToHead(int);
void addToTail(int);
void deletFromHead();
void deletFromTail();
void printFromHead();
void printFromTail();
};

void DoublyLinkedList::addToHead(int s)
{
node *tmp=new node(s,head,0);
if (tail!=0)
{ head->prev=tmp;
head=tmp;
}
else
tail=head=tmp;;
}

void DoublyLinkedList::addToTail(int s)
{
node *tmp=new node(s,0,tail);
if(tail==0)
{

head=tail=tmp;
}
else
{
tail->next=tmp;
tail=tmp;
}

}
void DoublyLinkedList::printFromHead()
{
for(node * i=head;i!=0;i=i->next)
cout<<i->info<<endl;
}
void DoublyLinkedList::printFromTail()
{
for(node * i=tail;i!=0;i=i->prev)
cout<<i->info<<endl;
}

void DoublyLinkedList::deletFromHead()
{
node *tmp=head;
if(head==tail)
head=tail=0;
else
{
head=head->next;
head->prev=0;
}
delete tmp;
}
void DoublyLinkedList::deletFromTail()
{
node * tmp;
if(head==tail)
{
tmp=head;
delete tmp;
head=tail=0;
}
else
{
tmp=tail;
tail->prev->next=0;
tail=tail->prev;
delete tmp;
}
}

void main()
{


DoublyLinkedList l;
l.addToTail(10);
l.addToTail(11);
l.addToHead(12);
l.printFromHead();
l.printFromTail();


}

حـ قطرة بر
14-03-2004, 08:32 PM
مشكورة حبيبتي



والله يعطيج العافية يا قمر

حبيبة الكل
16-03-2004, 11:49 PM
قطرة بحر... أنت ما خدا data structure أي lab يمكن أحنا مع بعض و ما نعرف بعض