C primer Plus 第七章编程练习

一系列C/C++编程题目,涉及字符处理、输入输出、字符串操作、条件判断、循环控制等基础知识。第一题计算空格、换行和其它字符的数量;第二题打印字符并换行;第三题计算偶数和奇数的平均值;第四题实现特殊字符替换;第五题与第四题类似,但用switch结构实现;第六题统计'e'后面跟着'i'的次数;第七题根据工时计算工资、税款和净收入;第八题增加工资选择,按不同工资计算;第九题判断素数;第十题按不同收入计算税款;第十一题计算蔬菜订购费用、运费、折扣和总费用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一题

#include<stdio.h>
#include <string.h>

int main(void)
{
    char ch;
    int kg = 0, hh = 0, qt = 0;
    while ((ch = getchar()) != '#')
    {
        
        if (ch == ' ')
            kg++;
        else if (ch == '\n')
            hh++;
        else
            qt++;
    }
    printf("%-5d %-5d %-5d\n", kg, hh, qt);
    return 0;
}

第二题

#include<stdio.h>
int main()
{
    char ch;
    int i = 0;
    while ((ch = getchar()) != '#')
    {
        printf("%d", ch);
        i++;
        if (i > 7)
        {
            printf("\n");
            i = 0;
        }
    }
    
}

第三题

int main()
{
    double sum1, sum2;
    int i,odd, number ;
    double average1, average2;
    odd = 0;
    number = 0;
    sum1 = 0;
    sum2 = 0;
    while ((scanf("%d", &i) == 1) && (i != 0))
    {
        if (i % 2 == 0)
        {
            number++;
            sum1 = sum1 + i;
        }
        else
        {
            odd++;
            sum2 = sum2 + i;
        }
        average1 = sum1 / number;
        average2 = sum2 / odd;

    }
     printf("The average of number is%lf\n", average1);
        printf("The average of odd is%lf\n", average2);
        printf("Tne number of 偶数 is %d\n",odd);
        printf("Tne number of 奇数 is %d\n",number);
}

第四题

int main()
{
    char ch;
    int i=0;
   ch=getchar();
    while(ch!='#')
    {
        if(ch=='.')
           {
           putchar(ch-13);
            i++;
            }
        else if(ch=='!')
           {
           putchar(ch);
           putchar(ch);
            i++;
            }
        else
            printf("%c",ch);

        ch=getchar();

    }
    printf("\n");
    printf("%d\n",i);
}

第五题

int main()
{
    char ch;
    int i=0;
    while((ch=getchar())!='#')
    {
        switch(ch)
        {
        case'.':
           putchar(ch-13);
            i++;
            break;
        case'!':
            putchar(ch);
            putchar(ch);
            i++;
            break;
        default:
            printf("%c",ch);
        }
    }

    printf("\n");
    printf("%d\n",i);
}

第六题

int main()
{
    char ch;
    int a=0;
    ch=getchar();
    while(ch!='#')
    {
        if(ch=='e')
          {
            ch=getchar();
            if(ch=='i')
            {
               a++;
            }
          }
          else
            ch=getchar();
    }
    printf("there are %d ei",a);
    return 0;
}

第七题

#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25

int main()
{
    double hours;
    double money;
    double levy;
    double sum;
    scanf("%lf",&hours);
    if(hours<40)
    {
        sum=10*hours;
        if(sum<=300)
        {
            levy=sum*RATE1;
            money=sum-levy;
        }
        else if(sum<=450)
        {
            levy=(sum-300)*RATE2+300*RATE1;
            money=sum-levy;
        }
        else
        {
            levy=150*RATE2+300*RATE1+(sum-450)*RATE3;
            money=sum-levy;
        }
    }
    else
    {
        sum=10*40+15*(hours-40);
        if(sum<=300)
        {
            levy=sum*RATE1;
            money=sum-levy;
        }
        else if(sum<=450)
        {
            levy=(sum-300)*RATE2+300*RATE1;
            money=sum-levy;
        }
        else
        {
            levy=150*RATE2+300*RATE1+(sum-450)*RATE3;
            money=sum-levy;
        }
    }
    printf("sum:%lf\n",sum);
    printf("levy:%lf\n",levy);
    printf("money:%lf\n",money);
}

第八题

