3つのテキストファイルから情報を読み取り全情報の表示を行う機能と時刻と表示数(1~10)を入力し,その時刻よりも後で最も早い回を早い順に表示数分だけ表示する機能を実装したプログラムを作成するといったもので、表示は行えるのですがどのようにして3つのファイルのデータをソートして表示すればいいのかわかりません.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct TM {
int s_h;
int s_m;
int e_h;
int e_m;
}TM;
typedef struct SN {
char title[50];
int theater;
struct TM table[8];
}SN;
typedef struct SHW {
char title[50];
int theater;
int s_hour;
int s_min;
int e_hour;
int e_min;
}SHW;
int i,cnt2;
void print_screen (SN *screening);
void read_screen (char filename[], SN *screening);
void select_show (SN *screening, int hour, int min, SHW *showing);
void print_show (SHW *showing);
int main(void){
int x,ch,rt,num;
int an=1;
int cnt=1;
int hour,min;
char name[30]="s01.txt";
SN screen[3];
SHW show[20];
for(rt=0;rt<3;rt++){
for(x=0;x<8;x++){
screen[rt].table[x].s_h=99;
screen[rt].table[x].s_m=99;
screen[rt].table[x].e_h=99;
screen[rt].table[x].e_m=99;
}
}
for(rt=0;rt<20;rt++){
show[rt].s_hour=99;
show[rt].s_min=99;
show[rt].e_hour=99;
show[rt].e_min=99;
}
for(rt=0;rt<3;rt++){
read_screen(name,&screen[rt]);
if(rt==0)
strcpy(name,"s02.txt");
if(rt==1)
strcpy(name,"s03.txt");
}
printf("機能を選択してください.(p:表示,s:検索)\n");
while(an != 0){
ch=getchar();
if(ch!='p' && ch!='s'){
printf("pかsを入力してください\n");
}else{
an=0;
}
}
switch(ch){
case 'p':
printf("上映スケジュールを表示します。\n");
for(rt=0;rt<3;rt++){
print_screen(&screen[rt]);
}
break;
case 's':
while(cnt != 0){
printf("時刻を(hh:mm)の形式で入力してください\n");
scanf("%d:%d",&hour,&min);
if( hour>24 || hour<0 ){
cnt=1;
printf("入力値が不正です\n");
}
else if( min>60 || min<0 ){
cnt=1;
printf("入力値が不正です\n");
}
else{
cnt=0;
}
}
while(cnt!=1){
printf("表示数を入力してください(1-10).\n");
scanf("%d",&num);
if(num<1||num>10){
printf("入力値が不正です\n");
cnt=0;
}
else{
cnt=1;
}
break;
}
select_show(screen,hour,min,show);
rt=0;
while(show[rt].e_hour!=99){
print_show (&show[rt]);
rt++;
}
break;
default:
break;
}
return 0;
}
void print_screen (SN *screening)
{
int t=0;
printf("「%s」 シアター%d\n",screening->title,screening->theater);
while(screening->table[t].s_h!=99){
printf("%d:%02d > %d:%02d\n",screening->table[t].s_h
,screening->table[t].s_m,screening->table[t].e_h
,screening->table[t].e_m);
t++;
}
}
void read_screen (char filename[], SN *screening)
{
FILE *fp;
int cnt=0;
char tn[50],ss[256];
if((fp=fopen(filename,"r"))==NULL){
printf("ファイルが開けません\n");
exit(1);
}
fscanf(fp,"%s",tn);
strcpy(screening->title,tn);
i=0;
while(fgets(ss,256,fp)!=NULL){
fscanf(fp,"%d,%2d:%d,%d:%2d",&screening->theater,&screening->table[i].s_h
,&screening->table[i].s_m,&screening->table[i].e_h
,&screening->table[i].e_m);
i++;
}
fclose(fp);
}
void select_show (SN *screening, int hour, int min, SHW *showing){
int t,cnt,gt=0;
cnt=0;
while(gt!=3){
for(t=0;t<8;t++)
{
if(cnt==0&&screening->table[t].s_h==99){
printf("入場可能な上映回が存在しません.\n");
break;
}
else if(screening->table[t].s_h==hour){
if(screening->table[t].s_m > min){
strcpy(showing->title,screening->title);
showing->theater = screening->theater;
showing->s_hour = screening->table[t].s_h;
showing->s_min = screening->table[t].s_m;
showing->e_hour = screening->table[t].e_h;
showing->e_min = screening->table[t].e_m;
showing++;
cnt++;
}
}
else if(screening->table[t].s_h > hour){
strcpy(showing->title,screening->title);
showing->theater = screening->theater;
showing->s_hour = screening->table[t].s_h;
showing->s_min = screening->table[t].s_m;
showing->e_hour = screening->table[t].e_h;
showing->e_min = screening->table[t].e_m;
showing++;
cnt++;
}
}
gt++;
screening++;
}
}
void print_show (SHW *showing){
printf("%2d:%02d > %2d:%02d 「%s」 シアター%d\n",showing->s_hour,showing->s_min,
showing->e_hour,showing->e_min,showing->title,showing->theater);
}
解説よろしくお願いします。