gpt4 book ai didi

c - 如何在 C 中打印结构体的内容?

转载 作者:行者123 更新时间:2023-11-30 21:41:23 24 4
gpt4 key购买 nike

我自己解决了问题,使用的方法与下面建议的方法不同! :)

感谢您查看我的问题! :)

我一直在学习结构体并在 C 语言的实践实验室中工作,但我的代码似乎无法通过我对其所做的任何更改正确编译。 目前我没有收到任何输出,程序崩溃了。我仍然对如何在将“*”和“&”符号传递给函数时正确使用它们感到非常困惑。我的这种做法的目标是:

  • 以与数据文件相同的格式打印数组内容
  • 打印出 GPA 最高的学生的全名
  • 计算并打印平均 GPA
  • 打印出所有 GPA 高于平均水平的学生的姓名
  • 打印 GPA 低于平均水平的最小学生的姓名
  • 将数组中的结构按从低到高的顺序排序平均绩点
  • 再次打印数组(现在的顺序与上次的顺序不同)时间)

如何正确调用并打印学生结构中的这些项目?我如何访问 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);
}

最佳答案

在 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;
}
}

试试这个......

关于c - 如何在 C 中打印结构体的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27328806/

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