Write a program that uses structure to add and subtract times
#include<stdio.h> typedef struct times { int h; int m; int s; } time; time addTime(time t1, time t2) { time a = {0,0,0}; a.s = t1.s + t2.s; if(a.s>=60) { a.s-=60; a.m+=1; } a.m += t1.m + t2.m; if(a.m >= 60) { a.m -= 60; a.h += 1; } a.h += t1.h + t2.h; return a; } time subTime(time t1, time t2){ time a = {0,0,0}; a.s = t1.s - t2.s; if(a.s < 0) { a.s += 60; a.m -= 1; } a.m += t1.m - t2.m; if(a.m < 0) { a.m += 60; a.h -= 1; } a.h += t1.h - t2.h; return a; } int main() { time t1 = {8,20,40},t2 = {5,50,30},ad,df; ad = addTime(t1,t2); df = subTime(t1,t2); printf("Add:\n %d : %d : %d\n",ad.h,ad.m,ad.s); printf("Sub:\n %d : %d : %d\n",df.h,df.m,df.s); return 0; }
Output
Add:
14 : 11 : 10
Sub:
2 : 30 : 10