ImageVerifierCode 换一换
格式:DOCX , 页数:33 ,大小:21.96KB ,
资源ID:10006748      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10006748.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(玩转c语言入门.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

玩转c语言入门.docx

1、玩转c语言入门#include stdio.h#include conio.h#include string.h#include stdlib.h#define CMD_START printf(nn# Start a command #n);/* 用来标记一个命令执行的开始*/#define CMD_END printf( n# End a command #nn);/* 用来标记一个命令执行的结束 ,这两个语句是为了提供更好的用户界面而写的 */#define DATA_FILE data.dat/* 这是 数据文件名 */#define TEMP_FILE temp.dat/* 这是一个

2、临时的文件的名字,在删除记录的函数中使用的,详细内容参考 Delete() 函数 */typedef struct tagStudentchar ID30; /* 学号 */char Name30; /* 姓名 */char Class255; /* 班级 */char Sex; /* 性别 ,值为 F 或 f 或 M 或 m */int Math; /* 数学成绩 */int English; /* 英语成绩 */int Compute; /* 计算机成绩 */int Philosophy; /* 哲学成绩 */int PE; /* 体育成绩 */ Student;/* 这是学生信息结构体

3、*/int ShowMenu(); /* 在屏幕上打印 主菜单 的函数,它的返回值为 所选 菜单的 项目编号 */int ReadData(FILE*,Student* ); /* 从一个 打开的数据文件中读取 记录的函数,错误返回 0 */int WriteData(FILE* , Student* ); /* 向一个数据文件中 写入 记录的函数,错误返回 0 */void Output_Rec(Student*); /* 在屏幕上 打印 一条记录 */void Input_Rec(Student*); /* 让用户输入 记录的 各个项目的 值,在添加记录时用到了 */void CopyRe

4、c(Student* , Student* ); /* 复制一条记录 到 另一个记录中 */* 题目中要求的函数 */void Print(); /* 实现查看数据文件内容的函数 */void Add(); /* 添加记录的函数 */void Delete(); /* 删除记录的函数 */void Statistics(); /* 对数据进行统计分析的函数 */void Find(int); /* 实现查找功能的函数,参数决定 是按 ID 查找 还是按 Name 查找 */int quit; /* 一个全局变量,在下面的 main() 函数中,用来决定何时退出主循环 */main()int c

5、md; /* 用户所选的 菜单 项目 的标号 */quit = 0; /* 初始化 为 不退出 */* 这是程序的主循环,每次都将 主菜单打印出来,供用户选择相应的 序号 来执行相应的功能 */while( ! quit )cmd = ShowMenu(); /* 显示 主菜单,并返回用户所选择的 菜单项 的 编号 */CMD_START /* 在屏幕上打印一行分隔符,告诉用户这是一个子功能的开始 */switch( cmd ) /* 用多项分支 根据 用户的选择 调用 相应的函数 */case 1:Print(); break; /* 用户选择 1 号菜单,程序执行 查看的数据文件的函数 *

6、/case 2:Add(); break; /* 用户选择 2 号菜单,程序执行 添加记录的函数 */case 3:Delete(); break; /* 用户选择 3 号菜单,程序执行 删除记录的函数 */case 4:Statistics(); break; /* 用户选择 4 号菜单,程序执行 统计数据的函数 */case 5:Find(5); break; /* Find_ID ,5 号菜单执行 按 ID(学号)查找的功能 */case 6:Find(6); break; /* Find_Name,6 号菜单执行 按 Name(姓名)查找的功能 */case 7:quit = 1; /

7、* 用户选择了退出菜单 */printf( Thank you for your using .nn Happy everyday !nn Bye Bye .n);break;default:printf( Please Input a number betweent1ttot7.n);/* 用户所输入的 序号不在所处理的范围内 */CMD_END /* 打印一行分隔符,告诉用户 他所选择的菜单的功能已经执行完毕 */if (quit != 1) /* 检查用户是否 要求 退出 */printf( Press any key to Return Main Menu .n); getch();

8、/* 用 一个 无回显 的 字符输入函数 来实现暂停执行,按 任意键 继续的功能 */int ShowMenu()int cmd = 0; /* 保存用户的选择 */* 定义 程序所支持的菜单项目 */char Menu_SeeData = t1 .tView the Records in the data filen; /* 查看数据文件 */char Menu_Add = t2 .tAdd New Recordn; /* 添加记录 */char Menu_Delete = t3 .tDelete an old Recordn; /* 删除记录 */char Menu_Statistics

9、= t4 .tMake a Statisticsn; /* 统计分析 */char Menu_Find_ID = t5 .tFind a Record from the IDn; /* 按 学号(ID) 查找 */char Menu_Find_Name = t6 .tFind a Record from the Namen; /* 按 姓名(Name) 查找 */char Menu_Quit = t7 .tQuitn; /* 退出 */* 在屏幕上打印 主菜单 */printf(nn# Main Menu #n);printf( #nn);printf(Menu_SeeData);printf

10、(Menu_Add);printf(Menu_Delete);printf(Menu_Statistics);printf(Menu_Find_ID);printf(Menu_Find_Name);printf(Menu_Quit);printf(n#);printf(nn Input the index of your choice : );scanf(%d , &cmd); /* 接受用户 选择 */printf(n);return cmd; /* 返回用户的输入,交给主循环处理 */void Print() /* 打印 数据文件的 记录内容 */FILE* fp = NULL; /* 文

11、件指针 */Student rec; /* 存放从文件中读取的记录 */int i = 0; /* 实现 计数 和 分屏打印的功能 */fp = fopen(DATA_FILE , rb); /* 以 二进制读 方式 打开数据文件 */if(fp = NULL) /* 打开文件出错 */printf( Can not open the data file : %sn , DATA_FILE);return ;while(ReadData(fp , &rec) /* ReadData() 函数出错或到文件末尾时返回 0,可以做循环条件 */Output_Rec(&rec); /* 正确读取,将记

12、录输出 */printf( -);/* 打印一行分隔符,营造好的用户界面 */i+; /* 计数器 加一 */if( i % 4 = 0) /* 显示 4 个暂停一下 */printf(n Press any key to continue . n);getch();printf(n The current data file havet%dtrecord .n , i );fclose(fp); /* 关闭文件 */void Add() /* 添加记录 */Student rec;FILE* fp = NULL;Input_Rec( &rec ); /* 让用户输入新记录的各项内容 */fp

13、 = fopen(DATA_FILE ,ab); /* 以 添加 方式打开数据文件 */if( fp = NULL)printf( Can not open the data file to write into . n);return ;if( WriteData(fp, &rec) = 1) /* 将 新记录 写入文件,并检查 是否正确写入*/printf(nn Add New Record Success nn);elseprintf(nn Failed to Write New Record into the data file n);fclose(fp);void Delete()

14、/* 删除记录 */Student rec;FILE* fpr,*fpw; /* 两个文件指针,分别用于 读 和 写 */char buf30; /* 接受 用户输入的 ID 缓冲区 */char cmd255; /* 执行的系统命令 */int del_count; /* 实际 删除的 记录数目 */del_count = 0;printf(n Please type the ID of the record you want me to delete .);printf(n The ID : ); /* 提示用户 输入 */scanf(%s , buf);fpr = fopen(DATA_

15、FILE ,rb); /* 从 原来的记录文件中读取数据,跳过将要删除的记录 */if( fpr = NULL)printf( Can not open the data file to read record . n);return ;fpw = fopen(TEMP_FILE,wb); /* 打开一个 临时文件 保存不删除的记录 */if( fpw = NULL)printf( Can not open the data file to write into . n);return ;while(ReadData(fpr , &rec) /* 读取 要保留的记录 */if(strcmp(r

16、ec.ID , buf) != 0)WriteData(fpw, &rec); /* 写入临时文件 ,然后删除 原数据文件,再将临时文件该名为原数据文件的名字*/elsedel_count+; /* 跳过的记录数目,即删除的数目 */fclose(fpr);fclose(fpw);strcpy(cmd , del ); /* 构造命令串,用 system() 函数执行 */strcat(cmd ,DATA_FILE);system(cmd);rename(TEMP_FILE,DATA_FILE); /* 直接调用 C 语言的改名函数将临时文件改名为数据文件的名字*/printf(n I hav

17、e deletet%dtrecord .n ,del_count);void Statistics() /* 统计分析函数 */int i50,i60,i70,i80,i90; /*平均分小于60 ,60-69,70-79,80-89,=90 的分数段的学生数目*/float avg; /*平均分*/int sum ,sum_high,sum_low; /* 总分,总分最高分,总分最低分 */Student stu_high,stu_low; /* 总分最高和最低 学生的信息 */Student stu_math_high,stu_english_high; /* 各科 最高分的学生记录副本

18、*/Student stu_compute_high,stu_philosophy_high,stu_PE_high;Student stu_math_low,stu_english_low; /* 各科最低的学生记录副本 */Student stu_compute_low, stu_philosophy_low ,stu_PE_low;FILE* fp;Student rec;int count; /* 一个计数器,用于判断是否第一次读取数据 */count = sum = sum_high = sum_low = i50 = i60 = i60 = i70 =i80 = i90 = 0;f

19、p = NULL; /* 对 数据初始化 */fp = fopen(DATA_FILE ,rb);if(fp = NULL)printf(nCan not open the data file to read .n);return;while(ReadData(fp , &rec) /* 读取数据 */count+; /* 计数器 加一 */sum = rec.Math + rec.English + rec.Compute + rec.PE+ rec.Philosophy; /* 求和 */* average */avg = (float)sum) / 5; /* 平均分 */* 下面对各个

20、分数段进行统计 */if(avg 60)i50+;else if(avg 70)i60+;else if(avg 80)i70+;else if(avg 90)i80+;elsei90+;/*highest and loeest*/if(count sum_high)sum_high = sum; /* 得到最高总分 */CopyRec(&stu_high , &rec); /* 保存总分最高的学生的信息 */if(sum stu_math_high.Math)CopyRec(&stu_math_high ,&rec);if(rec.English stu_english_high.Engli

21、sh)CopyRec(&stu_english_high ,&rec);if(rec.Compute stu_compute_high.Compute)CopyRec(&stu_compute_high , &rec);if(rec.Philosophy stu_philosophy_high.Philosophy)CopyRec(&stu_philosophy_high , &rec);if(rec.PE stu_PE_high.PE)CopyRec(&stu_PE_high , &rec);/* low */* 保存各科的最低分的学生的信息 */if(rec.Math stu_math_l

22、ow.Math)CopyRec(&stu_math_low ,&rec);if(rec.English stu_english_low.English)CopyRec(&stu_english_low ,&rec);if(rec.Compute stu_compute_low.Compute)CopyRec(&stu_compute_low , &rec);if(rec.Philosophy stu_philosophy_low.Philosophy)CopyRec(&stu_philosophy_low , &rec);if(rec.PE stu_PE_low.PE)CopyRec(&stu

23、_PE_low , &rec);/* While End */if(count 1)printf(n There is no record in the data file .n);else/* average */* 输出平均分的分段统计信息 */printf(n The count in each segment :n);printf(t = 90t:t%dn,i90);printf( -);getch();/*highest and loeest*/* 输出总分最高的学生的信息 */printf(n The Highest Mark Student:n);printf( The Mark

24、 is : %dn , sum_high);printf( The student is :n);Output_Rec(&stu_high);/* 输出总分最高的学生的信息 */printf(n The Lowest Mark Student:n);printf( The Mark is : %dn , sum_low);printf( The student is :n);Output_Rec(&stu_low);printf( -n);getch();/* subject highest and low */* 输出各科最高和最低分的统计信息 */printf( The HighesttM

25、ath :n);Output_Rec(&stu_math_high);printf( The Lowest Math :n);Output_Rec(&stu_math_low);printf( -n);getch(); /* 暂停 ,按任意键继续 */printf( The Highest English :n);Output_Rec(&stu_english_high);printf( The Lowest English :n);Output_Rec(&stu_english_low);printf( -n);getch();printf( The Highest Compute :n);Output_Rec(&stu_compute_high);printf( The Lowest Compute :n);Output_Rec(&stu_compute_low);printf( -n);getch();printf( The Highest Philosophy :n);Output_Rec(&stu_philosophy_high);printf( The Lowest Philosophy :n);Output_Rec(&stu_philosophy_low);printf( -

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1