1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define N 10
  5. struct student{ /* 学生信息结构 */
  6. char no[]; /* 学号 */
  7. char name[]; /* 姓名 */
  8. char sex[]; /* 性别 */
  9. int score[]; /* 成绩和总分 */
  10. };
  11.  
  12. int menu(); /* 菜单*/
  13. void readsi(struct student stud[],int *n); /* 读取数据*/
  14. void printsi(struct student *pstud,int n); /* 输出文件内容*/
  15. void ssort(struct student *pstud, int n); /*按总分的降序排序函数 */
  16. void xsort(struct student *pstud, int n);/*按学号的升序排序函数 */
  17. void tjave(struct student *pstud, int n);/*统计各门功课的平均分函数 */
  18. void tjrs(struct student *pstud,int n);/*统计男女同学的人数函数 */
  19. void namesort(struct student *pstud,int n);/*按姓名升序排序 */
  20. void math_excellent(struct student *pstud,int n);/*数学考试成绩优秀(>=90)*/
  21. void all_excellent(struct student *pstud,int n);/*每门考试成绩优秀(>=90)或者总分》=270*/
  22.  
  23. void main() /* 主函数 */
  24. {
  25. struct student stud[N];
  26. int code, n=;
  27. readsi(stud,&n);
  28. printf("\n 按<Enter>, 进入菜单: ");
  29. scanf("%*c"); /* 暂停 */
  30. while()
  31. {
  32. code=menu(); /* 调用主控菜单 */
  33. switch(code)
  34. {
  35. case : exit();
  36. case : printsi(stud,n);
  37. printf("\n 按<Enter>, 进入菜单: ");
  38. scanf("%*c");
  39. break;
  40. case : ssort(stud,n);break;
  41. case : xsort(stud,n);break;
  42. case : tjave(stud,n);break;
  43. case : tjrs(stud,n);break;
  44. case : namesort(stud,n);break;
  45. case : math_excellent(stud,n);break;
  46. case : all_excellent(stud,n);break;
  47.  
  48. } scanf("%*c");
  49. }
  50. }
  51.  
  52. int menu() /* 主控菜单函数 */
  53. {
  54. int code;
  55. printf(" 菜 单\n");
  56. printf(" *****************************************************\n");
  57. printf(" 0. 退出 1. 显示学生信息 \n");
  58. printf(" 2. 按总分排序 3. 按学号排序\n");
  59. printf(" 4. 统计各门功课平均分 5. 统计男女同学人数\n");
  60. printf(" 6. 按姓名排序 7. 数学考试成绩优秀人数\n");
  61. printf(" 8. 考试成绩优秀人数 \n");
  62. printf(" *****************************************************\n");
  63. printf(" 请按序号选择:\n");
  64. scanf("%d",&code);
  65. return code;
  66. }
  67.  
  68. void readsi(struct student stud[],int *n) /* 读数据函数 */ //int *n;n需要返回
  69. {
  70. FILE *fp;
  71. int i;
  72. // if((fp=fopen("studf.txt","r"))==NULL)
  73. if((fp=fopen("C:/Users/minmin/Desktop/studf.txt","r"))==NULL)//文件存放在指定路径,把路径写上就可以了
  74. {
  75. printf("Cannot open file!\n");
  76. exit();
  77. }
  78. for(i=;!feof(fp);i++)
  79. {
  80. (*n)++;
  81. fscanf(fp,"%s %s %s %d %d %d %d", stud[i].no,stud[i].name,stud[i].sex,
  82. &stud[i].score[], &stud[i].score[], &stud[i].score[], &stud[i].score[]);
  83.  
  84. stud[i].score[]=stud[i].score[]+stud[i].score[]+stud[i].score[];
  85. }
  86. fclose(fp);
  87. }
  88.  
  89. void printsi(struct student *pstud, int n) /* 输出数据函数 */
  90. {
  91. int i;
  92. printf(" 学号 姓名 性别 数学 英语 C 总分\n");
  93. printf("******************************************************\n");
  94. for(i=;i<n;i++)
  95. {
  96. printf("%-8s %-8s %-2s %4d %4d %4d %4d\n", pstud[i].no,pstud[i].name,pstud[i].sex,
  97. pstud[i].score[], pstud[i].score[], pstud[i].score[], pstud[i].score[]);
  98. }
  99. }
  100.  
  101. void ssort(struct student *pstud,int n) /*按总分的降序排序函数 */
  102. {
  103. struct student temp;
  104. int i,j,min;
  105. for(i=;i<n;i++)
  106. {
  107. min=i; /* 找最小值元素的下标*/
  108. for(j=i+;j<n;j++)
  109. if(pstud[j].score[]>pstud[min].score[]) min=j;
  110. if(min!=i) /* 交换 */
  111. {
  112. temp=pstud[i]; pstud[i]=pstud[min]; pstud[min]=temp;
  113. }
  114. }
  115. }
  116.  
  117. void xsort(struct student *pstud,int n) /*按学号的升序排序函数 */
  118. {
  119. struct student temp;
  120. int i, j;
  121. for(i=;i<n-;i++)
  122. {
  123. for(j=i+;j<n;j++)
  124. {
  125. if(strcmp(pstud[i].no,pstud[j].no)>)
  126. {
  127. temp=pstud[i];
  128. pstud[i]=pstud[j];
  129. pstud[j]=temp;
  130. }
  131. }
  132. }
  133. }
  134.  
  135. void tjave(struct student *pstud, int n) /*统计各门功课的平均分函数 */
  136. {
  137. float avemath=,aveeng=,avec=,avesum=;
  138. int i;
  139. for(i=;i<n;i++)
  140. {
  141. avemath+=pstud[i].score[];
  142. aveeng+=pstud[i].score[];
  143. avec+=pstud[i].score[];
  144. avesum+=pstud[i].score[];
  145. }
  146. avemath/=n; aveeng/=n; avec/=n; avesum/=n;
  147. printf("共有%d个同学,各门功课及总分的平均分为:\n",n);
  148. printf(" 数学 英语 C 总分\n");
  149. printf("%5.2f %5.2f %5.2f %5.2f\n",avemath,aveeng,avec,avesum);
  150. }
  151.  
  152. void tjrs(struct student *pstud,int n) /*统计男女同学的人数函数 */
  153. {
  154. int i, nummen=, numwomen=;
  155. for(i=;i<n;i++)
  156. {
  157. if(strcmp(pstud[i].sex,"男")==) nummen++;
  158. else numwomen++;
  159. }
  160. printf(" 共有%d个同学: \n",n);
  161. printf(" 其中男同学有%d个,女同学有%d个\n",nummen,numwomen);
  162. }
  163.  
  164. void namesort(struct student *pstud,int n)/*按姓名升序排序 */
  165. {
  166. struct student temp;
  167. int i, j;
  168. for(i=;i<n;i++)
  169. {
  170. for(j=i+;j<n;j++)
  171. {
  172. if(strcmp(pstud[i].name,pstud[j].name)>)
  173. {
  174. temp=pstud[i];
  175. pstud[i]=pstud[j];
  176. pstud[j]=temp;
  177. }
  178. }
  179. }
  180. }
  181.  
  182. void math_excellent(struct student *pstud,int n)/*数学考试成绩优秀(>=90)*/
  183. {
  184. int i, num = ;
  185. for(i=;i<n;i++)
  186. {
  187. if(pstud[i].score[]>=)
  188. {
  189. num++;
  190. printf("%-8s %-8s %-2s %4d %4d %4d %4d\n", pstud[i].no,pstud[i].name,pstud[i].sex,
  191. pstud[i].score[], pstud[i].score[], pstud[i].score[], pstud[i].score[]);
  192. }
  193. }
  194. printf("数学优秀的人数为:%d\n",num);
  195. }
  196.  
  197. void all_excellent(struct student *pstud,int n)/*每门考试成绩优秀(>=90)或者总分》=270*/
  198. {
  199. int i, num = ;
  200. for(i=;i<n;i++)
  201. {
  202. if(((pstud[i].score[]>=)&&(pstud[i].score[]>=)&&(pstud[i].score[]>=))||(pstud[i].score[]>=))
  203. {
  204. num++;
  205. printf("%-8s %-8s %-2s %4d %4d %4d %4d\n", pstud[i].no,pstud[i].name,pstud[i].sex,
  206. pstud[i].score[], pstud[i].score[], pstud[i].score[], pstud[i].score[]);
  207. }
  208. }
  209. printf("优秀的人数为:%d\n",num);
  210. }

