c - 使用结构体和数组编写函数

标签 c arrays struct

作业如下,要求您编写一个程序,对有关 COP 3223 的 TA 何时可用的信息进行排序。您的程序将读入所有 TA 办公时间轮类的数据,对数据进行排序,然后以预先指定的格式打印出信息。您可以假设最大类次数为 100。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Shift
{
char name[100];
char day_of_week[100];
int start_hour;
int end_hour;
};

struct Shift shift_data[100];
struct Shift temp;

int read_data(struct Shift shift_data[], int *num_shifts);
void sort_data(struct Shift shift_data[], int *num_shifts);
void print_data(struct Shift shift[], int num_shifts);

int main(void)
{
int num_shifts;

read_data(shift_data, &num_shifts);

sort_data(shift_data, &num_shifts);

print_data(shift_data, num_shifts);

return 0;
}

   /*** Preconditions: array of structure "Shift" to store data
 Postconditions: number of shifts read in from data file
 Actions: Ask user for name of input file. Read the number of shifts, then read in the data for all
      of the shifts. Return the number of shifts. ***/
int read_data(struct Shift shift_data[], int *num_shifts)
{
char input_schedule[100];
int i;
char shift_name[100], shift_day[100];

printf("Enter the name of the input file.\n");
scanf("%s", &input_schedule[100]);

FILE *ifp;
ifp = fopen("input_schedule.txt", "r");
fscanf(ifp, "%d", &num_shifts);

for(i=0; i<(*num_shifts); i++)
{
 fscanf(ifp, "%s %s %d %d\n", &shift_data[i].name, &shift_data[i].day_of_week, &shift_data[i].start_hour, &shift_data[i].end_hour);
}
fclose(ifp);
return num_shifts;
}
/*** Preconditions: array of structure "Shift" integer value indicating number of shifts
 Postconditions: none - this function does not return anything.
 Actions: Sort the shifts by the TA's first name. ***/
void sort_data(struct Shift shift_data[], int *num_shifts)
{
int i,j;

for(i=0; i<(*num_shifts); i++)
{
for(j=i+1; j<(*num_shifts); j++)
{
    if(strcmp(shift_data[i].name,shift_data[i].name) < 0)
    {
        temp.name = shift_data[i].name;
        shift_data[i].name = shift_data[j].name;
        shift_data[j].name = temp.name;
    }
    if(strcmp(shift_data[i].day_of_week,shift_data[i].day_of_week) < 0)
    {
        temp.day_of_week = shift_data[i].day_of_week;
        shift_data[i].day_of_week = shift_data[j].day_of_week;
        shift_data[j].day_of_week = temp.day_of_week;
    }
    if(strcmp(shift_data[i].start_hour,shift_data[i].start_hour) < 0)
    {
        temp.start_hour = shift_data[i].start_hour;
        shift_data[i].start_hour = shift_data[j].start_hour;
        shift_data[j].start_hour = temp.start_hour;
    }
    if(strcmp(shift_data[i].end_hour,shift_data[i].end_hour) < 0)
    {
        temp.end_hour = shift_data[i].end_hour;
        shift_data[i].end_hour = shift_data[j].end_hour;
        shift_data[j].end_hour = temp.end_hour;
    }
}

}
}

 /*** Preconditions: array of structure "Shift" integer value indicating number of shifts
 Postconditions: none - this function does not return anything.
 Actions: Print the sorted data in the format described below. **/
void print_data(struct Shift shift[], int num_shifts)
{
int i;

printf("\n\n\n");

for(i=0; i<num_shifts; i++)
{
printf("%s\t\t", shift_data[i].name);
printf("%s\t", shift_data[i].day_of_week);
printf("%d to ", shift_data[i].start_hour);
printf("%d\n", shift_data[i].end_hour);

if(shift_data[i].start_hour > 12)
{
    shift_data[i].start_hour = shift_data[i].start_hour - 12;
    printf("%2d:00 pm to  ", shift_data[i].start_hour);
}
else if(shift_data[i].start_hour < 12)
{
    printf("%2d:00 am to  ", shift_data[i].start_hour);
}
else if(shift_data[i].start_hour == 12)
{
    printf("%2d:00 pm to  ", shift_data[i].start_hour);
}

if(shift_data[i].end_hour > 12)
{
    shift_data[i].end_hour = shift_data[i].end_hour - 12;
    printf("%2d:00 pm\n", shift_data[i].end_hour);
}
else if(shift_data[i].end_hour < 12)
{
    printf("%2d:00 am\n", shift_data[i].end_hour);
}
else if(shift_data[i].end_hour == 12)
{
    printf("%2d:00 pm\n", shift_data[i].end_hour);
}
}
}

但是,我的临时变量在 sort_data 函数中遇到问题。我不断收到错误消息,错误:从类型“char *”分配给类型“char[100]”时不兼容的类型

最佳答案

知道轮类次数后,接下来的事情就很简单了。您只需循环 num_shifts 次并扫描相应的值即可。 像这样的事情:

for(i = 0; i < num_shifts; i++)
{
    int ret = fscanf(ifp, "%99s %99s %d %d\n",
                    shift_data[i].name, 
                    shift_data[i].day_of_week,
                    &shift_data[i].start_hour,
                    &shift_data[i].end_hour);
} 

关于c - 使用结构体和数组编写函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34012949/

相关文章:

用于读取测试结果的 C 程序

c - 在 C 中实现位图

java - 在不同平台上字节到字符串的转换,反之亦然

c++ - 在 C++ 中使用分隔符获取输入

c - C如何处理结构赋值

c - LPC1769 微 Controller 中批量传输的 USB 供应商特定类

c - 如何跟踪或调试GMP?

javascript - 从平面数组构建一棵树

c - typedef 与 C 中没有 typedef 的结构和枚举

c - C中的双向链表