c语言程序设计-数组与字符串

c语言程序设计-数组与字符串

ID:39963334

大小:647.31 KB

页数:124页

时间:2019-07-16

上传者:U-145848
c语言程序设计-数组与字符串_第1页
c语言程序设计-数组与字符串_第2页
c语言程序设计-数组与字符串_第3页
c语言程序设计-数组与字符串_第4页
c语言程序设计-数组与字符串_第5页
资源描述:

《c语言程序设计-数组与字符串》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

《程序设计基础(C)》第五章数组与字符串 本章内容学生需求2一维数组指针概念学生需求3字符串二维数组批量数据的组织与使用变量定义/声明变量初始化输入和输出相关运算符赋值和表达式常见使用方法 [案例需求2]编写学生信息管理程序,实现:命令行交互方式,提示符为“ST2>”帮助功能,命令符H,显示关于命令的使用说明退出功能,命令符Q,退出程序增加功能,命令符A,参数为学号、成绩删除功能,命令符D,参数为学号查找功能,命令符F,参数为学号列表功能,命令符L,按学号升序列表统计功能,命令符T,平均最大最小等名次功能,命令符S,按成绩从大到小并排名 运行示例ST2>A10289ST2>A10178.5ST2>A10389ST2>L10178.510289.010389.0ST2>TNumber=3Sum=256.5,Ave=85.5Max=89.0,Min=78.5ST2>A10181ST2>F10110181.0ST2>D102ST2>F102NotFound:102ST2>xyzBadCommandST2>QGood-Bye. 一、一维数组数组与数列一维数组的定义和引用非数组→数组:输入出、累加和、最大值数组与函数:ex5-1.c学生信息程序:ex5-2.c排序、二分法查找:ex5-3.c学生信息程序:ex5-4.c,ex5-5.c 数组和数列数列:x0、x1、……、xn数组:x[0]、x[1]、……、x[n]数组名+下标相同:通过一个名称和下标表示各个元素数组:数组名为C标识符,下标总是从0开始,元素个数必须编程时确定 一维数组使用示例intn;n=5;n++;s+=n;printf("%d",n);scanf("%d",&n);for(i=1;i<=n;i++)inta[5];a[0]=5;a[4]++;s+=a[1+2];printf("%d",a[i]);scanf("%d",&a[j]);for(i=0;ia[i])m=a[i];returnm;} 常规应用示例(ex5-1.c)#defineN5main(){inta[N],x,m;longs;sca(a,N);s=sum(a,N);x=max(a,N);m=min(a,N);printf("Data:");prt(a,N);printf("Sum=%ld ",s);printf("Max=%d,Min=%d ",x,m);} 程序文件ex5-1.c函数列表sca,prt,summax,min,main代码:55行运行示例3010205040Data:3010205040Sum=150Max=50,Min=10#includevoidsca(inta[],intn){inti;for(i=0;ia[i])m=a[i];returnm;}#defineN5main(){inta[N],x,m;longs;sca(a,N);s=sum(a,N);x=max(a,N);m=min(a,N);printf("Data:");prt(a,N);printf("Sum=%ld ",s);printf("Max=%d,Min=%d ",x,m);} 学生管理设计思路(简化版)合理假设:学生数不超过100人#defineN100学号:intnum[N];学号为0或负,表示没有学生成绩:intsc[N];当前数组元素个数:intn;初始时为0,0≤n≤N管理方式增加时,数据存贮到下标n处(空闲空间)删除时,仅将num数组中相应学号清0 函数列表命令行常规:prompt,wait,cmdHelp常规函数:sum,max,min,find管理功能:cmdAdd,cmdDel,cmdFind,cmdList,cmdTotal子功能:clean(清理)主函数:main 帮助命令:cmdHelpvoidcmdHelp(void){printf("StudentProgram2Help: ");printf("A:Add ");printf("D:Del ");printf("F:Find ");printf("L:List ");/*printf("T:Total ");*/printf("H:Help ");printf("Q:Quit ");} 增加命令:cmdAdd简版传递学号与成绩数组,输入新数据并贮存到下标n处(该空间空闲),返回新的学生数intcmdAdd(intnum[],intsc[],intn){if(n>=N)returnn;scanf("%d%d",&num[n],&sc[n]);n=n+1;returnn;}returnn+1; 常规函数——查找并返回下标intfind(inta[],intn,intx){inti;for(i=0;i=0)num[i]=0;returnn;} 查找命令:cmdFind(顺序查找)传递学号与成绩数组,输入学号,查找该学号并输出相应信息voidcmdFind(intnum[],intsc[],intn){intx,i;scanf("%d",&x);i=find(num,n,x);if(i>=0)printf("%d%d ",num[i],sc[i]);elseprintf("NotFound:%d ",x);} 列表命令:cmdList(未排序)传递学号与成绩数组,每行一个学生信息voidcmdList(intnum[],intsc[],intn){inti;for(i=0;i");do{cmd=getchar();}while(cmd=='');/*单引号中含空格*/if(cmd>='a'&&cmd<='z')cmd=cmd-'a'+'A';returncmd;} 程序ex5-2.c代码:93行函数:9个maincmdAddcmdDelfindcmdFindfindcmdListcmdHelppromptwait#include#defineN100charprompt(void){charcmd;printf("ST2>");do{cmd=getchar();}while(cmd=='');/*含空格*/if(cmd>='a'&&cmd<='z')cmd=cmd-'a'+'A';returncmd;}voidwait(charcmd){while(cmd!=' ')cmd=getchar();return;}intfind(inta[],intn,intx){inti;for(i=0;i:Add ");printf("D:Del ");printf("F:Find ");printf("L:List ");printf("H:Help ");printf("Q:Quit ");}intcmdAdd(intnum[],intsc[],intn){if(n>=N)returnn;scanf("%d%d",&num[n],&sc[n]);n=n+1;returnn;}intcmdDel(intnum[],intn){intx,i;scanf("%d",&x);i=find(num,n,x);if(i>=0)num[i]=0;returnn;}voidcmdFind(intnum[],intsc[],intn){intx,i;scanf("%d",&x);i=find(num,n,x);if(i>=0)printf("%d%d ",num[i],sc[i]);elseprintf("NotFound:%d ",x);}voidcmdList(intnum[],intsc[],intn){inti;for(i=0;i#defineN100promptwaitfindcmdHelpcmdAddcmdDelcmdFindcmdListmain 运行示例ST2>A10289ST2>a10178ST2>A10389ST2>L102891017810389ST2>F10210289ST2>D102ST2>F102NotFound:102ST2>A10181ST2>L101781038910181ST2>QGood-Bye. 清理子功能传递学号与成绩数组,消去学号为0的元素,返回清理后的元素个数遍历循环查找学号为0的元素,下标i查找后续中学号非0元素,下标j下标j搬移到下标I先复制再清0如果下标j出界,则下标i以后学号均为0,下标i以前非0intclean(intnum[],intsc[],intn){inti,j;for(i=0;i=n)returni;num[i]=num[j];sc[i]=sc[j];num[j]=0;}returnn;} 需改进增加命令:缺编辑功能列表命令:缺排序功能名次命令:缺统计命令:缺数据类型:num→long,sc→float关于清理功能? 编辑功能:cmdAdd输入后,先查找同学号元素,再查找已清0元素intcmdAdd(intnum[],intsc[],intn){intx,s,i;scanf("%d%d",&x,&s);i=find(num,n,x);if(i<0)i=find(num,n,0);if(i<0)if(n>=N)returnn;elsei=n++;num[i]=x;sc[i]=s;returnn;}输入学号与成绩查找是否编辑模式查找是否有空闲位置任何一种查找到?未找到且元素个数满放到下标n处同时元素个数n递增1复制数据返回元素个数 排序与查找2个数的排序3个数的排序N个数的排序冒泡法的优化二分法查找示例程序ex5-3.c多信息排序多排序 交换2个数的值交换a与b两个数的值临时变量t逆时针方向依次赋值inta,b,t;t=a;a=b;b=t; 2个数的交换比较/交换inta,b,t;if(a>b){t=a;a=b;b=t;}先比较再交换比较两个数的值如果与排序顺序相反则交换两个数的值运行后a≤b成立 3个数的排序任意相邻两数比较并交换inta,b,c;第1遍的相邻比较/交换a与b:比较/交换→a≤bb与c:比较/交换→b≤c,但a≤b不保证成立结论:c为a,b,c的最大值第2遍的相邻比较/交换a与b:比较/交换→a≤b结论:b为a,b的最大值 N个数的排序N-1遍的两两相邻比较/交换inta[N];第1遍的相邻比较/交换(共N-1次)a[0]与a[1]:比较/交换a[N-2]与a[N-1]:比较/交换第2遍比较/交换(共N-2次)a[0]与a[1]:比较/交换a[N-3]与a[N-2]:比较/交换第N-1遍的比较/交换(共1次)a[0]与a[1]:比较/交换 N个数排序核心代码inta[N],i,j,t;for(i=0;ia[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}变量定义共N-1遍每遍N-1-i次相邻比较如果反序则交换 常规函数:排序voidsort(inta[],intn){inti,j,t;for(i=0;ia[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}对a数组的n个元素进行排序(从小到大)简易冒泡法排序n-1遍n-1-i次相邻比较交换 冒泡法的优化每一遍比较交换之后判断本遍是否存在过交换,若无,则已排序状态,不必继续循环使用ct对每遍的交换进行计数每遍n-1-i次比较的之前,之中与之后intct;for(i=0;ia[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;ct++;}if(ct==0)break;} 查找的优化:二分法/折半法在已排序数组中查找某个值设待查找的下标范围为low至high初始时,low=0,high=n-1a[low]至a[high],从小到大排序下标范围取中值并判断midd=(low+high)/2中值:a[midd]前段:a[low]至a[midd-1]后段:a[midd+1]至a[high] 二分法查找函数调用前提:已排序从小到大若找到,返回下标没找到,返回-1intfind2(inta[],intn,intx){intlow,high,midd;low=0;high=n-1;while(low<=high){midd=(low+high)/2;if(a[midd]==x)returnmidd;elseif(x=0)printf("a[%d]=%d ",i,a[i]);elseprintf("NotFound:%d ",x);}输入N个整数,排序后输出,再输入一个数,查找该数在排序后的位置 程序文件ex5-3.c函数列表sca,prt,sortfind2,main代码:56行运行示例30801060209050401007010203040506070809010060a[5]=60#includevoidsca(inta[],intn){inti;for(i=0;ia[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}intfind2(inta[],intn,intx){intlow,high,midd;low=0;high=n-1;while(low<=high){midd=(low+high)/2;if(a[midd]==x)returnmidd;elseif(x=0)printf("a[%d]=%d ",i,a[i]);elseprintf("NotFound:%d ",x);} 按学号升序的交换条件常规条件:下标j的学号比下标j+1的学号大num[j]>num[j+1]可选条件:下标j的学号无效num[j]<=0必须条件:下标j+1的学号有效num[j+1]>0(num[j]>num[j+1]||num[j]<=0)&&num[j+1]>0 学生信息按学号升序:双信息排序voidsort_stu(intnum[],intsc[],intn){inti,j,t;for(i=0;inum[j+1]||num[j]<=0)&&num[j+1]>0){t=num[j];num[j]=num[j+1];num[j+1]=t;t=sc[j];sc[j]=sc[j+1];sc[j+1]=t;}}传递学号与成绩比较学号空学号在后交换学号与成绩 升序优化voidsort_stu(intnum[],intsc[],intn){inti,j,t,ct;for(i=0;inum[j+1]||num[j]<=0)&&num[j+1]>0){t=num[j];num[j]=num[j+1];num[j+1]=t;t=sc[j];sc[j]=sc[j+1];sc[j+1]=t;ct++;}if(ct==0)break;}} 按成绩从大到小排序同时满足按学号升序与按成绩降序num数组与sc数组按学号升序另设s数组为成绩顺序表s[0]记录最高成绩所在下标,s[1]记录次高成绩所在下标,依次类推则,sc[s[0]]为最高成绩,sc[s[n-1]]为最低成绩比较sc[s[j]]与sc[s[j+1]] 第j名与第j+1名的交换条件第j名的成绩:sc[s[j]],第j名的学号:num[s[j]]常规条件:第j名学生成绩不如第j+1名学生成绩sc[s[j]]0(sc[s[j]]0 学生信息按成绩降序:双排序voidsort_sc(intnum[],intsc[],ints[],intn){inti,j,t;for(i=0;i0){t=s[j];s[j]=s[j+1];s[j+1]=t;}}传递排序数组s初始i下标为第i名比较j名与j+1名的成绩交换j名与j+1名下标 成绩降序的优化voidsort_sc(intnum[],intsc[],ints[],intn){inti,j,t,ct;for(i=0;i0){t=s[j];s[j]=s[j+1];s[j+1]=t;ct++;}if(ct==0)break;}} 排名次voidcmdSort(intnum[],intsc[],ints[],intn){/*调用前提:成绩降序*/inti,r;/*r表示名次*/for(i=0;i0)printf("%d:%d%d ",r,num[s[i]],sc[s[i]]);}} 简易清理intclean(intnum[],intn){inti;for(i=n-1;i>=0;i--)if(num[i]!=0)break;returni+1;}传递学号数组,消去学号数组中末尾学号无效的元素,返回清理后的元素个数前提:学号升序后递减循环若有效,则退出下标0至i为有效个数=i+1 统计功能:cmdTotal传递成绩数组,统计并输出。调用条件:清理后voidcmdTotal(intsc[],intn){longs;floatave;intx,m;s=sum(sc,n);ave=n>0?(float)s/n:0;x=max(sc,n);m=min(sc,n);printf("Number=%d ",n);printf("Sum=%ld,Ave=%.1f ",s,ave);printf("Max=%d,Min=%d ",x,m);} 帮助命令:cmdHelpvoidcmdHelp(void){printf("StudentProgram2Help: ");printf("A:Add ");printf("D:Del ");printf("F:Find ");printf("L:ListByNum ");printf("S:SortByScore ");printf("T:Total ");printf("H:Help ");printf("Q:Quit ");} 函数列表cmdAdd:增加/编辑命令sort_stu:按学号升序sort_sc:按成绩降序find2:二分法查找cmdHelp:帮助命令cmdSort:排名clean:简易清理cmdTotal:统计功能 命令分支语句#defineN100intnum[N],sc[N],s[N],n=0;charcmd;switch(cmd){case'A':n=cmdAdd(num,sc,n);break;case'D':n=cmdDel(num,n);break;case'F':cmdFind(num,sc,n);break;case'L':sort_stu(num,sc,n);cmdList(num,sc,n);break;case'S':sort_sc(num,sc,s,n);cmdSort(num,sc,s,n);break;case'T':sort_stu(num,sc,n);n=clean(num,n);cmdTotal(sc,n);break;case'H':cmdHelp();case' ':break;default:printf("BadCommand ");} 程序集成(ex5-4.c)mainpromptwaitcmdHelpcmdAddcmdDelfindcmdFindfindsort_stusort_sccleancmdListcmdSortcmdTotalsum,max,min代码:198行函数:17个summaxminfindpromptwaitsort_stusort_sccleancmdHelpcmdAddcmdDelcmdFindcmdListcmdSortcmdTotalmain#include#defineN100longsum(inta[],intn){longs;inti;s=0;for(i=0;ia[i])m=a[i];returnm;}intfind(inta[],intn,intx){inti;for(i=0;i");do{cmd=getchar();}while(cmd=='');/*单引号中含空格*/if(cmd>='a'&&cmd<='z')cmd=cmd-'a'+'A';returncmd;}voidwait(charcmd){while(cmd!=' ')cmd=getchar();return;}voidsort_stu(intnum[],intsc[],intn){inti,j,t,ct;for(i=0;inum[j+1]||num[j]<=0)&&num[j+1]>0){t=num[j];num[j]=num[j+1];num[j+1]=t;t=sc[j];sc[j]=sc[j+1];sc[j+1]=t;ct++;}if(ct==0)break;}}voidsort_sc(intnum[],intsc[],ints[],intn){inti,j,t,ct;for(i=0;i0){t=s[j];s[j]=s[j+1];s[j+1]=t;ct++;}if(ct==0)break;}}intclean(intnum[],intn){inti;for(i=n-1;i>=0;i--)if(num[i]!=0)break;returni+1;}voidcmdHelp(void){printf("StudentProgram2Help: ");printf("A:Add ");printf("D:Del ");printf("F:Find ");printf("L:ListByNum ");printf("S:SortByScore ");printf("T:Total ");printf("H:Help ");printf("Q:Quit ");}intcmdAdd(intnum[],intsc[],intn){intx,s,i;scanf("%d%d",&x,&s);i=find(num,n,x);if(i<0)i=find(num,n,0);if(i<0)if(n>=N)returnn;elsei=n++;num[i]=x;sc[i]=s;returnn;}intcmdDel(intnum[],intn){intx,i;scanf("%d",&x);i=find(num,n,x);if(i>=0)num[i]=0;returnn;}voidcmdFind(intnum[],intsc[],intn){intx,i;scanf("%d",&x);i=find(num,n,x);if(i>=0)printf("%d%d ",num[i],sc[i]);elseprintf("NotFound:%d ",x);}voidcmdList(intnum[],intsc[],intn){inti;for(i=0;i0)printf("%d:%d%d ",r,num[s[i]],sc[s[i]]);}}voidcmdTotal(intsc[],intn){longs;floatave;intx,m;s=sum(sc,n);ave=n>0?(float)s/n:0;x=max(sc,n);m=min(sc,n);printf("Number=%d ",n);printf("Sum=%ld,Ave=%.1f ",s,ave);printf("Max=%d,Min=%d ",x,m);}main(){charcmd;intn=0;intnum[N],sc[N],s[N];while(1){cmd=prompt();if(cmd=='Q')break;switch(cmd){case'A':n=cmdAdd(num,sc,n);break;case'D':n=cmdDel(num,n);break;case'F':cmdFind(num,sc,n);break;case'L':sort_stu(num,sc,n);cmdList(num,sc,n);break;case'S':sort_sc(num,sc,s,n);cmdSort(num,sc,s,n);break;case'T':sort_stu(num,sc,n);n=clean(num,n);cmdTotal(sc,n);break;case'H':cmdHelp();case' ':break;default:printf("BadCommand ");}wait(cmd);}printf("Good-Bye. ");} 运行示例ST2>A10289ST2>a10178ST2>A10389ST2>L101781028910389ST2>F10210289ST2>A10181ST2>S1:102891:103893:10181ST2>D102ST2>F102NotFound:102ST2>QGood-Bye. 类型修改(ex5-5.c)longnum[N];floatsc[N];ints[N];intn=0;floatsum(floata[],intn);floatmax(floata[],intn);floatmin(floata[],intn);intfind(longa[],intn,longx);voidsort_stu(longnum[],floatsc[],intn);voidsort_sc(longnum[],floatsc[],ints[],intn);intclean(longnum[],intn);intcmdAdd(longnum[],floatsc[],intn);intcmdDel(longnum[],intn);voidcmdFind(longnum[],floatsc[],intn);voidcmdList(longnum[],floatsc[],intn);voidcmdSort(longnum[],floatsc[],ints[],intn);voidcmdTotal(floatsc[],intn);#include#defineN100floatsum(floata[],intn){floats;inti;s=0;for(i=0;ia[i])m=a[i];returnm;}longfind(longa[],intn,longx){inti;for(i=0;i");do{cmd=getchar();}while(cmd=='');/*单引号中含空格*/if(cmd>='a'&&cmd<='z')cmd=cmd-'a'+'A';returncmd;}voidwait(charcmd){while(cmd!=' ')cmd=getchar();return;}voidsort_stu(longnum[],floatsc[],intn){inti,j,ct;longtn;floatts;for(i=0;inum[j+1]||num[j]<=0)&&num[j+1]>0){tn=num[j];num[j]=num[j+1];num[j+1]=tn;ts=sc[j];sc[j]=sc[j+1];sc[j+1]=ts;ct++;}if(ct==0)break;}}voidsort_sc(longnum[],floatsc[],ints[],intn){inti,j,t,ct;for(i=0;i0){t=s[j];s[j]=s[j+1];s[j+1]=t;ct++;}if(ct==0)break;}}intclean(longnum[],intn){inti;for(i=n-1;i>=0;i--)if(num[i]!=0)break;returni+1;}voidcmdHelp(void){printf("StudentProgram2Help: ");printf("A:Add ");printf("D:Del ");printf("F:Find ");printf("L:ListByNum ");printf("S:SortByScore ");printf("T:Total ");printf("H:Help ");printf("Q:Quit ");}intcmdAdd(longnum[],floatsc[],intn){longx;floats;inti;scanf("%ld%f",&x,&s);i=find(num,n,x);if(i<0)i=find(num,n,0);if(i<0)if(n>=N)returnn;elsei=n++;num[i]=x;sc[i]=s;returnn;}intcmdDel(longnum[],intn){longx;inti;scanf("%ld",&x);i=find(num,n,x);if(i>=0)num[i]=0;returnn;}voidcmdFind(longnum[],floatsc[],intn){longx;inti;scanf("%ld",&x);i=find(num,n,x);if(i>=0)printf("%ld%.1f ",num[i],sc[i]);elseprintf("NotFound:%ld ",x);}voidcmdList(longnum[],floatsc[],intn){inti;for(i=0;i0)printf("%d:%ld%.1f ",r,num[s[i]],sc[s[i]]);}}voidcmdTotal(floatsc[],intn){floats,ave,x,m;s=sum(sc,n);ave=n>0?s/n:0;x=max(sc,n);m=min(sc,n);printf("Number=%d ",n);printf("Sum=%.1f,Ave=%.1f ",s,ave);printf("Max=%.1f,Min=%.1f ",x,m);}main(){charcmd;intn=0;longnum[N];floatsc[N];ints[N];while(1){cmd=prompt();if(cmd=='Q')break;switch(cmd){case'A':n=cmdAdd(num,sc,n);break;case'D':n=cmdDel(num,n);break;case'F':cmdFind(num,sc,n);break;case'L':sort_stu(num,sc,n);cmdList(num,sc,n);break;case'S':sort_sc(num,sc,s,n);cmdSort(num,sc,s,n);break;case'T':sort_stu(num,sc,n);n=clean(num,n);cmdTotal(sc,n);break;case'H':cmdHelp();case' ':break;default:printf("BadCommand ");}wait(cmd);}printf("Good-Bye. ");} 运行示例ST2>A10289.5ST2>a10178.5ST2>A10389.5ST2>L10178.510289.510389.5ST2>F10210289.5ST2>A10181ST2>S1:10289.51:10389.53:10181.0ST2>D102ST2>F102NotFound:102ST2>QGood-Bye. 一维数组注意点数组初始化输入n和n个整数输入直到0时终止求最大值所在下标选择法排序 一维数组的初始化求y年m月的天数intdays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};if(y%4==0&&y%100!=0||y%400==0)days[2]=29;/*闰年2月为29天*/n=days[m]; 应用——输入n和n个整数合理假设:输入的n介于0至100之间intn,a[100],i;/*空间有可能浪费,也可能溢出*/scanf("%d",&n);for(i=0;ia[j]){/*如果不是从小到大*/t=a[i];a[i]=a[j];a[j]=t;}} 选择法排序改进for(i=0;ia[j])/*如果发现更小值*/k=j;/*使a[k]指向最小值*/if(k!=i)/*如果a[i]不是a[i]至a[N-1]的最小值*/{/*则交换a[i]与a[k]*/t=a[i];a[i]=a[k];a[k]=t;}} 遍历+筛选+输出编写函数,输出数组a中大于x的元素voidprt_sel(inta[],intn,intx){inti;for(i=0;ix)printf("%d",a[i]);}} 遍历+筛选+生成将a数组n个元素中大于x的元素按原序保存到数组b中,并返回大于x的元素个数。intselect(inta[],intn,intx,intb[]);如inta[5]={30,20,50,10,40},b[5];则k=select(a,5,25,b);运行后b数组为{30,50,40,?,?}k=3 筛选函数intselect(inta[],intn,intx,intb[]){inti,k=0;for(i=0;ix){b[k]=a[i];k=k+1;}returnk;}k为计数器+下标初始为0若条件成立保存至b数组计数增1b[k++]=a[i]; 应用例示main(){inta[10],b[10],k,x;longs;sca(a,10);s=sum(a,10);k=select(a,10,s/10,b);x=min(b,k);printf("X=%d ",x);}输出数组中大于且最接近平均值的元素输入10个元素计算累加和超过平均值→bb数组的最小值输出 [案例需求3]编写学生信息管理程序,实现:命令行交互方式,提示符为“ST3>”命令符改为命令词,命令词不分大小写增加学生姓名信息(设姓名不超过19个字符)帮助功能,命令串Help,显示关于命令的使用说明退出功能,命令串Quit,退出程序增加功能,命令符Add,参数为学号、姓名、成绩删除功能,命令符Del,参数为学号查找功能,命令符Find,参数为学号列表功能,命令符List,按学号升序列表统计功能,命令符Total,平均最大最小等名次功能,命令符Sort,按成绩从大到小并排名 二、指针基本概念变量信息:值和地址指针:地址指针变量:值为地址的变量指针变量定义:变量名前带*int*p;p是一个变量,为指针变量p的值是一个int类型变量的地址取址运算符(&):对一般变量取其地址取值运算符(*):对指针变量取地址所存数据 [例7.1]指针使用举例intn,m;int*p;scanf("%d",&n);p=&n;m=*p;printf("%d ",m);m=n; 指针与一维数组inta[]={10,11,12,13,14};int*p;p=&a[0]; 三、字符串字符串常量字符串表示——数组法字符串输出字符串输入常用字符串函数程序举例 字符串常量使用双引号:"Hello"连续的字符序列'H','e','l','l','o',''字符尾部追加''字符字符串的实质是指针字符串在内存的首地址区分"A"和'A'Hello地址值0x3000'H'0x3001'e'0x3002'l'0x3003'l'0x3004'o'0x3005''…地址为假设 数组表示charstr[8]="Hello";字符数组str有8个元素前5个为字符H、e、l、l、o第6个为字符串结束符后2个为二进制0地址值变量0x2000'H'str[0]0x2001'e'str[1]0x2002'l'str[2]0x2003'l'str[3]0x2004'o'str[4]0x2005''str[5]0x20060str[6]0x20070str[7]str 输入输出函数charstr[80];char*p;输出函数puts(str);等价于printf("%s ",str);puts(p);等价于printf("%s ",p);输入函数gets(str);相当于scanf("%s",str);gets(p);相当于scanf("%s",p);gets输入一行,以回车结束scanf("%s",…)输入一个单词以空格、TAB和回车结束#include 字符串输出数组法charstr[]="Hello,world!";printf("%s ",str);或puts(str); 字符串输入数组法charstr[80];scanf("%s",str);或gets(str);字符串输入时必须先用数组法开好空间 字符串常用函数(1)#include计算并返回字符串长度函数声明:intstrlen(char*p);函数声明:intstrlen(charstr[]);区别strlen与sizeof/计算存贮空间charstr[80]="Hello";strlen(str)为5,strlen("Hello")为5sizeof(str)为80,sizeof("Hello")为6下标[0][1][2][3][4][5][6]…[79]strHello??? 字符串的循环处理按长度循环charstr[80];gets(str);n=strlen(str);for(i=0;i函数,参见教程Page293判断字母:isupper,islower,isalpha判断数字:isdigit,isalnum转换大小写:toupper,tolower设charc;if(islower(c))等价if(c>='a'&&c<='z')c=toupper(c)等价if(c>='a'&&c<='z')c=c-'a'+'A'; 小写转换为大写#includemain(){chara[80];inti;gets(a);for(i=0;a[i]!='';i++)/*i='a'&&a[i]<='z')/*islower(a)*/a[i]=a[i]-'a'+'A';/*a[i]=toupper(a[i])*/puts(a);} 筛选小写字符并生成字符串inti,n;charstr[80];gets(str);n=strlen(str);for(i=0;i字符串复制(将字符串s2复制到s1中)函数声明:char*strcpy(char*s1,char*s2);函数声明:char*strcpy(chars1[],chars2[]);例如:charstr[80];strcpy(str,"Hello");下标[0][1][2][3][4][5][6]…[79]原str?????????新strHello??? 字符串常用函数(3)#include字符串连接(将字符串s2拼接到s1后)函数声明:char*strcat(char*s1,char*s2);函数声明:char*strcat(chars1[],chars2[]);例如:charstr[80]="He";strcat(str,"llo");下标[0][1][2][3][4][5][6]…[79]原strHe??????新strHello??? 字符串常用函数(4)#include字符串比较(字典顺序,s1在后则>0)函数声明:intstrcmp(char*s1,char*s2);函数声明:intstrcmp(chars1[],chars2[]);例如:chara[80]="A123",b[50]="A4";则strcmp(a,b)<0为真下标[0][1][2][3][4][5]…[49]…[79]aA123?????bA4????? 字符串与整型数使用比较intm,n;scanf("%d",&m);printf("%d",m);m=5;n=m;if(m='a'&&s[i]<='z')s[i]=s[i]-'a'+'A';returns;} 命令编码命令编码,每个命令分配唯一编码#defineCMD_Quit1#defineCMD_Help2#defineCMD_Add3#defineCMD_Del4#defineCMD_Find5#defineCMD_List6#defineCMD_Sort7#defineCMD_Total8#defineCMD_Bad9chars[80];scanf("%s",s);if(stricmp(s,"Quit")==0)returnCMD_Quit;if(stricmp(s,"Help")==0)returnCMD_Help;…… 命令词解释提示符函数prompt2使用stricmp函数显示提示符输入命令词命令词是否Quit?是则返回CMD_Quit命令词是否Help?是则返回CMD_Help……返回CMD_Badintprompt2(void){chars[80];printf("ST3>");scanf("%s",s);if(stricmp(s,"Quit")==0)returnCMD_Quit;if(stricmp(s,"Help")==0)returnCMD_Help;if(stricmp(s,"Add")==0)returnCMD_Add;if(stricmp(s,"Del")==0)returnCMD_Del;if(stricmp(s,"Find")==0)returnCMD_Find;if(stricmp(s,"List")==0)returnCMD_List;if(stricmp(s,"Sort")==0)returnCMD_Sort;if(stricmp(s,"Total")==0)returnCMD_Total;returnCMD_Bad;} 附加解释附加对单命令符的解释插入到prompt2函数中命令串"Q"等同于"Quit"命令串"H"等同于"Help"以此类推if(stricmp(s,"Q")==0)returnCMD_Quit;if(stricmp(s,"H")==0)returnCMD_Help;if(stricmp(s,"A")==0)returnCMD_Add;if(stricmp(s,"D")==0)returnCMD_Del;if(stricmp(s,"F")==0)returnCMD_Find;if(stricmp(s,"L")==0)returnCMD_List;if(stricmp(s,"S")==0)returnCMD_Sort;if(stricmp(s,"T")==0)returnCMD_Total; 主函数cmd为命令编码,整数类型case使用符号常量取消wait函数main(){intcmd,n=0;longnum[N];floatsc[N];ints[N];while(1){cmd=prompt2();if(cmd==CMD_Quit)break;switch(cmd){caseCMD_Add:n=cmdAdd(num,sc,n);break;caseCMD_Del:n=cmdDel(num,n);break;caseCMD_Find:cmdFind(num,sc,n);break;caseCMD_List:sort_stu(num,sc,n);cmdList(num,sc,n);break;caseCMD_Sort:sort_sc(num,sc,s,n);cmdSort(num,sc,s,n);break;caseCMD_Total:sort_stu(num,sc,n);n=clean(num,n);cmdTotal(sc,n);break;caseCMD_Help:cmdHelp();break;default:printf("BadCommand ");}}printf("Good-Bye. ");} 帮助命令voidcmdHelp(void){printf("StudentProgram3Help: ");printf("Add:Add ");printf("Del:Del ");printf("Find:Find ");printf("List:ListByNum ");printf("Sort:SortByScore ");printf("Total:Total ");printf("Help:Help ");printf("Quit:Quit ");} 程序集成(ex5-6.c)代码:212行,函数:16个summaxminfindsort_stusort_sccleancmdAddcmdDelcmdFindcmdListcmdSortcmdTotalcmdHelpprompt2main#include#include#defineN100floatsum(floata[],intn){floats;inti;s=0;for(i=0;ia[i])m=a[i];returnm;}longfind(longa[],intn,longx){inti;for(i=0;inum[j+1]||num[j]<=0)&&num[j+1]>0){tn=num[j];num[j]=num[j+1];num[j+1]=tn;ts=sc[j];sc[j]=sc[j+1];sc[j+1]=ts;ct++;}if(ct==0)break;}}voidsort_sc(longnum[],floatsc[],ints[],intn){inti,j,t,ct;for(i=0;i0){t=s[j];s[j]=s[j+1];s[j+1]=t;ct++;}if(ct==0)break;}}intclean(longnum[],intn){inti;for(i=n-1;i>=0;i--)if(num[i]!=0)break;returni+1;}intcmdAdd(longnum[],floatsc[],intn){longx;floats;inti;scanf("%ld%f",&x,&s);i=find(num,n,x);if(i<0)i=find(num,n,0);if(i<0)if(n>=N)returnn;elsei=n++;num[i]=x;sc[i]=s;returnn;}intcmdDel(longnum[],intn){longx;inti;scanf("%ld",&x);i=find(num,n,x);if(i>=0)num[i]=0;returnn;}voidcmdFind(longnum[],floatsc[],intn){longx;inti;scanf("%ld",&x);i=find(num,n,x);if(i>=0)printf("%ld%.1f ",num[i],sc[i]);elseprintf("NotFound:%ld ",x);}voidcmdList(longnum[],floatsc[],intn){inti;for(i=0;i0)printf("%d:%ld%.1f ",r,num[s[i]],sc[s[i]]);}}voidcmdTotal(floatsc[],intn){floats,ave,x,m;s=sum(sc,n);ave=n>0?s/n:0;x=max(sc,n);m=min(sc,n);printf("Number=%d ",n);printf("Sum=%.1f,Ave=%.1f ",s,ave);printf("Max=%.1f,Min=%.1f ",x,m);}voidcmdHelp(void){printf("StudentProgram3Help: ");printf("Add:Add ");printf("Del:Del ");printf("Find:Find ");printf("List:ListByNum ");printf("Sort:SortByScore ");printf("Total:Total ");printf("Help:Help ");printf("Quit:Quit ");}#defineCMD_Quit1#defineCMD_Help2#defineCMD_Add3#defineCMD_Del4#defineCMD_Find5#defineCMD_List6#defineCMD_Sort7#defineCMD_Total8#defineCMD_Bad9intprompt2(void){chars[80];printf("ST3>");scanf("%s",s);if(stricmp(s,"Quit")==0)returnCMD_Quit;if(stricmp(s,"Help")==0)returnCMD_Help;if(stricmp(s,"Add")==0)returnCMD_Add;if(stricmp(s,"Del")==0)returnCMD_Del;if(stricmp(s,"Find")==0)returnCMD_Find;if(stricmp(s,"List")==0)returnCMD_List;if(stricmp(s,"Sort")==0)returnCMD_Sort;if(stricmp(s,"Total")==0)returnCMD_Total;if(stricmp(s,"Q")==0)returnCMD_Quit;if(stricmp(s,"H")==0)returnCMD_Help;if(stricmp(s,"A")==0)returnCMD_Add;if(stricmp(s,"D")==0)returnCMD_Del;if(stricmp(s,"F")==0)returnCMD_Find;if(stricmp(s,"L")==0)returnCMD_List;if(stricmp(s,"S")==0)returnCMD_Sort;if(stricmp(s,"T")==0)returnCMD_Total;returnCMD_Bad;}main(){intcmd,n=0;longnum[N];floatsc[N];ints[N];while(1){cmd=prompt2();if(cmd==CMD_Quit)break;switch(cmd){caseCMD_Add:n=cmdAdd(num,sc,n);break;caseCMD_Del:n=cmdDel(num,n);break;caseCMD_Find:cmdFind(num,sc,n);break;caseCMD_List:sort_stu(num,sc,n);cmdList(num,sc,n);break;caseCMD_Sort:sort_sc(num,sc,s,n);cmdSort(num,sc,s,n);break;caseCMD_Total:sort_stu(num,sc,n);n=clean(num,n);cmdTotal(sc,n);break;caseCMD_Help:cmdHelp();break;default:printf("BadCommand ");}}printf("Good-Bye. ");} 运行示例ST3>Add10289.5ST3>add10178.5ST3>ADD10389.5ST3>List10178.510289.510389.5ST3>Find10210289.5ST3>A10181ST3>SorT1:10289.51:10389.53:10181.0ST3>D102ST3>FIND102NotFound:102ST3>QUITGood-Bye. 四、二维数组整数的二维数组3行4列的整数矩阵:inta[3][4];字符的二维数组10行80列的字符数组,或10个字符串chars[10][80]; 输入N个姓名并排序字符串#defineN10charz[N][80],t[80];inti,j;for(i=0;i0){strcpy(t,z[j]);strcpy(z[j],z[j+1]);strcpy(z[j+1],t);}整数for(i=0;ia[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;} 输出N个字符串字符串for(i=0;i=N)returnn;elsei=n++;num[i]=x;strcpy(z[i],t);sc[i]=s;returnn;} cmdFind函数voidcmdFind(longnum[],charz[][80],floatsc[],intn){longx;inti;scanf("%ld",&x);i=find(num,n,x);if(i>=0)printf("%ld%s%.1f ",num[i],z[i],sc[i]);elseprintf("NotFound:%ld ",x);} sort_stu函数voidsort_stu(longnum[],charz[][80],floatsc[],intn){inti,j,ct;longtn;floatts;chart[80];for(i=0;inum[j+1]||num[j]<=0)&&num[j+1]>0){tn=num[j];num[j]=num[j+1];num[j+1]=tn;strcpy(t,z[j]);strcpy(z[j],z[j+1]);strcpy(z[j+1],t);ts=sc[j];sc[j]=sc[j+1];sc[j+1]=ts;ct++;}if(ct==0)break;}} cmdList函数voidcmdList(longnum[],charz[][80],floatsc[],intn){inti;for(i=0;i0)printf("%d:%ld%s%.1f ",r,num[s[i]],z[s[i]],sc[s[i]]);}} 命令分支控制charz[N][80];caseCMD_Add:n=cmdAdd(num,z,sc,n);break;caseCMD_Del:n=cmdDel(num,n);break;caseCMD_Find:cmdFind(num,z,sc,n);break;caseCMD_List:sort_stu(num,z,sc,n);cmdList(num,z,sc,n);break;caseCMD_Sort:sort_sc(num,sc,s,n);cmdSort(num,z,sc,s,n);break;caseCMD_Total:sort_stu(num,z,sc,n);n=clean(num,n);cmdTotal(sc,n);break;caseCMD_Help:cmdHelp();break;default:printf("BadCommand "); 程序(ex5-7.c)代码:217行函数:16个summaxminfindsort_stusort_sccleancmdAddcmdDelcmdFindcmdListcmdSortcmdTotalcmdHelppromptmain#include#include#defineN100floatsum(floata[],intn){floats;inti;s=0;for(i=0;ia[i])m=a[i];returnm;}longfind(longa[],intn,longx){inti;for(i=0;inum[j+1]||num[j]<=0)&&num[j+1]>0){tn=num[j];num[j]=num[j+1];num[j+1]=tn;strcpy(t,z[j]);strcpy(z[j],z[j+1]);strcpy(z[j+1],t);ts=sc[j];sc[j]=sc[j+1];sc[j+1]=ts;ct++;}if(ct==0)break;}}voidsort_sc(longnum[],floatsc[],ints[],intn){inti,j,t,ct;for(i=0;i0){t=s[j];s[j]=s[j+1];s[j+1]=t;ct++;}if(ct==0)break;}}intclean(longnum[],intn){inti;for(i=n-1;i>=0;i--)if(num[i]!=0)break;returni+1;}intcmdAdd(longnum[],charz[][80],floatsc[],intn){longx;floats;inti;chart[80];scanf("%ld%s%f",&x,t,&s);i=find(num,n,x);if(i<0)i=find(num,n,0);if(i<0)if(n>=N)returnn;elsei=n++;num[i]=x;strcpy(z[i],t);sc[i]=s;returnn;}intcmdDel(longnum[],intn){longx;inti;scanf("%ld",&x);i=find(num,n,x);if(i>=0)num[i]=0;returnn;}voidcmdFind(longnum[],charz[][80],floatsc[],intn){longx;inti;scanf("%ld",&x);i=find(num,n,x);if(i>=0)printf("%ld%s%.1f ",num[i],z[i],sc[i]);elseprintf("NotFound:%ld ",x);}voidcmdList(longnum[],charz[][80],floatsc[],intn){inti;for(i=0;i0)printf("%d:%ld%s%.1f ",r,num[s[i]],z[s[i]],sc[s[i]]);}}voidcmdTotal(floatsc[],intn){floats,ave,x,m;s=sum(sc,n);ave=n>0?s/n:0;x=max(sc,n);m=min(sc,n);printf("Number=%d ",n);printf("Sum=%.1f,Ave=%.1f ",s,ave);printf("Max=%.1f,Min=%.1f ",x,m);}voidcmdHelp(void){printf("StudentProgram3Help: ");printf("Add:Add ");printf("Del:Del ");printf("Find:Find ");printf("List:ListByNum ");printf("Sort:SortByScore ");printf("Total:Total ");printf("Help:Help ");printf("Quit:Quit ");}#defineCMD_Quit1#defineCMD_Help2#defineCMD_Add3#defineCMD_Del4#defineCMD_Find5#defineCMD_List6#defineCMD_Sort7#defineCMD_Total8#defineCMD_Bad9intprompt2(void){chars[80];printf("ST3>");scanf("%s",s);if(stricmp(s,"Quit")==0)returnCMD_Quit;if(stricmp(s,"Help")==0)returnCMD_Help;if(stricmp(s,"Add")==0)returnCMD_Add;if(stricmp(s,"Del")==0)returnCMD_Del;if(stricmp(s,"Find")==0)returnCMD_Find;if(stricmp(s,"List")==0)returnCMD_List;if(stricmp(s,"Sort")==0)returnCMD_Sort;if(stricmp(s,"Total")==0)returnCMD_Total;if(stricmp(s,"Q")==0)returnCMD_Quit;if(stricmp(s,"H")==0)returnCMD_Help;if(stricmp(s,"A")==0)returnCMD_Add;if(stricmp(s,"D")==0)returnCMD_Del;if(stricmp(s,"F")==0)returnCMD_Find;if(stricmp(s,"L")==0)returnCMD_List;if(stricmp(s,"S")==0)returnCMD_Sort;if(stricmp(s,"T")==0)returnCMD_Total;returnCMD_Bad;}main(){intcmd,n=0;longnum[N];floatsc[N];ints[N];charz[N][80];while(1){cmd=prompt2();if(cmd==CMD_Quit)break;switch(cmd){caseCMD_Add:n=cmdAdd(num,z,sc,n);break;caseCMD_Del:n=cmdDel(num,n);break;caseCMD_Find:cmdFind(num,z,sc,n);break;caseCMD_List:sort_stu(num,z,sc,n);cmdList(num,z,sc,n);break;caseCMD_Sort:sort_sc(num,sc,s,n);cmdSort(num,z,sc,s,n);break;caseCMD_Total:sort_stu(num,z,sc,n);n=clean(num,n);cmdTotal(sc,n);break;caseCMD_Help:cmdHelp();break;default:printf("BadCommand ");}}printf("Good-Bye. ");} 运行示例ST3>Add102Qian89.5ST3>add101Zhan78.5ST3>ADD103Sun89.5ST3>List101Zhan78.5102Qian89.5103Sun89.5ST3>Find102102Qian89.5ST3>A101Chen81ST3>SorT1:102Qian89.51:103Sun89.53:101Chen81.0ST3>D102ST3>FIND102NotFound:102ST3>QUITGood-Bye. 扩展需求在查找、列表与排名中增加显示绩点与等级101Zhan78.53.0C102Qian89.53.7B103Sun89.53.7B其它?

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。
大家都在看
近期热门
关闭