Created
November 27, 2011 14:31
-
-
Save ramntry/1397629 to your computer and use it in GitHub Desktop.
Вторая задача второй контрольной работы
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdio> | |
| #include <cstring> | |
| #include <cstdlib> | |
| union Date | |
| { | |
| struct | |
| { | |
| char year[5]; | |
| char mounth[3]; | |
| char day[3]; | |
| }; | |
| char toCmp[11]; | |
| }; | |
| int main(void) | |
| { | |
| printf("Exam 2, Task 2. Minimum date.\nAuthor: Tereshin Roman\nFilename = "); | |
| char fname[540]; | |
| scanf("%s", fname); | |
| FILE *file = fopen(fname, "r"); | |
| if (!file) | |
| { | |
| printf("File not found\n"); | |
| return 1; | |
| } | |
| Date min; | |
| strcpy(min.toCmp, "9999\199\199"); | |
| Date current = min; | |
| char buf[1024]; | |
| for (;;) | |
| { | |
| if(fscanf(file, "%*[^0-9]") == EOF) | |
| break; | |
| fscanf(file, "%[0-9]", buf); | |
| if (atoi(buf) > 31) | |
| continue; | |
| else | |
| strcpy(current.day, buf); | |
| if (fgetc(file) != '.') | |
| continue; | |
| fscanf(file, "%[0-9]", buf); | |
| if (atoi(buf) > 12) | |
| continue; | |
| else | |
| strcpy(current.mounth, buf); | |
| if (fgetc(file) != '.') | |
| continue; | |
| if (fscanf(file, "%[0-9]", buf) == 0) | |
| continue; | |
| if (atoi(buf) > 9999) | |
| continue; | |
| else | |
| strcpy(current.year, buf); | |
| if (fgetc(file) == EOF) | |
| break; | |
| current.toCmp[4] = '\1'; | |
| current.toCmp[7] = '\1'; | |
| if (strcmp(current.toCmp, min.toCmp) < 0) | |
| min = current; | |
| } | |
| min.toCmp[4] = '\0'; | |
| min.toCmp[7] = '\0'; | |
| printf("Minimum date: %s.%s.%s\n", min.day, min.mounth, min.year); | |
| fclose(file); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment