c语言程序设计精髓 第四周练兵题

1检测用户错误输入(4分)

题目内容:

根据scanf()的返回值判断scanf()是否成功读入了指定的数据项数,使程序在用户输入123a时,能输出如下运行结果:

123a↙

Input error!

输入格式: “%d %d”

输出格式:

如果成功读入指定的数据项数,输出格式为:“a = %d, b = %d\n” (注意:等号的两边各有一个空格)

输入非法数据,输出格式为:“Input error!”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int a,b;
    int c = scanf("%d %d",&a,&b);
    if(c != 2){
        printf("Input error!");
    }else{
        printf("a = %d, b = %d\n",a,b);
    }
}

###2闰年判断(6分)
题目内容:

从键盘任意输入一个公元年份(大于等于1),判断它是否是闰年。若是闰年输出“Yes”,否则输出“No”。要求对输入数据进行合法性判断。

已知符合下列条件之一者是闰年:

(1)能被4整除,但不能被100整除;

(2)能被400整除。

运行结果示例1:

2015↙

No

运行结果示例2:

2016↙

Yes

运行结果示例3:

-123↙

Input error!

运行结果示例4:

a↙

Input error!

输入格式: “%d”

输出格式:

是闰年,输出:“Yes\n”

不是闰年,输出:“No\n”

输入数据不合法,输出:“Input error!\n”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int a;
    int c = scanf("%d",&a);
    if(c != 1 || a <1){
        printf("Input error!\n");
    }else{
        if((a%4==0&&a%100!=0) || a % 400 ==0){
            printf("Yes\n");
        }else{
            printf("No\n");
        }
    }
}

3程序改错v1.0(7分)

题目内容:

下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出 Input error!,否则将其转换为5分制输出。目前程序存在错误,请将其修改正确。并按照下面给出的运行示例检查程序。

 #include<stdio.h>
   int main()
   {
       int score;
       char grade;
       printf("Please input  score:");
       scanf("%d", &score);
       if (score < 0 || score > 100)   
             printf("Input error!\n");
        else if (score >= 90) 
             grade = 'A’;
        else if (score >= 80)
             grade = 'B';   
        else if (score >= 70)
             grade = 'C';  
        else if (score >= 60)
             grade = 'D'; 
        else
             grade = 'E'; 
        printf("grade:%c\n", grade);
        return 0;
}

程序运行结果示例1:

Please input score:

-1↙

Input error!

程序运行结果示例2:

Please input score:

95↙

grade: A

程序运行结果示例3:

Please input score:

82↙

grade: B

程序运行结果示例4:

Please input score:

72↙

grade: C

程序运行结果示例5:

Please input score:

66↙

grade: D

程序运行结果示例6:

Please input score:

32↙

grade: E

程序运行结果示例7:

Please input score:

127↙

Input error!

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
   {
       int score;
       char grade;
       printf("Please input score:\n");
       scanf("%d", &score);
       if (score < 0 || score > 100)
             printf("Input error!\n");
       else{
            if (score >= 90)
             grade = 'A';
            else if (score >= 80)
                 grade = 'B';
            else if (score >= 70)
                 grade = 'C';
            else if (score >= 60)
                 grade = 'D';
            else
                 grade = 'E';
            printf("grade: %c\n", grade);
       }
        return 0;
}

###4字符类型判断(4分)
题目内容:

从键盘键入任意一个字符,判断该字符是英文字母(不区分大、小写)、数字字符还是其它字符。

若键入字母,则屏幕显示 It is an English character.;若键入数字则屏幕显示It is a digit character. ;若输入其它字符,则屏幕显示:It is other character.

程序的运行示例1:

Input simple:

b↙

It is an English character.

程序的运行示例2:

Input simple:

6↙

It is a digit character.

程序的运行示例3:

Input simple:

*↙

It is other character.

程序的运行示例4:

Input simple:

A↙

It is an English character.

输入信息提示:“Input simple:\n”

输入格式: “%c”

输出格式:

英文字符的输出格式:“It is an English character.\n”

数字的输出格式:“It is a digit character.\n”

其它字符的输出格式:“It is other character.\n”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
   {
       char a;
       printf("Input simple:\n");
       scanf(" %c", &a);
       if(a >= 48 && a <= 57){
            printf("It is a digit character.\n");
        }else if((a >= 65 && a <= 90)||(a >= 97 && a <= 122)){
            printf("It is an English character.\n");
        }else{
            printf("It is other character.\n");
        }

       return 0;
}

5快递费用计算(7分)

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
   {
       int area;
       float weight,fw,ow,price =0.0;
       scanf("%d,%f",&area,&weight);
       if(area ==0 ){
           fw = 10;
           ow = 3;
       }else if(area ==1 ){
            fw = 10;
            ow = 4;
       }else if(area ==2 ){
            fw = 15;
            ow = 5;
       }else if(area ==3 ){
            fw = 15;
            ow = 6.5;
       }else if(area ==4 ){
            fw = 15;
            ow = 10;
       }else{
            fw = 0;
            ow = 0;
            printf("Error in Area\n");
       }
       weight=ceil(weight);
       price = (weight-1)*ow+fw;
       printf("Price: %5.2f\n" ,price);
       return 0;
}

