1.题略

  1. /*返回较小值,设计驱动程序测试该函数*/
  2. #include <stdio.h>
  3. double min (double a, double b);
  4.  
  5. int main (void)
  6. {
  7. double x, y;
  8.  
  9. printf ("Please enter two numbers: \n");
  10. scanf ("%lf %lf", &x, &y);
  11. printf ("The smaller one is %lf\n", min (x, y));
  12. return ;
  13. }
  14.  
  15. double min (double a, double b)
  16. {
  17. return (a < b) ? a: b;
  18. }

2.题略

  1. /**/
  2. #include <stdio.h>
  3. void chline (char, int, int);
  4.  
  5. int main (void)
  6. {
  7. int i, j;
  8. char ch;
  9.  
  10. printf ("Please enter a char and two int: \n");
  11. scanf ("%c %d %d", &ch, &i, &j);
  12. chline (ch, i, j);
  13. return ;
  14. }
  15.  
  16. void chline (char ch, int i, int j)
  17. {
  18. int count;
  19. for (count = ; count <= j; count++)
  20. {
  21. if (count < i)
  22. putchar (' ');
  23. if (count >= i && count <= j)
  24. putchar (ch);
  25. if (count < || count > j)
  26. break;
  27. }
  28. putchar ('\n');
  29. }

3.题略

  1. /**/
  2. #include <stdio.h>
  3. void p_ch (char, int, int);
  4.  
  5. int main (void)
  6. {
  7. int i, j;
  8. char ch;
  9.  
  10. printf ("Please enter a char you like: \n");
  11. scanf ("%c", &ch);
  12. printf ("How many of them do you want to see in a line: \n");
  13. scanf ("%d", &i);
  14. printf ("How many lines do you want to have: \n");
  15. scanf ("%d", &j);
  16. p_ch (ch, i, j);
  17. printf ("Is it what you want? \n");
  18. return ;
  19. }
  20.  
  21. void p_ch (char ch, int cols, int rows)
  22. {
  23. int i, j;
  24. for (i = ; i <= rows; i++)
  25. {
  26. for (j = ; j <= cols; j++)
  27. putchar (ch);
  28. printf ("\n");
  29. }
  30. }

4.题略

  1. /**/
  2. #include <stdio.h>
  3. double calculate (double, double);
  4.  
  5. int main (void)
  6. {
  7. double a, b;
  8. printf ("input two doubles: ");
  9. scanf ("%lf %lf", &a, &b);
  10. printf ("1 / ((1/x + 1/y) / 2) = %.3f\n", calculate (a, b));
  11. return ;
  12. }
  13.  
  14. double calculate (double x, double y)
  15. {
  16. return / ((/x + /y) / );
  17. }

5.题略

  1. /**/
  2. #include <stdio.h>
  3. void larger_of (double *, double *);
  4.  
  5. int main (void)
  6. {
  7. double a, b;
  8. printf ("Please enter two numbers: ");
  9. scanf ("%lf %lf", &a, &b);
  10. larger_of (&a, &b);
  11. printf ("Now the a is %.2f and b is %.2f", a, b);
  12. return ;
  13. }
  14.  
  15. void larger_of (double * x, double * y)
  16. {
  17. *x = *y = (*x > *y) ? *x : *y;
  18. }

6.题略

  1. /**/
  2. #include <stdio.h>
  3. void Judge(int ch);
  4.  
  5. int main(void)
  6. {
  7. int ch;
  8.  
  9. printf("enter some txt to be analyzed(ctrl+z to end):\n");
  10. while ((ch = getchar()) != EOF)
  11. Judge(ch);
  12. printf("Done\n");
  13. }
  14.  
  15. void Judge(int ch)
  16. {
  17. if (ch>= && ch<=) printf("%c is number %d\n", ch, ch-);
  18. else if (ch>= && ch<=) printf("%c is number %d\n", ch, ch-);
  19. else printf("%c is not a letter\n",ch);
  20. }

7.题略

  1. #include <stdio.h>
  2. double power(double n, int p);
  3.  
  4. int main (void)
  5. {
  6. double Do;
  7. int In;
  8.  
  9. printf("Please enter a double and a int(to calculate pow):\n");
  10. while (scanf("%lf %d",&Do, &In))
  11. {
  12. printf("%lf\'s %d mi is %lf\n", Do, In, power(Do, In));
  13. }
  14. printf("Done!\n");
  15. }
  16.  
  17. double power(double n, int p)
  18. {
  19. int i;
  20. double pow=;
  21.  
  22. if (n==)
  23. return ;
  24.  
  25. if (p==)
  26. return ;
  27. else if (p > )
  28. {
  29. for (i=; i<p; i++)
  30. pow*=n;
  31. return pow;
  32. }
  33. else if (p < )
  34. {
  35. for (i=; i<(-p); i++)
  36. pow*=n;
  37. return /pow;
  38. }
  39. }

8.题略

主函数与7题相同,关键是幂函数(递归形式),代码如下,还是挺考验逻辑的。

  1. double power(double n, int p)
  2. {
  3. double ans;
  4.  
  5. if (p < )
  6. {
  7. ans = power(n,-p);
  8. ans = /ans;
  9. }
  10. else if (p > )
  11. ans = n * power(n,p-);
  12. else if (p == )
  13. ans = ;
  14. return ans;
  15. }

