-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.c
More file actions
99 lines (81 loc) · 1.9 KB
/
Time.c
File metadata and controls
99 lines (81 loc) · 1.9 KB
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
//3
#include <stdio.h>
#include <stdio.h>
typedef struct{
unsigned hour;
unsigned min;
unsigned sec;
}time;
void read(time*t){
printf("Enter hours: ");
scanf("%u",&t->hour);
printf("Enter minutes: ");
scanf("%u",&t->min);
printf("Enter seconds: ");
scanf("%u",&t->sec);
}
void display(time*t){
crt(t);
printf("TIME:\n%02d:%02d:%02d\n",t->hour,t->min,t->sec);
}
void crt(time*t){
if(t->hour>=24){
t->hour=t->min=t->sec=0;
}
if(t->min>=60){
t->hour+=t->min/60;
t->min%=60;
}
if(t->sec>=60){
t->min+=t->sec/60;
t->sec%=60;
}
}
void update(time*t){
t->sec+=1;
printf("Time Updated...\n");
}
void add(time*t){
printf("FOR TIME-1:\n");
read(t);
int t1hour=t->hour,t1min=t->min,t1sec=t->sec;
printf("FOR TIME-2:\n");
read(t);
int t2hour=t->hour,t2min=t->min,t2sec=t->sec;
t->hour=t1hour+t2hour;
t->min=t1min+t2min;
t->sec=t1sec+t2sec;
printf("Addition of two times is accomplished...\n");
}
void main(){
time*t=(time*)malloc(sizeof(time));
if(t==NULL){
printf("Memoer allocation failed...\n");
return;
}
int ch;
for(;;){
printf("\nENTER:\n1. To Read\n2. To Display\n3. To Update\n4. To Add two times\n0. To Exit\nEnter your choice from above: ");
scanf("%d",&ch);
switch(ch){
case 1:
read(t);
break;
case 2:
display(t);
break;
case 3:
update(t);
break;
case 4:
add(t);
break;
case 0:
free(t);
printf("Exiting...\n");
exit(0);
default:
printf("Invalid Choice! Please Enter Again...\n");
}
}
}