使用与下面建议的方法不同的方法解决了我自己的问题!:)
感谢您查看我的问题!:)
我一直在学习结构和在C中的练习实验室工作,我的代码似乎没有正确编译我对它做的任何改变.目前我没有收到任何输出,程序崩溃.我仍然非常困惑如何在将它们传递给函数时正确使用'*'和'&'符号.我的目标是:
以与数据文件相同的格式打印数组的内容
打印具有最佳GPA的学生的全名
计算并打印平均GPA
打印GPA高于平均水平的所有学生的姓名
打印GPA低于平均水平的最年轻学生的姓名
按照从最低到最高GPA的顺序对数组中的结构进行排序
再次打印数组(现在将与上次不同)
如何从学生结构中正确地调用和打印这些项目?我如何访问gpa值以传递给计算平均值的函数?
#include#include // 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); }
sharon.. 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; } }
试试这个.....
在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; } }
试试这个.....