C语言程序设计-笔记8-结构

例9-1  输出平均分最高的学生信息。根据学生的基本信息包括学号、姓名、三门课程成绩以及个人平均成绩。输入n个学生的成绩信息,计算并输出平均分最高的学生信息。

#include<stdio.h>

struct student{

int num;

char name[10];

int computer,english,math;

double average;

};

int main(void)

{

int i,n;

struct student max,stu;

printf("Input n:");

scanf("%d",&n);

printf("Input the student's number,name and course scores\n");

for(i=1;i<=n;i++)

{

printf("No.%d:",i);

scanf("%d%s%d%d%d",&stu.num,stu.name,&stu.math,&stu.english,

&stu.computer);

stu.average=(stu.math+stu.english+stu.computer)/3.0;

if(i==1)

{

max=stu;

}

else if(max.average<stu.average)

{

max=stu;

}

}

printf("num:%d,name:%s,average:%.2f\n",max.num,max.name,max.average);

return 0;

}

例9-2  学生成绩排序。输入n(n〈50)个学生的成绩信息,按照学生的个人平均成绩从高到低输出他们的信息。

#include<stdio.h>

struct student{

int num;

char name[10];

int computer,english,math;

double average;

};

int main(void)

{

int i,index,j,n;

struct student students[50],temp;

printf("Input n:");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("Input the info of No.%d:\n",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 score:");

scanf("%d",&students[i].computer);

students[i].average=(students[i].math+students[i].english+

students[i].computer)/3.0;

}

for(i=0;i<n-1;i++)

{

index=i;

for(j=i+1;j<n;j++)

{

if(students[j].average>students[index].average)

{

index=j;

}

}

temp=students[index];

students[index]=students[i];

students[i]=temp;

}

printf("num\t name\t average\n");

for(i=0;i<n;i++)

{

printf("%d \t%s \t%.2f\n",students[i].num,students[i].name,

students[i].average);

}

return 0;

}

例9-3  修改学生成绩。输入n(n<50)个学生的成绩信息,再输入一个学生的学号、课程以及成绩,在自定义函数中修改该学生指定课程的成绩。

#include<stdio.h>

struct student{

int num;

char name[10];

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:\n",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 score:");

scanf("%d",&students[i].computer);

}

printf("Input the number of the students to be updated:");

scanf("%d",&num);

printf("Choice the course:1.math 2.english 3.computer:");

scanf("%d",&course);

printf("Input the new score:");

scanf("%d",&score);

pos=update_score(students,n,num,course,score);

if(pos==-1)

{

printf("Not found!\n");

}

else

{

printf("After update:\n");

printf("num\t math\t english\t computer\n");

printf("%d\t %d\t %d\t %d\n",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;break;

}

pos=i;

}

else

{

pos=-1;

}

return pos;

}

参考资料

C语言程序设计/何钦铭,颜晖主编.---4版.---北京:高等教育出版社,2020.9

