1、试验九 函数一、基础能力落实:1) 设计函数 min(x,y),返回两个 double数值中较小的数值。#include double min(double a, double b);int main(void) double x, y;printf(“Enter two numbers: “);scanf(“%lf %lf“, printf(“The smaller number is %f.n“, min(x,y);return 0;double min(double a, double b)return a double add(double a, double b)return a+b
2、;double subtract(double a, double b)return a-b;double multiple(double a,double b)return a*b;int main()double num1,num2;printf(“please input tow number:”);scanf(“%lf %lf”,printf(“%5.2f + %5.2f = %5.2fn”,num1,num2,add(num1,num2);printf(“%5.2f + %5.2f = %5.2fn”,num1,num2,subtract(num1,num2);printf(“%5.
3、2f + %5.2f = %5.2fn”,num1,num2,multiple(num1,num2);3) 编写一个函数,函数的 3个参数是一个字符和两个整数。字符参数是需要输出的字符,第一个整数说明了在每行中该字符输出的个数,而第二个整数指的是需要输出的行数,编写一个调用该函数的程序。#include void chLineRow(char ch, int c, int r);int main(void) char ch;int col, row;printf(“Enter a character (# to quit): “);while ( (ch = getchar() != #)if
4、 (ch = n)continue;printf(“Enter number of columns and number of rows: “);if (scanf(“%d %d“, chLineRow(ch, col, row);printf(“nEnter next character (# to quit): “);printf(“Bye!n“);return 0;void chLineRow(char ch, int c, int r)int col, row;for (row = 0; row float p(int x,int n)float t,t1,t2;if(n= =0) r
5、eturn 1;else if(n= =1) return x;else t1=(2*n-1)*x*p(x,(n-1);t2=(n-1)*p(x,(n-2);t=(t1-t2)/n;return t;int main()int x,n;printf(“input two int (x and n):”);scanf(“%d%d”,printf(“%.2fn”,p(x,n);return 0;5)编写一个函数 taxis()实现数组的排序,在函数中调用 swap()实现两个数的交换。打印出排序结果。#include void swap(int *a,int *b)int tmp;tmp=*a;*
6、a=*b;*b=tmp;void taxis(int *a, int num)int i,j;for(i=0;i aj)swap(int main()int i,a=2,5,9,8,7,6,4,3,1;taxis(a, sizeof(a)/sizeof(int);for(i=0;iint my_strcmp(char *p,char *q)int i=0;while(*(p+i) = *(q+i)if( *(p+i) = =0 ) return 0;i+; return(*(p+i) - *(q+i);int main()int m;char str120,str220;printf(“Inp
7、ut tow strings:”);gets(str1);gets(str2);printf(“result:%dn”,my_strmp(str1,str2);return 0;2) 编写一个函数 is-within().它接受两个参数,一个是字符,另一个是字符串指针。其功能是如果字符在字符串中。就返回一个非 0值(真) ;如果字符不在字符串中,就返回0值(假) 。在一个使用循环语句为这个函数提供输入的完整程序中进行测试。#include #define LEN 80int is_within(const char * str, char c);int main(void)char input
8、LEN;char ch;int found;printf(“Enter a string: “);while (gets(input) scanf(“n%c”, found = is_within(input, ch);if (found = = 0)printf(“%c not found in string.n“, ch);elseprintf(“%c found in string %sn“, ch, input);printf(“Next string: “);puts(“Done.n“);return 0;int is_within(const char * str, char ch
9、)while (*str != ch return *str; /* = 0 if 0is reached, non-zero otherwise */3) 有一个班,有 3个学生,各四门课,计算总平均分数,以及地 n个学生的成绩。(用函数 average求平均成绩,用函数 search找出并输出第 i个学生的成绩)int main()int num;float score34=65,67,70,60,80,87,90,81,90,99,100,98;printf(“average=%5.2fn”,average(score0,12);while(1)printf(“Input a numbe
10、r(13):”);scanf(“%d”,if(num 3) | (num void larger_of(double *p1, double *p2);int main(void) double x, y;printf(“Enter two numbers (q to quit): “);while (scanf(“%lf %lf“, printf(“The modified values are %f and %f.n“, x, y);printf(“Next two values (q to quit): “);printf(“Bye!n“);return 0;void larger_of
11、(double *p1, double *p2)double temp = *p1 *p2 ? *p1 : *p2;*p1= *p2 = temp;5) 编写一个函数,其参数为一个字符串,函数删除字符串中的空格,在一个可以循环读取的程序中进行测试,直到用户输入空行,对于任何输入字符串,函数都应该使用和并可以显示结果#include #define LEN 81int drop_space(char * s);int main(void)char origLEN;while (gets(orig) puts(orig); puts(“Bye!“);return 0;int drop_space(char * s)char *p, *q;p=q=s;while ( *q != 0 )if ( *q != )*p+ = *q+;elseq+;*p = 0;6)给主函数传递参数实现 echo功能:#incldue int main(int argc,char *argv) int i=1;while( i argc )printf(“%s ”,argvi);i+;printf(“n”);return 0;