欢迎来到天天文库
浏览记录
ID:12333972
大小:97.00 KB
页数:16页
时间:2018-07-16
《考试一观 程序设计》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、程序设计==================================================平时练习题实验二/*------------------------------------------------【程序设计】--------------------------------------------------功能:计算并输出n(包括n)以内能被5或9整除的所有自然数的倒数之和。例如:若n=12,输出:0.411111------------------------------------------------*/voidTestFunc();#include2、dio.h>doublefun(intn){/*********Begin**********//**********End**********/}main(){intn;doubles;printf("Inputn:");scanf("%d",&n);s=fun(n);printf("s=%lf",s);TestFunc();}voidTestFunc(){FILE*IN,*OUT;intj;doublei;IN=fopen("in.dat","r");if(IN==NULL){printf("ReadFILEError");}OUT=fopen("out.dat","w");if3、(OUT==NULL){printf("WriteFILEError");}fscanf(IN,"%d",&j);i=fun(j);fprintf(OUT,"%lf",i);fclose(IN);fclose(OUT);}示例代码:inti;doublesum=0.0;for(i=1;i<=n;i++)if(i%5==04、5、i%9==0)sum+=1.0/i;returnsum;实验三/*------------------------------------------------【程序设计】-------------------------------------------------6、-功能:从字符串中删除指定的字符。同一字母的大、小写按不同字符处理。例如:若程序执行时输入字符串为:mmkknnnppp从键盘上输入字符:n,则输出后变为:mmkkppp,如果输入的字符在字符串中不存在,则字符串照原样输出。------------------------------------------------*/(参考例题)#includevoidmain(){inti,j;charstr1[80],c;puts(“请输入一个字符串:”);gets(str1);puts(“请输入一个字符:”);c=getchar();puts(“删除前:”);puts(str17、);i=0;while(str1[i]!=’ ’){if(str1[i]==c){for(j=i;str1[j]!=’ ’;j++)str1[j]=str1[j+1];}if(str1[i]!=c)i++;}puts(“删除后:”)puts(str1);}#includevoidTestFunc();voidfun(chars[],charc){/*********Begin**********//**********End**********/}main(){staticcharstr[]="mmkknnnppp";charch;printf("原始字符串:%s"8、,str);printf("输入一个字符:");scanf("%c",&ch);fun(str,ch);printf("str[]=%s",str);TestFunc();}voidTestFunc(){FILE*IN,*OUT;chari[200];IN=fopen("in.dat","r");if(IN==NULL){printf("ReadFILEError");}OUT=fopen("out.dat","w");if(OUT==NULL){printf("WriteFILEError");}fscanf(IN,"%s",i);fun(i,'n');fprintf(OUT,"%s"9、,i);fclose(IN);fclose(OUT);}示例代码:inti=0,j=0;while(s[i]!=' '){if(s[i]!=c)s[j++]=s[i];i++;}s[j]=' ';综合二/*------------------------------------------------【程序设计】-----------------------------------------------
2、dio.h>doublefun(intn){/*********Begin**********//**********End**********/}main(){intn;doubles;printf("Inputn:");scanf("%d",&n);s=fun(n);printf("s=%lf",s);TestFunc();}voidTestFunc(){FILE*IN,*OUT;intj;doublei;IN=fopen("in.dat","r");if(IN==NULL){printf("ReadFILEError");}OUT=fopen("out.dat","w");if
3、(OUT==NULL){printf("WriteFILEError");}fscanf(IN,"%d",&j);i=fun(j);fprintf(OUT,"%lf",i);fclose(IN);fclose(OUT);}示例代码:inti;doublesum=0.0;for(i=1;i<=n;i++)if(i%5==0
4、
5、i%9==0)sum+=1.0/i;returnsum;实验三/*------------------------------------------------【程序设计】-------------------------------------------------
6、-功能:从字符串中删除指定的字符。同一字母的大、小写按不同字符处理。例如:若程序执行时输入字符串为:mmkknnnppp从键盘上输入字符:n,则输出后变为:mmkkppp,如果输入的字符在字符串中不存在,则字符串照原样输出。------------------------------------------------*/(参考例题)#includevoidmain(){inti,j;charstr1[80],c;puts(“请输入一个字符串:”);gets(str1);puts(“请输入一个字符:”);c=getchar();puts(“删除前:”);puts(str1
7、);i=0;while(str1[i]!=’ ’){if(str1[i]==c){for(j=i;str1[j]!=’ ’;j++)str1[j]=str1[j+1];}if(str1[i]!=c)i++;}puts(“删除后:”)puts(str1);}#includevoidTestFunc();voidfun(chars[],charc){/*********Begin**********//**********End**********/}main(){staticcharstr[]="mmkknnnppp";charch;printf("原始字符串:%s"
8、,str);printf("输入一个字符:");scanf("%c",&ch);fun(str,ch);printf("str[]=%s",str);TestFunc();}voidTestFunc(){FILE*IN,*OUT;chari[200];IN=fopen("in.dat","r");if(IN==NULL){printf("ReadFILEError");}OUT=fopen("out.dat","w");if(OUT==NULL){printf("WriteFILEError");}fscanf(IN,"%s",i);fun(i,'n');fprintf(OUT,"%s"
9、,i);fclose(IN);fclose(OUT);}示例代码:inti=0,j=0;while(s[i]!=' '){if(s[i]!=c)s[j++]=s[i];i++;}s[j]=' ';综合二/*------------------------------------------------【程序设计】-----------------------------------------------
此文档下载收益归作者所有