Silence ! B0rsuk speaks:
#include <stdio.h>
#include <string.h>
/*clear sky*/
#define vis1 0
#define vis2 1
#define vis3 2
#define vis4 3
#define maxl 80
/*mist*/
#define wea1 0
/*fog*/
#define wea2 1
/*freezing fog*/
#define wea3 2
/*shallow fog*/
#define wea4 3
/*patches of fog*/
#define wea5 4
/* light rain, zeby sie odroznialo od zgrai mgiel */
#define wea6 8
/* chmurki, zeby sie odroznialo */
#define wea7 12
/* light rain showers, zeby sie odroznialo od zgrai mgiel */
#define wea8 9
main()
{
int smiec;
/* przyporzadkowywane odpowiedniemu niebu/pogodzie */
char line[80];
char word1[20];
char word2[20];
char word3[20];
FILE *pFile;
pFile = fopen("ofiara.met", "r");
/*
windchill ! (385 b)
*/
while (fgets(line, maxl, pFile) != NULL) {
/* zczytywanie i przepisywanie wiatru */
if (sscanf
(line, "Wind: from the %*s (%s degrees) at %s", word1,
word2) == 2) {
printf("Direction (deg): %s \\n", word1);
printf("Wind speed: %s MPH \\n", word2);
}
/* zmienny wiatr */
else if (sscanf(line, "Wind: Variable at %s", word1) == 1) {
printf("Wind variable, at %sMPH \\n", word1);
}
/* Widocznosc - wiecej niz... */
else if (sscanf(line, "Visibility: greater than %s", word1) == 1) {
printf("Visibility > %s miles \\n", word1);
/* widocznosc */
} else if (sscanf(line, "Visibility: %s", word1) == 1) {
printf("Visibility: %s miles \\n", word1);
/* niebo */
} else
if ((sscanf(line, "Sky conditions: %s %s", word1, word2) == 2)
|| (sscanf(line, "Sky conditions: %s %s", word1, word2) ==
1)) {
if (strcmp(word1, "clear") == 0)
printf("Sky conditions: CLEAR %d \\n", vis1);
else if ((strcmp(word1, "mostly") == 0)
&& (strcmp(word2, "clear") == 0))
printf("Sky conditions: MOSTLY CLEAR %d \\n", vis2);
else if ((strcmp(word1, "partly") == 0)
&& (strcmp(word2, "cloudy") == 0))
printf("Sky conditions: PARTLY CLOUDY %d \\n", vis3);
else if ((strcmp(word1, "mostly") == 0)
&& (strcmp(word2, "cloudy") == 0))
printf("Sky conditions: MOSTLY CLOUDY %d \\n", vis4);
else
printf("Nieznane zachmurzenie");
/* pogoda */
} else if (sscanf(line, "Weather: %s %s %s", word1, word2, word3)
>= 1) {
if (strncmp(line, "Weather: mist", 13) == 0) {
if ((strcmp(word2, "shallow") == 0)
&& (strcmp(word3, "fog") == 0))
printf("Weather: mist, shallow fog %d %d", wea1, wea4);
else
printf("Weather: mist %d \\n", wea1);
}
if (strncmp(line, "Weather: fog", 12) == 0)
printf("Weather: fog %d \\n", wea2);
if (strncmp(line, "Weather: freezing fog", 21) == 0)
printf("Weather: fog %d \\n", wea3);
if (strncmp(line, "Weather: shallow fog", 20) == 0)
printf("Weather: shallow fog %d \\n", wea4);
if (strncmp(line, "Weather: patches of fog", 23) == 0)
printf("Weather: patches of fog %d \\n", wea5);
if (strncmp(line, "Weather: light rain", 19) == 0) {
if (strcmp(word3, "showers") == 0)
printf("Weather: light rain %d \\n", wea8);
else if (strcmp(word3, "mist") == 0)
printf("Light rain;mist %d %d \\n", wea8, wea1);
else
printf("Weather: light rain %d \\n", wea6);
}
if (strncmp(line, "Weather: Cumulonimbus clouds observed", 37)
== 0)
printf("Chmurki %d \\n", wea7);
/* Temperatura w stopniach Fahrenheita, zeby bylo dokladniej */
} else if (sscanf(line, "Temperature: %s", word1) == 1) {
printf("Temperature: %sF \\n", word1);
/* Windchill jakis... */
} else if (sscanf(line, "Windchill: %s", word1) == 1) {
printf("Windchill: %sF \\n", word1);
/* Punkt rosy, cokolwiek by to bylo */
} else if (sscanf(line, "Dew Point: %s", word1) == 1) {
printf("Dew Point: %sF \\n", word1);
/* wilgotnosc */
} else if (sscanf(line, "Relative Humidity: %d%*s", &smiec) == 1) {
printf("Relative Humidity: %d\\n", smiec);
/* cisnienie */
} else if (sscanf(line, "Pressure %*s %*s %*s %*s (%s", word1) ==
1) {
printf("Pressure: %shPa \\n", word1);
}
}
return 0;
}
Now go ahead and praise me.
It reads one file correctly, taking even weird stuff like Windchill into account. Damned if I know what is it.
It replaces things like "
light rain"
or "
mist"
with defined numbers.
It works only for one file defined it source code. If I want to allow for parameters (filenames), I should make entire program a function, right ?
Now the fun stuff: with mess like this, probably the best way to make it write to files would be taking (almost) every printf line between "
fopen"
and "
fclose"
, right ? Not to mention changing it to fprintf...
As if I had not enough of monotonous work today already....
____________________________
http://www.gamasutra.com/features/20051128/adams_01.shtml