//在例8_2_3.c的基础上修改
#include <stdio.h>
#include <stdlib.h>
struct dat
{
int year;
int month;
int day;
};
struct STUDENT
{
int studentID;
char studentName[10];
char studentSex[4];
struct dat timeOfEnter;
int score[4]; //四门课的成绩
int total;
};
int RecordNum(struct STUDENT *p);
void CalculateStuTotal(struct STUDENT *p,int num);
void PrintStu(struct STUDENT *p,int num);
void CreateFile(char *s,struct STUDENT *p,int num);
void ReadOut(char *s,struct STUDENT *p,int num);
void main()
{ struct STUDENT stu[30]=
{ {1,"令狐冲","男",{1999,12,20},90,83,72,82},
{2,"林平之","男",{1999,07,06},78,92,88,78},
{3,"岳灵珊","女",{1999,07,06},89,72,98,66},
{4,"任莹莹","女",{1999,07,06},78,95,87,90}
};
char fileName[20];
int num=0; //存实际学生数
printf("please input the file name\n");
scanf("%s",fileName);
num=RecordNum(stu);
CalculateStuTotal(stu,num); //第一次运行时可将此行注释对比有无此语句输出内容的区别
// CreateFile(fileName,stu,num); //第二次运行时可将本行注释,直接从文件中读内容显示
printf("read from the file %s\n",fileName);
ReadOut(fileName,stu,num);
printf("print the students' information\n");
PrintStu(stu,num);
}
/*统计有多少个有效记录*/
int RecordNum(struct STUDENT *p)
{
struct STUDENT *pt=p;
int n=0;
for ( ; pt->studentID!=0;pt++) n++;
return n;
}
/*计算每个同学的总分*/
void CalculateStuTotal(struct STUDENT *p,int num)
{
struct STUDENT *pt =p; /* pt指向结构体数组的第一个元素 */
int j;
for(;pt<p+num;pt++)
{
pt->total=0;
for (j=0;j<4;j++)
pt->total+=pt->score[j];
}
}
/*输出结构体数组的内容*/
void PrintStu(struct STUDENT *p,int num)
{
struct STUDENT *pt =p; /* pt指向结构体数组的第一个元素 */
int j;
printf("学号 姓名 性别 年 月 日 计算机 英语 数学 音乐 总分\n");
for(;pt<p+num;pt++)
{
printf("%3d%7s%4s%7d%4d%4d ",pt->studentID,pt->studentName,pt->studentSex,pt->timeOfEnter.year,pt->timeOfEnter.month,pt->timeOfEnter.day);
for (j=0;j<4;j++)
printf("%6d",pt->score[j]);
printf("%6d\n",pt->total);
}
}
/*建立一个二进制文件,文件名由第一参数提供*/
void CreateFile(char *s,struct STUDENT *p,int num)
{
FILE *f;
if ((f=fopen(s,"wb"))==NULL)
{
printf("can not create file %s\n",s);
exit(0);
}
fwrite(p,sizeof(struct STUDENT),num,f);
fclose(f);
}
/*输出二进制文件的内容,文件名由第一参数提供*/
void ReadOut(char *s,struct STUDENT *p,int num)
{
FILE *f;
if ((f=fopen(s,"rb"))==NULL)
{
printf("can not create file %s\n",s);
exit(0);
}
// fread(p,sizeof(struct STUDENT),num,f);
while (!feof(f))
{
fread(p++,sizeof(struct STUDENT),1,f);
}
fclose(f);
}
没有合适的资源?快使用搜索试试~ 我知道了~
常见算法源代码速查.rar.rar
共47个文件
cpp:35个
c:8个
docx:4个
需积分: 5 0 下载量 16 浏览量
2022-10-11
14:44:21
上传
评论
收藏 83KB RAR 举报
温馨提示
常见算法源代码速查.rar.rar
资源详情
资源评论
资源推荐
收起资源包目录
常见算法源代码速查.rar.rar (47个子文件)
常见算法源代码速查
时间函数的应用
调用相关time函数进行日期推算.cpp 676B
调用时间函数得到当前日期时间.cpp 2KB
调用时间函数求运行时间.c 1KB
字符串操作
在串中查找一个子串出现的次数.cpp 505B
选择法进行字符串排序.cpp 404B
统计单词出现次数.cpp 869B
字符串中的大写字母改小写字母.cpp 219B
分割文件中读取的数据.cpp 2KB
字符串的选择法排序.cpp 725B
数字字符串转换成数值.cpp 199B
文件操作C语言风格
二进制文件操作.c 3KB
textFile.c 2KB
字符串读写存取文本文件.docx 16KB
块数据读写用二进制文件来存取结构体内容.docx 18KB
格式化读写用文本文件存取结构体的信息.docx 17KB
单字符读写存取文本文件.docx 17KB
矩阵相关
矩阵转置.cpp 364B
矩阵鞍点.cpp 410B
矩阵相乘.cpp 535B
互换两个值
交换两个变量_指针参数.cpp 79B
交换两个变量_引用参数.cpp 75B
随机函数的应用
调用随机函数产生数组元素.c 678B
生成正负随机数.cpp 374B
数组的应用
将数组中m个数循环后移n个位置.cpp 422B
数组元素逆置.cpp 246B
各种排序算法
直接插入排序.cpp 557B
简单选择排序.cpp 667B
冒泡法排序.cpp 358B
用指针完成选择排序.cpp 597B
快速排序_数据分类.cpp 438B
用指针完成冒泡排序.cpp 874B
顺序查找.cpp 380B
快速排序.cpp 1KB
调用随机函数产生数组元素.c 595B
各种查找算法
二分搜索非递归方法.cpp 442B
二分搜索的递归方法.cpp 948B
结构体及结构体数组
学生信息求课程平均分及学生总分(用函数).c 3KB
学生信息求课程平均分(函数实现).c 2KB
学生信息求课程平均分.c 1KB
数学问题
素数判断.cpp 275B
求最大公约数.cpp 184B
指针及动态数据结构
删除第i个结点.cpp 848B
链表的第i个结点的值通过参数x返回.cpp 450B
生成动态数组C语言风格.cpp 712B
生成动态数组C++风格.cpp 670B
链表的第i个结点后插入结点.cpp 646B
释放单链表所有结点空间的析构函数.cpp 215B
共 47 条
- 1
拾语"
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0