首页 > 其他 > 详细

C Primer Plus 7 学习记录

时间:2021-05-08 22:25:00      阅读:14      评论:0      收藏:0      [点我收藏+]

C Primer Plus 7

控制语句——分支与跳转

(1)编写一个程序,读取八个整数,并存入整型数组,倒序打印。

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
?
int main(void)
{
   int data[8];
   printf("Enter the 8 integer data (seperate by blank):\n");
   for(int i=0;i<8;i++)
  {
       data[i]=getchar() - 48;
  }
   printf("Ok,the reverse data is:");
   for(int i=7;i>=0;i--)
  {
       printf("%d",data[i]);
  }
   printf("Well done!\n");
?
   system("pause");
   return 0;
}

(2)编写一个程序,读取输入,读到#字符时停止,然后报告读取的空格数,换行符,和其它字符的数量。

#include<stdio.h>
#include<windows.h>
int main(void)
{
  char ch;
  int blank=0;
  int endline=0;
  int others=0;
  while((ch = getchar())!=‘#‘)
  {
      if(ch==‘ ‘)
      blank++;
      else if(ch==‘\n‘)
      endline++;
      else
      others++;
  }
  printf("%d blank,%d endline,%d others",blank,endline,others);
  system("pause");
  return 0;
}

(3)编写程序,读取整数,直到用户输入0。程序报告用户输入的偶数的个数,这些偶数的平均值,奇数的个数以及基数的平方和。

#include<stdio.h>
#include<windows.h>
int main(void)
{
  int odd_sum=0;
  int even_sum=0;
  int odd_count=0;
  int even_count=0;
  int odd_ave,even_ave;
  int input;
  while(scanf("%d",&input))
  {
      if(input==0)
      break;
      if(input%2==0)
      {
          even_sum+=input;
          even_count++;
          even_ave=even_sum/even_count;
      }
      else{
          odd_sum+=input;
          odd_count++;
          odd_ave=odd_sum/odd_count;
      }
  }
  printf("Have %d even number,average is %d.\n",even_count,even_ave);
  printf("Have %d odd number,average is %d.\n",odd_count,odd_ave);
  printf("Well done!\n");
  system("pause");
  return 0;
}

(4)使用if-else语句,读取输入,读到#字符为止。并用‘!’替换‘。’,用‘!!’替换‘!’。

#include<stdio.h>
#include<windows.h>
int main(void)
{
   int counter = 0;
   char ch;
   while((ch=getchar())!=‘#‘)
  {
       if (ch==‘!‘)
      {
           printf("!!");
           counter++;
      }
       else if (ch==‘.‘)
      {
           printf("!");
           counter++;
      }
       else{
           printf("%c",ch);
      }
  }
   printf("\nThe total replace time is%d",counter);
   printf("Done!\n");
   system("pause");
   return 0;
}

(5)编写一个程序,读取输入,读到#结束,并报告ei出现的次数。

#include<stdio.h>
#include<windows.h>
#include<conio.h>
?
int main(void)
{
   int counter=0;
   int matching=0;
   char ch;
   while((ch = getchar())!=‘#‘)
  {
       if (ch==‘e‘)
      {
           matching=1;    //字母e的匹配标记
      }
?
       else if(ch==‘i‘)
      {
           if (matching==1)
          {
               counter++;
               matching=0;    //清除标记
          }
      }
?
  }
   printf("The totally result is %d.\n",counter);
   system("pause");
   return 0;
}

(6)编写一个程序,提升用户输入1周工作的小时数,然后打印工资总额、税金和净收入。

/*30小时内无加班,标准基础税率
a:基本工资=10.00美元一小时
b:加班(工作时间超过40小时)=按1.5倍的时间计算
c:税率:前300美元为15%;接下来的150美元为20%;余下的为25%。*/
#include<stdio.h>
#include<windows.h>
#define Base_salary 10.00
#define Extra_hour 1.5
#define Base_tax 0.15
#define Extra_tax 0.2
#define Exceed_tax 0.25
int main(void)
{
   float hours = 0;
   float salary,tax,taxed_salary;    //工资,税金,净收入
   printf("Enter the working hours a week:\n");
   scanf("%f",&hours);
   if(hours<=30)
  {
       salary = hours*Base_salary;
       tax = salary*Base_tax;
       taxed_salary = salary - tax;
  }
   else if(hours<=40)
  {
       salary = hours*Base_salary;
       tax = 300*Base_tax+(salary-300)*Extra_tax;
       taxed_salary = salary-tax;
  }
   else
  {
       salary = (40+(hours-40)*Extra_hour)*Base_salary;
       if(salary<=450)
       tax = 3*Base_tax+(salary-300)*Extra_tax;
       else tax = 300*Base_tax + (salary-300)*Extra_tax + (salary-450)*Exceed_tax;
       taxed_salary = salary - tax;
  }
   printf("Your salary before tax is %.2f,tax is %.2f,salary after tax is %.2f.\n",salary,tax,taxed_salary);
   printf("Well Done!\n");
   system("pause");
   return 0;
}

 

C Primer Plus 7 学习记录

原文:https://www.cnblogs.com/jyyofficial/p/14745553.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!