全国计算机二级C语言程序设计讲义 习题_修改课件

上传人:我*** 文档编号:141398696 上传时间:2020-08-07 格式:PPT 页数:53 大小:82KB
返回 下载 相关 举报
全国计算机二级C语言程序设计讲义 习题_修改课件_第1页
第1页 / 共53页
全国计算机二级C语言程序设计讲义 习题_修改课件_第2页
第2页 / 共53页
全国计算机二级C语言程序设计讲义 习题_修改课件_第3页
第3页 / 共53页
全国计算机二级C语言程序设计讲义 习题_修改课件_第4页
第4页 / 共53页
全国计算机二级C语言程序设计讲义 习题_修改课件_第5页
第5页 / 共53页
点击查看更多>>
资源描述

《全国计算机二级C语言程序设计讲义 习题_修改课件》由会员分享,可在线阅读,更多相关《全国计算机二级C语言程序设计讲义 习题_修改课件(53页珍藏版)》请在金锄头文库上搜索。

1、习 题,练习1 程序利用函数sort( )对一组数按照从小到大顺序排序,请编写sort( )函数。,#include stdio.h void sort(int x ,int n) void main( ) int a10=2,14,44,1,22,16,10,9,28,6; int i; sort(a,10); for (i = 0 ; i 10 ; i+) printf(%d ,ai); printf(n); ,void sort(int x,int n) int i , j; int t; for (i=0;ixj) t = xi; xi = xj; xj = t; ,练习2 程序利用函

2、数average( )求一组数去掉一个最大数和去掉一个最小数后的平均数,请编写average( )函数。,#define N 10 float average(float x ,int n) void main( ) float aN=2,14.5,44.7,1,22.2,16,10.6,9,28.3,6; float aver; aver = average(a,N); printf(average = %f ,aver); ,float average(float x ,int n) float max,min,aver,sum; int i; sum = max = min = x0;

3、for( i=1;i xi) min = xi; aver = (sum - max - min) / (n-2); return aver; ,练习3 程序利用函数primecount( )求一组数中的质数个数,请编写primecount( )函数。,#include stdio.h #include math.h int primecount(int x ,int n) void main() int a10=133,141,44,17,22,16,109,9,27,61; int count; count = primecount(a,10); printf(Prime Count =%

4、dn,count); ,int primecount(int x ,int n) int count = 0; int flag ; /*是否为质数标志,0:否;1:是*/ int i,j,k; for (i=0;in;i+) k=sqrt(xi); flag = 1; for (j=2 ; j=k; j+) if (xi % j = 0) flag = 0; if (flag = 1) count +; return count; ,练习4程序利用函数primesum( )求一组数中所有质数的和,请编写primesum( )函数。,#include stdio.h #include math

5、.h int primesum(int x ,int n) void main( ) int a10=13,7,44,17,22,16,109,9,27,61; int sum; sum = sumcount(a,10); printf(Prime Sum =%dn,sum); ,int primesum(int x,int n) int sum = 0; int flag ; /*是否为质数标志,0:否;1:是*/ int i,j,k; for (i=0;in;i+) k=sqrt(xi); flag = 1; for (j=2 ; j=k; j+) if (xi % j = 0) flag

6、 = 0; if (flag = 1) sum += xi; return sum; ,练习6程序利用函数strcat(str1,str2)将str1,str2两个字符串合并到str1中,请编写strcat( )函数。,#include stdio.h void strcat(char * str1,char * str2) void main( ) char *str1 = Welcome ; char *str2 = Everyone!; strcat(str1,str2); puts(str1); ,void strcat(char * str1, char * str2) int i

7、= 0,j=0; while(*(str1+i) != 0) i+; while(*(str2+j) != 0) *(str1 + i) = *(str2 +j); i +;j+; *(str1+i) = 0; ,练习7程序利用函数strcpy(str1,str2)将字符串str2拷贝到字符串str1,请编写strpy( )函数。,#include stdio.h void strcpy(char * str1, char * str2) void main( ) char *str1 = Welcome ; char *str2 = Everyone!; strcpy(str1,str2);