9.题略

  1. /*binary.c 以二进制形式输出整数*/
  2. #include <stdio.h>
  3. void Binary(int x, int y);
  4.  
  5. int main(void)
  6. {
  7. int x, y;
  8.  
  9. printf("Please enter a int (>=0)\n");
  10. while (scanf("%d %d", &x,&y) == )
  11. {
  12. if (y> || y<) continue;
  13. Binary(x,y);
  14. printf("\nPlease enter 2 int(q to quit):\n");
  15. }
  16. printf("Done!\n");
  17. }
  18.  
  19. void Binary(int x, int y)
  20. {
  21. int z;
  22. z = x % y;
  23. if (x/y != )
  24. Binary(x/y, y);
  25. printf("%d",z);
  26. }

10.题略

关键点:利用循环的方式生成斐波那契数列,从下往上计算。即通过f(0) + f(1) 得到f(2), 再由f(1) + f(2)得到f(3)....直到计算出f(n),每次都必须从底层算起,然后三个变量相互赋值,迭代计算。

  1. int Fibonacci( unsigned int n )
  2. {
  3. unsigned int FibN, FibNOne, FibNTwo;
  4. unsigned int i;
  5.  
  6. int result[] = { , };
  7. if( n < )
  8. return result[n-];
  9.  
  10. FibNOne = ;
  11. FibNTwo = ;
  12. FibN = ;
  13. for( i = ; i <= n; i++ )
  14. { /*以第一次循环执行过程为例*/
  15. FibN = FibNOne + FibNTwo; /*f(2) = f(0) + f(1)*/
  16. FibNOne = FibNTwo; /*f(1)*/
  17. FibNTwo = FibN; /*f(2)*/
  18. }
  19. return FibN;
  20. }

C Primer Plus_第9章_函数_编程练习的更多相关文章

  1. C Primer Plus_第6章_循环_编程练习

    1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...

  2. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...

  3. C Primer Plus_第四章_字符串和格式化输入输出_编程练习

    Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...

  4. Oracle学习总结_day03_day04_条件查询_排序_函数_子查询

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day03_条件查询_排序_函数 清空回收站: PUR ...

  5. C Primer Plus_第10章_数组和指针_编程练习

    1. /*rain.c 针对若干年的降水量数据,计算年降水总量.年降水平均量,以及月降水平均量*/ #include <stdio.h> #define MONTHS 12 #define ...

  6. C Primer Plus_第8章_字符输入输出和输入确认_编程练习

    1.题略 #include <stdio.h> int main(void) { ; printf("Please enter text here(end with Ctrl + ...

  7. C Primer Plus_第三章_数据和C_复习题与编程练习

    Review long代替int类型变量的原因是什么? 在您的系统中,long可以容纳比int更大的数:如果您确实需要处理更大的值,那么使用一种在所有系统上都保证至少是32位的类型会使程序的可移植性更 ...

  8. [C++ Primer Plus] 第8章、函数探幽(二)课后习题

    1.编写通常接受一个参数(字符串的地址),并打印该字符串的函数.不过,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第 ...

  9. [C++ Primer Plus] 第8章、函数探幽(一)程序清单——内联、引用、格式化输入输出、模板、decltype

    程序清单8.1(inline内联函数) #include<iostream> using namespace std; inline double square(double x) {// ...

随机推荐

  1. .net Core学习笔记:Windows环境搭建

    1.安装 VS2015 Update3.如果已经安装了VS2015,但不是Update3版本,请在VS的工具 --> 扩展与更新 中执行update3的升级(大约需要2小时). 2..net C ...

  2. Ubuntu 下配置apache和APR

    软件环境:ubuntu14.04  虚拟机Vmware  软件:http://httpd.apache.org/  httpd-2.2.29.tar.gz  不需要单独下载APR. 1.解压apach ...

  3. .Net配置中心-Zookeper版

    简介    zookeeper的基本概念和作用这里不做介绍,现在很多的公司都在使用它,说起它的作用,可能最先想到的是配置中心,可以将配置项作为一个node存储在zookeeper中,其他应用可以“关注 ...

  4. JS/CSS缓存杀手——VS插件

    背景 前些天去考科目二,感觉经历了一场不是高考却胜似高考的考试(10年前的5分之差, 还是难以释怀)!    一行八人,就我学的时间最少(4天,8人一辆车),教练都觉得我肯定还得再来一次! 靠着运气和 ...

  5. 剑指offer 面试题65 滑动窗口的最大值

    import java.awt.print.Printable; import java.beans.VetoableChangeListenerProxy; import java.lang.ref ...

  6. 关于GIL

    1同一时刻只有一个线程通过一个线程到解释器运行 2在多核上会有些不一样 不仅仅会降低python的效率 并且还会影响到整个机器系统的效率 python的gil是每100条cpu指令开始check 如果 ...

  7. UIDynamic-附着行为:UIAttachmentBehavior

    直接上代码: // // YFAttachmentBehaviorViewController.m // BigShow1949 // // Created by apple on 16/8/25. ...

  8. 你所不了解的float(滥用float的怪异现象)

    float设计初衷就是为了实现文字环绕效果 原本页面流布局显示如上图所示,运用了float属性后就显示为如下图所示,这就是浮动的设计初衷 float的一些特性:包裹性.破坏性. 包裹的特性其实主要有三 ...

  9. 优化Table View

    优化Table View Table view需要有很好的滚动性能,不然用户会在滚动过程中发现动画的瑕疵. 为了保证table view平滑滚动,确保你采取了以下的措施: 正确使用`reuseIden ...

  10. linux下apache各种跳转(包括伪静态)的配置

      1.404跳转: vi /etc/httpd/conf/httpd.conf 在虚拟主机配置里添加一行:ErrorDocument 404 /404.html 2.301跳转: 1)将不带www的 ...