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

第一题
#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_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);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值