gpt4 book ai didi

c - 如何将结构体数组复制到同一数组中的另一个结构体?

转载 作者:行者123 更新时间:2023-11-30 20:14:15 25 4
gpt4 key购买 nike

我正在尝试处理我的代码,但由于尝试将特定数组位置的结构复制到同一数组中不同位置的另一个结构而导致崩溃,因此我无法使用排序算法。

//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';
}

有问题的行位于排序功能中。我怎样才能做到这一点,并将指定的结构复制到结构数组中的新地址?

最佳答案

您仅定义了 9 条学生记录

Student students[STUDENTS];   -> STUDENTS macro is 9

您正在迭代直到 STRUCTMAX 这是一个定义为 26 的宏所以你会出界导致崩溃

for(i = 1; i < STRUCTMAX; ++i){

关于c - 如何将结构体数组复制到同一数组中的另一个结构体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27140427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com