初学者C作业

时间:2015-09-28 15:28:53

标签: c arrays function pointers struct

这是我的代码,我遇到了问题。我一直收到这些错误:

  

bad.c:在函数'driver'中:

     

bad.c:41:警告:初始化使得指针来自整数而没有   投

     

bad.c:49:警告:赋值从没有a的整数中生成指针   投

     

bad.c:60:错误:函数'searchStudentID'的参数太少

     

bad.c:顶级:

     

bad.c:89:错误:'readStudents'的冲突类型

     

bad.c:41:注意:先前隐含的'readStudents'声明是   这里

     

bad.c:在函数'readStudents'中:

     

bad.c:95:警告:赋值从没有a的整数中生成指针   投

     

bad.c:103:错误:'fclose'之前预期';'

     

bad.c:在函数'printStudents'中:

     

bad.c:116:错误:请求成员'FName'的东西不是   结构或联盟

     

bad.c:116:错误:请求成员'LName'的东西不是   结构或联盟

     

bad.c:116:错误:请求成员'GPA'的东西不是   结构或联盟

     

bad.c:116:错误:在某个结构中请求成员'ID'   或者工会

     

bad.c:119:警告:'return'带有一个值,函数返回void

     

bad.c:在函数'searchStudentID'中:

     

bad.c:131:错误:无效操作数到二进制==(有'int'和   “学生”)

     

bad.c:顶级:

     

bad.c:160:错误:'sortStudentsByLastName'的冲突类型

     

bad.c:49:注意:之前的隐式声明   'sortStudentsByLastName'在这里

     

bad.c:在函数'sortStudentsByLastName'中:

     

bad.c:176:错误:初始化类型'int'时出现不兼容的类型   使用'学生'类型

     

bad.c:178:错误:分配到“学生”类型时出现不兼容的类型   来自'int'类型

     

bad.c:在函数'sortStudentsByID'中:

     

bad.c:201:错误:初始化类型'int'时出现不兼容的类型   使用'学生'类型

     

bad.c:203:错误:分配到“学生”类型时出现不兼容的类型   来自'int'类型

typedef struct student
{
char FName[20];
char LName[20];
float GPA;
int ID;
} Student;

#include <stdio.h>
#include <stdlib.h>
/*
 * 
 */

Student* ReadStudents(char* filename, int* numStudentsRef);
void printStudents(Student* students, int numStudents);
Student* searchStudentID(Student* students, int low, int high, int targetID);
Student* sortStudentsByID(Student* students, int numStudents);
Student* findHighestGPA(Student* students, int numStudents); 
void writeToReadableFile(Student* students, int numStudents, char* filename);
void driver(char* filename, char* filename2);

int main(int argc, char** argv) {

driver(argv[0], argv[1]); 

return (EXIT_SUCCESS);
}

void driver(char* filename, char* filename2)
{
    int numStudents;
    Student* students = readStudents(filename, &numStudents);
    printf("Sudents: \n");
    printStudents(students, numStudents);


printf("The highest GPA is %.2f.\n", findHighestGPA(students, numStudents).GPA);

printf("------------\nStudents sorted by last name:\n");
students=sortStudentsByLastName(students, numStudents);
printStudents(students, numStudents);

students = sortStudentsByID(students, numStudents);
int ID;
int control;
while(1)
{
    printf("------------\nGive me a student ID to search for: ");
    scanf("%d", ID); 

    Student* result = searchStudentID(students, numStudents-1, ID);

    if(result = NULL) 
    {
        printf("Student not found.\n");
    }
    else
    {
        printf("Found student: \n");
        printStudents(result, 1);
    }
    printf("\n\nEnter 0 to quit or 1 to search for another ID: ");
    scanf("%d", &control);
    while(control != 1 || control != 0)
    {
        printf("Sorry, I don't understand that command. Try again.\n");
        printf("\n\nEnter 0 to quit or 1 to search for another ID: ");
        scanf("%d", &control);
    }
    if(control == 0)
    {
        break;
    }
}

writeToReadableFile(students, numStudents, filename2);

}

Student* readStudents(char* filename, int* numStudentsRef)
{
    FILE* fp = fopen(filename, "r");

fseek(fp, 0, SEEK_END);

numStudentsRef = (int)ftell(fp) / sizeof(Student);
rewind(fp);

Student* students;

fread(&students, sizeof(Student), *numStudentsRef, fp);

free(students) 
fclose(fp);

return students;

 }

void printStudents(Student* students, int numStudents)
{
int i = 0; 
for(; i < numStudents; i++)
{
    printf("-----%s %s\n\tGPA: %f\n\tStudent ID: %d\n------\n", 
            students.FName, students.LName, students.GPA, students.ID);
}

return 0; 
}

Student* searchStudentID(Student* students, int low, int high, int targetID)
{
if(low<=high)
{
    return NULL;
}

int mid = (low+high)/2;

if(targetID == students[mid])
{
    return &students[mid];
}
else if(targetID > students[mid].ID)
{
    return searchStudentID(students, low, mid, targetID);
}
else
{
    return searchStudentID(students, mid+1, high, targetID);
}
}

Student* findHighestGPA(Student* students, int numStudents)
{
Student* max = &students[0];
int i = 1;
for(i; i < numStudents; i++)
{
    if(students[i].GPA > max->GPA)
    {
        max = &students[i];
    }
}

return max;
}

Student* sortStudentsByLastName(Student* students, int numStudents)
{
int i, j, min;

for(j = 0; j < numStudents-1; j++)
{
    for(i = j+1; i < numStudents; i++)
    {
        if(strcmp(students[i].LName, students[min].LName) > 0) 
        {
            min = i;
        }
    }

    if(min != j)
    {
        int tmp = students[j];
        students[j] = students[min];
        students[min] = tmp;
    }
}

return students;
}

Student* sortStudentsByID(Student* students, int numStudents)
{
int i, j, min;
for(j = 0; j < numStudents-1; j++)
{
    min = j;
    for(i = j+1; i < numStudents; i++)
    {
        if(students[i].ID < students[min].ID)
        {
            min = i;
        }
    }

    if(min != j)
    {
        int tmp = students[j];
        students[j] = students[min];
        students[min] = tmp;
    }
}

return students;
}

void writeToReadableFile(Student* students, int numStudents, char* filename)
{
FILE* fp = fopen(filename, "w");
int i = 0;
for(;i < numStudents; i++)
{
    fprintf(fp, "%s %s\n", students[i].FName, students[i].LName);
    fprintf(fp, "%d %d\n", &students[i].GPA, &students[i].ID);
}
fclose(fp);
free(students);
}

1 个答案:

答案 0 :(得分:1)

天啊,我不知道为什么我要回答这个问题,但是......

无论如何,这里有一些你需要解决的问题:

l38   )->GPA // You need -> for struct pointer, not a dot.
l52   (students, 0, numStudents-1, ID); // You are missing a parameter.
l15   readStudents // lower case for the first letter.
l122  if(targetID == students[mid]->ID) // You are comparing student to a number, I guess you want to compare the ID, right?

还有很多人......

我真的不知道你是怎么想出这些代码的。你在某些地方犯了错误,但在其他地方却没有。你知道我的意思吗?

无论如何,要注意类型。学生[mid]为您提供一个学生,您可以从中获得所需的属性,例如ID。不要混淆。指向学生的指针与参考不同。

Student s;
Student *ps;

他们是不同的。你必须以不同的方式使用它们。

如果你还没有这样做,你需要仔细检查你的代码。

但我怀疑这对其他人有帮助,真的。