c语言例题

上传人:luoxia****01812 文档编号:46317231 上传时间:2018-06-25 格式:DOC 页数:11 大小:73.50KB
返回 下载 相关 举报
c语言例题_第1页
第1页 / 共11页
c语言例题_第2页
第2页 / 共11页
c语言例题_第3页
第3页 / 共11页
c语言例题_第4页
第4页 / 共11页
c语言例题_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《c语言例题》由会员分享,可在线阅读,更多相关《c语言例题(11页珍藏版)》请在金锄头文库上搜索。

1、C 语言例题语言例题第第 1 页页 共共 11 页页习题 4.12 编程计算 1!+2!+3!+4!+5!+6!+7!+8!+9!+10!的值。 #include main() long term = 1,sum = 0; int i; for (i = 1; i int FindMax(int num, int n, int *pMaxPos); int FindMin(int num, int n, int *pMinPos); main() int num10, maxValue, maxPos, minValue, minPos, i; printf(“Input 10 numbers

2、:n “); for (i=0; i max) max = numi; *pMaxPos = i; return max ; /* 函数功能: 求 n 个数中的最小值及其所在下标位置 函数入口参数: 整型数组 num,存储 n 个整数整型变量 n,表示数组元素个数 函数出口参数: 整型指针变量 pMinPos,指向的地址单元存储最小值在数组中的下标 位置 函数返回值: 最小值*/ int FindMin(int num, int n, int *pMinPos) int i, min; min = num0; /*假设 num0为最小*/ *pMinPos = 0; /*假设最小值在数组中的下

3、标位置为 0 */ for (i = 1;i #include main() intn = 1, count =1; doublee = 1.0, term = 1.0; long fac = 1;for (n=1; fabs(term) = 1e-5; n+) fac = fac * n; term = 1.0 / fac; e = e + term; count+; printf(“e = %f, count = %dn“, e, count); 习题 4.18 打印所有的“水仙花数” 。所谓“水仙花数” ,是指一个三位数,其各位数字的立方和等于 该数本身。例如,153 是“水仙花数” ,

4、因为 153=1 #include main() int i, j, k, n;printf(“result is:“); for (n = 100; n main() int n, ret; printf(“Input n:“); scanf(“%d“, ret = IsPrimeNumber(n); if (ret != 0) printf(“%d is a prime numbern“,n); else printf(“%d is not a prime numbern“,n); /*函数功能: 判断参数是否是素数函数入口参数: number 为整型数,要求为正整数函数返回值: 非 0

5、值表示是素数,否则不是素数 */ int IsPrimeNumber(int number) int i;if (number int MaxCommonFactor(int a, int b); main() int a, b, x; printf(“Input a,b:“); scanf(“%d,%d“,x = MaxCommonFactor(a,b); printf(“MaxCommonFactor = %dn“, x); /*函数功能: 计算两个正整数的最大公约数函数入口参数:两个整型数函数返回值: 最大公约数 */ int MaxCommonFactor(int a, int b)

6、int r; do r = a % b; a = b; b = r; while (r != 0); return a; 习题 4.12 编程计算 1!+2!+3!+4!+5!+6!+7!+8!+9!+10!的值。 #include main() long term = 1,sum = 0; int i; for (i = 1; i 习题 6.5 #define ARR_SIZE 10 /* 函数功能: 找出 n 个数中的最大数与最小数并将其位置对换 函数参数: 整型数组 a, 存放待处理数据 整型变量 n,为数据个数 返回值: 无 */ void MaxMinExchang(int a, i

7、nt n) int maxValue = a0, minValue = a0, maxPos = 0, minPos = 0; int i, temp; for (i=1; i maxValue) maxValue = ai;maxPos = i; if (ai #include #define ARR_SIZE 80void Inverse(char str, char ptr);main() char aARR_SIZE, bARR_SIZE;printf(“Please enter a string: “);gets(a);Inverse(a, b);printf(“The invers

8、ed string is: “); puts(b); /*函数功能: 实现将字符数组中的字符串逆序存放函数参数: 字符数组 a,存放源字符串字符数组 b,存放逆序字符串 函数返回值:无 */ void Inverse(char str, char ptr) int i = 0, j;j = strlen(str) - 1;while (stri != 0)ptrj = stri;i+; j-;ptri=0; C 语言例题语言例题第第 8 页页 共共 11 页页习题 7.8 #include #include void Inverse(char *pStr);main() char str80;

9、printf(“Input a string:n“); gets(str);/*输入字符串*/Inverse(str);/*将存于 str 数组中的字符串逆序存放*/printf(“The inversed string is:n“); puts(str);/*输出字符串*/ /*函数功能: 实现字符串逆序存放函数参数: 字符指针变量,所指向的存储单元存放源字符串,逆序后的字符串也存放于 此返回值: 无 */ void Inverse(char *pStr) int len; char temp; char *pStart;/*指针变量 pStart 指向字符串的第一个字符*/ char *p

10、End;/*指针变量 pEnd 指向字符串的最后一个字符*/len = strlen(pStr);/*求出字符串长度*/ for (pStart=pStr,pEnd=pStr+len-1; pStartconst float PI = 3.14main() float r ; /*r 为半径变量*/ float s;printf(“Input r:“); scanf(“%f“, s = PI*r*r,printf(“s=%.2fn“, s); 计算圆的面积和周长和面积 #include #define PI 3.14main() float r ; /*r 为半径变量*/ float circ

11、um, area;printf(“Input r:“); scanf(“%f“, circum = 2*PI*r; area = PI*r*r,printf(“circum=%.2f,area=%.2fn“, circum, area); C 语言例题语言例题第第 10 页页 共共 11 页页二、从键盘输入三角形的三边长 a,b,c 计算并输出三角形的面积 area, (要求定义为 float 型,输出结果保留两位小数) #include #include main() float a, b, c; /*a,b,c 为三边变量*/ float s, area; printf(“Input a,

12、b,c:“); scanf(“%f,%f,%f“, s = 1.0 / 2 * (a + b + c); area = sqrt(s * (s - a) * (s - b) * (s - c); printf(“area=%.2fn“, area); 五、计算两给正整数的最大公约数,求最大公约数的过程用子函数来实现 #include int MaxCommonFactor(int a, int b); main() int a, b, x; printf(“Input a,b:“); scanf(“%d,%d“, x = MaxCommonFactor(a,b); printf(“MinCommonMultiple = %dn“, x); /* 函数功能: 计算两个正整数的最大公约数函数入口参数:两个整型数函数返回值: 最大公约数;-1 表示没有最大公约数*/ int MaxCommonFactor(int a, int b) if (a b) a = a - b; else if (b a) b = b - a; return a;C 语言例题语言例题第第 11 页页 共共 11 页页

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 资格认证/考试 > 计算机等级考试

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