玩转c语言入门.docx

上传人:b****2 文档编号:3265950 上传时间:2023-05-05 格式:DOCX 页数:33 大小:21.96KB
下载 相关 举报
玩转c语言入门.docx_第1页
第1页 / 共33页
玩转c语言入门.docx_第2页
第2页 / 共33页
玩转c语言入门.docx_第3页
第3页 / 共33页
玩转c语言入门.docx_第4页
第4页 / 共33页
玩转c语言入门.docx_第5页
第5页 / 共33页
玩转c语言入门.docx_第6页
第6页 / 共33页
玩转c语言入门.docx_第7页
第7页 / 共33页
玩转c语言入门.docx_第8页
第8页 / 共33页
玩转c语言入门.docx_第9页
第9页 / 共33页
玩转c语言入门.docx_第10页
第10页 / 共33页
玩转c语言入门.docx_第11页
第11页 / 共33页
玩转c语言入门.docx_第12页
第12页 / 共33页
玩转c语言入门.docx_第13页
第13页 / 共33页
玩转c语言入门.docx_第14页
第14页 / 共33页
玩转c语言入门.docx_第15页
第15页 / 共33页
玩转c语言入门.docx_第16页
第16页 / 共33页
玩转c语言入门.docx_第17页
第17页 / 共33页
玩转c语言入门.docx_第18页
第18页 / 共33页
玩转c语言入门.docx_第19页
第19页 / 共33页
玩转c语言入门.docx_第20页
第20页 / 共33页
亲,该文档总共33页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

玩转c语言入门.docx

《玩转c语言入门.docx》由会员分享,可在线阅读,更多相关《玩转c语言入门.docx(33页珍藏版)》请在冰点文库上搜索。

玩转c语言入门.docx

玩转c语言入门

#include"stdio.h"

#include"conio.h"

#include"string.h"

#include"stdlib.h"

#defineCMD_STARTprintf("\n\n#########Startacommand#########\n");

/*用来标记一个命令执行的开始*/

#defineCMD_ENDprintf("\n#########Endacommand#########\n\n");

/*用来标记一个命令执行的结束,这两个语句是为了提供更好的用户界面而写的*/

#defineDATA_FILE"data.dat"

/*这是数据文件名*/

#defineTEMP_FILE"temp.dat"

/*这是一个临时的文件的名字,在删除记录的函数中使用的,

详细内容参考Delete()函数*/

typedefstructtagStudent

{

charID[30];/*学号*/

charName[30];/*姓名*/

charClass[255];/*班级*/

charSex;/*性别,值为F或f或M或m*/

intMath;/*数学成绩*/

intEnglish;/*英语成绩*/

intCompute;/*计算机成绩*/

intPhilosophy;/*哲学成绩*/

intPE;/*体育成绩*/

}Student;

/*这是学生信息结构体*/

intShowMenu();/*在屏幕上打印主菜单的函数,它的返回值为所选菜单的项目编号*/

intReadData(FILE*,Student*);/*从一个打开的数据文件中读取记录的函数,错误返回0*/

intWriteData(FILE*,Student*);/*向一个数据文件中写入记录的函数,错误返回0*/

voidOutput_Rec(Student*);/*在屏幕上打印一条记录*/

voidInput_Rec(Student*);/*让用户输入记录的各个项目的值,在添加记录时用到了*/

voidCopyRec(Student*,Student*);/*复制一条记录到另一个记录中*/

/*题目中要求的函数*/

voidPrint();/*实现查看数据文件内容的函数*/

voidAdd();/*添加记录的函数*/

voidDelete();/*删除记录的函数*/

voidStatistics();/*对数据进行统计分析的函数*/

voidFind(int);/*实现查找功能的函数,参数决定是按ID查找还是按Name查找*/

intquit;/*一个全局变量,在下面的main()函数中,用来决定何时退出主循环*/

main()

