zoukankan      html  css  js  c++  java
  • 【C语言程序设计第四版】例9-3代码

    #include <stdio.h>
    
    struct student{
        int num;
        char name[40];
        int computer, english, math;
        double average;
    };
    int update_score(struct student *p, int n, int num, int course, int score);
    
    int main(void){
        int course, i , n, num, pos, score;
        struct student students[50];
        
        printf("Input n:");
        scanf("%d", &n);
        for (i = 0; i < n; i++) {
            printf("Input the info of No.%d:
    ", i+1);
            printf("number:");
            scanf("%d", &students[i].num);
            printf("name:");
            scanf("%s", students[i].name);
            printf("math score:");
            scanf("%d", &students[i].math);
            printf("english score:");
            scanf("%d", &students[i].english);
            printf("computer socre:");
            scanf("%d", &students[i].computer);
            students[i].average = (students[i].math + students[i].english + students[i].computer) / 3.0;
        }
        
        printf("input the number of the student to be updated");
        scanf("%d", &num);
        printf("Choice of course:1.math 2.english 3.computer");
        scanf("%d", &course);
        printf("Input the new sorce");
        scanf("%d", &score);
        
        pos = update_score(students, n, num, course, score);
        
        if (pos == -1) {
            printf("Not found!
    ");
        }else{
            printf("After update:
    ");
            printf("num	 math	 english	 computer 
    ");
            printf("%d	 %d	 %d	 %d
    ", students[pos].num, students[pos].math, students[pos].english, students[pos].computer);
        }
            
        
        return 0;
    }
    
    int update_score(struct student *p, int n, int num, int course, int score){
        int i, pos;
        for (i = 0; i < n; i++, p++) {
            if (p->num == num) break;
            
        }
        if (i < n) {
            switch (course) {
                case 1:
                    p ->math = score;
                    break;
                case 2:
                    p->english = score;
                    break;
                case 3:
                    p->computer =score;
                    break;
            }
            pos = i;
        }else{
            pos = -1;
        }
        
        return pos;
    }
  • 相关阅读:
    K-Means++ 聚类之数据可视化:使用gnuplot
    QQ设计第1-5步
    QQ设计第1-5步
    为什么有很深的windows基础还是不能动摇linux半步
    常用命令
    在线会计_金蝶友商网
    XP使用VNC远程桌面CentOS 6
    Fatal error: Call to undefined function mb_substr()
    如何汉化 po 文件及编译成 mo 文件
    idoerp
  • 原文地址:https://www.cnblogs.com/sidianok/p/15311421.html
Copyright © 2011-2022 走看看