抄写第九章例题

例9.1

#include<stdio.h>
#include<stdlib.h>
main()
{struct Student
{long int num;
char name[20];
char sex;
char addr[20];
}a={10101,"li lin",‘M‘,"123 beijing Road"};
printf("NO.;%ld\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,a.addr);
return 0;
}
NO.;10101
name:li lin
sex:M
address:123 beijing Road

--------------------------------
Process exited after 0.434 seconds with return value 0
请按任意键继续. . .

例9.2

#include<stdio.h>
#include<stdlib.h>
int main()
{struct Student
 {int num;
char name[20];
float score;
};
struct Student student1={10101,"wang",89};
struct Student student2={10103,"ling",98};
if(student1.score>student2.score)
printf("%d,%s,%f",student1.num,student1.name,student1.score);
else if(student1.score<student2.score)
printf("%d,%s,%f",student2.num,student2.name,student2.score);
else
{
printf("%d,%s,%f",student1.num,student1.name,student1.score);
printf("%d,%s,%f",student2.num,student2.name,student2.score);
}
return 0;
}
10103,ling,98.000000
--------------------------------
Process exited after 0.3591 seconds with return value 0
请按任意键继续. . .

例9.3

#include<string.h>
#include<stdio.h>
struct Person//声明结构体类型 struct person
{char name[20];//候选人姓名
int count;//候选人的票数
 } leader[3]={"li",0,"zhang",0,"Sun",0};//定义结构体数组并初始化
 int main()
 {int i,j;
 char leader_name[20];
 for(i=1;i<=10;i++)
 {scanf("%s",leader_name);//输入所选的候选人姓名
 for(j=0;j<3;j++)
 if(strcmp(leader_name,leader[j].name)==0)leader[j].count++;
 }
 printf("\nResult:\n");
 for(i=0;i<3;i++)
 printf("%5s:%d\n",leader[i].name,leader[i].count);
 return 0;
  }
 Li
Li
Sun
Zhang
Zhabg
Sun
Li
Sun
Zhang
Li

Result:
   li:0
zhang:0
  Sun:3

--------------------------------
Process exited after 85.36 seconds with return value 0
请按任意键继续. . .

例9.4

#include<stdio.h>
#include<stdlib.h>
struct Student//声明结构体类型 struct person
{
int num;
char name[20];
float score;
};
int main()
{
struct Student stu[5]={{10101,"zhang",78},{10103,"wang",98},{10106,"li",86},{10108,"ling",73},{10110,"sun",100}};//定义结构体数组并初始化
struct Student temp;
const int n=5;
int i,j,k;//定义常变量n
printf("the order is:\n");
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(stu[j].score>stu[k].score)
k=j;
temp=stu[k];stu[k]=stu[i];stu[i]=temp;    //stu[k]和stu[i]元素互换
}
for(i=0;i<n;i++)
printf("%d,%s,%d",stu[i].num,stu[i].name,stu[i].score);
printf("\n");
return 0;
}
the order is:
10110,sun,010103,wang,010106,li,010101,zhang,010108,ling,0

--------------------------------
Process exited after 0.2652 seconds with return value 0
请按任意键继续. . .

例9.5

#include<stdio.h>
#include<stdlib.h>
struct Student//声明结构体类型 struct person
{
int num;
char name[20];
char sex;
int age;
};
struct Student stu[3]={{10101,"zhang",‘M‘,78},{10103,"wang",‘M‘,98},{10106,"li",‘M‘,86}};//定义结构体数组并初始化
main()
{struct Student *p;//定义指向struct student类型数据的指针变量p
printf("NO.Name   sex age\n");
for(p=stu;p<stu+3;p++)
printf("%d,%s,%c,%d",p->num,p->name,p->sex,p->age);
return 0;
}
NO.Name   sex age
10101,zhang,M,7810103,wang,M,9810106,li,M,86
--------------------------------
Process exited after 0.4277 seconds with return value 0
请按任意键继续. . .

例9.6