{

intcmd;/*用户所选的菜单项目的标号*/

quit=0;/*初始化为不退出*/

/*这是程序的主循环,每次都将主菜单打印出来,

供用户选择相应的序号来执行相应的功能*/

while(!

quit)

{

cmd=ShowMenu();/*显示主菜单,并返回用户所选择的菜单项的编号*/

CMD_START/*在屏幕上打印一行分隔符,告诉用户这是一个子功能的开始*/

switch(cmd)/*用多项分支根据用户的选择调用相应的函数*/

{

case1:

Print();break;/*用户选择1号菜单,程序执行查看的数据文件的函数*/

case2:

Add();break;/*用户选择2号菜单,程序执行添加记录的函数*/

case3:

Delete();break;/*用户选择3号菜单,程序执行删除记录的函数*/

case4:

Statistics();break;/*用户选择4号菜单,程序执行统计数据的函数*/

case5:

Find(5);break;/*Find_ID,5号菜单执行按ID(学号)查找的功能*/

case6:

Find(6);break;/*Find_Name,6号菜单执行按Name(姓名)查找的功能*/

case7:

quit=1;/*用户选择了退出菜单*/

printf("Thankyouforyourusing.\n\nHappyeveryday!

!

\n\nByeBye....\n");

break;

default:

printf("PleaseInputanumberbetween\t1\tto\t7.\n");

/*用户所输入的序号不在所处理的范围内*/

}

CMD_END/*打印一行分隔符,告诉用户他所选择的菜单的功能已经执行完毕*/

if(quit!

=1)/*检查用户是否要求退出*/

{

printf("PressanykeytoReturnMainMenu....\n");

getch();/*用一个无回显的字符输入函数来实现暂停执行,按任意键继续的功能*/

}

}

}

intShowMenu()

{

intcmd=0;/*保存用户的选择*/

/*定义程序所支持的菜单项目*/

charMenu_SeeData[]="\t1.\tViewtheRecordsinthedatafile\n";/*查看数据文件*/

charMenu_Add[]="\t2.\tAddNewRecord\n";/*添加记录*/

charMenu_Delete[]="\t3.\tDeleteanoldRecord\n";/*删除记录*/

charMenu_Statistics[]="\t4.\tMakeaStatistics\n";/*统计分析*/

charMenu_Find_ID[]="\t5.\tFindaRecordfromtheID\n";/*按学号(ID)查找*/

charMenu_Find_Name[]="\t6.\tFindaRecordfromtheName\n";/*按姓名(Name)查找*/

charMenu_Quit[]="\t7.\tQuit\n";/*退出*/

/*在屏幕上打印主菜单*/

printf("\n\n############MainMenu###############\n");

printf("##############################################\n\n");

printf(Menu_SeeData);

printf(Menu_Add);

printf(Menu_Delete);

printf(Menu_Statistics);

printf(Menu_Find_ID);

printf(Menu_Find_Name);

printf(Menu_Quit);

printf("\n##############################################");

printf("\n\nInputtheindexofyourchoice:

");

scanf("%d",&cmd);/*接受用户选择*/

printf("\n");

returncmd;/*返回用户的输入,交给主循环处理*/

}

voidPrint()/*打印数据文件的记录内容*/

{

FILE*fp=NULL;/*文件指针*/

Studentrec;/*存放从文件中读取的记录*/

inti=0;/*实现计数和分屏打印的功能*/

fp=fopen(DATA_FILE,"rb");/*以二进制读方式打开数据文件*/

if(fp==NULL)/*打开文件出错*/

{

printf("Cannotopenthedatafile:

%s\n",DATA_FILE);

return;

}

while(ReadData(fp,&rec))/*ReadData()函数出错或到文件末尾时返回0,可以做循环条件*/

{

Output_Rec(&rec);/*正确读取,将记录输出*/

printf("------------------------------------------");

/*打印一行分隔符,营造好的用户界面*/

i++;/*计数器加一*/

if(i%4==0)/*显示4个暂停一下*/

{

printf("\nPressanykeytocontinue...\n");

getch();

}

}

printf("\nThecurrentdatafilehave\t%d\trecord.\n",i);

fclose(fp);/*关闭文件*/

}

voidAdd()/*添加记录*/

{

Studentrec;

FILE*fp=NULL;

Input_Rec(&rec);/*让用户输入新记录的各项内容*/

fp=fopen(DATA_FILE,"ab");/*以添加方式打开数据文件*/

if(fp==NULL)

{

printf("Cannotopenthedatafiletowriteinto...\n");

return;

}

if(WriteData(fp,&rec)==1)/*将新记录写入文件,并检查是否正确写入*/

printf("\n\nAddNewRecordSuccess\n\n");

else

printf("\n\nFailedtoWriteNewRecordintothedatafile\n");

fclose(fp);

}

voidDelete()/*删除记录*/

{

Studentrec;

FILE*fpr,*fpw;/*两个文件指针,分别用于读和写*/

charbuf[30];/*接受用户输入的ID缓冲区*/

charcmd[255];/*执行的系统命令*/

intdel_count;/*实际删除的记录数目*/

del_count=0;

printf("\nPleasetypetheIDoftherecordyouwantmetodelete.");

printf("\nTheID:

");/*提示用户输入*/

scanf("%s",buf);

fpr=fopen(DATA_FILE,"rb");/*从原来的记录文件中读取数据,跳过将要删除的记录*/

if(fpr==NULL)

{

printf("Cannotopenthedatafiletoreadrecord...\n");

return;

}

fpw=fopen(TEMP_FILE,"wb");/*打开一个临时文件保存不删除的记录*/

if(fpw==NULL)

{

printf("Cannotopenthedatafiletowriteinto...\n");

return;

}

while(ReadData(fpr,&rec))/*读取要保留的记录*/

{

if(strcmp(rec.ID,buf)!

=0)

{

WriteData(fpw,&rec);/*写入临时文件,然后删除原数据文件,

再将临时文件该名为原数据文件的名字*/

}

else

{

del_count++;/*跳过的记录数目,即删除的数目*/

}

}

fclose(fpr);

fclose(fpw);

strcpy(cmd,"del");/*构造命令串,用system()函数执行*/

strcat(cmd,DATA_FILE);

system(cmd);

rename(TEMP_FILE,DATA_FILE);/*直接调用C语言的改名函数将临时文件改名为数据文件的名字*/

printf("\nIhavedelete\t%d\trecord.\n",del_count);

}

voidStatistics()/*统计分析函数*/

{

inti50,i60,i70,i80,i90;/*平均分小于60,60-69,70-79,80-89,>=90的分数段的学生数目*/

floatavg;/*平均分*/

intsum,sum_high,sum_low;/*总分,总分最高分,总分最低分*/

Studentstu_high,stu_low;/*总分最高和最低学生的信息*/

Studentstu_math_high,stu_english_high;/*各科最高分的学生记录副本*/

Studentstu_compute_high,stu_philosophy_high,stu_PE_high;

Studentstu_math_low,stu_english_low;/*各科最低的学生记录副本*/

Studentstu_compute_low,stu_philosophy_low,stu_PE_low;

FILE*fp;

Studentrec;

intcount;/*一个计数器,用于判断是否第一次读取数据*/

count=sum=sum_high=sum_low=i50=i60=i60=i70=i80=i90=0;

fp=NULL;/*对数据初始化*/

fp=fopen(DATA_FILE,"rb");

if(fp==NULL)

{

printf("\nCannotopenthedatafiletoread...\n");

return;

}

while(ReadData(fp,&rec))/*读取数据*/

{

count++;/*计数器加一*/

sum=rec.Math+rec.English+rec.Compute+rec.PE+rec.Philosophy;/*求和*/

/*average*/

avg=((float)sum)/5;/*平均分*/

/*下面对各个分数段进行统计*/

if(avg<60)

i50++;

elseif(avg<70)

i60++;

elseif(avg<80)

i70++;

elseif(avg<90)

i80++;

else

i90++;

/*highestandloeest*/

if(count<=1)/*第一次读取,执行初始化,不进行比较*/

{

sum_high=sum_low=sum;

CopyRec(&stu_high,&rec);

CopyRec(&stu_low,&rec);

}

else

{

if(sum>sum_high)

{

sum_high=sum;/*得到最高总分*/

CopyRec(&stu_high,&rec);/*保存总分最高的学生的信息*/

}

if(sum

{

sum_low=sum;/*得到最低分*/

CopyRec(&stu_low,&rec);/*保存总分最低的学生的信息*/

}

}

/*subjecthighestandlow*/

if(count==1)/*同上面一样,执行初始化*/

{

CopyRec(&stu_math_high,&rec);

CopyRec(&stu_english_high,&rec);

CopyRec(&stu_compute_high,&rec);

CopyRec(&stu_philosophy_high,&rec);

CopyRec(&stu_PE_high,&rec);

CopyRec(&stu_math_low,&rec);

CopyRec(&stu_english_low,&rec);

CopyRec(&stu_compute_low,&rec);

CopyRec(&stu_philosophy_low,&rec);

CopyRec(&stu_PE_low,&rec);

}

else

{

/*High*/

/*保存各科的最高分的学生的信息*/

if(rec.Math>stu_math_high.Math)

CopyRec(&stu_math_high,&rec);

if(rec.English>stu_english_high.English)

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

CopyRec(&stu_math_low,&rec);

if(rec.English

CopyRec(&stu_english_low,&rec);

if(rec.Compute

CopyRec(&stu_compute_low,&rec);

if(rec.Philosophy

CopyRec(&stu_philosophy_low,&rec);

if(rec.PE

CopyRec(&stu_PE_low,&rec);

}

}/*WhileEnd*/

if(count<1)

{

printf("\nThereisnorecordinthedatafile.\n");

}

else

{

/*average*/

/*输出平均分的分段统计信息*/

printf("\nThecountineachsegment:

\n");

printf("\t<60\t:

\t%d\n",i50);

printf("\t60-69\t:

\t%d\n",i60);

printf("\t70-79\t:

\t%d\n",i70);

printf("\t80-90\t:

\t%d\n",i80);

printf("\t>=90\t:

\t%d\n",i90);

printf("------------------------------------------");

getch();

/*highestandloeest*/

/*输出总分最高的学生的信息*/

printf("\nTheHighestMarkStudent:

\n");

printf("TheMarkis:

%d\n",sum_high);

printf("Thestudentis:

\n");

Output_Rec(&stu_high);

/*输出总分最高的学生的信息*/

printf("\nTheLowestMarkStudent:

\n");

printf("TheMarkis:

%d\n",sum_low);

printf("Thestudentis:

\n");

Output_Rec(&stu_low);

printf("------------------------------------------\n");

getch();

/*subjecthighestandlow*/

/*输出各科最高和最低分的统计信息*/

printf("TheHighest\tMath:

\n");

Output_Rec(&stu_math_high);

printf("TheLowestMath:

\n");

Output_Rec(&stu_math_low);

printf("------------------------------------------\n");

getch();/*暂停,按任意键继续*/

printf("TheHighestEnglish:

\n");

Output_Rec(&stu_english_high);

printf("TheLowestEnglish:

\n");

Output_Rec(&stu_english_low);

printf("------------------------------------------\n");

getch();

printf("TheHighestCompute:

\n");

Output_Rec(&stu_compute_high);

printf("TheLowestCompute:

\n");

Output_Rec(&stu_compute_low);

printf("------------------------------------------\n");

getch();

printf("TheHighestPhilosophy:

\n");

Output_Rec(&stu_philosophy_high);

printf("TheLowestPhilosophy:

\n");

Output_Rec(&stu_philosophy_low);

printf("------------------------

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2