从键盘上输入10个整数多个非0实数可以定义长度为100的数组按0结束输入,求这些数中大于平均数的数字个数。

c语言,长度为100的一维数组,初始值为100以内随机数,并求出最大值最小值平均数-学路网-学习路上 有我相伴
c语言,长度为100的一维数组,初始值为100以内随机数,并求出最大值最小值平均数
来源:互联网 &责任编辑:鲁倩 &
c语言输入一个长度不超过100的字符串,删除串中的重复字符...#include&&stdio.h&int&main&()&&{&char&s[101];&int&i,j,k;&gets(s);&for(i=0;s[i];i++)&{&for(j=k...C语言怎样创建一个长度为100的一维数组,其中第i个元素a[i]=1...Pressanykeytocontinue#include&stdio.h&intfun(intn){inti,sum=0;for(i=1;i&=n;i++){sum+=i;}}main(){inti,arry[100];for(i=0;i&100;i...c语言编程,生成长度为100的斐波那契数列,并输出第73项的值理论上是这样的,不过我的电脑运行太久了,可能程序设计不够好吧#include&stdio.h&intmain(){intF(inta);for(i=0;i&=100;i++){printf(&F%d=%d\n&,i,F(i));}prin...c语言输入一个长度不超过100的字符串,删除串中的重复字符...for(i=0;i&k;i++){for(j=1;j&k-i;j++){if(a[i]==a[j]&&j!=i)//两者不是同一个字符{for(t=i;t&k;t++){a[t]=a[t+1];}j--;k--;i--;//确保把所有重复的删完,所以退回重...C语言程序题从键盘输入不多于100个字符长度的字符串,将其中...&stdio.h&voidmain(){charstr[101];printf(&请输入不多于100个字符...str[i]-=32;//转为大写字符}printf(&转换后的字符串为:\n&quot...c语言,长度为100的一维数组,初始值为100以内随机数,并求出最大值最小值平均数(图9)c语言,长度为100的一维数组,初始值为100以内随机数,并求出最大值最小值平均数(图18)c语言,长度为100的一维数组,初始值为100以内随机数,并求出最大值最小值平均数(图20)c语言,长度为100的一维数组,初始值为100以内随机数,并求出最大值最小值平均数(图24)c语言,长度为100的一维数组,初始值为100以内随机数,并求出最大值最小值平均数(图35)这是用户提出的一个学习问题,具体问题为:c语言,长度为100的一维数组,初始值为100以内随机数,并求出最大值最小值平均数我们通过互联网以及本网用户共同努力为此问题提供了相关答案,以便碰到此类问题的同学参考学习,请注意,我们不能保证答案的准确性,仅供参考,具体如下:C语言程序题从键盘输入不多于100个字符长度的字符串,将其中...&stdio.h&voidmain(){charstr[101];printf(&请输入不多于100个防抓取,学路网提供内容。用户都认为优质的答案:用C语言编程计算两个超长正整数(长度小于100)的加法这是大数加法问题,我给你一个参考代码输入至多100行的文本,每行是一个至多100位...k=j,c=0;1;k--){//updatesconst防抓取,学路网提供内容。//借楼上的修改了下C语言字符串长度(指针专题)(指针刚入门,帮我写写,不要写太难...&&&}&&&&return&i;&//返防抓取,学路网提供内容。#include&&time.h&&C语言中怎么能够输入输出长度大于长整形的长度的数字,10的...次方=0次方=...个位乘个位,个防抓取,学路网提供内容。#include&&stdio.h&&c语言写先用1-100的自然数填充一个长度为100的整型数组,然后...我觉得楼上说的有些问题(是能被3和5同时整除,不是3或5整除)所以,我修改了一下:&防抓取,学路网提供内容。#include&&stdlib.h&&c语言输入一个以回车为结束符的字符串(长度小于100),统计大...;if(lowerChar.indexOf(c)!=-1){//如果字符c在小写字母字符串中的位置不为-1,也就是说防抓取,学路网提供内容。int&main()&C语言中一个语句太长用什么换行?问:C语言中一个语句太长用什么换行?比如我有一个printf语句很长很长,问了美...答:区分不同情况,有多种方式可以采用。不管是哪种情况,都可以用\结尾作为换行标记,防抓取,学路网提供内容。{int&i&=&0,&j,k,t,a[100];&C语言基本整型与长整型区别答:C的基本整形和长整型一样是随着计算机技术的发展而来的。早期计算机是16位的,整数就是16位的,intlong是32位的,只是它们范围不一样。随着计算机的发展和C语言标准的防抓取,学路网提供内容。srand((unsigned&int)time(NULL));在C语言中,合法的长整型常数是问:A)OLB)4562&D)216Dwhy?答:选AA选项应该是0L不是OL不然就没有答案了以L或l结尾的都是长整型。如l。防抓取,学路网提供内容。for(i=0;i&=99;i++)&&&&&&&&&&&&&C语言怎么定义长整形问:我用longint怎么不行呢答:基本定义形式为:longvar_name=INIT_VALUE;一、长整型在C语言中类型关键字为long。定义时,var_name为变量名。IN防抓取,学路网提供内容。{a[i]=rand()&%&100;&&&//产生随机数组1到100关于C语言长整型的输出问题!答:已然给你改好叻~代码如下:#include__int64a=0,b=0,i=0,n;intmain(){while(scanf("%lld",&n)!=EOF){whi防抓取,学路网提供内容。printf(&%-3d&&&,a[i]);C语言中,合法的长整型数?问:C语言中,合法的长整型数?答案是AA.0LB..D.2.1...答:其实,在现在的计算机中通常使用的都是32位编译器(除非你还使用T防抓取,学路网提供内容。if((i+1)%10==0)&printf(&\n&);c语言中怎样正确输出双长整形变量问:#include&stdio.h&voidmain(){i=...答:#includevoi防抓取,学路网提供内容。a[i]++;在c语言中如何输入一个特别长的数答:在目前的32位编译器中(含多数的64位编译器)int与long的长度都是相等的,都是32位的它的unsigned范围为0到4,294,967,295当然,一般的运算防抓取,学路网提供内容。}&在C语言如果一行输入得过长如何续行?问:把一行变成两行中间应用什么符号连接?答:区分程序代码的类型,有如下三种写法:1对于所有的C语言语句,均可以在需要换行的地方加上\标记,即可实现换行效果。不过除宏防抓取,学路网提供内容。for(i=0;i&99-1;i++)&&&&&&&&&&&&&&&&//升序排序C语言中如何输出长整型数值问:main(){inta=111,b=111111,sum=a+b;printf("%d",sum);getch()...答:在C语言中可以通过输出函数printf中的输出格式说明符来输出长整型数值。具体格式可以参考如下程序段:longinta=;printf("%ld",a);//%ld表示按长整型的数据格式输出变量a,输出结果为123456防抓取,学路网提供内容。{&防抓取,学路网提供内容。k=i;&我们无比兴奋地想告诉大家,发布的.NETCore1.0,ASP.NETCore1.0和EntityFrameworkCore1.0在Windows、OSX和Linux系统上可用了。.NETCore是一防抓取,学路网提供内容。for(j=i+1;j&99;j++)此石叫硫铁矿,化学名称为硫化亚铁。亦称愚人金,比重约为5,硬度在6~6.5.是半导体矿物,在热作用下,其捕获的电子易于流动,且具方向性,形成电子流,因产生的热电动势而具热电性。矿石也可作为摆件赏石。最防抓取,学路网提供内容。if(a[j]&a[k])搞机老砖家告诉你:1、华为荣耀畅玩6X所有版本两个卡槽都支持中国移动、中国联通、中国电信4G卡,双卡盲插,双卡双待。你可以:移动4G+移动或者联通或者电信2G卡;也可以联通4G+移动或者联通或者电信2防抓取,学路网提供内容。k=j;幼儿园孩子在入园有异常状况时,当即时报警,免得证据不足,事实无从稽查从照片看,半年后指甲这样,孩子的发育可能是有点问题的。父母应及时去医院问诊,而不要自己在家里找原因。我家孩子也在幼儿园,一两周剪一次防抓取,学路网提供内容。t=a[k];元彪近况元彪近况怎样他又被冷藏了吗?[提要] 90年代初,成龙越来越红,元彪却是每况愈下,甚至到了嘉禾不让他拍片的程度。  元彪近况,元彪近况怎样他又被冷藏了吗?元彪21岁时,得师兄洪金宝力捧,在《杂防抓取,学路网提供内容。a[k]=a[i];身处职场,不可避免需要使用电脑处理一些文字、图片、表格之类的文件,不仅仅局限于会打字、打字快,也不仅仅是使用Word、Excel、PPT,往往需要综合使用多种软件,得到文字资料、图片资料、表格资料等等防抓取,学路网提供内容。a[i]=t;&问的就不对,中华田园犬这么多品种,洋狗无法代替的,因为大家不如国外注重狗的品种保护,以至于田园犬品种纯血统稀有,物以稀为贵,价格太高不是大多人买的起。所以洋狗便宜相对就普及了。别拿田园犬和国外品种狗相防抓取,学路网提供内容。}printf(&\nmin=%d,max=%d\n&,a[k],a[0]);这是一个尖锐的问题。在索尼IFA展前发布开始之前,知名爆料大神@Evleaks在Twitter上转推了一条推文「索尼应该在IFA上忽视手机,专讲PlayStation。没有人关心他们的手机」,并称,「防抓取,学路网提供内容。for(i=0,k=0;i&=99;i++)&&&&&&&&&&&&//求平均数  麻辣豆腐与麻婆豆腐只相差一个字,很多人弄不清楚区别在哪,下面就从特别和做法来看看麻辣豆腐和麻婆豆腐的区别在哪?  一、麻辣豆腐是东北菜,色深、味厚、麻辣烫鲜。麻婆豆腐是四川着名的传统美食特色菜,色泽金红,麻辣味浓,鲜香可口。  二、做法不同  麻辣豆腐  原料:豆腐一斤猪肉二两菜油一两五钱郫县豆办一两盐三分花椒面二分蒜苗一两五钱豆豉十余粒酱油五钱麻辣面四钱水豆粉一两五钱汤八两  做法:1、将豆防抓取,学路网提供内容。k=k+a[i];没有一个国产手机厂商敢说自己是最牛的国产手机吧,就算是全球第三、国内第一的华为也不敢这么说。毕竟对于手机这样的产品来说,每个消费者对手机的要求是不一样的,而且手机发展日新月异,也许今天是这家做的好,明防抓取,学路网提供内容。k=k/100;都知道,如今的微信成为许多人的社交工具,不止是亲人、朋友的聊天,甚至还运用到生活工作中。而对于微信名称或者微信签名,相信许多人都是精心思考的,一定要取个帅帅的、爱爱的微信名,留下一条有文艺的、有感情的防抓取,学路网提供内容。printf(&average=%d\n&,k);我们都知道,橱柜的高度很重要,高度不合适就会特别累腰累胳膊,因此橱柜的高度要根据使用者的身高来决定。那么到底如何根据使用者身高来算橱柜高度呢?别急,今天给大家介绍一种算法。(北京美宅客装饰)橱柜高度的防抓取,学路网提供内容。return&0;串串狗是所有杂交品种狗或品种特性不明显的狗的总称,不是某个具体的犬种,因为杂交优势,通常智商较高,抗病能力强,身体健康,有自己独特的个性。要怎样正确饲养串串狗呢?1、串串狗的饲养方法小狗狗最好给幼犬狗防抓取,学路网提供内容。&如果]有人类,宇宙照样安然无恙,这么大的无际宇宙。人类算什么,人类生存在在宇宙中,好比生存在地球上的细菌一样,倒反如果没有人类和动物出现,没有勾心斗角,没有战争,没有压迫,没有残忍,宇宙更会安静,羡慕防抓取,学路网提供内容。}用C语言编程计算两个超长正整数(长度小于100)的加法这是大数加法问题,我给你一个参考代码输入至多100行的文本,每行是一个至多100位...k=j,c=0;1;k--){//updatesconstintsum=(!c?r[i]:0)+s[k]+c;//localsumif((c=...C语言字符串长度(指针专题)(指针刚入门,帮我写写,不要写太难...&&&}&&&&return&i;&//返回长度}&void&main(){&&&&char&str[100];&&&nbsp...C语言中怎么能够输入输出长度大于长整形的长度的数字,10的...次方=0次方=...个位乘个位,个位乘十位,个位乘百位……C语言写法很多,但是基本思想都是这个,百度...c语言写先用1-100的自然数填充一个长度为100的整型数组,然后...我觉得楼上说的有些问题(是能被3和5同时整除,不是3或5整除)所以,我修改了一下:&
相关信息:
- Copyright & 2017 www.xue63.com All Rights Reserved以下试题来自:
填空题在主函数中从键盘输入若干个数放入数组中,用0结束输入并放在最后一个元素中。下列给定程序中,函数fun()的功能是计算数组元素中值为负数的平均值(不包括0)。
例如:数组中元素的值依次为43,47,-21,53,-8,12, 0,则程序的运行结果为-25.333333。
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include <conio.h>
#include <stdio.h>
double fun(int x[])
double sum=0.0;
int c=0,i=0;
/*************found*************/
while(x[i]==0)
if(x[i]<0)
sum=sum+x[i];
/************found***********/
sum=sum\c;
return sum;
int x[1000];
clrscr();
printf("\nPlease enter some data(end with
0) :”);
scanf("%d",&x[i]);
while(x[i++]!=0);
printf("%f\n",fun(x)); (1) 错误:while(x[i]==0)
正确:while(x[i]!=0)(2) 错误:sum=sum\c;
正确:sum=sum/c;
为您推荐的考试题库
你可能感兴趣的试题
热门相关试卷
最新相关试卷没有更多推荐了,
不良信息举报
举报内容:
C语言:从键盘输入10个数存入一维数组,求这10个数中的最大值和最小值并输出
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!【计算】 26.下列给定程序中, 函数 fun 的功能是: 找出 100~ 999 之间(含 100 和 999)所有整数中各位上数字之和 为 x(x 为正整数)的整数,并输出;符合条件的整数 个数作为函数值返回。 例如,当 x 值为 5 时,100~999 之间各位上数字之 和为 5 的整数有:104、113、122、131、140、203、 212、221、230、302、311、320、401、410、500, 共有 15 个。当 x 值为 27 时,各位数字之和为 27 的整数是:999,只有 1 个。 #include &stdio.h& int fun(int x) { int n, s1, s2, s3, n=0; t=100; /**********found**********/ while(t&=999){ /**********found**********/ s1=t%10; s2=(t/10)%10; s3=t/100; /**********found**********/ if(s1+s2+s3==x) { printf(&%d &,t); n++; } t++; } } main() { int x=-1; while(x&0) { printf(&Please input(x&0): &); scanf(&%d&,&x); } printf(&\nThe result is: %d\n&,fun(x)); } 31.下列给定程序中,函数 fun 的功能是:计算下式 前 n 项的和,并作为函数值返回。例如,若主函数 从键盘给 n 输入 50 后,则输出为 S=1.960784。? #include &stdio.h& double fun(int n) { int i,j; double sum=0.0,t; for(i=1;i&=n;i++) { t=0.0; for(j=1;j&=i;j++) t+=j; sum+=(double)1/t; } } void main() { printf(&\nInput n: &); scanf(&%d&,&n); s=fun(n); printf(&\n\ns=%f\n\n&,s); }c2=%d 69.给定程序的功能是:计算并输出下列级数的前 N 项之和 SN,直到 SN 大于 q 为止,q 的值通过形参 传入。 例如, q 的值为 50.0, 若 则函数值为 50.416687。 #include &stdio.h& double fun(double q) { double s,t; n=2; s=2.0; while(s&=q) { 63.请补充函数 fun,该函数的功能是:整除 x 且是 s=s+(double)(n+1)/n; 偶数的数,把这些数保存在数组 bb 中,并按从大 n++; 到小的顺序输出。例如当 x=20 时,依此输出: } 201042。 printf(&n=%d\n &,n); #include &stdlib.h& return (s); #include &conio.h& } #include &stdio.h& void main() void fun(int k,int bb[]) { { printf(&%f\n &,fun(50)); } int j=0; for(i=1;i&=k;i++) 70.请补充 main 函数,该函数的功能是:求 1+2! { +3!+…+N!的和。 if(k%i==0&&i%2==0) 例如,1+2!+3!+…+5!+6!的和为 873。 bb[j++]=i; #include &stdlib.h& } #include &stdio.h& printf(&\n\n&); void main() for(i=--j;i&=0;i--) { printf(&%d &,bb[i]); int i, } long s=0, t=1; system(&CLS&); void main() printf(&\nInput n:\n&); { scanf(&%d&,&n); int k=1; for(i=1;i&=n;i++) int bb[100]; { system(&CLS&); t=t*i; printf(&\nPlease input X=&); s=s+t; scanf(&%d&,&k); } fun(k,bb); printf(&1!+2!+3!...+%d!=%ld\n&,n,s); } } 71.请在函数 fun 的横线上填写若干表达式,使从键 盘上输入一个整数 n, 输出 n 对应的斐波那契数列。 main() 67.请补充 main 函数,该函数的功能是:求 n! 。例 斐波那契数列是一整数数列, 该数列自第三项开始, { 如,7!=5040。 每 数 等 于 前 面 两 数 之 和 , 即 int i, n, below[9]; #include &stdlib.h& 0,1,1,2,3,5,8,13,21,34,55,… int score[9]={10,20,30,40,50,60,70,80,90}; #include &stdio.h& #include &stdio.h& n=fun(score, 9, below); #include &conio.h& int fun(int n); printf(&\nBelow the average score are: &); void main() void main() for(i=0;i&n;i++) { { printf(&%d &,below[i]); int i,n; int i,n=0; 46.下列给定程序中,函数 fun 的功能是:将自然数 } long f=1; scanf(&%d&,&n); 1~10 及其平方根写到名为 myfile3.txt 的文本文件 57.请补充 main 函数,该函数的功能是:计算两个 62.函数 fun 的功能是:统计长整数 n 的各位上出现 system(&CLS&); for(i=0;i&n;i++) 中,然后再顺序读出显示在屏幕上。 自然数 n 和 m(m&10000)之间所有数的和(n 和 m 从 数字 1、2、3 的次数,并用外部(全局)变量 c1、c2、 printf(&Input n: &); printf(&%d &,fun(i)); #include &math.h& 键盘输入)。 c3 返回主函数。 scanf(&%d&,&n); } #include &stdio.h& 例如, n=1, 当 m=100 时, sum=5050; n=100, 例如, n= 时, 当 当 结果应该为: c1=3 c2 for(i=1;i&=n;i++) int fun(int n) m=1000 时,sum=495550。 =1 c3=2。 f*=i; { int fun(char *fname ) #include &stdlib.h& #include &stdio.h& printf(&%d!=%ld\n&,n,f); if(n==0) { FILE * int i,n; int c1,c2,c3; } return 0; if((fp=fopen(fname, &w&))==NULL) return #include &stdio.h& #include &conio.h& void fun(long n) else 0; void main() { 68. 程序的功能是计算 s=k0+k1+'''+kn 。 if(n==1) for(i=1;i&=10;i++) { c1=c2=c3=0; #include &stdio.h& return 1; /**********found**********/ int n,m; while(n) long fun(int n) else fprintf(fp,&%d %f\n&,i,sqrt((double)i)); { { return printf(&\nSucceed!!\n&); sum=0; switch(n%10) fun(n-1)+fun(n-2); /**********found**********/ system(&CLS&); { } fclose(fp); printf(&\nInput n,m\n&); case 1: s=1; 73.请补充函数 fun,该函数的功能是:判断一个数printf(&\nThe data in file :\n&); /**********found**********/ if((fp=fopen(fname,&r&))==NULL) return 0; fscanf(fp,&%d%f&,&n,&x); while(!feof(fp)) { printf(&%d %f\n&,n,x); fscanf(fp,&%d%f&,&n,&x); } fclose(fp); return 1; } main() { char fname[]=&myfile3.txt&; fun(fname); } 44.下列给定程序中,函数 fun 的功能是:找出 100 到 x(x≤999)之间各位上的数字之和为 15 的所有整 数,并在屏幕输出;将符合条件的整数的个数作为 函数值返回。 例如, n 值为 500 时, 当 各位数字之和为 15 的整数 有:159、168、177、186、195、249、258、267、 285、294、339、348、357、366、375、384、393、 429、438、447、456、465、474、483、492。共有 26 个。 #include &stdio.h& int fun(int x) { int n, s1, s2, s3, /**********found**********/ n=0; t=100; /**********found**********/ while(t&=x) { s1=t%10; s2=(t/10)%10; s3=t/100; if(s1+s2+s3==15) { printf(&%d &,t); n++; } /**********found**********/ t++; } } main() { int x=-1; while(x&999||x&0) { printf(&Please input(0&x&=999): &); scanf(&%d&,&x); } printf(&\nThe result is: %d\n&,fun(x)); } 56.给定程序的功能是:求二分之一的圆面积,函数 通过形参得到圆的半径,函数返回二分之一的圆面 积(注:圆面积公式为:S=π r2,在程序中定义的 变量名要与公式的变量相同)。 例 如 , 输 入 圆 的 半 径 值 : 19.527, 输 出 为 : s= 598.949991 #include &stdio.h& double fun( double r) { return 3.14159*r*r/2.0; } void main() { printf(&Enter x: &); scanf(&%lf&,&x); printf(&s=%lf\n&,fun(x)); }scanf(&%d,%d&,&n,&m); while(n&=m) { sum+=n; n++; } printf(&sum=%d\n&,sum); } 60.请补充函数 fun,该函数的功能是:判断某一个 年份是否为闰年。例如,1900 年不是闰年,2004 是闰年。 #include &stdlib.h& #include &stdio.h& #include &conio.h& int fun(int n) { int flag=0; if(n%4==0) { if (n%100) flag=1; } if (n%400==0) flag=1; } void main() { system(&CLS&); printf(&Input the year:&); scanf(&%d&,&year); if (fun(year)) printf(&%d is a leap year. \n&,year); else printf(&%d is not a leap year.\n&, year); } 61.给定程序的功能是: 计算 score 中 m 个人的平均 成绩 aver, 将低于 aver 的成绩放在 below 中, 并返 回人数。 例如, score={10,20,30,40,50,60,70,80,90}, 当 m=9 时, 函数返回的人数应该是 4, below={10,20,30,40} #include &stdio.h& #include &string.h& int fun(int score[],int m, int below[]) { int i,j=0; float aver =0.0; for(i=0;i&m;i++) aver+=score[i]; aver/=(float)m; for(i=0;i&m;i++) if(score[i]&aver) below[j++]=*(score+i); } } } main() {c1++; case 2: c2++; case 3: c3++; } n/=10;for(i=1;i&=n;i++) s=i*s; } void main() { int k,n; scanf(&%d&,&n); s=0; for(k=0;k&=n;k++) s=s+fun(k); printf(&%ld\n&,s); }long n=L; fun(n); printf(&\nThe result: \n&); printf(&n=%ld c1=%d c3=%d\n&,n,c1,c2,c3); }的个位数字和百位数字之和是否等于其十位上的数 字,是则返回&yes!&,否则返回&no!&。 #include &stdio.h& #include &conio.h& #include &stdlib.h& char *fun(int n) { int g,s,b; g=n%10; s=n/10%10; b=n/100%10; if((g+b)==s) return &yes!&; else return &no!&; } void main() { int num=0; system(&CLS&); printf(&******Input data *******\n&); scanf(&%d&,&num); printf(&\n\n\n&); printf(&****** The result *******\n&); printf(&\n\n\n%s&,fun(num)); } 91.请补充 main 函数,该函数的功能是:计算每个 学生成绩的平均分,并把结果保存在数组 bb 中。 例如,当 score[N][M]={{83.5,82,86,65,67},{80, 91.5,84,99,95},{90.5,95,86,95,97}}时,三个学生的 平均分为 76.7,89.9,92.7。 #include &stdio.h& #define N 3 #define M 5 void main() { int i,j; static float score[N][M]={{83.5,82,86,65,67},{80,91.5,84,99,95},{ 90.5,95,86,95,97}}; float bb[N]; system(&CLS&); for(i=0;i&N;i++) bb[i]=0; for(i=0;i&N;i++) { for(j=0;j&M;j++) bb[i]+=score[i][j]; bb[i]/=M; } for(i=0;i&N;i++) printf(&\nstudent%d\taverage=%5.1f&,i+1 ,bb[i]); } 95.请补充函数 fun,该函数的功能是:求一维数组 x[N]的平均值,并对所得结果进行四舍五入(保留两 位小数)。 例 如 , 当 x[10] = {15.6,19.9,16.7,15.2,18.3,12.1,15.5,11.0,10.0,16.0}, 结 果为:avg=15.030000。 #include &stdlib.h& #include &stdio.h& #include &conio.h& double fun(double x[10]) { double avg=0.0; double sum=0.0; for(i=0;i&10;i++) sum+=x[i]; avg=sum/10; avg=avg*1000; t=(avg+5)/10; avg=(double)t/100; } void main() { double avg,x[10]={15.6,19.9,16.7,15.2, 18.3,12.1,15.5,11.0,10.0,16.0};
system(&CLS&); printf(&\nThe original data is :\n&); for(i=0;i&10;i++) printf(&%6.1f&,x[i]); printf(&\n\n&); avg=fun(x); printf(&average=%f\n\n&,avg); } 100.请补充 main 函数,该函数的功能是求方程 ax2 +bx+c=0 的两个实数根。方程的系数 a、b、c 从 键盘输入,如果判别式(disc=b2-4ac)小于 0,则 要求重新输入 a、b、c 的值。 例如,当 a=1,b=2,c=1 时,方程的两个根分别 是 x1=-1.00,x2=-1.00。 #include &math.h& #include &stdio.h& #include &stdlib.h& void main() { float a,b,c, disc,x1,x2; system(&CLS&); do { printf(&Input a,b,c:&); scanf(&%f,%f,%f&,&a,&b,&c); disc=b*b-4*a*c; if(disc&0) printf(&disc=%f\n Input again!\n&,disc); }while(disc&0); printf(&*******the result*******\n&); x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); printf(&\nx1=%6.2f\nx2=%6.2f\n&,x1,x2); }【结构体、学生编号】19.程序通过定义学生结构体变量,存储学生的学 号、姓名和 3 门课的成绩。函数 fun 的功能是:将 形参 a 所指结构体变量中的数据赋给函数中的结构 体变量 b,并修改 b 中的学号和姓名,最后输出修 改后的数据。 例如,a 所指变量中的学号、姓名和三门课的成绩 依次是:10001、&ZhangSan&、95、80、88,则修 改后输出 b 中的数据应为:10002、&LiSi&、95、80、 88。 #include &stdio.h& #include &string.h& struct student { char name[10]; float score[3]; }; void fun(struct student a) { /**********found**********/ b = b.sno = 10002; /**********found**********/ strcpy(b.name, &LiSi&); printf(&\nThe data after modified :\n&); printf(&\nNo: %ld Name: %s\nScores: &,b.sno, b.name); /**********found**********/ for (i=0; i&3; i++) printf(&%6.2f &, b.score[i]); printf(&\n&); } main() { struct student s={10001,&ZhangSan&, 95, 80, 88}; printf(&\n\nThe original data :\n&); printf(&\nNo: %ld Name: %s\nScores: &,s.sno, s.name); for (i=0; i&3; i++) printf(&%6.2f &, s.score[i]);printf(&\n&); fun(s); } 24.程序通过定义学生结构体变量,存储学生的学 号、姓名和 3 门课的成绩。函数 fun 的功能是:将 形参 a 所指结构体变量 s 中的数据进行修改,并把 a 的地址作为函数值返回主函数,从主函数中输出 修改的数据。 例如,a 所指变量 s 中的学号、姓名和三门课的成 绩依次是:10001、&ZhangSan&、95、80、88,修 改后输出 t 中的数据应为:10002、&LiSi&、96、81、 89。 #include &stdio.h& #include &string.h& struct student { char name[10]; float score[3]; }; /**********found**********/ struct student * fun(struct student *a) { a-&sno = 10002; strcpy(a-&name, &LiSi&); /**********found**********/ for (i=0; i&3; i++) a-&score[i] += 1; /**********found**********/ } main() { struct student s={10001,&ZhangSan&, 95, 80, 88}, *t; printf(&\n\nThe original data :\n&); printf(&\nNo: %ld Name: %s\nScores: &,s.sno, s.name); for (i=0; i&3; i++) printf(&%6.2f &, s.score[i]); printf(&\n&); t = fun(&s); printf(&\nThe data after modified :\n&); printf(&\nNo: %ld Name: %s\nScores: &,t-&sno, t-&name); for (i=0; i&3; i++) printf(&%6.2f &, t-&score[i]); printf(&\n&); } 27.程序通过定义学生结构体变量,存储学生的学 号、姓名和 3 门课的成绩。函数 fun 的功能是:对 形参 b 所指结构体变量中的数据进行修改,并在主 函数中输出修改后的数据。 例如,若 b 所指变量 t 中的学号、姓名和三门课的 成绩依次是:10002、&ZhangQi&、93、85、87,修 改后输出 t 中的数据应为:10004、&LiJie&、93、 85、87。 #include &stdio.h& #include &string.h& struct student { char name[10]; float score[3]; }; void fun( struct student *b) { /**********found**********/ b-&sno = 10004; /**********found**********/ strcpy(b-&name, &LiJie&); } main() { struct student t={10002,&ZhangQi&, 93, 85, 87}; printf(&\n\nThe original data :\n&); printf(&\nNo: %ld Name: %s\nScores: &,t.sno, t.name); for (i=0; i&3; i++) printf(&%6.2f &, t.score[i]); printf(&\n&); /**********found**********/ fun(&t); printf(&\nThe data after modified :\n&); printf(&\nNo: %ld Name: %s\nScores: &,t.sno, t.name); for (i=0; i&3; i++) printf(&%6.2f &, t.score[i]); printf(&\n&); }30.程序通过定义学生结构体变量,存储学生的学 号、姓名和 3 门课的成绩。所有学生数据均以二进 制方式输出到 student.dat 文件中。函数 fun 的功能 是:从文件中找出指定学号的学生数据,读入此学 生数据,对该学生的分数进行修改,使每门课的分 数加 3 分,修改后重写文件中学生的数据,即用该 学生的新数据覆盖原数据, 其它学生数据指定不变; 若找不到,则不做任何操作。 #include &stdio.h& #define N 5 typedef struct student { char name[10]; float score[3]; } STU; void fun(char *filename, long sno) { FILE * STU fp = fopen(filename,&rb+&); /**********found**********/ while (!feof(fp)) { fread(&n, sizeof(STU), 1, fp); /**********found**********/ if (n.sno==sno) } if (!feof(fp)) { for (i=0; i&3; i++) n.score[i] += 3; /**********found**********/ fseek(fp, -(long)sizeof(STU), SEEK_CUR); fwrite(&n, sizeof(STU), 1, fp); } fclose(fp); } main() { STU t[N]={ {10001,&MaChao&, 91, 92, 77}, {10002,&CaoKai&, 75, 60, 88}, {10003,&LiSi&, 85, 70, 78}, {10004,&FangFang&, 90, 82, 87}, {10005,&ZhangSan&, 95, 80, 88}}, ss[N]; int i,j; FILE * fp = fopen(&student.dat&, &wb&); fwrite(t, sizeof(STU), N, fp); fclose(fp); printf(&\nThe original data :\n&); fp = fopen(&student.dat&, &rb&); fread(ss, sizeof(STU), N, fp); fclose(fp); for (j=0; j&N; j++) { printf(&\nNo: %ld Name: %-8s Scores: &,ss[j].sno, ss[j].name); for (i=0; i&3; i++) printf(&%6.2f &, ss[j].score[i]); printf(&\n&); } fun(&student.dat&, 10003); fp = fopen(&student.dat&, &rb&); fread(ss, sizeof(STU), N, fp); fclose(fp); printf(&\nThe data after modifing :\n&); for (j=0; j&N; j++) { printf(&\nNo: %ld Name: %-8s Scores: &,ss[j].sno, ss[j].name); for (i=0; i&3; i++) printf(&%6.2f &, ss[j].score[i]); printf(&\n&); } } 35.程序通过定义学生结构体变量,存储学生的学 号、姓名和 3 门课的成绩。函数 fun 的功能是:将 形参 a 中的数据进行修改,把修改后的数据作为函 数值返回主函数进行输出。 例如,若传给形参 a 的数据中学号、姓名和三门课 的成绩依次是:10001、&ZhangSan&、95、80、88, 修改后的数据应为:10002、&LISi&、96、81、89。 #include &stdio.h& #include &string.h& struct student { char name[10]; float score[3]; }; /**********found**********/struct student fun(struct student a) { a.sno = 10002; /**********found**********/ strcpy(a.name, &LiSi&); /**********found**********/ for (i=0; i&3; i++) a.score[i]+= 1; } main() { struct student s={10001,&ZhangSan&, 95, 80, 88}, printf(&\n\nThe original data :\n&); printf(&\nNo: %ld Name: %s\nScores: &,s.sno, s.name); for (i=0; i&3; i++) printf(&%6.2f &, s.score[i]); printf(&\n&); t = fun(s); printf(&\nThe data after modified :\n&); printf(&\nNo: %ld Name: %s\nScores: &,t.sno, t.name); for (i=0; i&3; i++) printf(&%6.2f &, t.score[i]); printf(&\n&); } 54.下列给定程序的功能是调用 fun 函数建立班级通 讯录。通讯录中记录每位学生的编号、姓名和电话 号码。班级人数和学生信息从键盘读入,每个人的 信息作为一个数据块写到名为 myfile5.dat 的二进制 文件中。 #include &stdio.h& #include &stdlib.h& #define N 5 typedef struct { char name[10]; char tel[10]; }STYPE; void check(); /**********found**********/ int fun(STYPE *std) { /**********found**********/ FILE * if((fp=fopen(&myfile5.dat&,&wb&))==NULL) return(0); printf(&\nOutput data to file !\n&); for(i=0; i&N; i++) /**********found**********/ fwrite(&std[i], sizeof(STYPE), 1, fp); fclose(fp); return (1); } main() { STYPE s[10]={ {1,&aaaaa&,&111111&},{1,&bbbbb&,&222222 &},{1,&ccccc&,&333333&}, {1,&ddddd&,&444444&},{1,&eeeee&,&555555&}}; k=fun(s); if (k==1) { printf(&Succeed!&); check(); } else printf(&Fail!&); } void check() { FILE * STYPE s[10]; if((fp=fopen(&myfile5.dat&,&rb&))==NULL) { printf(&Fail !!\n&); exit(0); } printf(&\nRead file and output to screen :\n&); printf(&\n num name tel\n&); for(i=0; i&N; i++) { fread(&s[i],sizeof(STYPE),1, fp); printf(&%6d s[i].tel); } fclose(fp); %s} 55.程序通过定义学生结构体数组,存储若干名学生 的学号、姓名和 3 门课的成绩。函数 fun 的功能是: 将存放学生数据的结构体数组, 按姓名的字典序(从 小到大)排序。 #include &stdio.h& #include &string.h& struct student { char name[10]; float score[3]; }; void fun(struct student a[], int n) { /**********found**********/ int i, /**********found**********/ for (i=0; i&n-1; i++) for (j=i+1; j&n; j++) /**********found**********/ if (strcmp(a[i].name,a[j].name) & 0) { t = a[i]; a[i] = a[j]; a[j] = } } main() { struct student s[4]={{10001,&ZhangSan&, 95, 80, 88},{10002,&LiSi&, 85, 70, 78}, {10003,&CaoKai&, 75, 60, 88}, {10004,&FangFang&, 90, 82, 87}}; int i, printf(&\n\nThe original data :\n\n&); for (j=0; j&4; j++) { printf(&\nNo: %ld Name: %-8s Scores: &,s[j].sno, s[j].name); for (i=0; i&3; i++) printf(&%6.2f &, s[j].score[i]); printf(&\n&); } fun(s, 4); printf(&\n\nThe data after sorting :\n\n&); for (j=0; j&4; j++) { printf(&\nNo: %ld Name: %-8s Scores: &,s[j].sno, s[j].name); for (i=0; i&3; i++) printf(&%6.2f &, s[j].score[i]); printf(&\n&); } } 83.人员的记录由编号和出生年、月、日组成,N 名 人员的数据已在主函数中存入结构体数组 std 中, 且编号唯一。函数 fun 的功能是:找出指定编号人 员的数据,作为函数值返回,由主函数输出,若指 定编号不存在,返回数据中的编号为空串。 #include &stdio.h& #include &string.h& #define N 8 typedef struct { char num[10]; int year,month, }STU; STU fun(STU *std,char *num) { STU a={&&,}; for(i=0;i&=N;i++) if(strcmp(std[i].num,num)==0) return (std[i]); } main() {p=fun(std,n); if(p.num[0]==0) { printf(&\nNot found!\n&); } else { printf(&\nSucceed!\n&); printf(&%s ar,p.month,p.day); } } %d-%d-%d\n&,p.num,p.ye【矩阵】 18.下列给定程序中,函数 fun 的功能是:有 N×N 矩阵,根据给定的 m(m&=N)值,将每行元素中的 值均向右移动 m 个位置, 左位置为 0。 例如, N=3, m=2,有下列矩阵 1 2 3 4 5 6 7 8 9 程序执行结果为 0 0 1 0 0 4 0 0 7 #include &stdio.h& #define N 4 void fun(int (*t)[N], int m) { int i, /**********found**********/ for(i=0; i&N; i++ ) { for(j=N-1-m; j&=0; j--) /**********found**********/ t[i][j+m]=t[i][j]; /**********found**********/ for(j=0; j&m; j++) t[i][j]=0; } } main() { int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33, 10}, i, j, printf(&\nThe original array:\n&); for(i=0; i&N; i++) { for(j=0; j&N; j++) printf(&%2d &,t[i][j]); printf(&\n&); } printf(&Input m (m&=%d): &,N);scanf(&%d&,&m); fun(t,m); printf(&\nThe result is:\n&); for(i=0; i&N; i++) { for(j=0; j&N; j++) printf(&%2d &,t[i][j]); printf(&\n&); } } 21.下列给定程序中,函数 fun 的功能是:有 N×N 矩阵,将矩阵的外围元素做顺时针旋转。操作顺序 是:首先将第一行元素的值存入临时数组 r,然后 使第一列成为第一行,最后一行成为第一列,最后 一列成为最后一行,再使临时数组中的元素成为最 后一列。 例如,若 N=3,有下列矩阵: 1 2 3 4 5 6 7 8 9 操作后应为: 7 4 1 8 5 2 9 6 3 #include &stdio.h& #define N 4 void fun(int (*t)[N]) { int j ,r[N];STU std[N]={{&1,2,15},{&2,9,21},{ &3,9,1},{&4,7,15},{&55555&,1 %s\n&,s[i].num,s[i].name, 984,9,28},{&83,11,14},{&7,6,2 2},{&8,8,18}}; STU char n[10]=&666666&; for(j=0; j&N; j++) r[j]=t[0][j]; for(j=0; j&N; j++) /**********found**********/ t[0][N-j-1]=t[j][0]; for(j=0; j&N; j++) t[j][0]=t[N-1][j]; /**********found**********/ for(j=N-1; j&=0;j--) t[N-1][N-1-j]=t[j][N-1]; for(j=N-1; j&=0; j--) /**********found**********/ t[j][N-1]=r[j]; } main() { int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33, 10}, i, printf(&\nThe original array:\n&); for(i=0; i&N; i++) { for(j=0; j&N; j++) printf(&%2d &,t[i][j]); printf(&\n&); } fun(t); printf(&\nThe result is:\n&); for(i=0; i&N; i++) { for(j=0; j&N; j++) printf(&%2d &,t[i][j]); printf(&\n&); } } 25.下列给定程序中,函数 fun 的功能是:将 N×N 矩阵主对角线元素的值与反向对角线对应位置上元 素的值进行交换。 例如,若 N=3,有下列矩阵: 1 2 3 4 5 6 7 8 9 交换后为: 3 2 1 4 5 6 9 8 7 #include &stdio.h& #define N 4 /**********found**********/ void fun(int t[][N], int n) { int i,s; /**********found**********/ for(i=0;i&n; i++) { s=t[i][i]; t[i][i]=t[i][n-i-1]; /**********found**********/ t[i][n-1-i]=s; } } main() { int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33, 10}, i, printf(&\nThe original array:\n&); for(i=0; i&N; i++) { for(j=0; j&N; j++) printf(&%d &,t[i][j]); printf(&\n&); } fun(t,N); printf(&\nThe result is:\n&); for(i=0; i&N; i++) { for(j=0; j&N; j++) printf(&%d &,t[i][j]); printf(&\n&); } } 29.下列给定程序中,函数 fun 的功能是:有 N×N 矩阵,以主对角线为对称线,对称元素相加并将结 果存放在左下三角元素中,右上三角元素置为 0。 例如,若 N=3,有下列矩阵: 1 2 3 4 5 6 7 8 9 计算后结果为 1 0 0 6 5 0 10 14 9 #include &stdio.h&#define N 4 /**********found**********/ void fun(int (*t)[N] ) { int i, for(i=1; i&N; i++) { for(j=0; j&i; j++) { /**********found**********/ t[i][j] =t[i][j]+t[j][i]; /**********found**********/ t[j][i] =0; } } } main() { int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33, 10}, i, printf(&\nThe original array:\n&); for(i=0; i&N; i++) { for(j=0; j&N; j++) printf(&%2d &,t[i][j]); printf(&\n&); } fun(t); printf(&\nThe result is:\n&); for(i=0; i&N; i++) { for(j=0; j&N; j++) printf(&%2d &,t[i][j]); printf(&\n&); } }printf(&-&End\n&); } } main( ) } { SLIST * int void outlist(SLIST *h) a[N]={1,2,2,3,4,4,4,5}; { SLIST *p; head=creatlist(a); p=h-& printf(&\nThe list before deleting :\n&); if (p==NULL) printf(&\nThe list is outlist(head); NULL!\n&); fun(head); else printf(&\nThe list after deleting :\n&); { printf(&\nHead&); outlist(head); do { printf(&-&%d&,p-&data); } p=p-& } while(p!=NULL); printf(&-&End\n&); 23.下列给定程序中,函数 fun 的功能是:计算一个 } 带头结点的单向链表中各结点的数据域中数值之 } 和,结果作为函数值返回。 main() #include &stdio.h& { SLIST * #include &stdlib.h& int a[N]={11,12,15,18,19,22,25,29}; #define N 8 head=creatlist(a); typedef struct list printf(&\nOutput from head:\n&); { outlist(head); struct list * printf(&\nOutput from tail: \n&); } SLIST; while (head-&next != NULL){ SLIST *creatlist(int *); fun(head); void outlist(SLIST *); printf(&\n\n&); int fun( SLIST *h) printf(&\nOutput from head again :\n&); { SLIST *p; int s=0; outlist(head); p=h-& } while(p) } { 43.下列给定程序中,函数 fun 的功能是:计算 N× /**********found**********/ N 矩阵的主对角线元素和反向对角线元素之和,并 28.下列给定程序中已建立了一个带头结点的单向 s+= p-& 作为函数值返回。 要求先累加主对角线元素中的值, 链表,链表中的各结点按数据域递增有序链接。函 /**********found**********/ 再累加反向对角线元素中的值。 数 fun 的功能是: 删除链表中数据域值相同的结点, p=p-& 例如,若 N=3,有下列矩阵: 使之只保留一个。 } 1 2 3 #include &stdio.h& 4 5 6 #include &stdlib.h& } 7 8 9 #define N 8 main() 首先累加 1、5、9,然后累加 3、5、7,函数返回值 typedef struct list { SLIST * 为 30。 { int a[N]={12,87,45,32,91,16,20,48}; #include &stdio.h& struct list * head=creatlist(a); outlist(head); #define N 4 } SLIST; /**********found**********/ fun(int t[][N], int n) void fun( SLIST *h) printf(&\nsum=%d\n&, fun(head)); { int i, { SLIST *p, *q; } /**********found**********/ p=h-& sum=0; if (p!=NULL) SLIST *creatlist(int a[]) for(i=0; i&n; i++) { q=p-& { SLIST *h,*p,*q; /**********found**********/ while(q!=NULL) h=p=(SLIST *)malloc(sizeof(SLIST)); sum+=t[i][i] ; { if (p-&data==q-&data) for(i=0; i&N; i++) for(i=0; i&n; i++) { p-&next=q-& { q=(SLIST *)malloc(sizeof(SLIST)); /**********found**********/ /**********found**********/ q-&data=a[i]; p-&next=q; p=q; sum+= t[i][n-i-1] ; 15.下列给定程序中已建立了一个带头结点的单向 free(q); } 链表,在 main 函数中将多次调用 fun 函数,每调 /**********found**********/ p-&next=0; } 用一次,输出链表尾部结点中的数据,并释放该结 q=p-& main() 点,使链表缩短。 } } { int #include &stdio.h& else void outlist(SLIST *h) t[][N]={21,2,13,24,25,16,47,38,29,11,32,54,42,21,3,10 #include &stdlib.h& { p=q; { SLIST *p; },i,j; #define N 8 /**********found**********/ p=h-& printf(&\nThe original data:\n&); typedef struct list q=q-& if (p==NULL) printf(&The list is NULL!\n&); for(i=0; i&N; i++) { } else { for(j=0; j&N; j++) printf(&%4d&,t[i][j]); struct list * } { printf(&\nHead &); printf(&\n&); } SLIST; } do } } { printf(&-&%d&, p-&data); p=p-& } printf(&The result is: %d&,fun(t,N)); void fun( SLIST *p) SLIST *creatlist(int *a) while(p!=NULL); } { SLIST *t, *s; { SLIST *h,*p,*q; printf(&-&End\n&); t=p-& s=p; h=p=(SLIST *)malloc(sizeof(SLIST)); } while(t-&next != NULL) for(i=0; i&N; i++) } { s=t; { q=(SLIST *)malloc(sizeof(SLIST)); 【链表】 /**********found**********/ q-&data=a[i]; p-&next=q; p=q; 50.下列给定程序中,函数 fun 的功能是:在带头结 t=t-& } 点的单向链表中, 查找数据域中值为 ch 的结点。 找 } p-&next=0; 到后通过函数值返回该结点在链表中所处的顺序 10.下列给定程序中已建立一个带头结点的单向链 号;若不存在值为 ch 的结点,函数返回 0 值。 表,链表中的各结点按结点数据域中的数据递增有 /**********found**********/ printf(& %d &,t-&data); } #include &stdio.h& 序链接。函数 fun 的功能是:把形参 x 的值放入一 s-&next=NULL; void outlist(SLIST *h) #include &stdlib.h& 个新结点并插入链表中,使插入后各结点数据域中 /**********found**********/ { SLIST *p; #define N 8 的数据仍保持递增有序。 free(t); p=h-& typedef struct list #include &stdio.h& } if (p==NULL) printf(&\nThe list is { #include &stdlib.h& SLIST *creatlist(int *a) NULL!\n&); struct list * #define N 8 { SLIST *h,*p,*q; else } SLIST; typedef struct list h=p=(SLIST *)malloc(sizeof(SLIST)); { printf(&\nHead&); SLIST *creatlist(char *); { for(i=0; i&N; i++) do { printf(&-&%d&,p-&data); void outlist(SLIST *); struct list * { q=(SLIST *)malloc(sizeof(SLIST)); p=p-& } while(p!=NULL); int fun( SLIST *h, char ch)} SLIST; void fun( SLIST *h, int x) { SLIST *p, *q, *s; s=(SLIST *)malloc(sizeof(SLIST)); /**********found**********/ s-&data=x; q=h; p=h-& while(p!=NULL && x&p-&data) { /**********found**********/ q=p; p=p-& } s-&next=p; /**********found**********/ q-&next=s; } SLIST *creatlist(int *a) { SLIST *h,*p,*q; h=p=(SLIST *)malloc(sizeof(SLIST)); for(i=0; i&N; i++) { q=(SLIST *)malloc(sizeof(SLIST)); q-&data=a[i]; p-&next=q; p=q; } p-&next=0; } void outlist(SLIST *h) { SLIST *p; p=h-& if (p==NULL) printf(&\nThe list is NULL!\n&); else { printf(&\nHead&); do { printf(&-&%d&,p-&data); p=p-& } while(p!=NULL); printf(&-&End\n&); } } main() { SLIST * int a[N]={11,12,15,18,19,22,25,29}; head=creatlist(a); printf(&\nThe list before inserting:\n&); outlist(head); printf(&\nEnter a number : &); scanf(&%d&,&x); fun(head,x); printf(&\nThe list after inserting:\n&); outlist(head); }q-&data=a[i]; } p-&next=0;p-&next=q;p=q;{SLIST *p; int n=0; p=h-& /**********found**********/ while(p!=NULL) { n++; /**********found**********/ if (p-&data==ch) else p=p-& } return 0; } main() { SLIST * char a[N]={'m','p','g','a','w','x','r','d'}; head=creatlist(a); outlist(head); printf(&Enter a letter:&); scanf(&%c&,&ch); /**********found**********/ k=fun(head,ch); if (k==0) printf(&\nNot found!\n&); else printf(&The sequence number is : %d\n&,k); } SLIST *creatlist(char *a) { SLIST *h,*p,*q; h=p=(SLIST *)malloc(sizeof(SLIST)); for(i=0; i&N; i++) { q=(SLIST *)malloc(sizeof(SLIST)); q-&data=a[i]; p-&next=q; p=q; } p-&next=0; } void outlist(SLIST *h) { SLIST *p; p=h-& if (p==NULL) printf(&\nThe list is NULL!\n&); else { printf(&\nHead&); do { printf(&-&%c&,p-&data); p=p-& } while(p!=NULL); printf(&-&End\n&); } } 53.下列给定程序中,函数 fun 的功能是:统计带头 结点的单向链表中结点的个数,并存放在形参 n 所 指的存储单元中 #include &stdio.h& #include &stdlib.h& #define N 8 typedef struct list { struct list * } SLIST; SLIST *creatlist(int *a); void outlist(SLIST *); void fun( SLIST *h, int *n) { SLIST *p; /**********found**********/ *n=0; p=h-& while(p) { (*n)++; /**********found**********/ p=p-& } } main() { SLIST * int a[N]={12,87,45,32,91,16,20,48}, head=creatlist(a); outlist(head); /**********found**********/ fun(head, &num); printf(&\nnumber=%d\n&,num); } SLIST *creatlist(int a[]) { SLIST *h,*p,*q; h=p=(SLIST *)malloc(sizeof(SLIST)); for(i=0; i&N; i++) int fun(char *source, char *target) { FILE *fs,* /**********found**********/ if((fs=fopen(source, &r&))==NULL) return 0; } if((ft=fopen(target, &w&))==NULL) void outlist(SLIST *h) return 0; { SLIST *p; printf(&\nThe data in file :\n&); p=h-& ch=fgetc(fs); if (p==NULL) printf(&The list is NULL!\n&); /**********found**********/ else while(!feof(fs)) { printf(&\nHead &); { putchar( ch ); do /**********found**********/ { printf(&-&%d&,p-&data); fputc(ch,ft); p=p-& } ch=fgetc(fs); while(p!=NULL); } printf(&-&End\n&); fclose(fs); fclose(ft); } printf(&\n\n&); } return 1; } main() { char sfname[20] =&myfile1&,tfname[20]=&myfile2&; 【其他】 FILE * myf=fopen(sfname,&w&); printf(&\nThe original data :\n&); 1.下列给定程序中,函数 fun 的功能是计算如下公 for(i=1; i&30; 式 i++){ c='A'+rand()%25;fprintf(myf,&%c&,c); 直到 ,并且把计算结果作为函数值返回。 例如,若形参 e 的值为 1e-3,则函数返回值为 printf(&%c&,c); } fclose(myf);printf(&\n\n&); 0.551690。 if (fun(sfname, tfname)) printf(&Succeed!&); #include &stdio.h& else printf(&Fail!&); double fun(double e) } { int i, double s, t, s=0; k=1; i=2; 1.下列给定程序中,函数 fun 的功能是计算如下公 /**********found**********/ 式 x=3.0/4; 直到 ,并且把计算结果作为函数值返回。 /**********found**********/ 例如,若形参 e 的值为 1e-3,则函数返回值为 while(x & e) 0.551690。 { s=s+k*x; #include &stdio.h& k=k* (-1); double fun(double e) t=2*i; { int i, double s, t, /**********found**********/ s=0; k=1; i=2; x=(t+1)/(t*t); /**********found**********/ i++; x=3.0/4; } /**********found**********/ while(x & e) } { s=s+k*x; main() k=k* (-1); { double e=1e-3; t=2*i; printf(&\nThe result is: %f\n&,fun(e)); /**********found**********/ } x=(t+1)/(t*t); 2.下列给定程序中,函数 fun 的功能是:计算如下 i++; 公式前 n 项的和并作为函数值返回。 } 例如,当形参 n 的值为 10 时,函数返回值为 9.612558。 } #include &stdio.h& main() double fun(int n) { double e=1e-3; { double s, printf(&\nThe result is: %f\n&,fun(e)); /**********found**********/ } s=0; /**********found**********/ 32. for(i=1; i&=n; i++) 下列给定程序中,函数 fun 的功能是计算下式, 直 { t=2.0*i; 到 ,并将计算结果作为函数值返回。例如,若形参 /**********found**********/ e 的值为 1e-3,函数的返回值为 2.985678。 s=s+(2.0*i-1)*(2.0*i+1)/(t*t); #include &stdio.h& } double fun(double e) { double s, } /**********found**********/ main() s=0; i=0; { int n=-1; x=1.0; while(n&0) while(x&e){ { printf(&Please input(n&0): &); /**********found**********/ scanf(&%d&,&n); } i++; printf(&\nThe result is: %f\n&,fun(n)); /**********found**********/ } x=(2.0*i-1)/((2.0*i)*(2.0*i)); 17.下列给定程序的功能是:调用函数 fun 将指定源 s=s+x; 文件中的内容复制到指定的目标文件中,复制成功 } 时函数返回 1,失败时返回 0。在复制的过程中,把 复制的内容输出到屏幕。主函数中源文件名放在变 } 量 sfname 中,目标文件名放在变量 tfname 中。 main() #include &stdio.h& { double e=1e-3; #include &stdlib.h& printf(&\nThe result is: %f\n&,fun(e));{ q=(SLIST *)malloc(sizeof(SLIST)); q-&data=a[i]; p-&next=q; p=q; } p-&next=0;}#define N 9 int fun(int a[], int n) { int i,j; j = 0; 2.下列给定程序中,函数 fun 的功能是:计算如下 for (i=0; i&n; i++) 公式前 n 项的和并作为函数值返回。 /**********found**********/ 例如,当形参 n 的值为 10 时,函数返回值为 if (a[i]%2==1) 9.612558。 { #include &stdio.h& /**********found**********/ double fun(int n) a[j] = a[i]; j++; { double s, } /**********found**********/ /**********found**********/ s=0; /**********found**********/ } for(i=1; i&=n; i++) main() { t=2.0*i; { int b[N]={9,1,4,2,3,6,5,8,7}, i, /**********found**********/ printf(&\nThe original data :\n&); s=s+(2.0*i-1)*(2.0*i+1)/(t*t); for (i=0; i&N; i++) printf(&%4d &, b[i]); } printf(&\n&); n = fun(b, N); } printf(&\nThe number of odd : %d \n&, n); main() printf(&\nThe odd number :\n&); { int n=-1; for (i=0; i&n; i++) printf(&%4d &, b[i]); while(n&0) printf(&\n&); { printf(&Please input(n&0): &); } scanf(&%d&,&n); } printf(&\nThe result is: %f\n&,fun(n)); 11.下列给定程序中,函数 fun 的功能是:将形参 a } 所指数组中的前半部分元素中的值与后半部分元素 中的值对换。形参 n 中存放数组中数据的个数,若 17.下列给定程序的功能是:调用函数 fun 将指定源 n 为奇数,则中间的元素不动。 文件中的内容复制到指定的目标文件中,复制成功 例如:若 a 所指数组中的数据为:1、2、3、4、5、 时函数返回 1,失败时返回 0。在复制的过程中,把 6、7、8、9,则调换后为:6、7、8、9、5、1、2、 复制的内容输出到屏幕。主函数中源文件名放在变 3、4。 量 sfname 中,目标文件名放在变量 tfname 中。 #include &stdio.h& #include &stdio.h& #define N 9 #include &stdlib.h& void fun(int a[], int n) int fun(char *source, char *target) { int i, t, { FILE *fs,* /**********found**********/ /**********found**********/ p = (n%2==0)?n/2:n/2+1; if((fs=fopen(source, &r&))==NULL) for (i=0; i&n/2; i++) return 0; { if((ft=fopen(target, &w&))==NULL) t=a[i]; return 0; /**********found**********/ printf(&\nThe data in file :\n&); a[i] = a[p+i]; ch=fgetc(fs); /**********found**********/ /**********found**********/ a[p+i] = while(!feof(fs)) } { putchar( ch ); } /**********found**********/ main() fputc(ch,ft); { int b[N]={1,2,3,4,5,6,7,8,9}, ch=fgetc(fs); printf(&\nThe original data :\n&); } for (i=0; i&N; i++) printf(&%4d &, b[i]); fclose(fs); fclose(ft); printf(&\n&); printf(&\n\n&); fun(b, N); return 1; printf(&\nThe data after moving :\n&); } for (i=0; i&N; i++) printf(&%4d &, b[i]); main() printf(&\n&); { char sfname[20] } =&myfile1&,tfname[20]=&myfile2&; FILE * 16.下列给定程序中,函数 fun 的功能是:逆置数组 myf=fopen(sfname,&w&); 元素中的值。例如:若 a 所指数组中的数据为:1、 printf(&\nThe original data :\n&); 2、3、4、5、6、7、8、9,则逆置后依次为:9、8、 for(i=1; i&30; 7、6、5、4、3、2、1。形参 n 给出数组中数据的个 i++){ c='A'+rand()%25;fprintf(myf,&%c&,c); 数。 printf(&%c&,c); } #include &stdio.h& fclose(myf);printf(&\n\n&); void fun(int a[], int n) if (fun(sfname, tfname)) printf(&Succeed!&); { int i,t; else printf(&Fail!&); /**********found**********/ } for (i=0; i&n/2; i++) { t=a[i]; /**********found**********/ 【数组】 a[i] = a[n-1-i]; /**********found**********/ a[n-i-1] = 4.下列给定程序中,函数 fun 的功能是:把形参 a } 所指数组中的奇数按原顺序依次存放到 a[0]、a[1]、 a[2]、……中,把偶数从数组中删除,奇数个数通 } main() 过函数值返回。 例如: a 所指数组中的数据最初排列为: 1、 若 9、 4、 { int b[9]={1,2,3,4,5,6,7,8,9}, printf(&\nThe original data :\n&); 2、3、6、5、8、7,删除偶数后 a 所指数组中的数 for (i=0; i&9; i++) 据为:9、1、3、5、7,返回值为 5。 printf(&%4d &, b[i]); #include &stdio.h& printf(&\n&);fun(b, 9); printf(&\nThe data after invert for (i=0; i&9; i++) printf(&%4d &, b[i]); printf(&\n&); }printf(&\n&); :\n&); } 49.下列给定程序中,函数 fun 的功能是:把形参 a 所指数组中的最大值放在 a[0]中,接着求出 a 所指 数组中的最小值放在 a[1]中,再把 a 所指数组元素 中的次大值放在 a[2]中,把 a 数组元素中的次小值 放在 a[3]中,以此类推。例如,若 a 所指数组中的 数据最初排列为:1、4、2、3、9、6、5、8、7,按 规则移动后,数据排列为:9、1、8、2、7、3、6、 4、5。形参 n 中存放 a 所指数组中数据的个数。 #include &stdio.h& #define N 9 /**********found**********/ void fun(int a[], int n) { int i, j, max, min, px, pn, /**********found**********/ for (i=0; i&n-1; i+=2) { max = min = a[i]; px = pn = /**********found**********/ for (j=i+1; j&n; j++) { if (max & a[j]) { max = a[j]; px = } if (min & a[j]) { min = a[j]; pn = } } if (px != i) { t = a[i]; a[i] = a[px] = if (pn == i) pn= } if (pn != i+1) { t = a[i+1]; a[i+1] = a[pn] = } } } main() { int b[N]={1,4,2,3,9,6,5,8,7}, printf(&\nThe original data :\n&); for (i=0; i&N; i++) printf(&%4d &, b[i]); printf(&\n&); fun(b, N); printf(&\nThe data after moving :\n&); for (i=0; i&N; i++) printf(&%4d &, b[i]); printf(&\n&); } 52.下列给定程序中,函数 fun 的功能是:把形参 a 所指数组中的偶数按原顺序依次存放到 a[0]、a[1]、 a[2]、……中,把奇数从数组中删除,偶数的个数 通过函数值返回。 例如,若 a 所指数组中的数据最初排列列为:9、1、 4、2、3、6、5、8、7,删除奇数后 a 所指数组中的 数据为:4、2、6、8,返回值为 4。 #include &stdio.h& #define N 9 int fun(int a[], int n) { int i,j; j = 0; for (i=0; i&n; i++) /**********found**********/ if (a[i]%2== 0) { /**********found**********/ a[j] = a[i]; j++; } /**********found**********/ } main() { int b[N]={9,1,4,2,3,6,5,8,7}, i, printf(&\nThe original data :\n&); for (i=0; i&N; i++) printf(&%4d &, b[i]); printf(&\n&); n = fun(b, N); printf(&\nThe number of even :%d\n&, n); printf(&\nThe even :\n&); for (i=0; i&n; i++) printf(&%4d &, b[i]); printf(&\n&); } 58.从键盘输入一组无符号整数并保存在数组 xx[N] 中,以整数 0 结束输入,要求这些数的最大位数不 超过 4 位,其元素的个数通过变量 num 传入函数 fun。请补充函数 fun,该函数的功能是:从数组 xx34.请补充函数 fun,该函数的功能是:把数组 aa 中的偶数元素按原来的先后顺序放在原数组后面。 例如,若输入&33,67,42,58,25,76,85,16,41,56&,输出 结果为&33,67,25,85,41,42,58,76,16,56&。 #include &stdio.h& #include &stdlib.h& #define N 10 void fun(int aa[]) { int i, j=0,k=0; int bb[N]; for (i=0;i&N; i++) { if (aa[i]%2==0) bb[k++]=aa[i]; else aa[j++]=aa[i]; } for(i=0;i&k;i++,j++) aa[j]=bb[i]; } void main() { int aa[N]={33,67,42,58,25,76,85,16,41,56}; system(&CLS&); printf(&\n*** original list ***\n&); for (i=0;i&N; i++) printf(&%4d&,aa[i]); fun(aa); printf(&\n*** new list ***\n &); for (i=0; i&N; i++) printf(&%4d&,aa[i]); } 48.下列给定程序中,函数 fun 的功能是:把形参 a 所指数组中的最大值放在 a[0]中,接着求出 a 所指 数组中的最小值放在 a[1]中,再把 a 所指数组元素 中的次大值放在 a[2]中,把 a 数组元素中的次小值 放在 a[3]中,以此类推。例如,若 a 所指数组中的 数据最初排列为:1、4、2、3、9、6、5、8、7,按 规则移动后,数据排列为:9、1、8、2、7、3、6、 4、5。形参 n 中存放 a 所指数组中数据的个数。 #include &stdio.h& #define N 9 /**********found**********/ void fun(int a[], int n) { int i, j, max, min, px, pn, /**********found**********/ for (i=0; i&n-1; i+=2) { max = min = a[i]; px = pn = /**********found**********/ for (j=i+1; j&n; j++) { if (max & a[j]) { max = a[j]; px = } if (min & a[j]) { min = a[j]; pn = } } if (px != i) { t = a[i]; a[i] = a[px] = if (pn == i) pn= } if (pn != i+1) { t = a[i+1]; a[i+1] = a[pn] = } } } main() { int b[N]={1,4,2,3,9,6,5,8,7}, printf(&\nThe original data :\n&); for (i=0; i&N; i++) printf(&%4d &, b[i]); printf(&\n&); fun(b, N); printf(&\nThe data after moving :\n&); for (i=0; i&N; i++) printf(&%4d &, b[i]); 中找出个位和十位的数字之和大于 5 的所有无符号 整数,结果保存在数组 yy 中,其个数由函数 fun 返 回。 例如,当 xx[8]={123,11,25,222,42,333,14,5451}时, bb[4]={25,42,333,5451}。 #include &stdio.h& #define N 1000 int fun(int xx[],int bb[],int num) { int i, n=0; int g, for(i=0;i&i++) { g=xx[i]%10; s=xx[i]/10%10; if((g+s)&5) bb[n++]=xx[i]; } } void main() { int xx[N]; int yy[N]; int num=0,n=0,i=0; printf(&Input number :\n&); do { scanf(&%u&,&xx[num]); } while(xx[num++]!=0); n=fun(xx,yy,num); printf(&\nyy=&); for(i=0;i&n;i++) printf(&%u &,yy[i]); } 76.给定程序的功能是: n 个人员的考试成绩进行 将 分段统计,考试成绩存到数组 a 中,各分段的人数 存到数组 b 中: 成绩为 60 到 69 的人数存到 b[0]中, 成绩为 70 到 79 的人数存到 b[l]中, 成绩为 80 到 89 的人数存到 b[2]中, 成绩为 90 到 99 的人数存到 b[3] 中,成绩为 100 的人数存到 b[4]中,成绩为 60 分以 下的人数存到 b[5]中。 例如,当数组 a 中的数据是:93、85、77、68、59、 43、94、75、98。调用该函数后,数组 b 中存放的 数据应是:l、2、l、3、0、2。 #include &stdio.h& # define N 50 # define M 11 void fun(int a[], int b[],int n) { for(i=0;i&6;i++) b[i]=0; for(i=0;i&n;i++) if(a[i]&60) b[5]++; else b[(a[i]-60)/10]++; } main() { int i,a[100]={93.,85,77,68,59,43,94,75,98},b[6]; fun(a,b,9); printf(&The resul\t is: &); for(i=0;i&6;i++) printf(&%d &,b[i]); printf(&\n&); } 82.请补充 main 函数,该函数的功能是:如果数组 aa 的前一个元素比后一个元素小,则把它保存在数 组 bb 中并输出。 例如,输入&40,51,62,33,35,52,48,95,66,73&,则结果 输出&40,51,33,35,48,66&。 #include &stdlib.h& #include &stdio.h& #define N 10 void main() { int i,n=0; int aa[N]={40,51,62,33,35,52,48,95,66,73};int bb[N]; system(&CLS&); for(i=0;i&N-1;i++) if(aa[i]&aa[i+1]) bb[n++]=aa[i]; printf(&\n*** display bb ***\n&); for (i=0;i&n;i++) printf(&bb[%d]=%2d &,i,bb[i]); } 88.请补充函数 fun,该函数的功能是:求 100(不包 括 100)以内能被 2 或 5 整除,但不能同时被 2 和 5 整除的自然数。结果保存在数组 bb 中,函数 fun 返回数组 bb 元素的个数。 #include &stdlib.h& #include &stdio.h& #define N 100 int fun(int bb[]) { int i, for(i=1,j=0; i&100; i++)if (!(n%i)) { m=0; } if (m==1&&n&1) return(&yes!&); else return(&no!&); } void main() { int k=0; system(&CLS&); printf(&Input:&); scanf(&%d&,&k); printf(&%s\n&,fun(k)); }/**********found**********/ x=(t+1)/(t*t); i++; } } main() { double e=1e-3; printf(&\nThe result is: %f\n&,fun(e)); }} main() { int b[N]={9,1,4,2,3,6,5,8,7}, i, printf(&\nThe original data :\n&); for (i=0; i&N; i++) printf(&%4d &, b[i]); printf(&\n&); n = fun(b, N); printf(&\nThe number of odd : %d \n&, n); printf(&\nThe odd number :\n&); for (i=0; i&n; i++) printf(&%4d &, b[i]); printf(&\n&); }for (i=0; i&N; i++) printf(&\n&); }printf(&%4d &, b[i]);2.下列给定程序中,函数 fun 的功能是:计算如下 公式前 n 项的和并作为函数值返回。 例如,当形参 n 的值为 10 时,函数返回值为 9.612558。 #include &stdio.h& double fun(int n) { double s, 89.请补充函数 fun,该函数的功能是:寻找两个整 /**********found**********/ 数之间的所有素数(包括这两个整数),把结果保存 s=0; 在数组 bb 中,函数返回素数的个数。 /**********found**********/ if((i%2!=0&&i%5==0)||(i%2==0&&i%5! 例如, 输入 6 和 21, 则输出为: 11 13 17 19。 for(i=1; i&=n; i++) 7 =0)) #include &stdlib.h& { t=2.0*i; bb[j++]=i; #include &conio.h& /**********found**********/ #include &stdio.h& s=s+(2.0*i-1)*(2.0*i+1)/(t*t); } #define N 1000 } void main() int fun(int n,int m,int bb[N]) { { } int i, int i,j,k=0, main() int bb[N]; for(j=n;j&=m;j++) { int n=-1; system(&CLS&); { while(n&0) n=fun(bb); flag=1; { printf(&Please input(n&0): &); for(i=0; i&n; i++) for(i=2;i&j;i++) scanf(&%d&,&n); } { if(j%i==0) printf(&\nThe result is: %f\n&,fun(n)); if(i%10==0) { } printf(&\n&); flag=0; printf(&%4d&,bb[i]); 3.给定程序中,函数 fun 的功能是:统计形参 s 所 } } 指的字符串中数字字符出现的次数,并存放在形参 } if(flag==1) t 所指的变量中,最后在主函数中输出。例如,若 bb[k++]=j; 形参 s 所指的字符串为 abcdef35adgh3kjsdf7,则输 【素数】 } 出结果为 4。 65.请补充 main 函数。该函数的功能是:求 1~ 果。 100(不包括 100)以内所有素数的平均值。程序运行 } #include &stdio.h& 后的结果为 42.40。 void fun(char *s, int *t) #include &stdlib.h& void main() { int i, #include &stdio.h& { n=0; void main() int n=0,m=0,i,k; /**********found**********/ { int bb[N]; for(i=0; s[i] !=0; i++) int i, j, n=0, system(&CLS&); /**********found**********/ float aver=0; printf(&Input n\n&); if(s[i]&='0'&&s[i]&= '9' ) n++; system(&CLS&); scanf(&%d&,&n); /**********found**********/ for(j=2; j&100; j++) printf(&Input m\n&); *t= { scanf(&%d&,&m); } flag=1; for(i=0;i&m-n;i++) main() for(i=2; i&j; i++) bb[i]=0; { char s[80]=&abcdef35adgh3kjsdf7&; if(j%i==0) k=fun(n,m,bb); { for(i=0;i&k;i++) printf(&\nThe original string is : %s\n&,s); flag=0; printf(&%4d&,bb[i]); fun(s,&t); } printf(&\nThe result is : %d\n&,t); } } if(flag==1) { 4.下列给定程序中,函数 fun 的功能是:把形参 a n++; 所指数组中的奇数按原顺序依次存放到 a[0]、a[1]、 【填空】 aver+=j; a[2]、……中,把偶数从数组中删除,奇数个数通 } 过函数值返回。 } 若 9、 4、 1.下列给定程序中,函数 fun 的功能是计算如下公 例如: a 所指数组中的数据最初排列为: 1、 printf(&\n\n average=%4.2f\n&,aver/n); 2、3、6、5、8、7,删除偶数后 a 所指数组中的数 式 } 据为:9、1、3、5、7,返回值为 5。 直到 ,并且把计算结果作为函数值返回。 &stdio.h& 例如,若形参 e 的值为 1e-3,则函数返回值为 #include #define N 9 0.551690。 75.请补充函数 fun,该函数的功能是:判断一个数 #include &stdio.h& int fun(int a[], int n) 是否为素数。该数是素数时,函数返回字符串&yes! double fun(double e) { int i,j; &,否则函数返回字符串&no!&,并在主函数中输 { int i, j = 0; double s, t, 出。 for (i=0; i&n; i++) s=0; k=1; i=2; #include &stdlib.h& /**********found**********/ /**********found**********/ #include &conio.h& if (a[i]%2==1) x=3.0/4; #include &stdio.h& { /**********found**********/ char *fun(int n) /**********found**********/ while(x & e) { a[j] = a[i]; j++; { s=s+k*x; int i, } k=k* (-1); m=1; /**********found**********/ t=2*i; for(i=2;i&n; i++)7.下列给定程序中,函数 fun 的功能是进行数字字 符转换。若形参 ch 中是数字字符'0'~'9',则将'0' 转换成'9','1'转换成'8','2'转换成'7',……,'9' 转换成'0';若是其它字符则保持不变;并将转换后 的结果作为函数值返回。 #include &stdio.h& /**********found**********/ char fun(char ch) { 5.下列给定程序中,函数 fun 的功能是:将形参 n /**********found**********/ 中,各位上为偶数的数取出,并按原来从高位到低 if (ch&='0' && ch&='9') 位相反的顺序组成一个新数,作为函数值返回。 /**********found**********/ 例 如 ,输 入一 个整 数 , 函数 返回 值 为 return '9'- (ch-'0'); 64862。 #include &stdio.h& } unsigned long fun(unsigned long n) main() { unsigned long x=0; { char c1, c2; while(n) printf(&\nThe result :\n&); { t=n%10; c1='2'; c2 = fun(c1); /**********found**********/ printf(&c1=%c c2=%c\n&, c1, c2); if(t%2==0) c1='8'; c2 = fun(c1); /**********found**********/ printf(&c1=%c c2=%c\n&, c1, c2); x=10*x+t; c1='a'; c2 = fun(c1); /**********found**********/ printf(&c1=%c c2=%c\n&, c1, c2); n=n/10; } } 8.下列给定程序中,函数 fun 的功能是:求 ss 所指 } 字符串数组中长度最短的字符串所在的行下标,作 main() 为函数值返回,并把其串长放在形参 n 所指的变量 { unsigned long n=-1; 中。ss 所指字符串数组中共有 M 个字符串, 且串长 while(n&||n&0) &N。 { printf(&Please input(0&n&): &); #include &stdio.h& scanf(&%ld&,&n); } #include &string.h& printf(&\nThe result is: %ld\n&,fun(n)); #define M 5 } #define N 20 int fun(char (*ss)[N], int *n) 6.下列给定程序中,函数 fun 的功能是:把形参 a { int i, k=0, len= N; 所指数组中的最小值放在元素 a[0]中,接着把 a 所 /**********found**********/ 指数组中的最大值放在 a[1]元素中;再把 a 所指数 for(i=0; i&M; i++) 组元素中的次小值放在 a[2]中,把 a 所指数组元素 { len=strlen(ss[i]); 中的次大值放在 a[3],以此类推。 if(i==0) *n= 例如, a 所指数组中的数据最初排列为: 1、 若 9、 4、 /**********found**********/ 2、3、6、5、8、7;则按规则移动后,数据排列为: if(len & *n) 1、9、2、8、3、7、4、6、5。形参 n 中存放 a 所指 { *n= 数组中数据的个数。 k=i; 规定 fun 函数中的 max 存放当前所找的最大值, px } 存放当前所找最大值的下标。 } # include &stdio.h& /**********found**********/ #define N 9 return(k); void fun(int a[], int n) } { int i,j, max, min, px, pn, main() for (i=0; i&n-1; i+=2) { char { ss[M][N]={&shanghai&,&guangzhou&,&beijing&,&tia /**********found**********/ njing&,&chongqing&}; max = min = a[i]; int n,k,i; px = pn = printf(&\nThe original strings are :\n&); for (j=i+1; j&n; j++) { for(i=0;i&M;i++)puts(ss[i]); /**********found**********/ k=fun(ss,&n); if (max&a[j]) printf(&\nThe length of shortest string { max = a[j]; px = } is : %d\n&,n); /**********found**********/ printf(&\nThe shortest string is : %s\n&,ss[k]); if (min&a[j]) } { min = a[j]; pn = } } 9.下列给定程序中,函数 fun 的功能是:将 s 所指 if (pn != i) 字符串中的所有数字字符移到所有非数字字符之 { t = a[i]; a[i] = a[pn] = 后, 并保持数字字符串和非数字字符串原有的次序。 if (px == i) px = 例如,s 所指的字符串为 def35adh3kjsdf7,执行后 } 结果为 defadhajsdf3537。 if (px != i+1) #include &stdio.h& { t = a[i+1]; a[i+1] = a[px] = } void fun(char *s) } { int i, j=0, k=0; char t1[80], t2[80]; } for(i=0; s[i]!='\0'; i++) main() if(s[i]&='0' && s[i]&='9') { int b[N]={9,1,4,2,3,6,5,8,7}, { printf(&\nThe original data :\n&); /**********found**********/ for (i=0; i&N; i++) printf(&%4d &, b[i]); t2[j]=s[i]; j++; printf(&\n&); } fun(b, N); else t1[k++]=s[i]; printf(&\nThe data after moving :\n&); t2[j]=0; t1[k]=0; /**********found**********/ for(i=0; i&k; i++) s[i]=t1[i]; /**********found**********/ for(i=0; i&j; i++) s[k+i]=t2[i]; } main() { char s[80]=&ba3a54j7sd567sdffs&; printf(&\nThe original string is : %s\n&,s); fun(s); printf(&\nThe result is : %s\n&,s); } 10.下列给定程序中已建立一个带头结点的单向链 表,链表中的各结点按结点数据域中的数据递增有 序链接。函数 fun 的功能是:把形参 x 的值放入一 个新结点并插入链表中,使插入后各结点数据域中 的数据仍保持递增有序。 #include &stdio.h& #include &stdlib.h& #define N 8 typedef struct list { struct list * } SLIST; void fun( SLIST *h, int x) { SLIST *p, *q, *s; s=(SLIST *)malloc(sizeof(SLIST)); /**********found**********/ s-&data=x; q=h; p=h-& while(p!=NULL && x&p-&data) { /**********found**********/ q=p; p=p-& } s-&next=p; /**********found**********/ q-&next=s; } SLIST *creatlist(int *a) { SLIST *h,*p,*q; h=p=(SLIST *)malloc(sizeof(SLIST)); for(i=0; i&N; i++) { q=(SLIST *)malloc(sizeof(SLIST)); q-&data=a[i]; p-&next=q; p=q; } p-&next=0; } void outlist(SLIST *h) { SLIST *p; p=h-& if (p==NULL) printf(&\nThe list is NULL!\n&); else { printf(&\nHead&); do { printf(&-&%d&,p-&data); p=p-& } while(p!=NULL); printf(&-&End\n&); } } main() { SLIST * int a[N]={11,12,15,18,19,22,25,29}; head=creatlist(a); printf(&\nThe list before inserting:\n&); outlist(head); printf(&\nEnter a number : &); scanf(&%d&,&x); fun(head,x); printf(&\nThe list after inserting:\n&); outlist(head); } 11.下列给定程序中,函数 fun 的功能是:将形参 a 所指数组中的前半部分元素中的值与后半部分元素 中的值对换。形参 n 中存放数组中数据的个数,若 n 为奇数,则中间的元素不动。 例如:若 a 所指数组中的数据为:1、2、3、4、5、 6、7、8、9,则调换后为:6、7、8、9、5、1、2、 3、4。#include &stdio.h& #define N 9 void fun(int a[], int n) { int i, t, /**********found**********/ p = (n%2==0)?n/2:n/2+1; for (i=0; i&n/2; i++) { t=a[i]; /**********found**********/ a[i] = a[p+i]; /**********found**********/ a[p+i] = } } main() { int b[N]={1,2,3,4,5,6,7,8,9}, printf(&\nThe original data :\n&); for (i=0; i&N; i++) printf(&%4d &, b[i]); printf(&\n&); fun(b, N); printf(&\nThe data after moving :\n&); for (i=0; i&N; i++) printf(&%4d &, b[i]); printf(&\n&); }fun(s); printf(&\nThe result is : %s\n&,s); }14.下列给定程序中,函数 fun 的功能是:在形参 ss 所指字符串数组中查找与形参 t 所指字符串相同的 串, 找到后返回该串在字符串数组中的位置(即下标 值),若未找到则返回-1。ss 所指字符串数组中共 有 N 个内容不同的字符串,且串长小于 M。 #include &stdio.h& #include &string.h& #define N 5 #define M 8 int fun(char (*ss)[M],char *t) { /**********found**********/ for(i=0; i&N; i++) /**********found**********/ if(strcmp(ss[i],t)==0 ) return N-1; return -1; } main() { char ch[N][M]={&if&,&while&,&switch&,&int&,&for&},t[M ]; int n,i; 12.下列给定程序中,函数 fun 的功能是:从形参 ss printf(&\nThe original string\n\n&); 所指字符串数组中, 删除所有串长超过 k 的字符串, for(i=0;i&N;i++)puts(ch[i]); printf(&\n&); 函数返回剩余字符串的个数。ss 所指字符串数组中 printf(&\nEnter a string for search: &); 共有 N 个字符串,且串长小于 M。 gets(t); #include &stdio.h& n=fun(ch,t); #include &string.h& /**********found**********/ #define N 5 if(n==0) printf(&\nDon't found!\n&); #define M 10 else printf(&\nThe position is %d .\n&,n); int fun(char (*ss)[M], int k) } { int i,j=0, /**********found**********/ 15.下列给定程序中已建立了一个带头结点的单向 for(i=0; i& N ; i++) 链表,在 main 函数中将多次调用 fun 函数,每调 { len=strlen(ss[i]); 用一次,输出链表尾部结点中的数据,并释放该结 /**********found**********/ 点,使链表缩短。 if(len&= k) #include &stdio.h& /**********found**********/ #include &stdlib.h& strcpy(ss[j++],ss[i]); #define N 8 } typedef
{ } struct list * main() } SLIST; { char x[N][M]={&Beijing&,&Shanghai&,&Tianjing&,&Nanj void fun( SLIST *p) ing&,&Wuhan&}; { SLIST *t, *s; int i,f; t=p-& s=p; printf(&\nThe original string\n\n&); while(t-&next != NULL) for(i=0;i&N;i++)puts(x[i]); printf(&\n&); { s=t; f=fun(x,7); /**********found**********/ printf(&The string witch length is less than or t=t-& equal to 7 :\n&); } for(i=0; i&f; i++) puts(x[i]);printf(&\n&); /**********found**********/ } printf(& %d &,t-&data); s-&next=NULL; 13.下列给定程序中,函数 fun 的功能是:把形参 s /**********found**********/ 所指字符串中下标为奇数的字符右移到下一个奇数 free(t); 位置,最右边被移出字符串的字符绕回放到第一个 } 奇数位置,下标为偶数的字符不动(注:字符串的长 SLIST *creatlist(int *a) 度大于等于 2)。 { SLIST *h,*p,*q; 例如,形参 s 所指字符串为 abcdefgh,执行结果为 h=p=(SLIST *)malloc(sizeof(SLIST)); ahcbedgf。 for(i=0; i&N; i++) #include &stdio.h& { q=(SLIST *)malloc(sizeof(SLIST)); void fun(char *s) q-&data=a[i]; p-&next=q; p=q; { int i, n, } n=0; p-&next=0; for(i=0; s[i]!='\0'; i++) n++; /**********found**********/ } if(n%2==0) k=n-1 ; void outlist(SLIST *h) else k=n-2; { SLIST *p; /**********found**********/ p=h-& c=s[k] ; if (p==NULL) printf(&\nThe list is for(i=k-2; i&=1; i=i-2) s[i+2]=s[i]; NULL!\n&); /**********found**********/ else s[1]= { printf(&\nHead&); } do { printf(&-&%d&,p-&data); main() p=p-& } while(p!=NULL); { char s[80]=&abcdefgh&; printf(&-&End\n&); printf(&\nThe original string is : %s\n&,s); }} main() { SLIST * int a[N]={11,12,15,18,19,22,25,29}; head=creatlist(a); printf(&\nOutput from head:\n&); outlist(head); printf(&\nOutput from tail: \n&); while (head-&next != NULL){ fun(head); printf(&\n\n&); printf(&\nOutput from head again :\n&); outlist(head); } } 16.下列给定程序中,函数 fun 的功能是:逆置数组 元素中的值。例如:若 a 所指数组中的数据为:1、 2、3、4、5、6、7、8、9,则逆置后依次为:9、8、 7、6、5、4、3、2、1。形参 n 给出数组中数据的个 数。 #include &stdio.h& void fun(int a[], int n) { int i,t; /**********found**********/ for (i=0; i&n/2; i++) { t=a[i]; /**********found**********/ a[i] = a[n-1-i]; /**********found**********/ a[n-i-1] = } } main() { int b[9]={1,2,3,4,5,6,7,8,9}, printf(&\nThe original data :\n&); for (i=0; i&9; i++) printf(&%4d &, b[i]); printf(&\n&); fun(b, 9); printf(&\nThe data after invert :\n&); for (i=0; i&9; i++) printf(&%4d &, b[i]); printf(&\n&); } 17.下列给定程序的功能是:调用函数 fun 将指定源 文件中的内容复制到指定的目标文件中,复制成功 时函数返回 1,失败时返回 0。在复制的过程中,把 复制的内容输出到屏幕。主函数中源文件名放在变 量 sfname 中,目标文件名放在变量 tfname 中。 #include &stdio.h& #include &stdlib.h& int fun(char *source, char *target) { FILE *fs,* /**********found**********/ if((fs=fopen(source, &r&))==NULL) return 0; if((ft=fopen(target, &w&))==NULL) return 0; printf(&\nThe data in file :\n&); ch=fgetc(fs); /**********found**********/ while(!feof(fs)) { putchar( ch ); /**********found**********/ fputc(ch,ft); ch=fgetc(fs); } fclose(fs); fclose(ft); printf(&\n\n&); return 1; } main() { char sfname[20] =&myfile1&,tfname[20]=&myfile2&; FILE * myf=fopen(sfname,&w&); printf(&\nThe original data :\n&); for(i=1; i&30; i++){ c='A'+rand()%25;fprintf(myf,&%c&,c); printf(&%c&,c); }{ struct student s={10001,&ZhangSan&, 95, 80, 88}; } printf(&\n\nThe original data :\n&); printf(&\nNo: %ld Name: %s\nScores: 18.下列给定程序中,函数 fun 的功能是:有 N×N &,s.sno, s.name); 矩阵,根据给定的 m(m&=N)值,将每行元素中的 for (i=0; i&3; i++) printf(&%6.2f &, s.score[i]); 值均向右移动 m 个位置, 左位置为 0。 例如, N=3, printf(&\n&); m=2,有下列矩阵 fun(s); 1 2 3 } 4 5 6 7 8 9 20.下列给定程序的功能是:从键盘输入若干行字符 程序执行结果为 串(每行不超过 80 个字符), 写入文件 myfile4.txt 中, 0 0 1 用-1 作字符串输入结束的标志,然后将文件的内 0 0 4 容显示在屏幕上。 文件的读写分别由函数 ReadText 0 0 7 和 WriteText 实现。 #include &stdio.h& #include&stdio.h& #define N 4 #include&string.h& void fun(int (*t)[N], int m) #include&stdlib.h& { int i, void WriteText(FILE *); /**********found**********/ void ReadText(FILE *); for(i=0; i&N; i++ ) main() { for(j=N-1-m; j&=0; j--) { FILE * /**********found**********/ if((fp=fopen(&myfile4.txt&,&w&))==NULL) t[i][j+m]=t[i][j]; { printf(& open fail!!\n&); exit(0); } /**********found**********/ WriteText(fp); for(j=0; j&m; j++) fclose(fp); t[i][j]=0; if((fp=fopen(&myfile4.txt&,&}

我要回帖

更多关于 键盘上的竖线怎么输入 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信