pvjc23/a1/program.c

121 lines
2.6 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define LINE_SIZE 150
int main() {
char znaky[LINE_SIZE];
memset(znaky, 0, LINE_SIZE);
char* r = NULL;
int dd = 0;
int mm = 0;
int yyyy = 0;
char buffer [LINE_SIZE];
char *koniec_cisla = NULL;
char* zaciatok_cisla = znaky;
int cislo = 0;
int counter = 0;
int che = 0;
int count = 0;
int toc = 0;
while ((r = fgets(znaky, LINE_SIZE, stdin)) != NULL) {
if (feof(stdin)) {
break;
}
if(znaky[0] == '\n'){
break;
}
for(int i = 0; i < 10; i++){
if(znaky[i] == '.'){
toc++;
}
}
//printf("-->%d\n", toc);
if(toc != 2){
che = 1;
}
toc = 0;
zaciatok_cisla = znaky;
counter = 0;
while (*zaciatok_cisla != '\0') {
cislo = strtol(zaciatok_cisla, &koniec_cisla, 10);
if (zaciatok_cisla == koniec_cisla) {
break;
} else {
if (counter == 0) {
dd = cislo;
if(dd <= 0){
che = 1;
}
} else if (counter == 1) {
mm = cislo;
if(mm <= 0){
che = 1;
}
} else if (counter == 2) {
yyyy = cislo;
if(yyyy <= 0){
che = 1;
}
if(yyyy < 1900 || yyyy > 2100){
che = 1;
}
if(yyyy % 100 != 0 && yyyy % 4 == 0){
count++;
}
if(yyyy % 400 == 0){
count++;
}
}
counter++;
zaciatok_cisla = koniec_cisla + 1;
}
}
if(count == 0 && mm == 2 && dd == 29){
//printf("--->>>%d\n", count);
che = 1;
}
if(counter != 3 || che == 1){
printf("Neplatny datum.\n");
che = 1;
}
//printf("%d %d %d\n", dd, mm, yyyy);
time_t t = time(NULL);
struct tm *tm_date = localtime(&t);
tm_date->tm_mday = dd + 7;
tm_date->tm_mon = mm - 1;
tm_date->tm_year = yyyy - 1900;
//printf("%d %d %d\n", tm_date->tm_mday, tm_date->tm_mon, tm_date->tm_year);
time_t daja = mktime(tm_date);
//printf("%ld", daja);
struct tm *tm_churka = localtime(&daja);
strftime(buffer, LINE_SIZE, "%e.%m.%Y", tm_churka);
for(int i = 0; i < LINE_SIZE; i++){
if(buffer[i] == ' '){
buffer[i] = '&';
}
}
if(buffer[0] == 0){
buffer[0] = '&';
}
if(buffer[3] == 48){
buffer[3] = '&';
}
if(che == 0){
for(int i = 0; i < 10; i++){
if(buffer[i] != '&'){
printf("%c", buffer[i]);
che = 0;
}
}
printf("\n\n");
}
else{
che = 0;
}
}
return 0;
}