1、设计一个函数min(i,j),返回两个double类型值的较小值,在一个简单的驱动程序中测试该函数。
#include<stdio.h>
double min(double i, double j);
int main(void)
{
double index;
double i, j;
char ch;
printf("Input two values:");
while (scanf("%lf,%lf", &i, &j) != 2)
{
while ((ch = getchar() )!= '\n')
continue;
printf("Enter two values:");
}
index = min(i, j);
printf("The smaller values is: %lf\n", index);
return 0;
}
double min(double i, double j)
{
double index;
index = (i<=j) ? i : j;
return index;
}
2、设计一个函数chline(ch,i,j),打印指定的字符j行i列。在一个简单的驱动程序中测试该函数。
#include <stdio.h>
void chline(void);
int get_number(void);
int main(void)
{
chline();
return 0;
}
void chline(void)
{
int i, j;
int num1, num2;
char index;
printf("Input character:");
index=getchar();
printf("Enter number:");
i = get_number();
if (i <= 0)
{
printf("Enter number:");
i = get_number();
}
printf("Enter number:");
j = get_number();
if (j <= 0)
{
printf("Enter number:");
j = get_number();
}
for (num1 = 0; num1 < i; num1++)
{
for (num2 = 0; num2 < j; num2++)
printf("%c", index);
printf("\n");
}
}
int get_number(void)
{
int index;
while (scanf("%d", &index) != 1)
{
while (getchar() != '\n')
continue;
printf("Re-enter number:");
}
return index;
}
3、编写一个函数,接受3个参数:一个字符和两个整数。字符参数是待打印的字符,第一个整数指定一行中打印字符的次数,第二行整数参数指定打印字符的行数。编写一个调用该函数的程序。
#include <stdio.h>
char get_character(void);//获取输入的字符
void chline(char ch, int i, int j);//ch:要打印的字符,i、j:打印字符的列数、行数
int get_number(void);//获取整数
int main(void)
{
char ch;
int i, j;
printf("Enter a character:");
ch = get_character();
printf("Enter a number:");
i = get_number();
if (i <= 0)
{
printf("Re-enter a number:");
i = get_number();
}
printf("Enter a number:");
j = get_number();
if (j <= 0)
{
printf("Re-enter a number:");
j = get_number();
}
chline(ch, i, j);
return 0;
}
int get_number(void)
{
int input;
while (scanf("%d", &input) != 1)
{
while (getchar() != '\n')
continue;
printf("Re-enter a number:");
}
return input;
}
void chline(char ch, int i, int j)//ch字符,i列数,j行数
{
int index1, index2;
for (index1 = 0;index1 < j; index1++)
{
for (index2 = 0; index2 < i; index2++)
putchar(ch);
printf("\n");
}
}
char get_character(void)
{
char ch;
char input;
input = getchar();
while ((ch = getchar()) != '\n')
continue;
return input;
}
4、两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数的平均值,最后取计算结果的倒数。编写一个函数,接受两个double类型的参数,返回这两个参数的调和平均数。
#include <stdio.h>
double tiaohe(double i, double j);
double get_number(void);
int main(void)
{
double index1, index2,sum;
printf("Input the first non-zero parameter:");
index1 = get_number();
if (index1 == 0)
{
printf("Input the first non-zero parameter:");
index1 = get_number();
}
printf("Input the second non-zero parameter:");
index2=get_number();
while (index2 + index1 == 0 || index2 == 0)
{
printf("Input the second non-zero parameter:");
index2 = get_number();
}
sum=tiaohe(index1, index2);
printf("sum=%.2lf\n", sum);
return 0;
}
double tiaohe(double i, double j)
{
double sum;
sum = 1 / (1 / i + 1 / j);
return sum;
}
double get_number(void)
{
double input;
while (scanf("%lf", &input) != 1)
{
while (getchar() != '\n')
continue;
printf("Re-enter the non-zero parameter:");
}
return input;
}
5、 编写测试一个函数large_of(),该函数把两个double类型变量的值替换为较大的值。例如large_of(x,y)会把x和y中较大的值重新赋给两个变量。
#include<stdio.h>
void large_of(double *x, double *j);
int main(void)
{
double index1, index2;
printf("Enter two parameter:");
while (scanf_s("%lf,%lf", &index1, &index2) != 2)
{
scanf_s("%*s");
printf("Now, re-enter two parameter:");
}
large_of(&index1, &index2);
printf("index1=%lf,index2=%lf\n",index1,index2 );
return 0;
}
void large_of(double *x, double *j)
{
*x = (*x > *j) ? *x : *j;
*j = *x;
}
6、编写并测试一个函数,该函数以3个double变量的地址作为参数,把最小值放入第1个变量,中间值放入第2个变量,最大值放入第3个变量。
#include<stdio.h>
void sort(double *a, double *b, double *c);
int main(void)
{
double a, b, c;
printf("Input three parameters:");
while (scanf_s("%lf,%lf,%lf", &a, &b, &c) != 3)
{
scanf_s("%*s");
printf("Re-enter three parameters:");
}
sort(&a, &b, &c);
printf("a=%.3lf,b=%.3lf,c=%.3lf\n", a, b, c);
return 0;
}
void sort(double *a, double *b, double *c)
{
double temp;
if (*a > *b)
{
temp = *a;
*a = *b;
*b = temp;
}
if(*a>*c)
{
temp = *a;
*a = *c;
*c = temp;
}
if (*b > *c)
{
temp = *b;
*b = *c;
*c = temp;
}
}
7、编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告每个字符是否是字母。如果是,还要报告该字母在字母表中的数值位置。例如,c和C在字母表中的位置都是3。合并一个函数,一个字符作为参数,如果该字符是一个字母,则返回一个数值位置,否则返回-1。
#include <stdio.h>
#include<ctype.h>
int alpha(char ch);
int main(void)
{
char ch;
int num;
printf("Input character:");
while ((ch = getchar())!= '#')
{
num = alpha(ch);
if(num>0)
{
printf("%c is alpha.\n", ch);
printf("The numeric position of letters in the alphabet is %d\n", num);
}
}
return 0;
}
int alpha(char ch)
{
int input;
if (ch >= 'a'&&ch <= 'z')
input = ch - 'a' + 1;
else if (ch >= 'A'&&ch <= 'Z')
input = ch - 'A' + 1;
else
input = -1;
return input;
}
8、第6章的程序清单6.20中,power()函数返回一个double类型的正整数次幂。改进该函数,使其能正确计算负幂。另外函数要处理0的任何次幂都是0,任何数的0次幂都是1(函数应报告0的0次幂未定义,因此把该值处理为1)。要使用一个循环,并在程序中测试该函数。
在这里插入代码片
9、使用递归函数重写编程练习8。
在这里插入代码片
10、为了让程序清单9.8中的to_binary()函数更通用,编写一个to_base_n()函数接受两个参数,且第2个参数是2~10范围内的参数,然后第2个参数中指定的进制打印第1个参数的数值。例如,to_base_n(129,8)显示的结果是201,也就是129的八进制数。在一个完整的程序中测试该函数。
在这里插入代码片
11、编写并测试Fibonacci()函数,该函数用循环代替递归计算斐波那契数。
在这里插入代码片