c语言 文件写入和读取的更多相关文章

  1. Python学习笔记——文件写入和读取

    1.文件写入 #coding:utf-8 #!/usr/bin/env python 'makeTextPyhton.py -- create text file' import os ls = os ...

  2. 【PHP】文件写入和读取详解

    文章提纲: 一.实现文件读取和写入的基本思路 二.使用fopen方法打开文件 三.文件读取和文件写入操作 四.使用fclose方法关闭文件 五.文件指针的移动 六.Windows和UNIX下的回车和换 ...

  3. Java文件写入与读取实例求最大子数组

    出现bug的点:输入数组无限大: 输入的整数,量大: 解决方案:向文件中输入随机数组,大小范围与量都可以控制. 源代码: import java.io.BufferedReader; import j ...

  4. PHP 文件写入和读取(必看篇)

    文章提纲: 一.实现文件读取和写入的基本思路 二.使用fopen方法打开文件 三.文件读取和文件写入操作 四.使用fclose方法关闭文件 五.文件指针的移动 六.Windows和UNIX下的回车和换 ...

  5. sql注入文件写入和读取

    系统固定文件路径:https://blog.csdn.net/ncafei/article/details/54616826 /etc/passwd c:/windows/win.ini 文件读取使用 ...

  6. 从PCD文件写入和读取点云数据

    (1)学习向PCD文件写入点云数据 建立工程文件ch2,然后新建write_pcd.cpp  CMakeLists.txt两个文件 write_pcd.cpp : #include <iostr ...

  7. unity文件写入与读取

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; us ...

  8. 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取

    装饰者模式的学习(c#) 案例转自https://www.cnblogs.com/stonefeng/p/5679638.html //主体基类 using System;using System.C ...

  9. Java 通过 BufferReader 实现 文件 写入读取 示例

    package com.javatest.techzero.gui; import java.io.BufferedReader; import java.io.File; import java.i ...

随机推荐

  1. BZOJ 3368 约翰看山(扫描)O(N)

    这题,简直丧心病狂了. 大意是给你一个环上一些覆盖的区间,让你求总覆盖长度. 非常坑的点是这个区间因为是个环,所以可能逆时针给你,也可能顺时针给你,你特别要注意.那么区分顺时针和逆时针的方法 就是,题 ...

  2. Remove Duplicate Letters

    316. Remove Duplicate Letters Total Accepted: 2367 Total Submissions: 12388 Difficulty: Medium Given ...

  3. wamp出现问题#1045 - Access denied for user 'root'@'localhost' (using password: NO)的解决方法

    打开wamp->apps->phpmyadmin目录下面的config.inc.php文件 cfg['Servers'][$i]['verbose'] = 'localhost';$cfg ...

  4. python之函数的使用

    备注:本篇文章主要讲一讲函数及集合的一些常用用法: 一.首先先看下,集合(set): 集合的特点:无序.不重复(这点跟字典有点像) <1>,在需要访问集合的时候,由于集合本身是无序的,所以 ...

  5. leetcode 237 Delete Node in a Linked List python

    题目: Write a function to delete a node (except the tail) in a singly linked list, given only access t ...

  6. QT5删除隐藏目录+隐藏文件

    1.功能需求 删除一个目录(包括目录本身),同时删除该目录中所有文件及目录(含隐藏的) 2.遇到的问题 qt5中已经有了递归删除目录的函数--->bool QDir::removeRecursi ...

  7. 利用Azure backup备份和恢复Azure虚拟机(1)

    中国区Azure最近发布了关于使用Azure Backup来备份VM服务,于3月1日正式上线,该功能对于需要对关键工作负载进行备份的用户来讲,极大的降低了操作复杂度.以前我们所使用Powershell ...

  8. Nginx 配置指令的执行顺序(五)

    Nginx 的 content 阶段是所有请求处理阶段中最为重要的一个,因为运行在这个阶段的配置指令一般都肩负着生成“内容”(content)并输出 HTTP 响应的使命.正因为其重要性,这个阶段的配 ...

  9. MySQL_数据分页查询(limit用法)

    取前5条数据 select * from table_name limit 0,5 或 select * from table_name limit 5 取第11条到第15条数据,共5条 select ...

  10. cp | mv | rm

    linux下文件的复制.移动与删除命令为:cp,mv,rm 一.文件复制命令cp 命令格式: cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [opt ...