#include<stdio.h>
#include<string.h>
main()
{struct Student
{long num;
char name[20];
char sex;
float score;
};
struct Student stu_1;//定义struct student类型的变量stu_1
struct Student *p;//定义指向struct student类型数据的指针变量p
p=&stu_1;//p指向stu—1
stu_1.num=10101;//对结构体变量的成员赋值
strcpy(stu_1.name,"li lin");//用字符串复制函数给stu_1.name赋值
stu_1.sex=‘M‘;
stu_1.score=89;
printf("NO.:%ld\nname:%s\nsex:%c\nscore:%d\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
printf("NO.:%ld\nname:%s\nsex:%c\nscore:%d\n",(*p).num,(*p).name,(*p).sex,(*p).score);
return 0;
}
NO.:10101
name:li lin
sex:M
score:0
NO.:10101
name:li lin
sex:Mscore:0

--------------------------------
Process exited after 0.4299 seconds with return value 0
请按任意键继续. . .

例9.7

#include<stdio.h>
#define N 3    //学生数为3
struct Student//声明结构体类型 struct person
{
int num;
char name[20];
float score[3];//三门课成绩
float aver;//平均成绩
};
int main()
{void input(struct Student stu[]);
struct Student max(struct Student stu[]);
void print(struct Student stu[]);
struct Student stu[N],*p=stu;//定义结构体数组和指针
input(p);//调用input函数
print(max(p));//调用print函数,以max函数的返回值作为实参
return 0;
}
void input(struct Student stu[])
{int i;
printf("请输入个学生的信息:学号,姓名,三门课成绩:\n");
for(i=0;i<N;i++)
{scanf("%d,%s,%f,%f,%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);//输入数据
stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;//求平均值
}
}
struct Student max(struct Student stu[])
{int i,m=0;//用m存放成绩最高的学生在数组中的序号
for(i=0;i<N;i++)
if(stu[i].aver>stu[m].aver)m=i;//找出平均成绩最高的学生在数组中的序号
return stu[m];//返还值包含该生信息的结构体元素
}
void print(struct Student stud)
{printf("\n成绩最好的学生是:\n");
printf("学号:%d\姓名:%s\n三门课成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f,stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver");
}
请输出各学生的信息:学号、姓名、三门课成绩:
10101 li 78 89 98
10103 wang 98.5 87 69
10106 sun 88 76.5 89

成绩最高的学生是:
学号:10101
姓名:10485108
三门课成绩: 78.0, 89.0, 98.0
平均成绩: 88.33

--------------------------------
Process exited after 99.13 seconds with return value 0
请按任意键继续. . 

总结:通过本次作业,觉得编程要多练习,熟能生巧,抄写时出现了忘记加等号等的细节错误,弄了一天,学到了很多。

时间: 2024-08-05 00:17:10

抄写第九章例题的相关文章

第九章、文件与文件系统的压缩与打包 3. 打包命令: tar

打包命令: tar gzip 与 bzip2 也能够针对目录来进行压缩, 不过,这两个命令对目录的压缩指的是『将目录内的所有文件 "分别" 进行压缩』! 将多个文件或目录包成一个大文件的,『打包命令』 tar [[email protected] ~]# tar [-j|-z] [cv] [-f 创建的档名] filename... <==打包与压缩 [[email protected] ~]# tar [-j|-z] [tv] [-f 创建的档名] <==察看档名 [[e

第九章、安全管理

第九章.安全管理 内容提要: 1.理解安全控制的基本概念 2.了解SQL Server 2008的存取控制 3.掌握SQL Server 2008的安全控制实现方式 4.了解Oracle的安全管理 第一节 安全控制概述 数据库安全性不同于数据的完整性. 安全性:保护数据以防止不合法用户故意造成破坏. (确保用户被允许做其想做的事情.) 完整性:保护数据以防止合法用户无意中造成的破坏. (确保用户做的事情是正确的.) 1.数据库安全控制的目标 保护数据免受意外或故意的丢失.破坏或滥用. 2.数据库

javascript高级程序设计 第九章-- 客户端检测

javascript高级程序设计 第九章-- 客户端检测 客户端检测是javascript开发中最具争议的一个话题,由于浏览器间存在差别,通常需要根据不同浏览器的能力分别编写不同的代码.有下列常使用的客户端检测方法:能力检测:在编写代码之前先检测特定浏览器的能力.例如,脚本在调用某个函数之前,可能要先检测该函数是否存在.这种检测方法将开发人员从考虑具体的浏览器类型和版本中解放出来,让他们把注意力集中到相应的能力是否存在上.能力检测无法精确地检测特定的浏览器和版本.怪癖检测:怪癖实际上是浏览器中存

zabbix专题:第九章 自定义key(案例:监控内存,监控nginx状态)

第九章 自定义key 对Linux有兴趣的朋友加入QQ群:476794643 在线交流 本文防盗链:http://zhang789.blog.51cto.com 为什么要自定义KEY 有时候我们想让被监控端执行一个zabbix没有预定义的检测,zabbix的用户自定义参数功能提供了这个方法.我们可以在客户端配置文件zabbix_angentd.conf里面配置UserParameter. 语法如下: UserParameter=key,command 用户自定义参数包含一个key和一个命令,ke

读书笔记第九章

第九章HAL是建立在linux驱动之上的一套程序库.这套程序库并不属于linux内核,而是属于linux内核层之上的应用层.可以用来保护不想公开源代码的作者.HAL架构比较简单,其基本原理就是在安卓系统中使用程序库调用位于内核空间的linux驱动,然后安卓应用程序可以通过NDK程序访问HAL中的程序库,或直接在安卓应用程序中访问HAL中的程序库.编写一款支持HAL的linux驱动程序的步骤:1.编写linux驱动,linux驱动的代码要尽量简介,尽可能将业务逻辑放到HAL library中.2.

第九章 两种模式的比较

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include <string.h> #include

第九章 用多线程来读取epoll模型下的客户端数据

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include <string.h> #include

第九章 TCP和UDP同时用复用一个端口实现一个回射服务器

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include <string.h> #include

C++ PRIMER 第九章

顺序容器:vector list deque 顺序容器适配器: stack queue priority_quequ(没见过,第一轮不管) C<T> c; C c(c2); C c(b,e) ///b e 都是迭代器; c(n,t)///只用于顺序容器; C c(n) ///只用于顺序容器 const list<int>::size_type list_size = 64; list<string> slist(size_type,"eh?"); 支