当前位置:网站首页>【C Primer Plus第九章课后编程题】
【C Primer Plus第九章课后编程题】
2022-07-30 00:52:00 【御膳房总试吃】
9.1
#include<stdio.h>
#include<stdlib.h>
double min (double x,double y);
int main (void)
{
double x,y;
printf("Please enter two numbers:");
scanf("%lf %lf",&x,&y);
printf("The smaller is %lf", min(x,y));
system("Pause");
return 0;
}
double min (double x,double y)
{
return (x>y)?y:x;
}
9.2
#include<stdio.h>
#include<stdlib.h>
void chline(char ch,int i,int j);
int main (void)
{
char ch;
int i,j;
printf("Please enter a char:");
scanf("%c",&ch);
printf("Please enter the row and line you want print:");
scanf("%d %d",&i,&j);
chline(ch,i,j);
system("Pause");
return 0;
}
void chline(char ch,int i,int j)
{
int row,line;
for (row= 0;row<j;row++)
{
for (line=0;line<i;line++)
{
printf("%c",ch);
}
printf("\n");
}
}
9.3
#include<stdio.h>
#include<stdlib.h>
void chline(char ch,int i,int j);
int main (void)
{
char ch;
int i,j;
printf("Please enter a char:");
scanf("%c",&ch);
printf("Please enter the row and line you want print:");
scanf("%d %d",&i,&j);
chline(ch,i,j);
system("Pause");
return 0;
}
void chline(char ch,int i,int j)
{
int row,line;
for (row= 0;row<j;row++)
{
for (line=0;line<i;line++)
{
printf("%c",ch);
}
printf("\n");
}
}
9.4
#include<stdio.h>
#include<stdlib.h>
double average(double a,double b);
int main (void)
{
double a,b;
printf("Please enter two numbers:");
scanf("%lf %lf",&a,&b);
printf("%lf",1/average(a,b));
system("Pause");
return 0;
}
double average(double a,double b)
{
double average;
average=1/a+1/b;
return average;
}
9.5
#include<stdio.h>
#include<stdlib.h>
double larger_of(double x,double y);
int main (void)
{
double x,y;
printf("Please enter two numbers:\n");
scanf("%lf %lf",&x,&y);
printf("%lf",larger_of(x,y));
system("Pause");
return 0;
}
double larger_of(double x,double y)
{
(x>y)?(y=x):(x=y);
return x;
}
9.6
#include<stdio.h>
#include<stdlib.h>
double arrange(double* x,double* y,double* z);
int main (void)
{
double x,y,z;
printf("Please enter three numbers:");
scanf("%lf %lf %lf",&x,&y,&z);
arrange(&x,&y,&z);
printf("%lf %lf %lf",x,y,z);
system("Pause");
return 0;
}
double arrange(double* x,double* y,double* z)
{
double temp;
if (*x>*y)
{
temp=*x;
*x=*y;
*y=temp;
}
if (*x>*z)
{
temp=*x;
*x=*z;
*z=temp;
}
if (*y>*z)
{
temp=*y;
*y=*z;
*z=temp;
}
}
9.7
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int posion(int ch);
int main (void)
{
int ch;
int temp;
printf("Please enter a letter:");
while ((ch=getchar())!=EOF)
{
while (getchar()!='\n')
continue;
temp=posion(ch);
if (temp==-1)
{
printf("It is not a letter:%d\n",temp);
}
else
{
printf("It is the %d letter in alphabet\n",temp);
}
}
system("Pause");
return 0;
}
int posion(int ch)
{
int letter;
if (isupper(ch))
{
letter=ch-'A'+1;
}
else if(islower(ch))
{
letter=ch-'a'+1;
}
else
{
return -1;
}
return letter;
}
9.8
#include<stdio.h>
#include<stdlib.h>
double power(double n,int p);
int main (void)
{
double x,xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised.Enter q");
printf(" to quit.\n");
while (scanf("%lf%d",&x,&exp)==2)
{
xpow=power(x,exp);
printf("%.3g to the power %d is %.5g\n",x,exp,xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed thie power trio -- bye!\n");
system("Pause");
return 0;
}
double power(double n,int p)
{
double pow=1;
int i;
if ((n==0)&&(p==0))
{
return 0;
}
else if (p==0)
{
return 1;
}
else if (p>0)
{
for (i = 1; i <= p; i++)
{
pow*=n;
}
return pow;
}
else
{
for (i = 1; i <= -p; i++)
{
pow*=1/n;
}
return pow;
}
}
9.9
#include<stdio.h>
#include<stdlib.h>
double power(double n,int p);
int main (void)
{
double x,xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised.Enter q");
printf(" to quit.\n");
while (scanf("%lf%d",&x,&exp)==2)
{
xpow=power(x,exp);
printf("%.3g to the power %d is %.5g\n",x,exp,xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed thie power trio -- bye!\n");
system("Pause");
return 0;
}
double power(double n,int p)
{
double pow=1;
int i;
if ((n==0)&&(p==0))
{
return 0;
}
else if (p==0)
{
return 1;
}
else if (p>0)
{
return n*power(n,p-1);
}
else
{
return power(n,p+1)/n;
}
}
9.10
#include<stdio.h>
#include<stdlib.h>
void to_base_n(unsigned long num,int n);
int main (void)
{
unsigned long number;
int n;
printf("Enter two integers (q to quit):\n");
while (scanf("%lu %d",&number,&n)==2)
{
printf("%d equivalent:",n);
to_base_n(number,n);
printf("\n");
}
system("Pause");
return 0;
}
void to_base_n(unsigned long num,int n)
{
int r;
r=num%n;
if (num>=n)
{
to_base_n(num/n,n);
}
printf("%d",r);
}
9.11
#include<stdio.h>
#include<stdlib.h>
void Fibonacci(int length);
int main (void)
{
int length;
printf("Please enter a number (q to quit):");
while (scanf("%d",&length)==1)
{
Fibonacci(length);
printf("\n");
printf("Please enter a number (q to quit):");
}
system("Pause");
return 0;
}
void Fibonacci (int length)
{
int len,x,y,temp;
x=y=temp=1;
for (len = 0;len<=length; len++)
{
printf("%d",x);
printf(" ");
temp=x+y;
x=y;
y=temp;
}
}
边栏推荐
- 【Incubator DAY18】Interesting exchange【Simulation】【Math】
- Weekly recommended short video: What is R&D efficiency?It can achieve anti "involution"?
- 基于TNEWS‘ 今日头条中文新闻(短文本)分类
- go语言解决自定义header的跨域问题
- Replace the executable file glibc version of the one
- 【MySQL系列】MySQL数据库基础
- Worthington酶促细胞收获&细胞粘附和收获
- How Filebeat ensures that the log file is still correctly read when the log file is split (or rolled)
- Fabric 编写案例 链码
- Minimum number to rotate array
猜你喜欢

7.27

Introduction to Worthington Elastase & Hyaluronidase
[email protected](using passwordYES)"/>Navicat error: 1045-Access denied for user [email protected](using passwordYES)

He cell separation technology 丨 basic primary cell separation methods and materials

Navicat for mysql破解版安装

重新定义分析 - EventBridge 实时事件分析平台发布

@RequestParam注解的详细介绍

自学HarmonyOS应用开发(47)- 自定义switch组件

KDE Frameworks 5.20.0:Plasma迎来诸多改进

Recommendation systems: feature engineering, common features
随机推荐
Navicat for mysql crack version installation
记笔记!电源自动测试系统测试电源纹波详细方法
Worthington解离酶:胰蛋白酶及常见问题
高德地图jsapi不生效 INVALID_USER_SCODE
基于TNEWS‘ 今日头条中文新闻(短文本)分类
Finding a 2D Array
Douyin short video traffic acquisition strategy, mastering these will definitely be a hit
专心致志做事情
从尾到头打印链表
Since the media increase play a short video?From the three aspects
@RequestParam注解的详细介绍
会议OA之待开会议&&所有会议
The range of motion of the robot
中文语义匹配
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
WeChat developer tools set the tab size to 2
How to realize the frame selection of objects in canvas (6)
Fabric Private Data Case
Missing X64 mfc140u. DLL file - > application cannot normal boot (0 xc000007b) solution
【励志】科比精神