您的位置:首页 > 编程语言

C Primer Plus 第九章 编程练习 1-9题

2017-03-09 20:02 232 查看
第一题
#include<stdio.h>
double Min(double x , double y);
int main(void)
{
double X;
double Y;
printf("Please Input First Number:\n");
scanf("%lf",&X);
getchar();
printf("Please Input First Number:\n");
scanf("%lf",&Y);
getchar();
printf("Min Number Is %.2lf.\n",Min(X,Y));

return 0;
}
double Min(double x , double y)
{
return (x<y)?x:y;
}


第二题
#include<stdio.h>
const char Ch = '*';
const int column = 10;
const int row = 10;
void chline(char ch , int x , int y);
int main(void)
{
char Letters;
int COL;
int ROW;
printf("PLease Input Any Charactor:\n");
scanf("%c",&Letters);
getchar();
printf("Which Row Is You Want To Display On?\n");
scanf("%d",&ROW);
getchar();
printf("Which Column Is You Want To Display On?\n");
scanf("%d",&COL);
getchar();

chline(Letters,ROW,COL);

return 0;
}
void chline(char ch , int x , int y)
{
for(int i = 1 ; i <= row ; ++i)
{
for(int j = 1 ; j <= column ; ++j)
{
if(i == x && j == y)
printf("%c",ch);
else printf("%c",Ch);
}
printf("\n");
}
}


第三题
#include<stdio.h>
void showChar(char ch , int x , int y);
int main(void)
{
char Letters;
int COL;
int ROW;
printf("PLease Input Any Charactor:\n");
scanf("%c",&Letters);
getchar();
printf("How Many Row Is You Want To Display ?\n");
scanf("%d",&ROW);
getchar();
printf("How Many Column Is You Want To Display On?\n");
scanf("%d",&COL);
getchar();

showChar(Letters,ROW,COL);

return 0;
}
void showChar(char ch , int x , int y)
{
for(int i = 1 ; i <= x ; ++i)
{
for(int j = 1 ; j <= y ; ++j)
printf("%c",ch);
printf("\n");
}
}


第四题
#include<stdio.h>
void Counts(double x , double y);
int main(void)
{
double X;
double Y;
printf("Please Input First Number:\n");
scanf("%lf",&X);
getchar();
printf("Please Input Second Number:\n");
scanf("%lf",&Y);
getchar();

Counts(X,Y);

return 0;
}
void Counts(double x , double y)
{
printf("Result is %.2lf\n",1/((1/x+1/y)/2));
}


第五题
#include<stdio.h>
void larger_of(double* x ,double* y);
int main(void)
{
double X;
double Y;
printf("PLease Input First Number:\n");
scanf("%lf",&X);
getchar();
printf("PLease Input Second Number:\n");
scanf("%lf",&Y);
getchar();
larger_of(&X,&Y);

printf("%.2lf   %.2lf",X,Y);

return 0;
}
void larger_of(double* x ,double* y)
{
(*x > *y)?(*y = *x):(*x = *y);
}


第六题
#include<stdio.h>
#include<ctype.h>
int checkLetter(char ch);
int main(void)
{
char Letters;
while((Letters = getchar()) != '\n')  //'\n' replace EOF
printf("%d ",checkLetter(Letters));

return 0;
}
int checkLetter(char ch)
{
if(isalpha(ch))
{
if(islower(ch))
return (ch-96);
if(isupper(ch))
return (ch-64);
}
return -1;
}


第七题
#include<stdio.h>
#include<stdbool.h>
double myPow(double p ,int n);
int main(void)
{
double X;
int Y;
while(true)
{
printf("Please Input Any Double To Test:\n");
if(!scanf("%lf",&X))
break;
getchar();
printf("Please Input Another Integer To Test:\n");
if(!scanf("%d",&Y))
break;
getchar();
printf("%.2lf POW %d Is %.2lf.\n",X,Y,myPow(X,Y));
}
printf("Done!");

return 0;
}
double myPow(double p ,int n)
{
double ps = 1.0;
if(p == 0)
ps = 0;
else
{
if(n > 0)
{
for(int i = 0 ; i < n ; ++i)
ps *= p;
}
if(n < 0)
ps = 1/ps;
}
return ps;
}


第八题
#include<stdio.h>
#include<stdbool.h>
double myPow(double p ,int n);
int main(void)
{
double X;
int Y;
while(true)
{
printf("Please Input Any Double To Test:\n");
if(!scanf("%lf",&X))
break;
getchar();
printf("Please Input Another Integer To Test:\n");
if(!scanf("%d",&Y))
break;
getchar();
printf("%.2lf POW %d Is %.2lf.\n",X,Y,myPow(X,Y));
}
printf("Done!");

return 0;
}
double myPow(double p ,int n)
{
double POW = 1;
if(n > 0)
{
--n;
POW = p * myPow(p,n);
}
else  if(n < 0)
{
++n;
POW = p * myPow(p,n);
}
return POW;
}


第九题
#include<stdio.h>
#include<stdbool.h>
void to_base_n(unsigned long n , int types);
int main(void)
{
unsigned long Number;
int Type;
bool on_off = true;
printf("Please Input Any Integer To Test:\n");
int switch_1 = scanf("%lu",&Number);
printf("Please Input Types(Integer 2 To 10):\n");
int switch_2 = scanf("%d",&Type);
while(on_off)
{
if(switch_1 != 1 || switch_2 != 1 || Type > 10 || Type < 2)
{
on_off = false;
continue;
}
to_base_
9db2
n(Number,Type);
printf("\n");
printf("Please Input Any Integer To Test:\n");
switch_1 = scanf("%lu",&Number);
printf("Please Input Types(Integer 2 To 10):\n");
switch_2 = scanf("%d",&Type);
}

printf("Done!");

return 0;
}
void to_base_n(unsigned long n , int types)
{
int r;
r = n%types;
if(n >= types)
to_base_n(n/types,types);
putchar('0'+r);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: