-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeather.c
55 lines (47 loc) · 1.25 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
#include "Weather.h"
int initWeather(Weather* weather, eWeatherType condition, float temp) {
if (!isValidWeather(condition, temp)) {
return 0;
}
weather->condition = condition;
weather->temp = temp;
return 1;
}
int isValidWeather(eWeatherType condition, float 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: %.2f%s\n", WeatherTypeStr[weather->condition], weather->temp, DEFUALT_WEATHER_SYMBOL);
}
int compareWeatherByTemp(const Weather* weather1, const Weather* weather2) {
return (int)(weather1->temp - weather2->temp);
}
void initWeatherByUser(Weather* weather) {
int flag = 0;
int condition;
float 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("%f", &temp);
flag = 1;
} while (!initWeather(weather, condition - 1, temp));
}