C语言程序设计-笔记8-结构的更多相关文章

  1. JAVA语言程序设计-笔记摘录

    JAVA 程序语言设计(基础篇) 笔记摘录 为避免输入错误, 不要在nextByte().nextShort().nextInt()等等后面使用nextLine() nextXXXXX()都称为令牌读 ...

  2. [C语言入门笔记]分支结构与数组

    分支结构与数组 什么是分支结构? 分支结构是用户或者程序可以选择下一步执行哪个语句 分支结构有哪些? If If Else If Else If Switch 在初学者的学习过程中第一种和第二种比较普 ...

  3. C语言学习笔记--枚举&结构体

    枚举 枚举是一种用户定义的数据类型,它用关键字enum以如下语法格式来声明: enum 枚举类型名字 {名字0,名字1,...,名字n}: 枚举类型名字通常并不真的使用,要用的是大括号里面的名字,因为 ...

  4. c语言学习笔记之结构体存储

    今天讲讲结构体存储问题 首先,结构体简单说是对不同类型的封装,一开始我们可能会想结构体在内存中的存储的大小是直接元素的和 例如 我们可能会觉得是 结构体大小=int(4个字节)+ short(2个字节 ...

  5. Python语言程序设计(笔记)

    1.平方根的格式化 知识点:平方根计算 pow(a,0.5)[可以计算负数,结果为复数] a**b 例题: 获得用户输入的一个整数a,计算a的平方根,保留小数点后3位,并打印输出.‪‬‪‬‪‬‪‬‪‬ ...

  6. 160809209_李梦鑫_C语言程序设计实验3 循环结构程序设计

    <C语言程序设计>实验报告 学 号 160809209 姓 名 李梦鑫 专业.班 计科16-2班 学    期 2016-2017 第1学期 指导教师 黄俊莲 吉吉老师 实验地点 C05 ...

  7. 160809209_李梦鑫_C语言程序设计实验2+选择结构程序设计_进阶

    <C语言程序设计>实验报告 学 号 160809209 姓 名 李梦鑫 专业.班 计科16-2班 学    期 2016-2017 第1学期 指导教师 黄俊莲 吴喆 实验地点 C05 机 ...

  8. C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | IT宅.com

    原文:C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | IT宅.com C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表 | I ...

  9. C语言程序设计第六次作业——循环结构(2)

    C语言程序设计第六次作业--循环结构(2) 之前的博客园图片没处理好,对大家说一声抱歉.希望大家能够多多指出我的错误,我来认真修改 ^ - ^ !. (1)改错题 序列求和:输入一个正实数eps,计算 ...

  10. C语言程序设计第二次作业--顺序结构

    C语言程序设计第二次作业--顺序结构 1.输出带框文字:在屏幕上输出以下3行信息. ************* Welcome ************* 源程序 #include <stido ...

随机推荐

  1. NSSCTF—Crypyo "第一页" ԅ(≖‿≖ԅ) (待续……)

    [鹤城杯 2021]easy_crypto 题目: 公正公正公正诚信文明公正民主公正法治法治诚信民主自由敬业公正友善公正平等平等法治民主平等平等和谐敬业自由诚信平等和谐平等公正法治法治平等平等爱国和谐 ...

  2. ubuntu18.04如何运行.exe文件

    在Ubuntu上安装Wine 到wine官网查看的安装步骤 如果您之前安装过来自其他仓库的 Wine 安装包,请在尝试安装 WineHQ 安装包之前删除它及依赖它的所有安装包(如:wine-mono. ...

  3. Boruta特征选择

    Boruta特征选择 官方github地址:https://github.com/scikit-learn-contrib/boruta_py?tab=readme-ov-file 论文地址:http ...

  4. css实现多余文字隐藏,用省略号代替

    .txt{ overflow: hidden; //溢出内容隐藏 white-space: nowrap; //强制文本在一行内显示 text-overflow: ellipsis; //当对象内文本 ...

  5. Python---flask框架实现免密登录功能

    思路总结: html代码: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta c ...

  6. #点分树#洛谷 6626 [省选联考 2020 B 卷] 消息传递

    题目 多组数据多组询问,对于一个点 \(x\) 和 树上的距离 \(k\),问 \(\sum_{i=1}^n[Dis(x,i)==k]\) 分析 卡了一页的常,发现两个 \(\log\) 过不去,有一 ...

  7. #拓扑排序#洛谷 4645 [COCI2006-2007 Contest#3] BICIKLI

    题目 这个地方有 \(n\) 个城镇,从 \(1\sim n\) 编号, 其中有 \(m\) 条单向道路连接它们. 比赛将在 \(1\) 号城镇开始并在 \(2\) 号城镇结束. 主办方想知道,一共有 ...

  8. 成长计划校园极客秀 | 玩转OpenHarmony开发智能煤气检测系统

    成果展示 1.整体展示 2.碰一碰无感配网 3.报警 简介 目前,煤气泄漏给居民生活带来伤害的事情仍时有发生,但我相信万物互联能够有效避免这种伤害,于是我基于OpenHarmony设计了一款煤气检测装 ...

  9. Go 语言中结构体的使用和示例

    结构体(简称struct)用于创建不同数据类型的成员集合,放入一个单一的变量中.虽然数组用于将相同数据类型的多个值存储在单一变量中,但结构体用于将不同数据类型的多个值存储在单一变量中.结构体对于将数据 ...

  10. 【FAQ】HarmonyOS SDK 闭源开放能力 —Push Kit(2)

    1.问题描述: 开发服务端推送,客户端能收到离线推送,但是推送收到的通知只能从手机顶部下拉看到,无法收到一个顶部的弹框.请问是什么原因? 解决方案: 可能原因一: 消息提醒的方式与消息类别有关,比如: ...