#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25
int main()
{
    double hours;
    double money;
    double levy;
    double sum;
    double a;
    int choice=0;
    while(choice>=0)
{


    printf("Enter the number corresponding to the desired pay rate or action:\n");
    printf("1)$8.75/hr\t");
    printf("2)$9.33/hr\n");
    printf("3)$10.00/hr\t");
    printf("4)$11.20/hr\n");
    printf("5)quit\n");
    scanf("%d",&choice);
       switch(choice)
       {
       case 1:
            a=8.75;
            break;
       case 2:
            a=9.33;
            break;
       case 3:
            a=10.00;
            break;
       case 4:
            a=11.20;
            break;
       case 5:
            return 0;
            break;
       default:
            printf("please enter a right number!");
            break;
       }
    printf("%lf\n",a);
    scanf("%lf",&hours);
    if(hours<40)
    {
        sum=a*hours;
        if(sum<=300)
        {
            levy=sum*RATE1;
            money=sum-levy;
        }
        else if(sum<=450)
        {
            levy=(sum-300)*RATE2+300*RATE1;
            money=sum-levy;
        }
        else
        {
            levy=150*RATE2+300*RATE1+(sum-450)*RATE3;
            money=sum-levy;
        }
    }
    else
    {
        sum=a*40+1.5*a*(hours-40);
        if(sum<=300)
        {
            levy=sum*RATE1;
            money=sum-levy;
        }
        else if(sum<=450)
        {
            levy=(sum-300)*RATE2+300*RATE1;
            money=sum-levy;
        }
        else
        {
            levy=150*RATE2+300*RATE1+(sum-450)*RATE3;
            money=sum-levy;
        }
    }
    printf("sum:%lf\n",sum);
    printf("levy:%lf\n",levy);
    printf("money:%lf\n",money);
    }
    return 0;
}

第九题

int main()
{
	int num;
	int a;
	int div;
	bool isPrime;
	printf("Please enter an number:");
	scanf("%d",&num);
	while(num>0)
	{
		
		for(div=2,isPrime=true;(div*div)<=num;div++)
		{
			if(num%div==0)
			isPrime=false;
		}
		if(isPrime)
		printf("%d is prime\n",num);
		num--;
	}
	printf("Done!"); 
	return 0;
 }

第十题

#define rate1 0.15
 #define rate2 0.28
 #define sort1 17850
 #define sort2 23900
 #define sort3 29750
 #define sort4 14875
 int main()
 {
 	double money=0;
 	double levy;
 	long i;
 	int choice;
 	printf("Enter the number corresponding: \n");
 	printf("1)单身\t");
 	printf("2)户主\t");
 	printf("3)已婚,共有\t");
 	printf("4)已婚,离异\n");
 	scanf("%d",&choice);
 	while(money>=0)
 	{
	
 	switch(choice)
 	{
 		case 1:
 			i=17850;
 			break;
		case 2:
		 	i=23900;
		 	break;
		case 3:
			 i=29750;
			 break;
		case 4:
			 i=14875;
			 break;
		default:
			 printf("Done!");	
	}
	printf("%ld\n",i);
	scanf("%lf",&money);
	if(i=sort1)
	{
		if(money<=sort1)
			levy=money*rate1;
		else
			levy=sort1*rate1+(money-sort1)*rate2;
	} 
	else if(i=sort2)
	{
		if(money<=sort2)
			levy=money*rate1;
		else
			levy=sort2*rate1+(money-sort2)*rate2;
	}
	else if(i=sort3)
	{
		if(money<=sort3)
			levy=money*rate1;
		else
			levy=sort3*rate1+(money-sort3)*rate2;
	}
	else
	{
		if(money<=sort4)
			levy=money*rate1;
		else
			levy=sort4*rate1+(money-sort4)*rate2;
	}
	printf("The levy is %lf\n",levy);
	scanf("%d",&choice);
	}	
 }

第十一题

#define money1 2.05
 #define money2 1.15
 #define money3 1.09
 #define Discount 0.05
 int main()
 {
 	int weight;
 	double money,sum1,discount,cost,sum3;//money 订购蔬菜的费用 sum1订单总费用 cost 运费和包装费 sum2折扣费用总额sum3所有费用总额 
 	int yl,tc,carrot,i;
 	char ch;
 	i=1;
 	double sum2=0;
 	scanf("%c",&ch);
 	while(i>0)
 {
	 	switch(ch)
 	{
 		case'a':
 			scanf("%d\n",&yl);
 			break;
 		case'b':
 			scanf("%d\n",&tc);
 			break;
 		case'c':
 			scanf("%d\n",&carrot);
 			break;
 		case'q':
 			i--;
 			break;
 		default:
 			printf("Done");
 			
	}
	scanf("%c",&ch);
}
	printf("the yangli number is %d\n",yl);
	printf("the tiancai number is %d\n",tc);
	printf("the carrot number is %d\n",carrot);
	weight=yl+tc+carrot;
	printf("the sum is %d\n",weight);
	money=yl*money1+tc*money2+carrot*money3;
	
	if(money>100)
	{
		money=money-money*Discount;
		if(weight<5)
		{
			cost=6.5;
			}	
		else if(weight<20)
			cost=14;
		else
		    cost=14+(weight-20)*0.5;
		sum2=money*Discount;
	
	}
	 else
	 {
	 	if(weight<5)
			cost=6.5;	
		else if(weight<20)
			cost=14;
		else
		    cost=14+(20-14)*0.5;
	 }
 	sum1=money;
 	sum3=money+cost;
 	printf("the yl cost 2.05\n");
 	printf("the tc cost 1.15\n");
 	printf("the carrot cost 1.09\n");
 	printf("the vegetables money is %.2lf\n",sum1);
 	printf("运费包装费为%.2lf\n",cost);
 	printf("折扣为%.2lf\n",sum2);
 	printf("所有的费用总额为 %.2lf\n",sum3);
 	
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值