提示
8—11题是递归部分,为赶进度暂时不做,后面有时间再补上,持续更新。
1、设计一个函数min(x, y),返回两个double类型值的较小值。在一个简单的驱动程序中测试该函数。
// 9.1
#include<stdio.h>
double min(double x, double y);
int main(void)
{
double num1, num2;
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
printf("The minist number is %g", min(num1, num2));
}
double min(double x, double y)
{
return x < y ? x : y;
}
2、 设计一个函数chline(ch, i, j),打印指定的字符j行i列。在一个简单的驱动程序中测试该函数
// 9.2
#include<stdio.h>
void chline(ch, i, j);
int main(void)
{
char ch;
int row, column;
printf("Enter a character: ");
ch = getchar();
printf("Enter two integer: ");
scanf("%d %d", &row, &column);
chline(ch, row, column);
}
void chline(ch, i, j)
{
for(int row = 0; row < i; row++){
for(int column = 0; column < j; column++)
putchar(ch);
putchar('\n');
}
}
3、编写一个函数,接受3个参数:一个字符和两个整数。字符参数是待打印的字符,第1个整数指定一行中打印字符的次数,第2个整数指定打印指定字符的行数。编写一个调用该函数的程序。
和第二题一样,就不做了。
4、两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数的平均值,最后取计算结果的倒数。编写一个函数,接受两个double类型的参数,返回这两个参数的调和平均数。
和第一题差不多。
5、编写并测试一个函数larger_of(),该函数把两个double类型变量的值替换为较大的值。例如, larger_of(x, y)会把x和y中较大的值重新赋给两个变量。
// 9.5
#include<stdio.h>
void larger_of(double * num1, double * num2);
int main(void)
{
double num1, num2;
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
printf("num1 = %g, num2 = %g\n", num1, num2);
larger_of(&num1, &num2);
printf("num1 = %g, num2 = %g", num1, num2);
}
// void larger_of(double * num1, double * num2)
// {
// if(*num1 >= *num2)
// *num2 = *num1;
// else
// *num1 = *num2;
// }
void larger_of(double * num1, double * num2)
{
*num1 > *num2 ? (*num2 = *num1) : (*num1 = *num2)
}
6、编写并测试一个函数,该函数以3个double变量的地址作为参数,把最小值放入第1个变量,中间值放入第2个变量,最大值放入第3个变量。
// 9.6
#include<stdio.h>
void sort_of(double * num1, double * num2, double * num3);
int main(void)
{
double num1, num2, num3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &num1, &num2, &num3);
printf("num1 = %g, num2 = %g, num3 = %g\n", num1, num2, num3);
sort_of(&num1, &num2, &num3);
printf("num1 = %g, num2 = %g, num3 = %g\n", num1, num2, num3);
}
void sort_of(double * num1, double * num2, double * num3)
{
double max, min, sum;
sum = *num1 + *num2 + *num3;
//找出最大值
*num1 > *num2 ? (max = *num1) : (max = *num2);
if(*num3 > max)
max = *num3;
//找出最小值
*num1 < *num2 ? (min = *num1) : (min = *num2);
if(*num3 < min)
min = *num3;
*num1 = min;
*num3 = max;
*num2 = sum - max - min;
}
7、编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告每个字符是否是字母。如果是,还要报告该字母在字母表中的数值位置。例如,c和C在字母表中的位置都是3。合并一个函数,以一个字符作为参数,如果该字符是一个字母则返回一个数值位置,否则返回-1。
// 9.7
#include<stdio.h>
//#include<ctype.h>
int letter_location(char ch);
int main(void)
{
char ch;
int res;
printf("Enter a character: ");
while((ch = getchar()) != EOF){
// ch = getchar();
// res = letter_location(ch);
if(-1 == (res = letter_location(ch)))
printf("%c is not a letter.\n", ch);
else
printf("%c: %d\n", ch, res + 1);
printf("Enter a character: ");
while(getchar() != '\n')
continue;
}
}
int letter_location(char ch)
{
int ans;
if(ch >= 'a' && ch <= 'z' )
ans = ch - 'a';
else if(ch >= 'A' && ch <= 'Z')
ans = ch - 'A';
else
ans = -1;
return ans;
}
//或者直接用 ctype.h 函数
// int letter_location(char ch)
// {
// int ans;
// if(isalpha(ch))
// ans = tolower(ch) - 'a' + 1;
// else
// ans = -1;
// return ans;
// }
文章评论