1 Bài tập chương 4,5 Tue Jun 11, 2013 4:32 pm
Message reputation : 100% (1 vote)
#include <iostream.h>
#define MAX 6
void push(int stack[],int &top,int value)
{
if(top<MAX) //con nhap duoc
{
top++;
stack[top]=value;
}
else
{
cout<<"Error";
}
}
void pop(int stack[],int &top,int &value)
{
if(top>=0) //con gia tri
{
value=stack[top];
top--;
}
else
{
cout<<"Error";
}
}
void menu(int stack[],int &top)
{
int chon;
int value;
cout<<"\n1.Push Item\n";
cout<<"2.Pop Item\n";
cout<<"3.Exit\n";
cout<<" #Lua chon: ";
cin>>chon;
switch(chon)
{
case 1:
cout<<"\nValue: ";
cin>>value;
push(stack,top,value);
break;
case 2:
pop(stack,top,value);
cout<<"\nValue:"<<value<<endl;
break;
case 3:exit(0);
default:
cout<<"Khong co muc ban chon";
}
}
int main()
{
int stack[MAX];
int top=-1;
while(true)
{
menu(stack,top);
}
}
#include <iostream.h>
#define MAX 6
void Enqueue(int queue[],int &top,int &count,int value)
{
if(count<MAX) //con nhap duoc
{
top=(top+1)%MAX;
queue[top]=value;
count++;
}
else
{
cout<<"Error";
}
}
void Dequeue(int queue[],int top,int &bottom,int &count,int &value)
{
if(count!=0) //con gia tri
{
bottom=(bottom+1)%MAX;
value=queue[bottom];
count--;
}
else
{
cout<<"Error";
}
}
void menu(int queue[],int &top,int &bottom,int &count)
{
int chon;
int value;
cout<<"\n1.Enqueue Item\n";
cout<<"2.Dequeue Item\n";
cout<<"3.Exit\n";
cout<<"Chon: ";
cin>>chon;
switch(chon)
{
case 1:
cout<<"\nValue: ";
cin>>value;
Enqueue(queue,top,count,value);
break;
case 2:
Dequeue(queue,top,bottom,count,value);
cout<<"\nValue="<<value<<endl;
break;
case 3:exit(0);
default:
cout<<"Khong co";
}
}
int main()
{
int queue[MAX];
int count=0,bottom,top;
top=bottom=-1;
while(true)
menu(queue,top,bottom,count);
}
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
void nhap(int a[],int n)
{
srand(time(NULL));
for(int i=0;i<n;i++)
a[i]=rand()%100;
}
void xuat(int a[],int n)
{
for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
}
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void boblesort(int a[],int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n-i;j++)
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
}
void selectionsort(int a[],int n)
{
int min;
for(int i=0;i<n;i++)
{
min=i;
for(int j=i+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
swap(a[i],a[min]);
}
}
void exchagesort(int a[],int n)
{
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
swap(a[i],a[j]);
}
void quickSort(int arr[], int lower, int upper)
{
int x = arr[(lower + upper) / 2];
int i = lower;
int j = upper;
do{
while(arr[i] < x)
i ++;
while (arr[j] > x)
j --;
if (i <= j)
{
swap(arr[i], arr[j]);
i ++;
j --;
}
}while(i <= j);
if (j > lower)
quickSort(arr, lower, j);
if (i < upper)
quickSort(arr, i, upper);
}
void insetionsort(int a[],int n)
{
int key;
int j;
for(int i=0;i<n;i++)
{
key=a[i];
j=i-1;
while(j>=0 && key<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=key;
}
}
void menu(int a[],int &n)
{
cout<<"\n1. Nhap mang\n";
cout<<"2. Xuat mang\n";
cout<<"3. Buble Sort\n";
cout<<"4. Selection Sort\n";
cout<<"5. Exchage Sort\n";
cout<<"6. Quick Sort\n";
cout<<"7. Insertion Sort\n";
cout<<"8. Exit\n";
int chon;
cout<<"Lua Chon: ";
cin>>chon;
switch(chon)
{
case 1:
cout<<"n: ";
cin>>n;
nhap(a,n);
break;
case 2:
xuat(a,n);
break;
case 3:
boblesort(a,n);
xuat(a,n);
break;
case 4:
selectionsort(a,n);
xuat(a,n);
break;
case 5:
exchagesort(a,n);
xuat(a,n);
break;
case 6:
quickSort(a,0,n);
xuat(a,n);
break;
case 7:
insetionsort(a,n);
xuat(a,n);
break;
case 8:
exit(0);
default:
cout<<"Khong co";
}
}
int main()
{
int a[MAX];
int n;
while(true)
menu(a,n);
}
Similar topics
Permissions in this forum:
Bạn không có quyền trả lời bài viết