计算机三级网络上机100题.docx
题目1(无忧id 14、id 27(提供isP()函数;捷成id23题)请编写一个函数jsValue(int m,int k,int xx),该函数的功能是:将大于整数m且紧靠m的k个素数存入数组xx 传回。最后调用函数writeDat。读取10组数据,分别得出结果且把结果输出到文件out.dat中。部分源程序存在文件progl.c中。例如:若输入175则应输出:19,23,29,31,37。请勿改动主函数main。和写函数writeDat。的内容。#include <conio.h>#include <stdio.h>void readwriteDat();int isP(int m)int i;for(i=2;i<m;i+)if(m % i=0)return 0;return 1;)void num(int mjnt kjnt xx)int s=0;for(m=m+l;k>0;m+)if(isP(m) xxs+=m; k;)main()(int m,n,xx1000;clrscr();printf(MnPlease enter two integers:*');scanf(,%d%d,&m,&n);num(m,n,xx);fbr( m=0;m vn ;m+)printf(M%d H,xxm);printf('*n");readwriteDat();void readwriteDat()(int m,n,xx1000, i;FILE *rf,*wf;rf=fope n("in. dat","r");wf=fopen(,'out.dat,(w");for(i=0;i<10;i+)fscanf(rf,H%d %dM,&m,&n);num(m,n,xx);for(m=0;m<n;m+)fprintf(wf,%d xxm); fprintf(wf,"nH);fclose(rf);fclose(wf);)题目2(无忧id 143)已知数据文件IN.DAT中存有200个四位数,并已调用读函数readDat。把这些数存入数组a中,请考生编制一函数jsVal(),其功能是:如果四位数各位上的数字均是0或2或4或6或8,则统计出满足此条件的个数ent,并把这些四位数按从大到小的顺序存入数组b中。最后main()函数调用写函数writeDat。把结果ent以及数组b中符合条件的四位数输出到OUT.DAT文件中。注意:部分源程序存在文件progl.c中。程序中已定义数组:a200, b200,已定义变量:ent请勿改动数据文件1N.DAT中的任何数据、主函数main。、读函数readDat。和写函数writeDat。的内容。#include <stdio.h>#define MAX 200int aMAX, bMAX, ent =0;void jsVal()int iJ,qw,bw,sw,gw;for(i=0;i<MAX;i+) qw=ai/1000; bw=ai/100%10;sw=ai%100/10; gw=ai%10;if(qw&&qw%2=0&&bw%2=0&&sw%2=0&&gw%2=0) bcnt+=ai;)for(i=0;i<cnt-l ;i+)for(j=i+l;j<cnty+)if(bi<bj) qw=bi; bi=bj; bj=qw;)void readDat()(inti;FILE *fp;fp = fopen("in.dat",;for(i =0; i < MAX ; i+) fscanf(fp, H%dn,&ai);fclose(fp);)void main()(inti;readDat();jsVal();printf(”满足条件的数=%dn”, ent);for(i =0; i < ent; i+) printf(H%d ", bi);printfCAn1');writeDat();writeDat()(FILE *fp;inti;fp = fopen(nout.datM, HwM);fprintf(fp,"%dn", ent);for(i =0; i < ent; i+) fprintf(fp, H%dn' b(i);fclose(fp);题目3(无忧id 133题;捷成id 59、99题)函数ReadDat()实现从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中;请编制函数StrOR(),其函数的功能是:以行为单位依次把字符串中所有小写字母。左边的字符串内容移到该串的右边存放,然后把小写字母。删除,余下的字符串内容移到已处理字符串的左边存放,之后把己处理的字符串仍按行重新存入字符串数组XX 中。最后main。函数调用函数WriteDat()把结果xx输出到文件OUT5.DAT中。例如:原文:n any field.Yu can create an indexyou have the correct record.结果:n any field. Yu can create an indexrd. yu have the crrect rec原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。注意:部分源程序存放在文件progl.c中。请勿改动主函数main。、读数密函数ReadDat。和输出数据函数WriteDat。的内容。#include <stdio.h>#include <string.h>#include <conio.h>char xx5080;int maxline =0;/*文章的总行数*/int ReadDat(void);void WriteDat(void);void StrOR(void)int i,rightochar teml80|;for(i=0;i<maxline;i+)/*倒序循环*/for(j=strien(xxi)-l;j>=0;j-) k=0;memset(tem,0,80);/*初始化字符串数组tem*/if(xxij=,o,)/*如果当前字符为b',进入以下语句*/righto二j;/*则将此字符中位置j的值赋给righto*/for(s=righto+l;s<strlen(xxi);s+)temk+=xxis;/*从righto的下跳开始将其*所有的字符都存入到tem中*/tbr(s=0;s<rlghto;s+)/*从当前行首部开始到出现字符刊的位置(righoto)之前开始循环*/if(xxis!='o,) temk+=xxis;/*将不是字符的字符全存入到 tem 中*/strcpy(xxi,tem);/*将当前己处理的不符重新存入当前行xx*/)else continue;)void main()(clrscr();if(ReadDat()printf("数据文件IN.DAT不能打开!n007”);return ;)StrOR();WriteDat();int ReadDat(void)(FILE *fp;int i =0;char *p;if(fp = fopen("IN.DAT", Y1)= NULL) return 1;while(fgets(xxi,80, fp)!= NULL)p = strchr(xxi, S');if(p)*p =0;i+;)maxline = i ;fclose(fp);return 0;void WriteDat(void)(FILE *fp;inti;clrscr();fp = fopen(,OUT5.DATM, nwn);for(i =0; i < maxline ; i+)printf(M%snH, xxi);fprintf(fp,"%snn, xxi);fclose(fp);相关库函数解释:char *strncat(char *dest, const char *src, size_t maxlen)功能:将字符串src中前maxlen个字符连接到dest中相关头文件:string.hchar *strcpy(char *dest, const char *src)功能:将字符串src拷贝到字符串dest中去相关头文件:string.h 题目4(无忧id24题捷成id9题)函数ReadDat。实现从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中,请编制函数StrOL(),其函数的功能是:以行为单位对行中以空格或标点符号为分隔的所有单词进行倒排。最后把已处理的字符串(应不含标点符号)仍按行重新存入字符串数组xx中,最后调用函数writeDat()把结果xx输出到文件OUT6.DAT中。例如:原文:You He MeI am a student.结果:Me He Youstudent a am I原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。部分源程序存在文件progl.c中。请勿改动主函数main。、读数据函数ReadDat。和输出数据函数writeDat。的内容。#include <stdio.h>#include <string.h>#include <conio.h>#include <ctype.h>char xx5080;int maxline=0;/*文章的总行数*/int ReadDat(void);void WriteDat(void);void StrOL(void)int ij9k9s9m9stri;char str80;for(i=0;i<maxline;i+) strl=strlen(xxi);memset(sti;0,80);/*初始化这字符半数组str*/Sk05foitj=strl-l;j>=0;j.)/*从当前字符串尾部开始向前倒序循环,实现题意要求的倒排*/ if(isalpha(xxij) k+;/*如果当前字符是字母 a-z 或 A-Z,则 k 加*/else for(m=l;m<=k;m+)/否则将长度为k的单词顺序存入到字符串数组str »|s值加1*/ strs+=xxij+m;k=0;/*将k值清0,以方便下一个单词的长度计数*/if(!isalpha(xxij) strs+='*;/*如果当前字符不是字母az或A乙则以空格代之存入到字符串数组str中,s值加一for(ni=l;niv=k;in+)。此时的k值为当前字符串中第一个单词的长度,但在上一个fbr循环中没能存入到字符串字组扣中,所以在这里将其存入到sir中*/strs+=xxij+m;strs='0'/*在当前行尾加0以标记此行的结束*/strcpy(xxi,str);/*将倒排好的当前字符串重新存回到当前行xx中*/ void main()(clrscr();if(ReadDat()printf("数据文件IN.DAT不能打开!n007”);return;)StrOL();WriteDat();int ReadDat(void)FILE *fp;int i=0;char *p;if(fp=fopen(,'IN.DAT,;Y,)=NULL) return 1;while(fgets(xxi,80,fp)!=NULL)p=strchr(xxinf);if(p)*p=O;i+;maxline=i;fclose(fp);return 0;void WriteDat(void)(FILE *fp;int i;clrscr();fp=fopen(',OUT6.DATM;(wM);for(i=0;i<maxline;i+)printf( M%sn M, xx i );fprintf(fp,M%sn",xxi);)fclose(fp);题目5(无忧id 8题)在文件in.dat中有200个正整数,且每个数均在1000至9999之间。函数ReadDat()读取这200个数存放到数组 aa中。请编制函数jsSortO,其函数的功能是:要求按每个数的后三位的大小进行升序排列,然后取出满足此条件的前10个数依次存入数组bb中,如果后三位的数值相等,则按原先的数值进行降序排列。最后调用函数 WriteDat。把结果bb输出到文件out.dat中。例:处理前60125099901270258088处理后90126012702580885099部分源程序存在文件progl.c中。请勿改动主函数main。、读数据函数ReadDat。和输出数据函数WriteDat。的内容。#include <stdio.h>#include <string.h>#include <conio.h>int aa200,bb10;void jsSort()(int ij,data;for(i=0;i<199;i+)for(j=i+ly<200;j+)if(aai%1000>aaj%1000llaai%1000=aaj%1000&&aai<aaj)data=aai;aai=aaj;aaj=data;for(i=0;l<10;i+)bbi=aai;void main()(readDat();jsSort();writeDat();readDat()(FILE *in;int i;in=fopen(,in.dat',;,rn);for(i=0; i<200; i+) fscanf(in,n%d,'&aai);fclose(in);writeDat()(FILE *out;int i;clrscr();out=fopen("out.dat","”);for(i=0; i<10; i+)printf(H %dn,bbi);fprintf(out,"dn”,bbi);)fclose(out);)题目6题Fl 6:在文件in.dat中有200个正整数,且每个数均在1000至9999之间。函数ReadDat()读取这200个数存放到数组aa中。请编制函数jsSort(),其函数的功能是:要求按每个数的后三位的大小进行降序排列,然后取出满足此条件的前10个数依次存入数组b中,如果后三位的数值相等,则按原先的数值进行升序排列。最后调用函数WriteDat()把结果bb输出到文件out.dat中。例:处理前90125099601270258088处理后50998088702560129012注意:部分源程序已给出。请勿改动主函数main。、读数据函数ReadDat。和输出数据函数WriteDat()的内容。#include<stdio.h>#incl ude<stri ng.h>#include<conio.h>int aa200|,bb10;void jsSort()int ij,data;for(i=0;i<199;i+)for(j=i+ly<200;j+)if(aai%1000<aaj%1000llaai%1000=aaj%1000&&aai>aaj)data=aai;aai=aaj;aaj=data;for(i=0;i<10;i+)bbi=aai;void main()(readDat();jsSort();writeDat();system("pause");readDat()FILE *in;int i;in=fopen("in.dat","r");for(i=0; i<200; i+) fscanf(in;'%d;&aai);fclose(in);)writeDat()(FILE *out;int i;clrscr();out=fopen(nout.dat,;,wM);for(i=0; i<10; i+)printf("i=%d,%dnn,i+Lbbi);fprintf(out,n%dn'bbi);fclose(out);题目7(无忧id 15题)已知在文件IN.DAT中存有100个产品销售记录,每个产品销售记录由产品代码dm(字符型4位),产品名称me(字符型10位),单价dj(整型),数量si(整型),金额je(长整型)五部分组成。其中:金额=单价*数量计算得出。函数 ReadDat。是读取这100个销售记录并存入结构数组sell中。请编制函数SortDat。,其功能要求:按产品代码从大到小进行排列,若产品代码相同,则按金额从大到小进行排列,最终排列结果仍存入结构数组sell中,最后调用函数WriteDat。把结果输出到文件OUT6.DAT中。部分源程序存在文件progl.c中。请勿改动主函数main。、读数据函数ReadDat。和输出数据函数WriteDat。的内容。#include <stdio.h>#include <mem.h>#include <string.h>#include <conio.h>#include <stdlib.h>#define MAX 100typedef struct char dm5;/*产品代码*/char mcl 1;/*产品名称*/int dj;/*单价*/int si;/*数量*/longje;/*金额*/PRO;PRO sellMAX;void ReadDat();void WriteDat();void SortDatO(int IJ;PRO xy;for(I=0;I<99;I+)for(j=I+l;j<100y+)if(strcmp(sellI.dm,seilj.dm)<Ollstrcmp(selllI.dm,sellj).dm)=O&&sellI.je<seil|j).je)xy=sellI; sell I=sellj; sell|j=xy;void main()(memset(sell,0,sizeof(sell);ReadDat();SortDatO;WriteDat();void ReadDat()(FILE *fp;char str80,chll;int i;fp=fopen(,IN.DATn;,rH);for(i=i);i<100;i4-+)fgets(str,80,fp);memcpy(selli.dm,str,4);memcpy(sell|i|.mc,str+4,10);memcpy(ch,str+14,4);ch4=0;selli.dj=atoi(ch);memcpy(ch,str+18,5);ch|5=0;selli.sl=atoi(ch);selli.je=(long)selli.dj*selli.sl;Ifclose(fp);void WriteDat(void)(FILE *fp;int i;fp=fopen(,OUT6.DAT,;,wH);for(i=0;i<100;i+)printf(n%s %s %4d %5d %5dnM, selli.dm,selli.mc,selli.dj,selli.sl,selli.je);fprintf(fp,H%s %s %4d %5d %5dn' selli.dm,selli.mc,selli.dj,seni.sl,selli.je);)fclose(fp);题目8(无忧id 83字符替换题)函数ReadDat。实现从文件ENGIN中读取一篇英文文章,存入到字符串数组xx中;请编制函数encryptChar。,按给定的替代关系对数组xx中的所有字符进行替代(注意中间变量用无符号整型),仍存入数组xx的对应的位置上,最后调用函数WriteDat。把结果xx输出到文件PS1.DAT中。替代关系:f(p)=p*11 mod 256(p是数组中某一个字符的ASCH值,f(p)是计算后新字符的ASCII值,如果计算后f(p)值小于等于32或大于130,则该字符不变,否则将f(p)所对应的字符进行替代。部分源程序存在文件 progl.c中。原始数据文件存放的格式是:每行的宽度均小于80个字符。请勿改动主函数main。、读数据函数ReadDat。和输出数据函数WriteDat。的内容。#include <stdio.h>#include <string.h>#include <conio.h>#include <ctype.h>unsigned char xx5080;int maxline=0;/*文章的总行数*/int ReadDat(void);void WriteDat(void);void encr)ptChar()intij;for(i=0;i<maxline;i+)for(j=0;j<strlen(xxi);j+)if(xxij*11%256<=32llxxij*U %256>130) continue;else xxij=xxij*ll%256;)void main()(clrscr();if(ReadDat()prinlf("数据文件ENGIN不能打开!n007n);return;)encryptChar();WriteDatO;int ReadDat(void)(FILE *fp;int i=0;unsigned char *p;if(fp=fopen(,'eng.in,r,')=NULL) return 1;while(fgets(xxi,80,fp)!=NULL)p=strchr(xxi,n,);if(p)*p=O;i+;maxline=i;fclose(fp);return 0;void WriteDat(void)(FILE *fp;int i;fp=fopen(,psl.dat,;,w,');for(i=0;i<maxline;i+) printf("%snn,xxi); fprintf(fp;'%snH,xxi);)fclose(fp);解法二:void encryptChar()int ij;unsigned char ch;for(i=0;i<maxline;i+)for(j=0;j<strlen(xxi);j+)ch=xxij*ll%256;if(ch<=32llch>130) continue; else xxlj=ch;题目9(无忧id28; id 124题;捷成id 16题)函数ReadDat()实现从文件1N.DAT中读取一篇英文文章存入到字符串数组xx中,请编制函数SortCharDO,其函数的功能是:以行为单位对字符按从大到小的顺序进行排序,排序后的结果仍按行重新存入字符串数组xx中,最后调用函数writeDat。把结果xx输出到文件OUT2.DAT中。例:原文:dAe,BfC.CCbbAA结果:fedCBA.bbCCAA原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。部分源程序存在文件progl.c中。请勿改动主函数main。、读数据函数ReadDat。和输出数据函数writeDat。的内容。#include <stdio.h>#include <string.h>#include <conio.h>char xx5080;int maxline=0;/*文章的总行数*/int ReadDat(void);void WriteDat(void);void SortCharD(void)int Ij,k,strl;char ch;for(l=0;I<maxline;I+)strl=strlen(xxI);for(j=0;j<strl-l;j+)for(k=j+l;k<strl;k+)if(xxIj<xxIlk)ch=xxILj; xxIj=xxIk; xxIk=ch;void main()(clrscr();if(ReadDat()printf("数据文件IN.DAT不能打开!n007”);return;)SortCharD();WriteDat();int ReadDat(void)FILE *fp;int i=0;char *p;if(fp=fopen(,IN.DAT,Y,)=NULL) return 1;while(fgets(xxi,80,fp)!=NULL)p=strchr(xxi,nr);if(p)*p=O;i+;maxline=i;fclose(fp);return 0;void WriteDat(void)(FILE *fp;int i;fp=fopen(,'OUT2,DAT,;,wM);for(i=0;i<maxline;i+) printf(n%sn",xxi);fprintf(fp,"sn”,xxi);fclose(fp);题目10(无忧id 68题)函数ReadDat。实现从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中,请编制函数ConvertCharA(),其函数的功能是:以行为单位把字符串中的所有小写字母改写成该字母的下一个字母,如果是字母z,则改写成字母a。大写字母仍为大写字母,小写字母仍为小写字母,其他字符不变。把已处理的字符串仍按行重新存入字符串数组xx中,最后调用函数writeDat。把结果xx输出到文件OUT1.DAT中。例:原文:Adb.Bcdzaabck.LLhj结果:Aec.Bdeabbcdl.LLik原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。部分源程序存在文件progLc中。请勿改动主函数main。、读数据函数ReadDal。和输出数据函数writeDat。的内容。#include <stdio.h>#include <string.h>#include <conio.h>char xx|50|80;int maxline=O;/*文章的总行数*/int ReadDat(void);void WriteDat(void);void ConvertCharA(vold)int ij;for(i=0;i<maxline;i+)for(j=0;j<strlen(xx|i);j+)if(xxij='z') xxij='a'else if(xxij>='a'&&xxij<='y') xxij+=l;void main()(clrscr();if(ReadDat()printf("数据文件1N.DAT不能打开!n007");return;)ConvertCharA();WriteDat();int ReadDat(void)(FILE *fp;int i=0;char *p;if(fp=fopen(MIN.DAT'Y,)=NULL) return 1;while(fgets(xxi,80,fp)!=NULL)p=strchr(xxi,n,);if(p)*p=O;i+;)maxline=i;fclose(fp);return 0;)void WriteDat(void)(FILE *fp;int i;clrscr();fp=fopen(MOUT l.DATJw");for(i=0;i<maxline;i+)printf(M%snH,xxi);fprintf(fp,%sn",xxi);)fclose(fp);题目11(无忧id 93题)程序progl.c的功能是:把s字符串中的所有字母改写成该字母的下一个字母,字母z改写成字母a。要求大写字母仍为大写字母,小写字母仍为小写字母,其它字符不做改变。请考生编写函数chg(char *s)实现程序要求,最后调用函数readwriteDAT()把结果输出到文件be 1.out中。例如:s字符串中原有的内容为:Mn.l23Zxy,则调用该函数后,结果为:No.l23Ayz«注意:部分源程序存在文件progl.c文件中。请勿改动主函数main()和输出数据函数readwriteDAT。的内容。#include <conio.h>#include <string.h>#include <stdio.h>#include <ctype.h>#define N81void readwriteDAT();void chg(char *s)int I;for(1=0; I<str)en(s); I+)if(si=,z,llsi='Z,) si-=25;else lf(si>=*a,&&sl<=yilsi>=,A,&&si<=,Y,) si+=l;)main()(char aN;clrscr();printf(nEnter a string :"); gets(a);printf('The original string is :"); puts(a);chg(a);printf('The string after modified :");puts (a);readwriteDAT();void readwriteDAT()(inti;char aN;FILE *rf,*wf;rf = fopen("bcLin",T');wf=fopen(,'bcl.outH, nwH);for(i =0; i <50; i+)fscanf(rf, M%s a);chg(a);fprintf(wf, H%snM, a);)fclose(rf);fclose(wf);题目12(无忧id 78题)已知在文件IN.DAT中存有100个产品销售记录,每个产品销售记录由产品代码dm(字符型4位),产品名称me(字符型10位),单价dj(整型),数量si(整型),金额je(长整型)五部分组成。其中:金额=单价*数量计算得出。函数 ReadDat。是读取这100个销售记录并存入结构数组sell中。请编制函数SortDat。,其功能要求:按产品名称从小到大进行排列,若产品名称相等,则按金额从小到大进行排列,最终排列结果仍存入结构数组 sell中,最后调用函数WriteDal。把结果输出到文件OUT5.DAT中。部分源程序存在文件progl.c中。请勿改动主函数main。、读数据函数ReadDat。和输出数据函数WriteDat。的内容。#include <stdio.h>#include <mem.h>#include <string.h>#include <conio.h>#include <stdlib.h>#define MAX 100typedef structchar dm5;/*产品代码*/char产品名称*/int dj;/*单价*/int si;/*数量*/long je;/*金额*/PRO;PRO sellMAX;void ReadDat();void WriteDat();void SortDat()int ij;PRO xy;for(i=0;i<99;i+)for(j=i+ly<100;j+)if(strcmp(selli.mc9sellj.inc)>0llstrcinp(selli.mc9sellj.inc)=0&&selli.je>sellj.je)xy=selli;selli=sellj;sellj=xy;void main()(memset(sell,0,sizeof(sell);ReadDat();SortDat();WriteDatO;)void ReadDat()(FILE *fp;char str80,chl 1;int i;fp=fopen(,'IN.DAT',;,rH);for(i=0;i<100;i+)fgets(str,80,fp);memcpy(selli.dm,str,4);memcpy(selli.mc,str+4J0);memcpy(ch,str+14,4);ch|4|=0;selli.dj=atoi(ch);memcpy(ch,str+l 8,5);ch5=0;selli.sl=atoi(ch);selli.je=(lo