作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我自己解决了问题,使用的方法与下面建议的方法不同! :)
感谢您查看我的问题! :)
我一直在学习结构体并在 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);
}
最佳答案
在 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/
我是一名优秀的程序员,十分优秀!