9.11.1
#include<stdio.h>
double min(double x, double y);
int main()
{
double x ,y;
printf("请输入两个double类型的数");
scanf_s("%lf %lf", &x, &y);
printf("你输入的 %g and %g. The min is %g",x, y ,min(x, y));
return 0;
}
double min(double x, double y)
{
//if (x < y)
//{
// return x;
//}
//else
// return y;
return x < y ? x : y;
}
9.11.2
#include<stdio.h>
void chline(char ch, int cols, int rows);//注意顺序
int main(void)
{
char c;
int i, j;
printf("输入你想要输入的char字符:");
scanf_s("%c", &c);
printf("输入你想要输入的cols and rows:");
scanf_s("%d %d",&j, &i);
chline(c, i, j);
return 0;
}
void chline(char c, int i, int j)
{
for (int n = 0; n < j; n++)//行
{
for (int m = 0; m < i; m ++)//列
{
printf(" %c", c);
}
printf("\n");
}
}
#include <stdio.h>
#include <ctype.h>
void chline(char ch, int cols, int rows);
int main(void)
{
char c;
int a, i, j;
printf("请输入一个字符:\n");
do
{
c = getchar();
} while (isspace(c));
while (getchar() != '\n')
{
continue;
}
printf("请输入两个整数:\n");
while (scanf("%d %d", &j, &i) == 2)
{
if (j <= 0|| i <= 0)//对输入的数字大小判断
{
printf("输入有误,重新输入。\n");
continue;
}
printf("打印%c 的%d 行 %d列", c, j ,i);
chline(c, j, i);
printf("你还可以再次输入两个整数(或按q退出).\n");
}
printf("结束。\n");
return 0;
}
void chline(char c, int j, int i)
{
for (int n = 0; n < j; n++)
{
for (int m = 0; m < i; m++)
{
putchar(c);
}
puts("\n");
}
return;
}
9.11.3
#include<stdio.h>
#include<ctype.h>
void chline(char c, int i, int j);
int main()
{
int w;
char str[] ="123c @# FD\tsP[e?\n";
for(w = 0; str[w] != 0; w++)
{
if(isspace(str[w]))
printf("str[%d]isawhite-spacecharacter:%d\n", w, str[w]);//不是特殊字符就略过
}
int a, i, j;
printf("请输入字符:\n");
do
{
a = getchar();
} while (isspace(a));
while (getchar() != '\n')
{
continue;
}
puts("请输入两个整数:");
while (scanf("%d %d", &i, &j) == 2)
{
if (i <= 0 || j <= 0)
{
printf("请重新输ru。\n");
continue;
}
printf("输入了%d行的字符%c,有%d列\n", i, a, j);
chline(a, i, j);
printf("请再次输入(或者按q退出)\n");
}
printf("程序完成。\n");
return 0;
}
void chline(char a, int i, int j)
{
for (int n = 0; n < i; n++)
{
for (int m = 0; m < j; m++)
{
putchar(a);
}
printf("\n");
}
}
//对isspause函数进行验证:检查参数c是否为空格字符,也就是判断是否为空格(' ')、水平定位字符
//('\t')、归位键('\r')、换行('\n')、垂直定位字符('\v')或翻页('\f')的情况。
//若参数c为空格 字符,则返回TRUE,否则返回NULL(0)。
//#include<ctype.h>
//#include<stdio.h>
//int main()
//{
// int i;
// char str[] ="123c @# FD\tsP[e?\n";
// for(i = 0; str[i] != 0; i++)
// {
// if(isspace(str[i]))
// printf("str[%d]isawhite-spacecharacter:%d\n", i, str[i]);
// }
// return 0;
//}
9.11.4
#include<stdio.h>
double harmean(double , double );
int main()
{
double x, y;
puts("请输入两个double类型的数:");
while (scanf("%lf %lf", &x, &y) == 2)
{
if (x <= 0 || y <= 0)
{
puts("输入错误请新输入");
continue;
}
printf("输入的x=%g,y=%g,它们的调和平均数为 %g\n",x, y, harmean(x, y));
puts("qin请再次输入两个数字...(或按q退出)");
}
puts("完成程序");
return 0;
}
double harmean(double x, double y)
{
return 2 / (1/x + 1/y);
}
9.11.5
#include <stdio.h>
void larger_of(double *x, double *y);
int main(void)
{
double a, b;
printf("请输入2个数(按q退出本程序):");
while (scanf("%lf %lf", &a, &b) == 2)
{
larger_of(&a, &b);
printf("替换成较大的值是%g和%g\n", a, b);
printf("您可以再次输入2个数(或按q退出):");
}
printf("本程序完成!\n");
return 0;
}
void larger_of(double *x, double *y)
{
*x = *y = (*x > *y ? *x : *y);
return;
}
9.11.6
第一种:
scanf需要带上&,莫忘
#include<stdio.h>
void sort(double *, double *, double *);
int main()
{
double a, b, c;
puts("请输入3个数(按q退出本程序):");
while(scanf("%lf %lf %lf", &a, &b ,&c) == 3)
{
if(a <= 0 || b <= 0 || c <= 0)
{
puts("输入错误请重新输入:");
continue;
}
sort(&a, &b, &c);
printf("最小值是%g\n", a);
printf("中间值是%g\n", b);
printf("最大值是%g\n", c);
puts("可以再次输入3个数(按q退出本程序).");
}
printf("本程序完成!\n");
return 0;
}
void sort(double *a, double *b, double *b)
{
//***对于这个里面的逻辑判断,刚开始出现了错误,2 5 1这样的数字判断出来是2 1 5,后来改正了,下面是两种实现方式,切记验算一遍,上诉情况是在linux环境下发现的,vs2017没有
//①:
/* double tp;
if (*a > *b)
{
tp = *a;
*a = *b;
*b = tp;
}
if (*a > *c)
{
tp = *a;
*a = *c;
*c = tp;
}
if (*b > *c)
{
tp = *b;
*b = *c;
*c = tp;
}
*/
//第②种:
double m;
*a < *b ? (*a < *c ? (*b < *c ? : m = *b, *b = *c, *c = m) : m = *b, *b = *a, *a = *c, *c = m) : (*b < *c ? : (*a < *c ? : m = *b, *b = *a, *a = *c, *c = m));
return;
}
第二种
#include<stdio.h>
void sort(double* a, double* b, double* c);
void swap(double* x, double* y);
int main(void)
{
double a, b, c;
printf("Please enter three decimal numbers.\n");
scanf_s("%lf %lf %lf", &a, &b, &c);
printf("Originally a= %.3f b= %.3f c=%.3f\n", a, b, c);
sort(&a, &b, &c);
printf("Now a= %.3f b=%.3f c=%.3f\n", a, b, c);
return 0;
}
//调用交换函数实现a b c的值互换
void sort(double* a, double* b, double* c)
{
if (*a > * b)
swap(a, b);
if (*b > * c)
swap(b, c);
if (*a > * b)
swap(a, b);
}
//使用算法进行x y 的值互换,先把x的值给了y,再把y的值给了x
void swap(double* x, double* y)
{
*x = *x + *y;
*y = *x - *y;
*x = *x - *y;
}
9.11.7
字符本质上就是整形,对这个刚开始不理解,所以对 int ch的理解有所偏差;
还有position()函数里对大小写字母的序号,字符ch减去字符’A’+1或’a’+1才是当前字符本身的序号,可以参考ASCII码,看看A与a的编码号是多少;
还有figure在if的条件判断里不可少,它是为了判断当我们输入的不是字母的时候,无法返回字母编号的情况下才会返回-1,让条件为假;
( ch = getchar() ) != EOF,ch = getchar()外层忘记了加**()**,注意先后顺序,符号的优先等级
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
void attain_pos(void);
int position(int ch);
int main(void)
{
attain_pos();
return 0;
}
void attain_pos(void)
{
int ch;
int figure;
printf("请您输入一些字符(遇到EOF结束):\n");
while ((ch = getchar()) != EOF)
{
figure = position(ch);
if ((figure != -1) && isupper(ch)) //isupper 检查所传的字符是否是大写字母
{
printf("%c是第%d个大写字母.\n", ch, figure);
}
else if ((figure != -1) && islower(ch)) //islower 判断字符是否为小写字母
{
printf("%c是第%d个小写字母.\n", ch, figure);
}
else //ascll码表里面非大小写字母进入else
{
printf("%c不是字母!\n", ch); //包括回车换行符和空白符;
}
}
printf("本程序完成!\n");
}
int position(int ch)
{
if (isupper(ch))//如果是大写字母
{
return (ch - 'A' + 1);
}
else if (islower(ch)) //如果是小写字母
{ //比如 a-a+1====也就是ascll表里的97-97=0而0你看下while循环的figure=0是不是你while逻辑就错了你想想,所以里面这就是必须+1的原因
return (ch - 'a' + 1);
}
return -1; //如果二者都不是则用-1代表非字母字符
}
9.11.8
对整数、指数各自为0的情况也需要考虑上
#include<stdio.h>
double power(double x, int index);
int main()
{
double x, xpow;
int index;
printf("Enter a number and the integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while ((scanf_s("%lf %d", &x, &index) == 2))
{
if (x == 0 && index ==0)
{
puts("0 的0次幂未定义,设定结果是1");
continue;
}
xpow = power(x, index);
printf("数字%.3g的%d次幂是%.5g\n", x, index, xpow);
puts("enter next pair of numbers or q to quit .");
}
puts("程序结束。");
return 0;
}
double power(double x, int index)
{
double xpow = 1.0;
int i;
if (index == 0) //次幂为0和整数为0的情况
{
return xpow;
}
if (x == 0)
{
xpow = 0.0;
return xpow;
}
else if (index < 0)
{
for (i = 0; i < -index; i++) //指数为负数时,遍历计算for循环
{
xpow *= x;
}
return 1 / xpow;
}
else
{
for (i = 0; i < index; i++) //指数为正数时,遍历计算for循环
{
xpow *= x;
}
return xpow;
}
}
9.11.9
递归就是在不停的调用自身,这里是在不停的调用函数power()实现次幂的计算。
正数次幂在不停的减小,负数次幂在不停的变大
#include<stdio.h>
double power(double x, int index);
int main()
{
double x, xpow;
int index;
printf("Enter a number and the integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while ((scanf_s("%lf %d", &x, &index) == 2))
{
if (x == 0 && index ==0)
{
puts("0 的0次幂未定义,设定结果是1");
continue;
}
xpow = power(x, index);
printf("数字%.3g的%d次幂是%.5g\n", x, index, xpow);
puts("enter next pair of numbers or q to quit .");
}
puts("程序结束。");
return 0;
}
double power(double n, int index)
{
double xpow = 1.0;
int i;
if (index == 0) //次幂为0和整数为0的情况
{
return xpow;
}
if (n == 0)
{
xpow = 0.0;
return xpow;
}
else if (index > 0)
{
return n * power(n, index - 1); //调用自身的power函数,不断的递减至0,不满足条件结束 指数index在变小
}
else
{
return power(n, index + 1) / n; //不断的递增至0,不满足条件结束 index在变大
}
}
图示
9.11.10
#include<stdio.h>
#include<string.h>
void to_base_n(int n,int b);
void Conversion_Of_Number_Ystems();
int main(void)
{
Conversion_Of_Number_Ystems();
return 0;
}
void Conversion_Of_Number_Ystems()
{
int b;
long int n;
printf("请输入一个正整数(按q退出本程序):");
while (scanf("%ld", &n) == 1)
{
if (n <= 0)
{
printf("数据无效!请您重新输入(或按q退出):");
continue;
}
printf("请输入一个需要转换的进制(2-10):");
while ((scanf("%d", &b) != 1) || (b < 2 || b > 10))
{
while (getchar() != '\n')
continue;
printf("输入无效,进制转换的范围仅限于2-10:");
}
printf("%d的%d进制是:", n, b);
to_base_n(n, b);
printf("\n您可以再次输入一个正整数(或按q退出):");
}
printf("本程序完成!\n");
}
void to_base_n(int x, int base)
{
int r;
r = x % base;
if (x >= base)
{
to_base_n(x / base, base);
}
printf("%d", r);
return;
}
void Conversion_Of_Number_Ystems()函数还可以这样写
void Conversion_Of_Number_Ystems()
{
int b;
long int n;
printf("请输入一个正整数和需要转换的进制数(按q退出本程序):\n");
while (scanf("%ld %d", &n, &b) == 2)
{
if ( n <= 0 && (b != 1 || (b < 2 || b > 10)) )
{
while (getchar() != '\n')
{
printf("数据无效!请您重新输入(或按q退出):\n");
continue;
}
}
printf("%d的%d进制是:\n", n, b);
to_base_n(n, b);
printf("\n您可以再次输入一个正整数(或按q退出):");
}
printf("本程序完成!\n");
}
图示
9.11.11
#include <stdio.h>
int Fibonacci_Numbers(int n);
int main()
{
int num;
long int n;
puts("请输入一个整数:");
while ( scanf("%ld", &n) == 1)
{
if (n <= 0)
{
puts("输入无效,请重新输入:");
continue;
}
num = Fibonacci_Numbers(n);
printf("第%ld个整数的斐波那契数为%d\n",n, num);
puts("请再次输入或按q退出:");
}
puts("结束。");
return 0;
}
int Fibonacci_Numbers(int n)
{
int i = 0;
unsigned long int t, x, y;
x = y = 1;
if (n == 1 || n == 2)
{
return 1;
}
else
{
for (i = 0; i < n; i++)
{
t = x + y;
x = y;
y = t;
}
}
return t;
}