关于函数参数循环的问题
#include <stdio.h>#define HOUR 60
typedef struct {
int hour;
int minute;
} Ttime ;
void read(Ttime *,char *);
Ttime cal(Ttime *,Ttime *);//declaration
void print(Ttime,char *);
int compare_times(Ttime , Ttime);
int main () {
Ttime start,end;
Ttime time_print;
Ttime time[3]={0};
read(&start,"please enter the starting time for leg1\n");
read(&end,"please enter the ending time for leg1\n");
compare_times(start, end);
if (compare_times(start, end) == -1)
printf("\nstarting time is earlier\n");
if (compare_times(start, end) == 0)
printf("\nTimes are equal\n");
while (compare_times(start, end) == 1){
printf("\nthe starting time is later,try again!!!\n");
read(&start,"please enter the starting time for leg1\n");
read(&end,"please enter the ending time for leg1\n");
}
time_print=cal(&start,&end);
for(int i=0;i<3;i++){
time[i]=time_print;
}
print(time_print,"the duration time is");
fflush(stdin);
getchar();
return 0;
}
void read(Ttime *p,char *prompt){
int time1,time2;
printf("\n%s",prompt);
scanf("\n%d %d",&time1,&time2);
p->hour=time1;
p->minute=time2;
}
Ttime cal(Ttime *stime,Ttime *etime){
int time;
Ttime time_return;
time=(etime->hour*HOUR+etime->minute)-(stime->hour*HOUR+stime->minute);
time_return.hour=time/HOUR;
time_return.minute=time%HOUR;
return time_return;
}
int compare_times(Ttime t1, Ttime t2){
if(t1.hour>t2.hour||(t1.hour==t2.hour&&t1.minute>t2.minute))
return 1;
if(t1.hour==t2.hour&&t1.minute==t2.minute)
return 0;
if(t1.hour<t2.hour||(t1.hour==t2.hour&&t1.minute<t2.minute))
return -1;
}
void print(Ttime time,char *prompt){
printf("%s %d hour(s) %d minute(s)",prompt,time.hour,time.minute);
}
题目要求提问
Enter starting time for leg 1 :
Enter ending time for leg 1 :
Enter starting time for leg 2 :
Enter ending time for leg 2:
Enter starting time for leg 3 :
Enter ending time for leg 3 :
但是这里的"Enter starting time for leg1"字符串指针作为函数参数输入的,请问如何实现比如一个for循环让它自动地显示123呢?
要求只能修改main函数,执行函数不能修改