C Primer Plus 第9章 函数 编程练习
复习题:
8.
int choice(int a,int b,int c){
int max;
max = a;
if (b > max)
max = b;
if (c > max)
max = c;
return max;
}
9.
#include <stdio.h>
void menu(void);
int choice(int low,int high);
int main(void){
menu();
int ch = choice(1,4);
switch (ch){
case 1:printf("you choice is : copy files\n");
break;
case 2:printf("you choice is : move files\n");
break;
case 3:printf("you choice is : remove files\n");
break;
case 4:break;
}
printf("Bye~");
return 0;
}
void menu(void){
printf("Please choose one of the following:\n");
printf("1) copy files 2) move files\n");
printf("3) remove files 4) quit\n");
printf("Enter the number of your choice:");
return;
}
int choice(int low,int high){
int ch;
if (scanf("%d", &ch) != 1)
return 4;
while (ch > high || ch < low){
menu();
ch = choice(1,4);
}
return ch;
}
编程练习:
1.
#include <stdio.h>
double min(double a, double b);
int main(void){
double x = 1.0;
double y = 2.0;
double c;
c = min(1,2);
printf("%.2f", c);
}
double min(double a, double b){
return ( b < a ? b : a);
}
2.
#include <stdio.h>
void chline(char ch, int i, int j);
int main(void){
char ch = '*';
int row = 5, col = 10;
chline(ch,row,col);
return 0;
}
void chline(char ch, int i, int j){
for (int x = 0; x < i; ++x) {
for (int y = 0; y < j; ++y) {
putchar(ch);
}
putchar('\n');
}
}
3.
#include <stdio.h>
void chline(char ch, int i, int j);
char character(void);
int number(void);
int main(void){
char ch;
int row, col;
while ((ch = character()) != 'q'){
printf("Please enter the number of lines to be printed:");
row = number();
printf("Please enter the number of columns to be printed:");
col = number();
chline(ch, row, col);
}
return 0;
}
void chline(char ch, int i, int j){
for (int x = 0; x < i; ++x) {
for (int y = 0; y < j; ++y) {
putchar(ch);
}
putchar('\n');
}
}
char character(void){
char ch;
printf("Please enter the character you need print(q to quit):");
while ((ch = getchar()) == '\n')
continue;
while (getchar() != '\n')
continue;
return ch;
}
int number(void){
int num;
char ch;
while (scanf("%d", &num) != 1){
while ((ch = getchar()) != '\n') //处理错误输出***
putchar(ch);
printf("is not an number.\n");
printf("Please enter an number such as 3,5");
}
if (num <= 0){
printf("Please enter the number than 0:");
while ((ch = getchar()) != '\n') //处理错误输出
putchar(ch);
num = number();
}
return num;
}
4.
#include <stdio.h>
double countdouwn_average(double num1, double num2);
int main(void){
double a = 10, b = 20, c;
c = countdouwn_average(a,b);
printf("%lf",c);
return 0;
}
double countdouwn_average(double num1, double num2){
double countdown1,countdown2,ctd_average;
countdown1 = 1 / num1;
countdown2 = 1 / num2;
ctd_average = 1 / ((countdown1 + countdown2) / 2);
return ctd_average;
}
5.
#include <stdio.h>
void larger_of(double * num1, double * num2);
int main(void){
double a = 10, b = 20;
larger_of(&a,&b);
printf("%.2lf %.2lf", a, b);
return 0;
}
void larger_of(double * num1, double * num2){
double max;
max = *num1 > *num2 ? *num1 : *num2;
*num1 = max;
*num2 = max;
return;
}
6.
#include <stdio.h>
void larger_of(double * num1, double * num2, double * num3);
int main(void){
double a = 60, b = 39, c = 50;
larger_of(&a,&b,&c);
printf("%.2lf %.2lf %.2lf", a, b, c);
return 0;
}
void larger_of(double * num1, double * num2, double * num3){
double max,min,c;
max = * num1;
if (max < *num2)
max = *num2;
if (max < *num3)
max = *num3;
min = * num1;
if (min > *num2)
min = *num2;
if (min > *num3)
min = *num3;
c = *num1 + *num2 + *num3 - min - max;
*num1 = min;
*num2 = c;
*num3 = max;
}
7.
#include <stdio.h>
#include <ctype.h>
int show_location(char ch);
int main(void){
char ch;
printf("Please enter the charaster:");
while ((ch = getchar()) != '\n'){ //ch = getchar()要框起来。
printf("%c,%d\n", ch,show_location(ch));
}
return 0;
}
//用isaplpa 和 toupper我是真不会。看了答案
//发现答案真机智~
int show_location(char ch){
int result;
if (isalpha(ch)){
result = toupper(ch) - 'A' + 1;
} else result = -1;
return result;
}
8.
#include <stdio.h>
double power(double n, double p);
int main(void){
double a = 2,b = 2;
printf("%lf", power(a,b));
return 0;
}
double power(double n, double p){
double pow = 1;
int i;
if (n == 0)
return 0;
if (p == 0)
return 1;
if (p > 0){
for (i = 1;i <= p; i++) {
pow = pow * n;
}
return pow;
}
if (p < 0){
for (i = 1;i <= -p ; i++) {
pow = pow * n;
}
return 1 / pow;
}
}
9.
#include <stdio.h>
double power(double n, double p);
int main(void){
double a = 2,b = -2;
printf("%lf", power(a,b));
return 0;
}
double power(double n, double p){
double pow = 1;
int i;
if (n == 0)
return 0;
if (p == 0)
return 1;
if (p > 0){
for (i = 1;i <= p; i++) {
pow = pow * n;
}
return pow;
}
if (p < 0){
return 1 / power(n, -p);
}
}
10.
#include <stdio.h>
void to_binary(int a, int b);
int main(void){
int a,b;
char ch;
printf("Please enter an integer(q to quit):");
while (scanf("%d", &a) == 1){
printf("Please enter number base (2-10):");
while (scanf("%d", &b) == 1 && b >= 2 && b <= 10){
to_binary(a,b);
putchar('\n');
break;
}
while ((ch = getchar()) != '\n'); //处理错误输入
printf("Please enter an integer(q to quit):");
}
return 0;
}
void to_binary(int a, int b){
int r;
r = a % b;
if (a > b)
to_binary(a / b, b);
printf("%d", r); //暂时不理解putchar('0' + r)是什么意思,所以就先用这个吧。
return;
}
C Primer Plus 第9章 函数 编程练习的更多相关文章
- C++ Primer 5th 第6章 函数
正如第一章所说:C++的函数是一个能够完成一个功能的模块或者说是一段命名了的代码块. 如下图所示,函数可以重载,是一段实现某些功能命名了的代码. 一个完整的函数的构成有四部分: 1.返回类型 2.函数 ...
- C++ primer plus读书笔记——第7章 函数——C++的编程模块
第7章 函数--C++的编程模块 1. 函数的返回类型不能是数组,但可以是其他任何一种类型,甚至可以是结构和对象.有趣的是,C++函数不能直接返回数组,但可以将数组作为结构或对象的组成部分来返回. 2 ...
- 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 ...
- C Primer Plus_第5章_运算符、表达式和语句_编程练习
Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- 《C++ Primer》 第四版 第7章 函数
<C++ Primer> 第四版 第7章 函数 思维导图笔记 超级具体.很具体,图片版,有利于复习查看 http://download.csdn.net/detail/onlyshi/94 ...
- C++ primer plus读书笔记——第8章 函数探幽
第8章 函数探幽 1. 对于内联函数,编译器将使用相应的函数代码替换函数调用,程序无需跳到一个位置执行代码,再调回来.因此,内联函数的运行速度比常规函数稍快,但代价是需要占用更多内存. 2. 要使用内 ...
- <<C++ Primer>> 第 6 章 函数
术语表 第 6 章 函数 二义性调用(ambiguous call): 是一种编译时发生的错误,造成二义性调用的原因时在函数匹配时两个或多个函数提供的匹配一样好,编译器找不到唯一的最佳匹配. 实 ...
- 《深入浅出Node.js》第4章 异步编程
@by Ruth92(转载请注明出处) 第4章 异步编程 Node 能够迅速成功并流行起来的原因: V8 和 异步 I/O 在性能上带来的提升: 前后端 JavaScript 编程风格一致 一.函数式 ...
随机推荐
- mysql进阶(二十三)数据库事务四大特性
数据库事务四大特性 原子性.一致性.分离性.持久性 原子性 事务的原子性指的是,事务中包含的程序作为数据库的逻辑工作单位,它所做的对数据修改操作要么全部执行,要么完全不执行.这种特性称为 ...
- 【一天一道LeetCode】#46. Permutations
一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ...
- (转载)SQL Server2008附加数据库之后显示为只读时解决方法
SQL Server2008附加数据库之后显示为只读时解决方法 啰嗦的话就不多说了,直入主题吧! 方案一: 碰到这中情况一般是使用的sa账户登录的,只要改为Windows身份验证,再附加数据库即可搞定 ...
- 【面试笔试算法】Program 2:Amusing Digits(网易游戏笔试题)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 网易成立于1997年6月,是中国领先的互联网技术公司.其相继推出了门户网站.在线游戏.电子邮箱.在线教育.电子商务等多种服 ...
- 巧用FineReport搭建成本管控监测系统
一.应用背景 企业在近几年快速发展,规模也越来越大,而信息传递与反馈手段却仍然比较落后,随着信息技术的不断发展,人们开始通过尝试利用技术手段改善这种环境.企业的项目不断增多,而作为高层管理者,通过层级 ...
- EBS R12安装升级(FRESH)(三)
5 EBS R12.1.1安装后配置 5.1 新建patch文件夹 1 2 3 su - root mkdir /stage/patch chmod 777 /stage/patch 打补丁说明:随便 ...
- CentOS6.4下安装Nginx1.12.2
1.安装GCC安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装 yum install gcc-c++ 2.安装PCRE-devel PCR ...
- Eclipse常见设置
当新建一个workspace时,习惯做下面的设置: 1. 在eclipse中,默认的Text file encoding是GBK(操作系统是中文简体):如果操作系统是中文繁体,默认是MS950(Big ...
- java线程池原理
在什么情况下使用线程池? 1.单个任务处理的时间比较短 2.将需处理的任务的数量大 使用线程池的好处: 1.减少在创建和销毁线程上所花的时间以及系统资源的开销 ...
- access按钮事件在子窗体打开窗体或报表
Private Sub Com1_Click()Me.win.SourceObject = "窗体1"End Sub Private Sub Com2_Click()Me.win. ...