统计科目以及学生

#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
    int xuehao;
    char name[30];
    int Class;
    char sex[6];
    float ZGPA;
    float KGPA;
}Student;
typedef struct course
{
    int daihao;
    char name[30];
    float GPA;
    float xuefen;
    float chengji;
}Course;
typedef struct Lcourse
{
    struct course data;
    struct Lcourse *next;
}CourseListNode;
struct Lstudent
{
    struct student data;
    struct Lcourse *chead;
    struct Lstudent *next;
};
//输入姓名,学号,班级,性别
Student stinputdata(Student StudentNode)
{
    printf("请依次输入以下信息:姓名,学号,班级,性别(男,女)\n");
    scanf("%s %d %d %s",StudentNode.name,&StudentNode.xuehao,&StudentNode.Class,StudentNode.sex);
    return StudentNode;
}
//输入课程编号,课程名称,课程学分,课程成绩
Course coinputdata(Course CourseNode)
{
     do {
        printf("请依次输入以下信息:课程编号,课程名称,课程学分,课程成绩(0-100)\n");
        scanf("%d %s %f %f", &CourseNode.daihao, CourseNode.name, &CourseNode.xuefen, &CourseNode.chengji);
        // 验证成绩是否在0-100之间
        if (CourseNode.chengji < 0 || CourseNode.chengji > 100) {
            printf("输入的课程成绩无效,请确保成绩在0到100之间。\n");
        }
    } while (CourseNode.chengji < 0 || CourseNode.chengji > 100);
    float a;
    a=CourseNode.chengji;
    if (a < 60) { CourseNode.GPA = 0; }
    else if (a >= 90) { CourseNode.GPA = 4.0; }
    else if (a<64&&a >= 60) { CourseNode.GPA = 1.0; }
    else if (a < 67&&a>=64) { CourseNode.GPA= 1.3; }
    else if (a < 70 && a >= 67) { CourseNode.GPA = 1.7; }
    else if (a < 74 && a >= 70) { CourseNode.GPA = 2.0; }
    else if (a < 77 && a >= 74) { CourseNode.GPA = 2.3; }
    else if (a < 80 && a >= 77) { CourseNode.GPA = 2.7; }
    else if (a < 84 && a >= 80) { CourseNode.GPA = 3.0; }
    else if (a < 87 && a >= 84) { CourseNode.GPA = 3.3; }
    else if (a < 90 && a >= 87) { CourseNode.GPA = 3.7; }
    return CourseNode;
}
struct Lstudent* initStudentList() {
    return NULL;
}
struct Lstudent* addStudentToList(struct Lstudent* head) {
    Student newStudent;
    struct Lstudent* newNode = (struct Lstudent*)malloc(sizeof(struct Lstudent));
    if (!newNode) {
        printf("内存分配失败!\n");
        return head;
    }

    newStudent = stinputdata(newStudent);

    // 检查学号是否已存在
    struct Lstudent* currStudent = head;
    while (currStudent != NULL) {
        if (currStudent->data.xuehao == newStudent.xuehao) {
            printf("学号 %d 已存在,不能重复添加,请重新输入!\n", newStudent.xuehao);
            free(newNode); // 释放新分配的内存
            return head;
        }
        currStudent = currStudent->next;
    }

    newNode->chead=NULL;
    newNode->data = newStudent;
    newNode->next = head;
    return newNode;
}
void printStudentList(struct Lstudent* head) {
    while (head != NULL) {
        printf("姓名:%s,学号:%d,班级:%d,性别:%s\n",
               head->data.name,
               head->data.xuehao,
               head->data.Class,
               head->data.sex);
               head = head->next;
    }
}
CourseListNode* addCourseToListForStudent(CourseListNode** chead) {
    // 调用输入函数获取课程信息
    Course newCourse = coinputdata((Course){});

    // 创建新节点并分配内存
    CourseListNode* newNode = (CourseListNode*)malloc(sizeof(CourseListNode));
    if (!newNode) {
        printf("内存分配失败!\n");
        return *chead;
    }

    // 将获取的课程信息赋值给新节点
    newNode->data = newCourse;
    newNode->next = *chead;
    *chead = newNode;
    return newNode;
}
void printStudentCourses(CourseListNode* chead) {
    CourseListNode* current = chead;
    while (current != NULL) {
        printf("课程编号:%d,课程名称:%s,课程学分:%f,课程成绩:%.1f, 课程绩点:%.1f",
               current->data.daihao, current->data.name, current->data.xuefen, current->data.chengji, current->data.GPA);
        current = current->next;
    }
}

struct Lstudent* findStudentByXuehao(struct Lstudent* studentsHead, int targetXuehao) {
    struct Lstudent* currStudent = studentsHead;
    while (currStudent != NULL) {
        if (currStudent->data.xuehao == targetXuehao) {
            return currStudent; // 找到匹配的学生节点,返回其指针
        }
        currStudent = currStudent->next;
    }
    return NULL; // 如果没有找到匹配的学生,则返回NULL
}

int countCoursesForStudent(struct Lstudent* student) {
    int courseCount = 0;
    CourseListNode* currentCourse = student->chead;

    while (currentCourse != NULL) {
        courseCount++;
        currentCourse = currentCourse->next;
    }

    return courseCount;
}

int countStudents(struct Lstudent* studentsHead) {
    int studentCount = 0;
    struct Lstudent* currStudent = studentsHead;

    while (currStudent != NULL) {
        studentCount++;
        currStudent = currStudent->next;
    }
    return studentCount;
}

int main() {
    // 初始化链表
    struct Lstudent* head = NULL; // 初始化为空指针
    initStudentList(head); // 假设initStudentList负责初始化链表并返回头指针

    // 添加学生到链表
    char continueInput = 'y';
    char continueCourseInput='y';
    while (continueInput == 'y' || continueInput == 'Y') {
        head = addStudentToList(head); // 添加学生到链表

        printf("是否继续输入学生信息?(y/n): ");
        scanf(" %c", &continueInput); // 注意前面的空格用于跳过回车符
    }
    int targetXuehao;
    printf("请输入目标学号:");
    scanf("%d", &targetXuehao);
    struct Lstudent* targetStudent = findStudentByXuehao(head, targetXuehao);


    if (targetStudent == NULL) {
        printf("未找到学号为%d的学生。\n", targetXuehao);
    } else {
        int coursesNumber = countCoursesForStudent(targetStudent);
        printf("目标学生已修的课程数目为:%d\n", coursesNumber);
        char continueCourseInput = 'y';
        while (continueCourseInput == 'y' || continueCourseInput == 'Y') {
            targetStudent->chead = addCourseToListForStudent(&targetStudent->chead);

            printf("是否继续输入该学生的课程信息?(y/n): ");
            scanf(" %c", &continueCourseInput);
        }
    }

 // 调用addStudentToList函数,并更新头指针
    printf("\n已输入的学生信息如下:\n");
    printStudentList(head);
    if (targetStudent != NULL) {
        printf("\n目标学生的课程信息如下:\n");
        printStudentCourses(targetStudent->chead);
        int coursesNumber = countCoursesForStudent(targetStudent);
        printf("\n目标学生已修的课程数目为:%d\n", coursesNumber);
    }
    // 其他操作...
     int totalStudents = countStudents(head);
      printf("\n系统中共有 %d 名学生。\n", totalStudents);
    return 0;
}
 

  • 18
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值