C Primer Plus 第八章 编程练习 1-8题

第一题
#include<stdio.h>
int main(void)
{
  int Counts = 0;
  char Letters;
  printf("Please Input:\n");
  while((Letters = getchar()) != EOF)
    ++Counts;
  printf("You Had Inputed %d Letters.",Counts);

 return 0;
}


第二题
#include<stdio.h>
const int LenColumn = 10;
const int MaxLen = 100;
int main(void)
{
  char Letters;
  char Words[MaxLen];
  int Counts = 0;
  printf("PLease Input Words To Test:\n");
  while((Letters = getchar()) != EOF)
  {
    Words[Counts] = Letters;
    ++Counts;
  }
  Words[Counts] = '\0';
  
  for(int i = 0 ; i < Counts ; ++i)
  {
    if(Words[i] == '\n')
      printf("\\n/%d ",Words[i]);
    else if(Words[i] == '\t')
           printf("\\t/%d ",Words[i]);
    else if(Words[i] == ' ')
           printf("\\b/%d ",Words[i]);
    else printf("%c/%d ",Words[i],Words[i]);
    if((i + LenColumn) % LenColumn == 0 && i != 0)
      printf("\n");
  }

 return 0;
}


第三题
#include<stdio.h>
#include<ctype.h>
int main(void)
{
  int UpperCounts = 0;
  int LowerCounts = 0;
  char Letters;
  printf("Please Input Word To Test:\n");
  while((Letters = getchar()) != EOF)
  {
    if(isupper(Letters))
      ++UpperCounts;
    if(islower(Letters))
      ++LowerCounts;
  }

  printf("%d Lower And %d Upper.\n",LowerCounts,UpperCounts);
 
 return 0;
}


第四题
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
int main(void)
{
  int WordsCounts = 0;
  int LettersCounts = 0;
  bool isWord = false;
  char Letters;
  printf("PLease Input Words To Test:\n");
  while((Letters = getchar()) != EOF)
  {
    if(isalpha(Letters))
    {
      isWord = true;
      ++LettersCounts;
    }
    if(!isalpha(Letters) && isWord == true)
    {
      ++WordsCounts;
      isWord = false;
    }
  }

  printf("We Get %d Words And %d Letters.\n",WordsCounts,LettersCounts);
  printf("Every Word Have %0.2lf Letters.\n",(double)LettersCounts/WordsCounts);

 return 0;
}


第五题
#include<stdio.h>
int main(void)
{
  printf("I Guess 50\n");
  int realResult = 50;
  char ch;
  int MAX = 100;
  int MIN = 0;
  while((ch = getchar()) != 'y')
  {
    if(ch == 'u')  //to up
      MIN = realResult;
    else if(ch == 'l')  //to low
      MAX = realResult;
    else
      printf("Sorry ,I Can't UnderStand %c",ch);
    printf("I Guess %d",(MAX+MIN)/2);
    realResult = (MAX + MIN) / 2;
    getchar();
  }
  printf("I Get Real Result %d.",realResult);
    
  return 0;
}


第六题
#include<stdio.h>
const char Space = ' ';
int get_first(void);
int main(void)
{
  char showChar;
  showChar = get_first();
  printf("%c",showChar);
 
 return 0;
}
int get_first(void)
{
  char ch;
  while((ch = getchar()) == Space)
    continue;
  return ch;
}


第七题
#include<stdio.h>
#include<stdbool.h>
#include<iso646.h>
#define PayHour 40.0
#define PayLevel_1 300.0
#define PayLevel_2 450.0
int main(void)
{
  bool on_off = true;
  bool Choose = true;
  char HR;
  double WageHour;
  while(on_off)
  {
    printf("**********************************************\n");
    printf("A)8.75/hr            B)9.33/hr\n");
    printf("C)10.00/hr           D)11.20/hr\n");
    printf("Q)Quit\n");
    printf("**********************************************\n");    //Create menu
    scanf("%c",&HR);
    getchar();
    switch(HR)
    {
      case 'A':WageHour = 8.75;
             break;
      case 'B':WageHour = 9.33;
             break;
      case 'C':WageHour = 10.00;
             break;
      case 'D':WageHour = 11.20;
             break;
      case 'Q':on_off = false;
             printf("Done!");
             break;
      default:printf("There.s A Wrong,Please Try Input Again.\n");
             Choose = false;
    }
    if(Choose == false or on_off == false)  //choose 5 or 4
      continue;
    
    double WorkTime;
    printf("Please Input Your Hours:\n");
    scanf("%lf",&WorkTime);
    getchar();
    if(WorkTime > PayHour)
      WorkTime = (WorkTime-PayHour) * 1.5 + PayHour;

    double Wages = WorkTime * WageHour;
    double Level_1 = PayLevel_1 * (1-0.15);
    double Level_2 = (PayLevel_2 - PayLevel_1) * (1-0.2);
    if(Wages < PayLevel_1)
      Wages = Wages * (1-0.15);
    else 
      if(Wages >= PayLevel_1 && Wages < PayLevel_2)
        Wages = Level_1 + (Wages-PayLevel_1) * (1-0.2);
      else
        if(Wages >= PayLevel_2)
          Wages = Level_1 + Level_2 +(Wages-PayLevel_2) * (1-0.25);

    printf("Your Wages Are %.2lf\n",Wages);
  }


 return 0;
}


第八题
#include<stdio.h>
#include<stdbool.h>
void Counts(char types);
int main(void)
{
  bool on_off = true;
  while(on_off)
  {
    char Choice;
    printf("Please Input Your Choice:\n");
    printf("a.add s.subtract\n");
    printf("m.multiply d.divide\n");
    printf("q.quit\n");
    scanf("%c",&Choice);
    getchar();
    switch(Choice)
    {
      case 'a':Counts(Choice);
               break;
      case 's':Counts(Choice);
               break;
      case 'm':Counts(Choice);
               break;
      case 'd':Counts(Choice);
               break;
      case 'q':printf("Bye.\n");
               on_off = false;
               break;
      default:printf("Bye!\n");
              on_off = false;
    }
  }

 return 0;
}
void Counts(char types)
{
  double FirstNumber;
  double SecondNumber;
  printf("Input First Number:\n");
  while(!scanf("%lf",&FirstNumber))
  {
    printf("Input First Number:\n");
    getchar();
  }
  printf("Input Second Number:\n");
  while(!scanf("%lf",&SecondNumber) || (SecondNumber == 0 && types == 'd'))
  {
    printf("Input Second Number:\n");
    getchar();
  }
  switch(types)
  {
    case 'a':printf("%.2lf + %.2lf = %.2lf\n",FirstNumber,SecondNumber,FirstNumber+SecondNumber);
             break;
    case 's':printf("%.2lf - %.2lf = %.2lf\n",FirstNumber,SecondNumber,FirstNumber-SecondNumber);
             break;
    case 'm':printf("%.2lf * %.2lf = %.2lf\n",FirstNumber,SecondNumber,FirstNumber*SecondNumber);
             break;
    case 'd':printf("%.2lf / %.2lf = %.2lf\n",FirstNumber,SecondNumber,FirstNumber/SecondNumber);
             break;
  }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值