1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
#include <stdio.h>
#include <stdlib.h>
typedef struct hotel_tag
{
char surname[20];
char first_name[20];
int year_in;
int year_out;
int month_in;
int month_out;
int day_in;
int day_out;
int num_days;
float price;
float tot_price;
} hotel_type;
int read_info(char* file_name, hotel_type res[]);
void compute(hotel_type res[], int size);
void compute_total(hotel_type res[], int size);
void print_res (hotel_type res[], int size);
void write_records (char* file_name, hotel_type res[], int size);
int main()
{
hotel_type res[100];
int norecs;
int choice;
do
{
printf ("welcome to my program\n\n");
printf ("MENU\n\n");
printf ("1.\tRead data from files\n");
printf ("2.\tcompute total number of booked days for 2009 and 2010\n");
printf ("3.\tCompute total amount received by hotel\n");
printf ("4.\tShow on screen the details of persons who paid over 1000 euro\n");
printf ("5.\tSave data to file: week2009.txt\n\n");
printf ("Please enter your choice.\n\n");
scanf ("%d", &choice);
switch ( choice )
{
case 1:
norecs = read_info("year2009.txt", res);
break;
case 2:
compute(res, 100);
break;
case 3:
compute_total(res, 100);
break;
case 4:
print_res (res, 100);
break;
case 5:
write_records ("week2009.txt", res, norecs);
break;
default:
printf ("invalid Choice");
break;
}
} while (choice <5);
return (EXIT_SUCCESS);
}
int read_info(char* file_name, hotel_type res[])
{
FILE* fp;
int norecs;
char ch;
fp = fopen(file_name, "r");
while (!feof(fp))
{
fscanf (fp, "%c%c%d%d%d%d%d%d%f", &res[norecs].surname,
&res[norecs].first_name,
&res[norecs].year_in,
&res[norecs].month_in,
&res[norecs].day_in,
&res[norecs].year_out,
&res[norecs].month_out,
&res[norecs].day_out,
&res[norecs].price);
norecs++;
}
fclose(fp);
return norecs;
}
void compute(hotel_type res[], int size)
{
int i;
for (i=0; i < size; i++)
if (res[i].day_out > res[i].day_in)
res[i].num_days = res[i].day_out - res[i].day_in;
else
res[i].num_days = res[i].day_out + (30 - res[i].day_in);
}
void compute_total(hotel_type res[], int size)
{
int i;
for (i=0; i < size; i++)
res[i].tot_price = res[i].num_days * res[i].price;
}
void print_res (hotel_type res[], int size)
{
int i;
printf ("\t| Surname\t | First name\t | Total amount |\n\n");
for(i=0; i < size; i ++)
if (res[i].tot_price > 1000)
printf ("\t| %s\t | %s\t | %f", res[i].surname, res[i].first_name, res[i].tot_price);
}
void write_records (char* file_name, hotel_type res[], int size)
{
FILE* fp;
int i;
fp = fopen(file_name, "w");
for (i = 0; i < size; i++)
fprintf (fp, "%s %s %f \n", res[i].surname,
res[i].first_name,
res[i].tot_price);
fflush(fp);
fclose(fp);
}
|