c语言程序设计项目教程教学课件ppt作者王瑞红c语言程序设计项目教程习题答案内容摘要:

int a[]={1,2,3,4},i,s=0,j=1。 for(i=3。 i=0。 i) { s=s+a[i]*j。 j=j*10。 } printf(“s=%d\n”,s)。 } 程序的运行结果 : ( 2) include void main( ) { int i,j,s=0。 int a[3][3]={1,2,3,4,5,6,7,8,9}。 s=0。 for(i=0。 i3。 i++) for(j=0。 j3。 j++) s=s+a[i][i]。 printf(s=%d\n,s)。 } 程序的运行结果 : ( 3) include void main( ) { char ch[7]={65ab21}。 int i,s=0。 for(i=0。 ch[i]=39。 039。 amp。 amp。 ch[i]=39。 939。 i+=2) s=10*s+ch[i]39。 039。 printf(%d\n,s)。 } 程序的运行结果 : ( 4)运行时输入“ Hello!” include void main( ) { char ss[10]=1,2,3,4,5。 gets(ss)。 strcat(ss,6789)。 printf(%s\n,ss)。 } 程序的运行结果 : ( 5) include include void main( ) { char arr[2][4]。 strcpy(arr[0],you)。 strcpy(arr[1],me)。 arr[0][3]=39。 amp。 39。 printf(%s\n,arr[0])。 } 2. 编程实现以下功能 ( 1) 输入一个数,插入到某升序排列的一维数组中,使插入后的数组仍然升序。 include void main() { int a[11]={1,4,6,9,13,16,19,28,40,100}。 int num,i,j。 printf(数组 a:\n)。 for(i=0。 i10。 i++) printf(%5d,a[i])。 printf(\n)。 printf(插入一个整数 :)。 scanf(%d,amp。 num)。 if(numa[9]) a[10]=num。 /*num 比 a[9]大则 num 插入到最后 */ else { for(i=0。 i10。 i++) /*如果 num 比 a[9]小则从第一个数开始比较 */ if(a[i]num) /*只到 num 比 a[i]小则将 num 插入到 a[i]*/ { for(j=9。 j=i。 j) /*要把 num 插入到 a[i]就必须把 a[i]到 a[9]的所有数依次后移一位 */ a[j+1]=a[j]。 a[i]=num。 break。 } } printf(插入后的数组 a:\n)。 for(i=0。 i11。 i++) printf(%5d,a[i])。 printf(\n)。 } ( 2)从键盘上输入一行由小写英文组成的字符串,用置换法(置换规律:按字母表逆序)对其加密。 include void main() { char list[27]=zyxwvutsrqponmlkjihgfedcba。 char str[80]。 int i,j。 printf(请输入一小写字母串(长度小于 80): )。 scanf(%s,amp。 str)。 i=0。 while(str[i]!=39。 \039。 ) { j=str[i]97。 str[i]=list[j]。 i++。 } printf(加密成为: %s\n,str)。 } ( 3)输入一行简单英文句子,统计其中单词的个数。 include void main() { char s[80],c。 int i,n=0,word=0。 printf(请输入一句英语: )。 gets(s)。 for(i=0。 (c=s[i])!=39。 \039。 i++) if(c==39。 39。 ) word=0。 else if(word==0) { word=1。 n++。 } printf(这个句子里包含 %d 个单词。 \n,n)。 } /*单词的数目可以由空格出现的次数决定,连续若干个空格作为出现一次空格;一行开头的空格不统计在内 .如果测出某一个字符为非空格,它的前面的字符是空格,则表示新的单词开始了,此时 n++.若当前字符为非空格而其前面的字符也是非 空格,则意味着仍然是原来那个单词的继续, n 不加 word=0,则表示前一个字符是空格; .若 word=1,则表示前一个字符不是空格; */ ( 4)编写程序,输入 10 个整数放入数组 a 中,求数组中最小值 min 及其下标 k 并输出。 include void main() { int a[10],i,min,k。 printf(请输入 10 个整数 :)。 for(i=0。 i10。 i++) scanf(%d,amp。 a[i])。 min=a[0]。 k=0。 for(i=1。 i10。 i++) { if(a[i]min) { min=a[i]。 k=i。 } } for(i=0。 i10。 i++) printf(%d ,a[i])。 printf(\n 最小值是 %d,其下标是 %d\n,min,k)。 } ( 5) 求一个 3*3 矩阵对角线元素之和。 include void main() { int a[3][3],i,j,s=0。 printf(请输入一个 3x3 的 矩阵 :)。 for(i=0。 i3。 i++) for(j=0。 j3。 j++) scanf(%d,amp。 a[i][j])。 for(i=1。 i10。 i++) { for(j=0。 j3。 j++) { if(i==j) s=s+a[i][j]。 } } for(i=0。 i3。 i++) { for(j=0。 j3。 j++) printf(%4d,a[i][j])。 printf(\n)。 } printf(\n 对角线之和是 :%d\n,s)。 } ( 6) 编程 打印出 图 52 所示的 杨辉三角形。 (10 行 ) 规律:除两侧元素均为 1 之外,其余每个位置上的元素的值为其左上角元素与其正上方元素之和。 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 图 52 杨辉三角形 include define N 10 void main() { int i,j,n。 int a[N][N]。 a[0][0]=1。 a[1][0]=1。 a[1][1]=1。 for (i=2。 iN。 i++){ for (j=0。 j=i。 j++) { if (j==0) a[i][j]=1。 else if (i==j) a[i][j]=1。 else a[i][j]=a[i1][j1]+a[i1][j]。 } } for (i=0。 iN。 i++) { for (j=0。 j=i。 j++) printf(%d\t,a[i][j])。 printf(\n)。 } } 模块 6 函数 1. 程序填空题 (请按要求填空,补充以下程序 ) ( 1) 下列程序功能是统计从键盘上输入的字符中大写字母的个数 , 输入时用 “*”作为输入结束标志。 include include void main( ) { char c1。 int count=0。 scanf(%c,amp。 c1)。 while(( c1 ) !=’*’) { if (isupper(c1)) count++。 scanf(%c,amp。 c1)。 } printf(“ %d\n ” ,count)。 } ( 2)下列程序的功能是求 10~ 1000 之间的所有素数。 include void main( ) { int i。 for (i=10。 i=1000)。 i++) if (isprime( i )) printf(“%d, ”,i)。 printf(“\n”)。 } include isprime(int n) { int i。 for (i=2。 i=sqrt(n)。 i++) if(n%i==0) return( 0 )。 return( 1 )。 } 2. 分析下列程序的运行结果 ( 1) include int d=1。 f(int p) { int d=1。 d+=p++。 } void main() { int a=5。 f(a)。 d+=a++。 printf(“%d\n”,d)。 } 程序的运行结果 : ( 2) include void main( ) { int k=4,m=1,p。 p=fun(k,m)。 printf(%d ,p)。 p=fun(k,m)。 printf(“%d”,p)。 } fun(int a,int b) { static int m=0,i=2。 i+=m+1。 m=i+a+b。 return (m)。 } 程序的运行结果 : ( 3) include void main( ) { int a=2,i。 for (i=0。 i3。 i++)。 printf(“%d”,func(a))。 } func(int a) { int b=0。 static c=3。 b++。 c++。 return (a+b+c)。 } 程序的运行结果 : 3. 编程实现以下功能 ( 1)编写一个函数,统计一个字符串中所含字母、数字、空格和其他字符的个数。 include include int letter,digit,space,other。 void count(char str[])。 main() { char str[100]。 letter=digit=spac。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。