我正在尝试处理我的代码,由于尝试将特定阵列位置的结构复制到另一个位置的同一阵列中的另一个,导致崩溃,因此我无法使用排序算法。
//include statements
#include <stdio.h>
#include <stdlib.h>
//defines
#define MAX 100
#define STRUCTMAX 26
#define STUDENTS 9
//Struct declarations
typedef struct{
char street[STRUCTMAX];
char city[STRUCTMAX];
char state[STRUCTMAX];
char zip[STRUCTMAX];
}Address;
typedef struct{
char firstName[STRUCTMAX];
char initial[STRUCTMAX];
char lastName[STRUCTMAX];
Address adress;
int age;
double gpa;
}Student;
//prototypes
void readFile(Student students[]);
void printAll(Student students[]);
void printBestGpaName(Student students[]);
double averageGPA(Student students[]);
void printAboveAverageGPA(Student students[]);
void printYoungestLowGPA(Student students[]);
void sortStruct(Student students[]);
void strSub(char s1 [], char s2 [], int start, int size);
void initialize(Student students[]);
void main(void){
Student students[STUDENTS];
readFile(students);
printAll(students);
printBestGpaName(students);
printf("Average G.P.A is %.2lf\n" ,averageGPA(students));
printAboveAverageGPA(students);
printYoungestLowGPA(students);
sortStruct(students);
printf("\n");
printAll(students);
}
void readFile(Student students[]){
int i = 0;
char numberValue[10];
char line[MAX];
FILE *fp;
fp = fopen(
"/Users/derekroy/Desktop/Lab_6/Lab_6A/Students.dat", "r");
if (fp == NULL) {
printf("Students.dat file not found.\n");
exit(1);
}
while (!feof(fp)) {
fgets(line, MAX, fp);
strSub(line, students[i].firstName, 0, 7);
strSub(line, students[i].initial, 10, 1);
strSub(line, students[i].lastName, 11, 9);
strSub(line, students[i].adress.street, 20, 16);
strSub(line, students[i].adress.city, 36, 13);
strSub(line, students[i].adress.state, 49, 2);
strSub(line, students[i].adress.zip, 52, 5);
strSub(line, numberValue, 58, 2);
students[i].age = atoi(numberValue);
strSub(line, numberValue, 60, 5);
students[i].gpa = atof(numberValue);
i++;
}
}
void printAll(Student students[]){
int i;
printf("All listed Students: \n");
for(i = 0; i < STUDENTS; ++i){
printf("%s %s %s %s %s %s, %s %d %.2lf\n" , students[i].firstName, students[i].initial,
students[i].lastName, students[i].adress.street, students[i].adress.city,
students[i].adress.state, students[i].adress.zip, students[i].age, students[i].gpa);
}
printf("\n");
printf("******");
}
void printBestGpaName(Student students[]){
int i, best = 0;
for(i = 0; i < STUDENTS; ++i){
if(students[i].gpa > students[best].gpa)
best = i;
}
printf("Student with best G.P.A: ");
printf("%s %s %s\n" , students[best].firstName, students[best].initial, students[best].lastName);
}
double averageGPA(Student students[]){
int i;
double sum = 0.0;
for(i = 0; i < STUDENTS; ++i){
sum += students[i].gpa;
}
return sum / i;
}
void printAboveAverageGPA(Student students[]){
int i;
double average = averageGPA(students);
printf("Students with above average G.P.A: \n");
for(i = 0; i < STUDENTS; ++i){
if(students[i].gpa > average)
printf("%s %s %s\n" , students[i].firstName, students[i].initial, students[i].lastName);
}
}
void printYoungestLowGPA(Student students[]){
int i, j, young = 1000;
double average = averageGPA(students);
for(i = 0; i < STUDENTS; ++i){
if(students[i].gpa < average){
if(students[i].age < young){
j = i;
young = students[i].age;
}
}
}
printf("The youngest student with a below average G.P.A: ");
printf("%s %s %s\n" ,students[j].firstName, students[j].initial, students[j].lastName);
}
void sortStruct(Student students[]){
int i, j;
Student temp;
for(i = 1; i < STRUCTMAX; ++i){
/*temp.firstName = students[i].firstName;
temp.initial = students[i].initial;
temp.lastName = students[i].lastName;
temp.adress.street = students[i].adress.street;
temp.adress.city = students[i].adress.city;
temp.adress.state = students[i].adress.state;
temp.adress.zip = students[i].adress.zip;
temp.age = students[i].age;
temp.gpa = students[i].gpa;*/
temp = students[i];
j = i - 1;
while(j >= 0 && temp.gpa < students[j].gpa){
//students[j+1] = students[j];
j = j - 1;
}
//students[j+1] = temp;
}
}
void strSub(char s1 [], char s2 [], int start, int size){
int i;
for(i = 0; i < size; ++i){
s2[i] = s1[start];
start++;
}
s2[i] = '\0';
}
违规行在Sort函数中。 如何使这项工作,并将指定的结构复制到结构数组中的新地址?
答案 0 :(得分:2)
您只定义了9条学生记录
Student students[STUDENTS]; -> STUDENTS macro is 9
您正在迭代直到STRUCTMAX,这是一个定义为26的宏 所以你将走出界限导致崩溃
for(i = 1; i < STRUCTMAX; ++i){