-
Notifications
You must be signed in to change notification settings - Fork 0
/
human readable time.cpp
125 lines (119 loc) · 4.23 KB
/
human readable time.cpp
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
int lg(int n){
int k=0;
while(n){k++;
n/=10;}
return k;
}
int m(int t[]){
int m=0;
for(int i=0;i<5;i++){
if(t[i]>m)
m=t[i];
}
return m;
}
int nn(int t[]){
int m=0;
for(int i=0;i<5;i++){
if(t[i]!=0)
m++;
}
return m;
}
void aff(int t[]){
for(int i=0;i<5;i++){
printf("%d",t[i]);
}
}
void init(int t[]){
for(int i=0;i<5;i++){
t[i]=0;
}
}
int main(){
do{
puts("Provide time in seconds\n");int n;scanf("%d",&n);
int scnds,min,hours,days,years;years=n/31536000;n%=31536000;days=n/86400;n%=86400;hours=n/3600;n%=3600;min=n/60;n%=60;scnds=n;
int t[5];
t[0]=lg(years);
t[1]=lg(days);
t[2]=lg(hours);
t[3]=lg(min);
t[4]=lg(scnds);
int mx=m(t),s=nn(t);
char *c=(char*)malloc(sizeof(char)*(mx*5+52));
c[0]='\0';
if(s==0){sprintf(c,"now");}
if(s>2){
for(int i=0;i<5;i++){if(s>2){
if(t[i]){
switch(i+1){
case(1):if(years==1){sprintf(c +strlen(c),"1 year, ");}else{sprintf(c+strlen(c),"%d years, ",years);};break;
case(2):if(days==1){sprintf(c+strlen(c),"1 day, ");}else{sprintf(c+strlen(c),"%d days, ",days);};break;
case(3):if(hours==1){sprintf(c+strlen(c),"1 hour, ");}else{sprintf(c+strlen(c),"%d hours, ",hours);};break;
case(4):if(min==1){sprintf(c+strlen(c),"1 minute, ");}else{sprintf(c+strlen(c),"%d minutes, ",min);};break;
}s--;
}
}
else if(s==2){
if(t[i]){
switch(i+1){
case(2):if(days==1){sprintf(c+strlen(c),"1 day and ");}else{sprintf(c+strlen(c),"%d days and ",days);};break;
case(3):if(hours==1){sprintf(c+strlen(c),"1 hour and ");}else{sprintf(c+strlen(c),"%d hours and ",hours);};break;
case(4):if(min==1){sprintf(c+strlen(c),"1 minute and ");}else{sprintf(c+strlen(c),"%d minutes and ",min);};break;
}s--;
}
}
else{
if(t[i]){
switch(i+1){
case(2):if(days==1){sprintf(c+strlen(c),"1 day");}else{sprintf(c+strlen(c),"%d days",days);};break;
case(3):if(hours==1){sprintf(c+strlen(c),"1 hour");}else{sprintf(c+strlen(c),"%d hours",hours);};break;
case(4):if(min==1){sprintf(c+strlen(c),"1 minute");}else{sprintf(c+strlen(c),"%d minutes",min);};break;
case(5):if(scnds==1){sprintf(c+strlen(c),"1 second");}else{sprintf(c+strlen(c),"%d seconds",scnds);};break;
}s--;
}
}
}
}
else if(s==2){int j=0; for(int i=0;i<5;i++){
if(t[i]&&j==0){
switch(i+1){
case(1):if(years==1){sprintf(c,"1 year and ");}else{sprintf(c,"%d years and ",years);};break;
case(2):if(days==1){sprintf(c,"1 day and ");}else{sprintf(c,"%d days and ",days);};break;
case(3):if(hours==1){sprintf(c,"1 hour and ");}else{sprintf(c,"%d hours and ",hours);};break;
case(4):if(min==1){sprintf(c,"1 minute and ");}else{sprintf(c,"%d minutes and ",min);};break;
}
j++;
}
else if(t[i]){
switch(i+1){
case(2):if(days==1){sprintf(c+strlen(c),"1 day");}else{sprintf(c+strlen(c),"%d days",days);};break;
case(3):if(hours==1){sprintf(c+strlen(c),"1 hour");}else{sprintf(c+strlen(c),"%d hours",hours);};break;
case(4):if(min==1){sprintf(c+strlen(c),"1 minute");}else{sprintf(c+strlen(c),"%d minutes",min);};break;
case(5):if(scnds==1){sprintf(c+strlen(c),"1 second");}else{sprintf(c+strlen(c),"%d seconds",scnds);};break;
}}
}
}
else{for(int i=0;i<5;i++){
if(t[i]){
switch(i+1){
case(1):if(years==1){sprintf(c+strlen(c),"1 year");}else{sprintf(c+strlen(c),"%d years",years);};break;
case(2):if(days==1){sprintf(c+strlen(c),"1 day");}else{sprintf(c+strlen(c),"%d days",days);};break;
case(3):if(hours==1){sprintf(c+strlen(c),"1 hour");}else{sprintf(c+strlen(c),"%d hours",hours);};break;
case(4):if(min==1){sprintf(c+strlen(c),"1 minute");}else{sprintf(c+strlen(c),"%d minutes",min);};break;
case(5):if(scnds==1){sprintf(c+strlen(c),"1 second");}else{sprintf(c+strlen(c),"%d seconds",scnds);};break;
}}
}
}
puts(c);
Sleep(1000);
puts("Exit? yes(1) no(0)\n");scanf("%d",&n);
if(n==1)break;
}while(1);
puts("Bye <3");
}