基于visual Studio2013解决C语言竞赛题之0904文件排序
来源:互联网 发布:单片机哪种好 编辑:程序博客网 时间:2024/03/29 19:25
题目
解决代码及点评
/*4.将存放在worker1.rec中的职工数据按工资高低排序,将排好序的各记录存放在 worker2.rec中(用 fread和fwrite函数)。*/#include <stdio.h>#include <stdlib.h>typedef struct Staff_9_4{int num;char name[30];char sex[5];int age;float salary;} Employee;/*读取员工信息*/void readAllInfo94(Employee *tempEmp,int fileType){Employee temp;FILE *fp = NULL;if (fileType == 1){fopen_s(&fp, "worker1.rec","rb");}else if (fileType == 2){fopen_s(&fp, "worker2.rec","rb");}if (fp){int i = 0;fread(&temp,sizeof(temp),1,fp);while(!feof(fp)){tempEmp[i] = temp;printf("num = %d,name = %s,sex = %s,age = %d,salary = %f\n",tempEmp[i].num,tempEmp[i].name,tempEmp[i].sex,tempEmp[i].age,tempEmp[i].salary);fread(&temp,sizeof(temp),1,fp);tempEmp[i] = temp;i++;}fclose(fp);}else{printf("open file failed!");}}/*按照工资从高到低排序*/void sortAllEmp94(Employee *emp,int n){Employee tempEmp;for (int i = 0; i < n - 1; i++){for (int j = i + 1; j < n; j++){if (emp[i].salary < emp[j].salary){tempEmp = emp[i];emp[i] = emp[j];emp[j] = tempEmp;}}}}/*保存员工信息*/void saveAllInfo94(Employee * emp,int n){FILE *fp = NULL;fopen_s(&fp, "worker2.rec","wb");if (fp){for (int i = 0; i < n; i++){fwrite(&emp[i],sizeof(emp[i]),1,fp);}fclose(fp);}else{printf("open file failed!");}}void main(){const int N = 10;Employee emp[N];//读取所有员工保存到empprintf("排序前的数据:\n");readAllInfo94(emp,1);//参数1表示打开worker1.rec文件//给emp排序sortAllEmp94(emp,N);//保存排序后的员工信息saveAllInfo94(emp,N);printf("排序后的数据:\n");//排序后员工的信息readAllInfo94(emp,2);//参数2表示打开worker2.rec文件system("pause");}
代码编译以及运行
由于资源上传太多,资源频道经常被锁定无法上传资源,同学们可以打开VS2013自己创建工程,步骤如下:
1)新建工程
2)选择工程
3)创建完工程如下图:
4)增加文件,右键点击项目
5)在弹出菜单里做以下选择
6)添加文件
7)拷贝代码与运行
程序运行结果
代码下载
http://download.csdn.net/detail/yincheng01/6694119解压密码:c.itcast.cn
- 基于visual Studio2013解决C语言竞赛题之0904文件排序
- 基于visual Studio2013解决C语言竞赛题之1055排序
- 基于visual Studio2013解决C语言竞赛题之前言
- 基于visual Studio2013解决C语言竞赛题之0401阶乘
- 基于visual Studio2013解决C语言竞赛题之0408素数
- 基于visual Studio2013解决C语言竞赛题之0516人来人往
- 基于visual Studio2013解决C语言竞赛题之0517矩阵
- 基于visual Studio2013解决C语言竞赛题之0519最大值
- 基于visual Studio2013解决C语言竞赛题之0605strcat
- 基于visual Studio2013解决C语言竞赛题之0607strcpy
- 基于visual Studio2013解决C语言竞赛题之0612递归
- 基于visual Studio2013解决C语言竞赛题之0803报数
- 基于visual Studio2013解决C语言竞赛题之0810链表去重
- 基于visual Studio2013解决C语言竞赛题之1004平均值
- 基于visual Studio2013解决C语言竞赛题之1006填空
- 基于visual Studio2013解决C语言竞赛题之1010计算
- 基于visual Studio2013解决C语言竞赛题之1011对称
- 基于visual Studio2013解决C语言竞赛题之1017次数
- 蓝牙配对失误两则
- 写代码的女汉子
- Higher Math
- 12月3号面试题记录
- fecn-create和fecn-adapt的作用
- 基于visual Studio2013解决C语言竞赛题之0904文件排序
- wikioi 1042 进制转换
- TCP Keepalive 笔记
- SRM 599 DIV2 950
- Problem 56: 某种序列(大数加法+数列)
- Windows下Emacs+ Distel的配置
- pache 合理配置KeepAlive选项对服务器性能的影响
- openresty php luajit
- struts整合spring插件出现如下异常:Unable to load jar:struts2-spring-plugin-2.3.15.3.jar!/struts-plugin.xml