使用与下面建议的方法不同的方法解决了我自己的问题! :)
感谢您查看我的问题! :)
我一直在学习C语言中的结构和练习实验室,而且我的代码似乎没有正确编译我对其所做的任何更改。 目前我没有收到任何输出,程序崩溃。我仍然很困惑如何正确使用' *'和'&'将符号传递给函数时的符号。我的目标是:
如何从学生结构中正确调用和打印这些项目?我如何访问gpa值以传递给计算平均值的函数?
#include <stdio.h>
#include <stdlib.h>
// define constants
#define ARR 100
#define FIRST 7
#define MIDINIT 1
#define LAST 9
#define STREET 16
#define CITY 11
#define STATE 2
#define ZIP 5
#define AGE 3
#define GPA 4
#define START 0
#define FIRSTID 8
#define INITID 10
#define STREETID 20
#define CITYID 37
#define STATEID 49
#define ZIPID 52
#define AGEID 57
#define GPAID 64
// defined structs
typedef struct {
char street[STREET + 1];
char city[CITY + 1];
char state[STATE + 1];
char zip[ZIP + 1];
} Address;
typedef struct {
char firstname[FIRST + 1];
char initial[MIDINIT + 1];
char lastname[LAST + 1];
Address ofstudent;
int age;
double gpa;
} Student;
// function prototype
void strsub(char buf[], char s[], int start, int size);
void processStudent(int *id, Student students[]);
void sortStudentGpa(Student *students, int id);
void maxGpa(Student *students, int id);
/* lab6student.c: creates an array of student structures and outputs reports */
int main(void)
{
Student students[ARR]; // creates an array of student structures
int id = 0; // counter for student
processStudent(&id, students);
maxGpa(students, id);
}
void strsub(char buf[], char s[], int start, int size) {
int i;
for (i = 0; i < size && buf[start + i] != '\0'; i++) {
// loops as long as iterator is less than size
// and while string has not run out of characters
s[i] = buf[i + start];
}
s[i] = '\0';
}
/* void sort(Student *students, int id) {
int j, i;
for(i = 1; i < n; i++) {
for(j = 0; j < id - i; j++) {
if(students[j].gpa > students[j + 1].gpa) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
} */
void processStudent(int *id, Student students[]) {
FILE *data;
char line[ARR];
*id = 0; // counter for student
data = fopen("Students.dat", "r");
if (data == NULL) {
printf("Students.dat file not found!\n");
exit(1);
}
// process file
while (!feof(data)) {
// organize student info into separate arrays
fgets(line, ARR, data);
strsub(line, students[*id].firstname, START, FIRST);
strsub(line, students[*id].initial, FIRSTID, MIDINIT);
strsub(line, students[*id].lastname, INITID, LAST);
strsub(line, students[*id].ofstudent.street, STREETID, STREET);
strsub(line, students[*id].ofstudent.city, CITYID, CITY);
strsub(line, students[*id].ofstudent.state, STATEID, STATE);
strsub(line, students[*id].ofstudent.zip, ZIPID, ZIP);
students[*id].age = atoi(&line[AGEID]);
students[*id].gpa = atoi(&line[GPAID]);
(*id)++;
}
fclose(data);
}
//sorts struct student array containing num (gpa) elements into
//ascending order
void sortStudentGpa(Student *students, int id) {
int i, j; // indexes into unsorted and sorted partitions
Student temp; // temporarily holds an element from the array
for (i = 1; i < id; ++i) {
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 maxGpa(Student *students, int id) {
int iwithmax, i;
float max = 0;
for ( i = 0 ; i < id ; i++) {
if (students -> gpa > max) {
max = students -> gpa;
iwithmax = i;
}
}
printf("\n\nHighest GPA is done by Student %d with GPA = %f", iwithmax, max);
}
答案 0 :(得分:6)
在maxGpa函数中,只需将定义更改为
void maxGpa(Student *students, int id);
然后在maxGpa函数中进行以下更改
void maxGpa(Student *students, int id) {
int iwithmax, i;
float max = 0;
for ( i = 0 ; i < id ; i++) {
if (students -> gpa > max) {
max = students -> gpa;
iwithmax = i;
}
}
试试这个......