1.题略

/*返回较小值,设计驱动程序测试该函数*/
#include <stdio.h>
double min (double a, double b); int main (void)
{
double x, y; printf ("Please enter two numbers: \n");
scanf ("%lf %lf", &x, &y);
printf ("The smaller one is %lf\n", min (x, y));
return ;
} double min (double a, double b)
{
return (a < b) ? a: b;
}

2.题略

/**/
#include <stdio.h>
void chline (char, int, int); int main (void)
{
int i, j;
char ch; printf ("Please enter a char and two int: \n");
scanf ("%c %d %d", &ch, &i, &j);
chline (ch, i, j);
return ;
} void chline (char ch, int i, int j)
{
int count;
for (count = ; count <= j; count++)
{
if (count < i)
putchar (' ');
if (count >= i && count <= j)
putchar (ch);
if (count < || count > j)
break;
}
putchar ('\n');
}

3.题略

/**/
#include <stdio.h>
void p_ch (char, int, int); int main (void)
{
int i, j;
char ch; printf ("Please enter a char you like: \n");
scanf ("%c", &ch);
printf ("How many of them do you want to see in a line: \n");
scanf ("%d", &i);
printf ("How many lines do you want to have: \n");
scanf ("%d", &j);
p_ch (ch, i, j);
printf ("Is it what you want? \n");
return ;
} void p_ch (char ch, int cols, int rows)
{
int i, j;
for (i = ; i <= rows; i++)
{
for (j = ; j <= cols; j++)
putchar (ch);
printf ("\n");
}
}

4.题略

/**/
#include <stdio.h>
double calculate (double, double); int main (void)
{
double a, b;
printf ("input two doubles: ");
scanf ("%lf %lf", &a, &b);
printf ("1 / ((1/x + 1/y) / 2) = %.3f\n", calculate (a, b));
return ;
} double calculate (double x, double y)
{
return / ((/x + /y) / );
}

5.题略

/**/
#include <stdio.h>
void larger_of (double *, double *); int main (void)
{
double a, b;
printf ("Please enter two numbers: ");
scanf ("%lf %lf", &a, &b);
larger_of (&a, &b);
printf ("Now the a is %.2f and b is %.2f", a, b);
return ;
} void larger_of (double * x, double * y)
{
*x = *y = (*x > *y) ? *x : *y;
}

6.题略

/**/
#include <stdio.h>
void Judge(int ch); int main(void)
{
int ch; printf("enter some txt to be analyzed(ctrl+z to end):\n");
while ((ch = getchar()) != EOF)
Judge(ch);
printf("Done\n");
} void Judge(int ch)
{
if (ch>= && ch<=) printf("%c is number %d\n", ch, ch-);
else if (ch>= && ch<=) printf("%c is number %d\n", ch, ch-);
else printf("%c is not a letter\n",ch);
}

7.题略

#include <stdio.h>
double power(double n, int p); int main (void)
{
double Do;
int In; printf("Please enter a double and a int(to calculate pow):\n");
while (scanf("%lf %d",&Do, &In))
{
printf("%lf\'s %d mi is %lf\n", Do, In, power(Do, In));
}
printf("Done!\n");
} double power(double n, int p)
{
int i;
double pow=; if (n==)
return ; if (p==)
return ;
else if (p > )
{
for (i=; i<p; i++)
pow*=n;
return pow;
}
else if (p < )
{
for (i=; i<(-p); i++)
pow*=n;
return /pow;
}
}

8.题略

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

double power(double n, int p)
{
double ans; if (p < )
{
ans = power(n,-p);
ans = /ans;
}
else if (p > )
ans = n * power(n,p-);
else if (p == )
ans = ;
return ans;
}

9.题略

/*binary.c 以二进制形式输出整数*/
#include <stdio.h>
void Binary(int x, int y); int main(void)
{
int x, y; printf("Please enter a int (>=0)\n");
while (scanf("%d %d", &x,&y) == )
{
if (y> || y<) continue;
Binary(x,y);
printf("\nPlease enter 2 int(q to quit):\n");
}
printf("Done!\n");
} void Binary(int x, int y)
{
int z;
z = x % y;
if (x/y != )
Binary(x/y, y);
printf("%d",z);
}

10.题略

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

int Fibonacci( unsigned int n )
{
unsigned int FibN, FibNOne, FibNTwo;
unsigned int i; int result[] = { , };
if( n < )
return result[n-]; FibNOne = ;
FibNTwo = ;
FibN = ;
for( i = ; i <= n; i++ )
{ /*以第一次循环执行过程为例*/
FibN = FibNOne + FibNTwo; /*f(2) = f(0) + f(1)*/
FibNOne = FibNTwo; /*f(1)*/
FibNTwo = FibN; /*f(2)*/
}
return FibN;
}

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. C#反射在ADO中的巧用

    在C#原生开发网站的时候,经常会碰到一个问题,后台用原生查出来的数据一般是DataReader或者是DataTable,这样就把数据从数据库中拿到了,然后就是把数据绑定到前台页面进行输出,这是最原生态 ...

  2. 【CSS】css自定义字体

    源:http://www.cnblogs.com/lhb25/archive/2011/02/11/1951035.html 1.@font-face 定义一个字体 2.例子

  3. 关于dll

    今日看到一个不带dllmain的dll,忽然觉得有点奇怪,然后查了一下,原来dll还可以不需要dllmain,甚至可以自己定义入口 先mark以下的资料,有空再总结一下...同时dll劫持,有必要亲身 ...

  4. 个人作业——关于K米的产品案例分析

    Notice:本文所采用的K米版本为 Version:4.3.0 Release:20161014 第一部分 调研,评测 评测: 软件的bug,功能评测,黑箱测试 1.下载并使用,描述最简单直观的个人 ...

  5. ORB-SLAM(三)地图初始化

    单目SLAM地图初始化的目标是构建初始的三维点云.由于不能仅仅从单帧得到深度信息,因此需要从图像序列中选取两帧以上的图像,估计摄像机姿态并重建出初始的三维点云. ORB-SLAM中提到,地图初始化常见 ...

  6. c#使用正则表达式抓取a标签的链接和innerhtml

    //读取网页html string text = File.ReadAllText(Environment.CurrentDirectory + "//test.txt", Enc ...

  7. sublime text 3 3114 注册码

    -– BEGIN LICENSE -– Michael Barnes Single User License EA7E-821385 8A353C41 872A0D5C DF9B2950 AFF6F6 ...

  8. 【Beta】Scrum05

    Info 由于12.9~12.11三天,PM和测试到上海参加比赛,期间PM对博客更新暂停,功能测试暂停,Scrum会议暂停,12.12日起补充及恢复之前未能完成的工作. 时间:2016.12.08 2 ...

  9. JBoss 系列十四:JBoss7/WildFly如何加载外部的文件或properties文件

    http://www.tuicool.com/articles/M7ZR3y 原文 http://blog.csdn.net/kylinsoong/article/details/12623997 主 ...

  10. 介绍编译的less的两种IDE工具

    介绍编译的less的两种IDE工具 现在css预编译越来越普及了,著名的有less.sass.stylus等等等等.功能上基本上都是大同小异.这些个玩意儿主要表达的意思就是:"像编程一样的编 ...