###6数位拆分v2.0(4分)
题目内容:

从键盘上输入一个4位数的整数n,编写程序将其拆分为两个2位数的整数a和b,计算并输出拆分后的两个数的加、减、乘、除和求余运算的结果。例如n=-4321,设拆分后的两个整数为a,b,则a=-43,b=-21。除法运算结果要求精确到小数点后2位,数据类型为float。求余和除法运算需要考虑除数为0的情况,即如果拆分后b=0,则输出提示信息"The second operater is zero!"

程序的运行结果示例1:

Please input n:

1200↙

12,0

sum=12,sub=12,multi=0

The second operator is zero!

程序的运行结果示例2:

Please input n:

-2304↙

-23,-4

sum=-27,sub=-19,multi=92

dev=5.75,mod=-3

输入提示信息:“Please input n:\n”

输入格式: “%d”

输出格式:

拆分后的两个整数的输出格式:"%d,%d\n"

加法、减法、乘法的输出格式:“sum=%d,sub=%d,multi=%d\n”

除法和求余的输出格式:“dev=%.2f,mod=%d\n”

除数为0的提示信息:“The second operator is zero!\n”

int main()
{
    int x,y,a,b;
    printf("Please input n:\n");
    scanf("%d",&x);
    a = x / 100;
    b = x % 100;
    printf("%d,%d\n",a,b);
    printf("sum=%d,sub=%d,multi=%d\n",a+b,a-b,a*b);
    if(b ==0 ){
       printf("The second operator is zero!\n");
    }else{
        printf("dev=%.2f,mod=%d\n",a*1.0/b,a%b);
    }
    return 0;
}

###7出租车计价(4分)
题目内容:

已知某城市普通出租车收费标准为:起步里程为3公里,起步费为8元,10公里以内超过起步里程的部分,每公里加收2元,超过10公里以上的部分加收50%的回空补贴费,即每公里3元。出租车营运过程中,因堵车和乘客要求临时停车等客的,按每5分钟加收2元

计算,不足5分钟的不计费。从键盘任意输入行驶里程(精确到0.1公里)和等待时间(精确到分钟),请编程计算并输出乘客应支付的车费,对结果进行四舍五入,精确到元。

程序运行结果示例1:

Input distance and time:2,2↙

fee = 8

程序运行结果示例2:

Input distance and time:5,5↙

fee = 14

程序运行结果示例3:

Input distance and time:12,15↙

fee = 34

程序运行结果示例4:

Input distance and time:20,0↙

fee = 52

输入提示信息:“Input distance and time:”

输入格式:

用逗号分隔的两个数字,第一个表示距离、第二个表示时间:"%f,%d"

输出格式:“fee = %.0f\n” (注意:等号的两边各有一个空格)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int time,timeFee=2;
    float distance,fee=0.0;
    printf("Input distance and time:");
    scanf("%f,%d",&distance,&time);
    //时间收费
    int num = time/5;
    fee=(float)timeFee * num;
    if(distance>0){
        fee+=8;
    }
    if( distance<=10 && distance>3){
        fee = fee + (distance-3)*2;
    }
    if( distance > 10 ){
        fee += 14;
        fee = fee + (distance-10)*3;
    }

    printf("fee = %.0f\n",round(fee));

    return 0;
}

###8数据区间判断(6分)
题目内容:

从键盘输入一个int型的正整数n(已知:0<n<10000),编写程序判断n落在哪个区间。如果用户输入的数据不在指定的范围里,程序输出 “error!”。例如,输入265,则该数属于区间 100-999。

程序运行结果示例1:

Please enter the number:

2563↙

2563: 1000-9999

程序运行结果示例2:

Please enter the number:

156↙

156: 100-999

程序运行结果示例3:

Please enter the number:

36↙

36: 10-99

程序运行结果示例4:

Please enter the number:

3↙

3: 0-9

程序运行结果示例5:

Please enter the number:

10923↙

error!

输入提示信息:“Please enter the number:\n”

输入错误提示信息:“error!\n”

输入格式: “%d”

输出格式:

输出的区间判断:

“%d: 1000-9999\n”

“%d: 100-999\n”

“%d: 10-99\n”

“%d: 0-9\n”

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int data;
    printf("Please enter the number:\n");
    scanf("%d",&data);
    if(data<=0 || data >= 10000){
        printf("error!\n");
    }else if(data>0 && data <=9){
        printf("%d: 0-9\n",data);
    }else if(data>=10 && data <=99){
        printf("%d: 10-99\n",data);
    }else if(data>=100 && data <=999){
        printf("%d: 100-999\n",data);
    }else if(data>1000 && data <10000){
        printf("%d: 1000-9999\n",data);
    }
    return 0;
}

###9计算一元二次方程的根v2.0(4分)
在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    float a,b,c,x1,x2;
    printf("Please enter the coefficients a,b,c:\n");
    scanf("%f,%f,%f",&a,&b,&c);
    float d = b*b-4*a*c;
    if(d < 0){
        printf("error!\n");
    }else{
        x1 = (sqrt(d)-b)/(2*a);
        x2 = (-sqrt(d)-b)/(2*a);
        printf("x1=%7.4f, x2=%7.4f\n",x1,x2);
    }
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值