【发布时间】:2015-12-05 17:17:10
【问题描述】:
对于我的一项作业,我需要编写一个代码,该代码从一个文本文件中获取输入,该文本文件的顶部有许多可用的助教,之后的每一行都包含名称、日期、开始时间和结束时间的值.
现在我有读取文件的函数和打印输出的函数,它们按照我需要的方式工作,但是每当我包含我编写的排序函数时,程序就会崩溃。
我在嵌套 forloop 中使用 strcmp() 对排序函数做了什么,以便循环遍历数组并确定哪些值按字母顺序取代另一个值。然后我使用 strcpy() 和一个临时结构来存储一个值,将一个值与另一个交换,然后将另一个值与临时结构中存储的值交换。
我认为问题可能与我在结构中使用 strcpy() 的方式有关,因为我认为它的工作方式是我调用地址而不是实际值,我可能需要使用指针,但我'不完全确定。这是给我带来麻烦的功能:
void sort_data(struct Shift shift_data[],int num_shifts)
{
int x,y;
for(x = 0 ; x < num_shifts ; x++)
{
for(y = 0; y < num_shifts ; y++)
{
if(strcmp(shift_data[x].name,shift_data[y].name)<0)
{
strcpy(temp.name, shift_data[x].name);
strcpy(shift_data[y].name, shift_data[x].name);
strcpy(shift_data[y].name, temp.name);
}
if(strcmp(shift_data[x].name,shift_data[y].name)<0)
{
strcpy(temp.day_of_week, shift_data[x].day_of_week);
strcpy(shift_data[y].day_of_week, shift_data[x].day_of_week);
strcpy(shift_data[y].day_of_week, temp.day_of_week);
}
if(strcmp(shift_data[x].name,shift_data[y].name)<0)
{
strcpy(temp.start_hour, shift_data[x].start_hour);
strcpy(shift_data[y].start_hour, shift_data[x].start_hour);
strcpy(shift_data[y].start_hour, temp.start_hour);
}
if(strcmp(shift_data[x].name,shift_data[y].name)<0)
{
strcpy(temp.end_hour, shift_data[x].end_hour);
strcpy(shift_data[x].end_hour, shift_data[y].end_hour);
strcpy(shift_data[y].end_hour, temp.end_hour);
}
}
}
}
这是完整的代码:
// Henry Moench
// 12/2/15
// COP3223C-15FALL 0002
// Homework 7
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//initializing structures
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 main()
{
//initializing variable and calling functions
int num_shifts;
num_shifts= read_data(shift_data);
sort_data(shift_data, num_shifts);
print_data(shift_data, num_shifts);
return 0;
}
//creating function that reads the text file indicated by the user
//and then storing the data into a structure array
int read_data(struct Shift shift_data[])
{
char input[100];
int x;
int num_shifts;
printf("What is the name of the file? ");
scanf("%s", &input);
FILE *ifp;
ifp = fopen(input, "r");
fscanf(ifp, "%d", &num_shifts);
for(x = 0 ; x < num_shifts; x++)
{
fscanf(ifp, "%s", &shift_data[x].name);
fscanf(ifp, "%s", &shift_data[x].day_of_week);
fscanf(ifp, "%d", &shift_data[x].start_hour);
fscanf(ifp, "%d", &shift_data[x].end_hour);
}
fclose(ifp);
return num_shifts;
}
//creates function that prints out the output in the format specified
void print_data(struct Shift shift[], int num_shifts)
{
int x;
printf("TA Shifts \n");
printf("================================================ \n");
for(x = 0; x < num_shifts; x++)
{
printf(" %s",shift_data[x].name);
printf(" %s",shift_data[x].day_of_week);
if(shift_data[x].start_hour>12)
{
shift_data[x].start_hour=shift_data[x].start_hour-12;
printf("%2d:00 pm to ", shift_data[x].start_hour);
}
else if(shift_data[x].start_hour<12)
{
printf("%2d:00 am to ", shift_data[x].start_hour);
}
else if(shift_data[x].start_hour==12)
{
printf("%2d:00 pm to ", shift_data[x].start_hour);
}
if(shift_data[x].end_hour>12)
{
shift_data[x].end_hour=shift_data[x].end_hour-12;
printf("%2d:00 pm\n", shift_data[x].end_hour);
}
else if(shift_data[x].end_hour<12)
{
printf("%2d:00 am\n", shift_data[x].end_hour);
}
else if(shift_data[x].end_hour==12)
{
printf("%2d:00 pm\n", shift_data[x].end_hour);
}
}
}
void sort_data(struct Shift shift_data[],int num_shifts)
{
int x,y;
for(x = 0 ; x < num_shifts ; x++)
{
for(y = 0; y < num_shifts ; y++)
{
if(strcmp(shift_data[x].name,shift_data[y].name)<0)
{
strcpy(temp.name, shift_data[x].name);
strcpy(shift_data[y].name, shift_data[x].name);
strcpy(shift_data[y].name, temp.name);
}
if(strcmp(shift_data[x].name,shift_data[y].name)<0)
{
strcpy(temp.day_of_week, shift_data[x].day_of_week);
strcpy(shift_data[y].day_of_week, shift_data[x].day_of_week);
strcpy(shift_data[y].day_of_week, temp.day_of_week);
}
if(strcmp(shift_data[x].name,shift_data[y].name)<0)
{
strcpy(temp.start_hour, shift_data[x].start_hour);
strcpy(shift_data[y].start_hour, shift_data[x].start_hour);
strcpy(shift_data[y].start_hour, temp.start_hour);
}
if(strcmp(shift_data[x].name,shift_data[y].name)<0)
{
strcpy(temp.end_hour, shift_data[x].end_hour);
strcpy(shift_data[x].end_hour, shift_data[y].end_hour);
strcpy(shift_data[y].end_hour, temp.end_hour);
}
}
}
}
【问题讨论】: