Define a structure that describes a hotel. It should have members that include the name, address, grade, average room charge and number of rooms.
i) To print all hotels details of a given grade in order of charges.
ii) To print hotel details with room charges less than given values.
Write a function to perform the following tasks.
i) To print all hotels details of a given grade in order of charges.
ii) To print hotel details with room charges less than given values.
Solution :
/********************************
Program : Structure sorting,searching with
array
for more program visit :http://girfahelp.blogspot.in/p/c-language-structure-programming.html
**********************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 10
typedef struct ht
{
char name[30];
char address[40];
char grade;
int room;
int charge;
}hotel;
void input(hotel*,int);
void print(hotel *);
void printbyRate(hotel *,int,int);
void printByGrade(hotel *str,char,int s);
void main()
{
int i,opt,rate;
hotel rec[MAX];
char ch;
clrscr();
printf("How
many record you want to insert>> ");
scanf("%
d",&i);
if(i>MAX)
printf("Maximum
capacity of record holding is %d",MAX);
else
{
input(rec,i);
do
{
clrscr();
printf("\n************************\n");
printf("1. Print by Rate\n2. Print by grade\n0. Exit");
printf("\n************************\n");
printf("Enter your choice>> ");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("Enter Rate for search>> ");
scanf("%d",&rate);
printbyRate(rec,rate,i);
getch();
break;
case 2:
printf("Enter Grade>> ");
fflush(stdin);
scanf("%c",&ch);
printByGrade(rec,ch,i);
getch();
break;
case 0:
break;
}
}while(opt!=0);
}
}
void input(hotel *str,int n)
{
int j;
for(j=0;j<n;j++)
{
printf("\nInput
for %d'st record\n",j+1);
printf("Enter
Hotel name>> ");
fflush(stdin);
gets(str->name);
printf("Enter
Hotel Address>> ");
fflush(stdin);
gets(str->address);
printf("Enter
Grade>> ");
fflush(stdin);
scanf("%c",&str->grade);
printf("Enter
number of rooms>> ");
scanf("%d",&str->room);
printf("Enter
average room charge>> ");
scanf("%d",&str->charge);
str++;
}
}
void printbyRate(hotel *str,int rate,int size)
{
int i;
for(i=0;i<size;i++)
{
if(str->charge<rate)
print(str);
str++;
}
}
void printByGrade(hotel *str,char ch,int s)
{
hotel tmp[MAX],tv;
int i,j,k=0;
char g;
/*
Saving data into array for sort */
for(i=0;i<s;i++)
{
if(str->grade==ch)
{
strcpy(tmp[k].name,str->name);
strcpy(tmp[k].address,str->address);
tmp[k].grade=str->grade;
tmp[k].room=str->room;
tmp[k].charge=str->charge;
k++;
}
str++;
}
/*
Sorting with bubble sort */
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
if(tmp[j].charge>tmp[j+1].charge)
{
strcpy(tv.name,tmp[j].name);
strcpy(tv.address,tmp[j].address);
tv.grade=tmp[j].grade;
tv.room=tmp[j].room;
tv.charge=tmp[j].charge;
strcpy(tmp[j].name,tmp[j+1].name);
strcpy(tmp[j].address,tmp[j+1].address);
tmp[j].grade=tmp[j+1].grade;
tmp[j].room=tmp[j+1].room;
tmp[j].charge=tmp[j+1].charge;
strcpy(tmp[j+1].name,tv.name);
strcpy(tmp[j+1].address,tv.address);
tmp[j+1].grade=tv.grade;
tmp[j+1].room=tv.room;
tmp[j+1].charge=tmp[j+1].charge;
}
}
}
for(i=0;i<k;i++)
{
printf("\nName=%s",tmp[i].name);
printf("\nAddress=%s",tmp[i].address);
printf("\nGrade=%c",tmp[i].grade);
printf("\nRoom=%d",tmp[i].room);
printf("\nCharge
%d",tmp[i].charge);
printf("\n");
}
}
void print(hotel *str)
{
printf("\nName\t%s",str->name);
printf("\nAddress\t%s",str->address);
printf("\nGrade\t%c",str->grade);
printf("\nRoom\t%d",str->room);
printf("\nCharge\t%d\n",str->charge);
No comments:
Post a Comment