年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13655|回复: 1

C++实现的一个学生成绩管理示例

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2016-5-20 14:12:14 | 显示全部楼层 |阅读模式
    [C++] 纯文本查看 复制代码
    //
    //  main.cpp
    //  Hello
    //
    //  Created by 余西安 on 16/5/19.
    //  Copyright © 2016年 Sian. All rights reserved.
    //
    
    #include <stdio.h>
    #include <string.h>
    
    // 创建基本数据结构
    struct Student{
        int id;
        char name[10];
        int score[3];
    };
    
    // 创建数据存储体
    Student sa[100];
    int count = 0;
    
    // 创建实体对象
    Student create(){
        Student s;
        printf("ID:");
        scanf("%d", &s.id);
        printf("Name:");
        scanf("%s", s.name);
        printf("Score:");
        scanf("%d, %d, %d", &s.score[0], &s.score[1], &s.score[2]);
        return s;
    }
    
    // 创建添加函数
    int add(const Student *s){
        if (count > 100) return -1;
        sa[count] = *s;
        count++;
        return 0;
    }
    
    // 创建查找方法
    int find(char name[10], Student *s){
        for(int i = 0; i < count ; i++){
            Student fs = sa[i];
            if (strcmp(name, fs.name) == 0){
                s->id = fs.id;
                strcpy(s->name, fs.name);
                s->score[0] = fs.score[0];
                s->score[1] = fs.score[1];
                s->score[2] = fs.score[2];
                return 0;
            }
        }
        return -1;
    }
    
    // 创建展现方法
    void list(){
        if (count == 0){
            printf("Nothing...\n");
            return;
        }
        printf("---------------------\n");
        printf("ID\tName\tScore\t\n");
        printf("---------------------\n");
        for (int i = 0; i < count; i++){
            Student s = sa[i];
            printf("%d\t%s\t%d,%d,%d\n", s.id, s.name, s.score[0], s.score[1], s.score[2]);
        }
        printf("---------------------\n");
    }
    
    int main(){
        while(true){
            char cmd[10];
            printf(">");
            scanf("%s", cmd);
            // 录入成绩
            if(strcmp(cmd, "add") == 0){
                Student s = create();
                int resutl = add(&s);
                if (resutl == 0) printf("添加成功!\n");
                if (resutl == -1) printf("添加失败!\n");
                continue;
            }
            if (strcmp(cmd, "list") == 0){
                list();
                continue;
            }
            if(strcmp(cmd, "exit") == 0){
                printf("Bye!\n");
                break;
            }
            if (strcmp(cmd, "count") == 0){
                printf("Total:%d\n", count);
                continue;
            }
            if (strcmp(cmd, "find") == 0){
                Student s;
                char name[10];
                printf("请输入需要查询的学生姓名:");
                scanf("%s", name);
                int result = find(name, &s);
                if (result == 0) printf("%d\t%s\t%d,%d,%d\n", s.id, s.name, s.score[0], s.score[1], s.score[2]);
                if (result == -1) printf("没有查询到相关信息\n");
                continue;
            }
            printf("Error Code, please input code like \"add\", \"list\", \"exit\", \"count\"\n");
        }
        return 0;
    }

  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

     楼主| 发表于 2016-5-20 14:13:32 | 显示全部楼层
    调试输出结果:
    [C++] 纯文本查看 复制代码
    >?
    Error Code, please input code like "add", "list", "exit", "count"
    >list
    Nothing...
    >add
    ID:1
    Name:sian
    Score:1,2,3
    添加成功!
    >list
    ---------------------
    ID	Name	Score	
    ---------------------
    1	sian	1,2,3
    ---------------------
    >count
    Total:1
    >find
    请输入需要查询的学生姓名:sian
    1	sian	1,2,3
    >exit
    Bye!
    Program ended with exit code: 0
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-12-4 16:29 , Processed in 0.044388 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表