8、 puts(str1); ,void strcpy(char * str1, char * str2) int i = 0; while(*(str2+i) != 0) *(str1 + i) = *(str2 +i); i +; *(str1+i) = 0; ,练习8 程序利用函数strcmp(str1,str2)比较两个字符串str1,str2的大小,如果,str1str2, 返回1; str1=str2,返回0; str1str2,返回-1.请编写strcat( )函数。,#include stdio.h int strcmp(char * str1,char * str2) void

9、main() char *str1 = Welcome ; char *str2 = Everyone!; int a; a = strcmp(str1,str2); switch (a) case 1:printf(Str1Str2.n);break; case 0:printf(Str1=Str2.n);break; case -1:printf(Str1Str2.n);break;,int strcmp( char * str1, char * str2) int i = 0; while( (*(str1+i) != 0) ,练习9程序利用函数insert( ) 向一组有序的数中插入一

10、个数x,请编写insert( )函数。,#define N 11 void insert(int a ,int x, int n) void main( ) int aN=2,6,11,15,19,24,30,33,40,55; int x; int i; printf(Please input x:); scanf(%d, ,void insert(int a,int x,int n) int location;/x应插入的位置 int i; /定位 for(location=0; alocation location ; i- -) ai = ai-1; /插入数 alocation =

11、x; ,练习10 程序利用函数count( )统计输入的一个字符串中包含多少个单词,请编写count( )函数。,#include stdio.h int count(char str) void main( ) char str100; int wordcount; gets(str); wordcount = count(str); printf(The Number of word is %dn,wordcount); ,int count(char str ) int wordnum = 0; int word = 0; int i ; for( i = 0 ; stri!=0; i+

12、) if (stri = )word = 0; else if ( word = 0) wordnum +; word = 1; return wordnum; ,练习11 程序利用函数count( )统计输入的一个字符串中包含多少个字母,请编写count( )函数。,#include stdio.h int count(char str ) void main() char str100; int wordcount; gets(str); wordcount = count(str); printf(The Number of word is %dn,wordcount); ,int co

13、unt(char str ) int num = 0; int i; for( i=0; stri!=0; i+) if( (stri=a ,练习12程序利用函数count( )统计输入的一个字符串中包含多少个数字字符,请编写count( )函数。,#include stdio.h int count(char str ) void main( ) char str100; int wordcount; gets(str); wordcount = count(str); printf(The Number of word is %dn,wordcount); ,int count(char

14、str ) int num = 0; int i; for( i=0; stri!=0; i+) if(stri=0 ,练习13程序利用函数upper( )将字符串中所有单词的第一个字符变成大写字母,请编写upper( )函数。,#include stdio.h void upper(char str ) void main() char str100; int count; printf(Please input a string:); gets(str); upper(str); printf(The new String is: %sn,str); ,void upper(char st

15、r ) int word = 0; int i = 0; for( i = 0 ; stri!=0; i+) if (stri = ) word = 0; elseif ( word = 0) if(stri=a ,练习14程序利用函数change( )将10进制数转换成2进制数输出,请编写change( )函数。,#include stdio.h int change(int a,int x ) void main( ) int a, length, i; int x32; printf(Please Input a number:); scanf(%d, ,int change(int a

16、,int x ) int i=0; while( a 0) xi = a % 2; a = a/2; i +; return i; ,练习15程序利用函数invert( )使输入的字符串反序存放,请编写invert( )函数。,#include stdio.h void invert(char x ) void main( ) char str80; printf(Please input a String:); gets(str); invert(str); printf(Inverted String is :%s ,str); printf(n); ,void invert(char x ) int i,j; char ch; for(i=0, j=strlen(x)-1; ij; i+, j- - ) ch = xi; xi = xj; xj = ch; ,练习16程序中函数fun的功能是:计算n!。例如,给n输入5,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号