forked from Danielbet21/Amusement_Park
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeather.c
148 lines (114 loc) · 2.98 KB
/
Weather.c
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
#include "Weather.h"
int initWeather(Weather* weather, eWeatherType condition, int temp) {
if (!isValidWeather(condition, temp)) {
return 0;
}
weather->condition = condition;
weather->temp = temp;
return 1;
}
int isValidWeather(eWeatherType condition, int temp) {
if (temp < MIN_TEMP || temp > MAX_TEMP) {
return 0;
}
if (condition < 0 || condition >= eNofWeatherTypes) {
return 0;
}
return 1;
}
void printWeather(const Weather* weather) {
if (isValidWeather(weather->condition, weather->temp) == 0) {
printf("Invalid weather\n");
return;
}
printf("Weather condition: %s, Temperature: %d%s\n", WeatherTypeStr[weather->condition], weather->temp, DEFUALT_WEATHER_SYMBOL);
}
int compareWeatherByTemp(const Weather* weather1, const Weather* weather2) {
return (weather1->temp - weather2->temp);
}
void initWeatherByUser(Weather* weather) {
int flag = 0;
int condition;
int temp;
do
{
if (flag > 0) {
printf("Invalid weather try again\n");
}
printf("Enter weather condition\n");
for (int i = 0; i < eNofWeatherTypes; i++) {
printf("%d. %s\n", i + 1, WeatherTypeStr[i]);
}
scanf("%d", &condition);
printf("Enter temperature: ");
scanf("%d", &temp);
flag = 1;
} while (!initWeather(weather, condition - 1, temp));
}
void printWeatherForcastTemp(int temp) {
if (temp < MIN_TEMP || temp > MAX_TEMP) {
printf("Invalid temperature");
return;
}
if (temp < 0) {
printf("the temp is %d, it's Freezing outside join the snowball fight!\n", temp);
}
if (temp >= 0 && temp <= 10) {
printf("the temp is %d, It's cold outside, don't forget Your coat!\n", temp);
}
if (temp > 10 && temp <= 20) {
printf("the temp is %d, It's chilly outside for all the adventurers\n", temp);
}
if (temp > 20 && temp <= 30) {
printf("the temp is %d It's a fine day, go have fun!\n", temp);
}
if (temp > 30) {
printf("the temp is %d, Very hot day drink alot of water", temp);
}
}
// ---- save functions ----
int saveWeatherToTextFile(const Weather* weather, FILE* fp) {
IS_FILE_NULL(fp);
if (weather == NULL || isValidWeather(weather->condition, weather->temp) == 0) {
return 0;
}
if (writeIntToTextFile(fp, weather->condition) == 0) {
return 0;
}
if (writeIntToTextFile(fp, weather->temp) == 0) {
return 0;
}
return 1;
}
int loadWeatherFromTextFile(Weather* weather, FILE* fp) {
IS_FILE_NULL(fp);
int condition;
int temp;
if (readIntFromTextFile(fp, &condition) == 0) {
return 0;
}
if (readIntFromTextFile(fp, &temp) == 0) {
return 0;
}
if (initWeather(weather, condition, temp) == 0) {
return 0;
}
return 1;
}
int saveWeatherToBinFile(const Weather* weather, FILE* fp) {
IS_FILE_NULL(fp);
if (weather == NULL || isValidWeather(weather->condition, weather->temp) == 0) {
return 0;
}
if (writeGeneralToBinFile(fp, weather, sizeof(Weather)) == 0) {
return 0;
}
return 1;
}
int loadWeatherFromBinFile(Weather* weather, FILE* fp) {
IS_FILE_NULL(fp);
if (readGeneralFromBinFile(fp, weather, sizeof(Weather)) == 0) {
return 0;
}
return 1;
}