C primer plus
第九章
第1题:
#include <stdio.h>
double min(double u, double v);
int main(void)
{
double m;
double x = 5, y = 2;
m = min(x, y);
printf("%g", m);
return 0;
}
double min(double u, double v)
{
double temp;
if (u <= v)
temp = u;
else
temp = v;
return temp;
}
第2题:
#include <stdio.h>
void chline(char ch, int r, int c);
int main(void)
{
chline('a', 4, 7);
return 0;
}
void chline(char ch, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
putchar(ch);
}
putchar('\n');
}
}
第3题:这题和上一题一样,略微改动了一下,使程序更加智能一些。
#include <stdio.h>
#include <ctype.h>
void chline(char ch, int r, int c);
int get_first(void);
int main(void)
{
int c, a, b;
while ((c = get_first()) != EOF)
{
printf("For how many row and colum:");
scanf("%d %d", &a, &b);
chline(c, a, b);
}
return 0;
}
void chline(char ch, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
putchar(ch);
}
putchar('\n');
}
}
int get_first(void)
{
int ch;
printf("Please enter the charcter you wanto print (can't print blank):");
do
{
ch = getchar();
} while (isspace(ch));
while (getchar() != '\n')
continue;
return ch;
}
第4题:
#include <stdio.h>
double h_ave(double u, double v);
int main(void)
{
double x, y, r;
printf("Please enter two numbers:");
scanf("%lf %lf", &x, &y); #注:接受数据需要用对应数值类型,double需要用lf 而不是 f或者g。
r = h_ave(x, y);
printf("The harmonic average for %g and %g is %g", x, y, r);
return 0;
}
double h_ave(double u, double v)
{
double reslut;
reslut = 1 / ((1 / u + 1 / v) / 2);
return reslut;
}
第5题:#注:如果需要改变主函数中两个以上的实参,子函数中就需要用到指针
#include <stdio.h>
void larger_of(double* u, double* v);
int main(void)
{
double x, y;
printf("Now enter:");
scanf("%lf %lf", &x, &y);
larger_of(&x, &y);
printf("Now x is %g, y is %g", x, y);
}
void larger_of(double* u, double* v)
{
if (*u < *v)
*u = *v;
else
*v = *u;
}
第6题:
#include <stdio.h>
void og_p(double* u, double* v, double* w);
int main(void)
{
double x, y, z;
printf("Now enter:");
scanf("%lf %lf %lf", &x, &y, &z);
og_p(&x, &y, &z);
printf("Now x is %g, y is %g, z is %g", x, y, z);
return 0;
}
void og_p(double* u, double* v, double* w)
{
double temp;
if (*v < *u && *v < *w && *u< *w) //low to high: v u w
{
temp = *u;
*u = *v;
*v = temp;
}
if(*w<*u && *w<*v && *u < *v)//low to high w u v
{
temp = *w;
*v = *u;
*u = temp;
*w = *v;
}
if (*v < *u && *v<*w && *u>*w)//low to high v w u
{
temp = *u;
*u = *v;
*v = *w;
*w = temp;
}
if (*u<*w && *w<*v && *u<*v) //low to high u w v
{
temp = *w;
*w = *v;
*v = temp;
}
if (*w < *u && *w < *v && *v < *u) //low to high w v u
{
temp = *u;
*u = *w;
*w = temp;
}
}
第7题:
#include<stdio.h>
#include<ctype.h>
int check(int ch);
int main(void)
{
int c;
int position;
printf("Please enter:");
while ((c = getchar()) != EOF)
{
if (c == '\n') #注:可以去掉末尾的'\n'
continue;
position = check(c);
if (position)
{
printf("%c is a alphabet and it's position is %d\n", c, position);
}
else
printf("%c is not a char.\n", c);
}
printf("done!");
return 0;
}
int check(int ch)
{
if (isalpha(ch))
{
if (islower(ch))
return ch - 'a' + 1;
else if (isupper(ch))
return ch - 'A' + 1;
}
else
return 0;
}
加油!