课后练习4-流程控制

// TheSeventhChapterStudy.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>

void readChar();
void isALeapYear();
//课后练习
void practiceOfOne();
void practiceOfTwo();
void practiceOfThree();
void practiceOfFour();
void practiceOfFive();
void practiceOfSix();
void practiceOfSeven();
void practiceOfEigth();
void practiceOfNine();
void practiceOfTen();
void practiceOfEleven();

int main()
{
    //readChar();
    //isALeapYear();

    //practiceOfOne();
    //practiceOfTwo();
    //practiceOfThree();
    //practiceOfFour();
    //practiceOfFive();
    //practiceOfSix();
    //practiceOfSeven();
    //practiceOfEigth();
    //practiceOfNine();
    //practiceOfTen();
    practiceOfEleven();
}

//输入一个字符,输出此字符所属的类别
void readChar() {
    char c;
    printf("input a character:");
    c = getchar();
    if (c<32) {
        printf("This is a control character.\n");
    }
    else if (c >= '0' && c <= '9') {
        printf("This is a digit.\n");
    }
    else if (c >= 'A' && c <= 'Z') {
        printf("This is a capital letter.\n");
    }
    else if (c >= 'a' && c <= 'z') {
        printf("This is a small letter.\n");
    }
    else {
        printf("This is an other character.\n");
    }

    printf("c = %c", c);
}

//输入一个年份,判断是否为闰年
void isALeapYear() {
    printf("请输入一个年份:");
     unsigned int year;
    scanf_s("%u", &year);
    year % 4 > 0 ? printf("%u年不是闰年。", year): printf("%u年是闰年。", year);
}

/*1、要求:
    * 当x<0时,y=-1;
    * 当x=0时,y=0;
    * 当x>0时,y=1;
    * 编写一个C程序,输入一个x值,要求输出相应的y值
*/
void practiceOfOne() {
    printf("请输入一个x值:");
    int x, y;
    scanf_s("%d", &x);
    if (x < 0) {
        y = -1;
    }
    else if (x == 0) {
        y = 0;
    }
    else {
        y = 1;
    }
    printf("y = %d", y);
}

//2、编写一个C程序,要求按照考试成绩的等级输出百分制的分数段。A等为85分以上,
//B等为70~84分,C等为60~69分,D等为60分以下。成绩的等级由键盘输入。
void practiceOfTwo() {
    printf("请输入成绩的等级:");
    char grade;
    scanf_s("%c", &grade);
    switch (grade) {
        case 'A': {
            printf("85分以上成绩的等级为:%c", grade);
            break;
        }
        case 'B': {
            printf("70~84分之间,成绩的等级为:%c", grade);
            break;
        }
        case 'C': {
            printf("60~69分之间,成绩的等级为:%c", grade);
            break;
        }
        case 'D': {
            printf("60分以下,成绩的等级为:%c", grade);
            break;
        }
        default: {
            printf("输入错误!");
        }
    }
}

//3、编写一个C程序,用switch语句处理菜单命令,在许多应用程序中,都用菜单队流程
//进行控制,例如:从键盘输入一个字符'A'或者'a',就会执行A操作,输入一个字符'B'
//或者'b'就会执行B操作
void practiceOfThree() {
    printf("请输入一个字符:");
    char ch;
    scanf_s("%c", &ch);
    switch (ch) {
        case 'A':
        case 'a':{
            printf("执行A操作");
            break;
        }
        case 'B':
        case 'b':{
            printf("执行B操作");
            break;
        }
        default: {
            printf("执行其他操作");
        }
    }
}

//4、编写一个C程序,从键盘输入一个小于1000的正数,输出它的平方根。如果平方根不
//是整数,则输出它的整数部分,要求在输入数据后先检查是否为小于1000的正数,若
//不是,则要重新输入。
void practiceOfFour() {
     unsigned int number = 0;
    do {
        printf("请输入一个小于1000的正数:");
        scanf_s("%u", &number);
    } while (number >= 1000);

    printf("平方根:%.0f", sqrt(number));//会四舍五入
    //printf("平方根:%d", (int)sqrt(number));//不会四舍五入
}

/*5、编写一个C程序,计算运输公司对用户的运输费用。路程s(单位:km)越远,折扣率越高
* 计算标准如下:
* (1)s<250            没有折扣
* (2)250<=s<500        折扣率是2%
* (3)500<=s<1000    折扣率是5%
* (4)1000<=s<2000    折扣率是8%
* (5)2000<=s<3000    折扣率是10%
* (6)3000<=s        折扣率是15%
*/
void practiceOfFive() {
    printf("请输入运输距离:");
    float s = 0;
    scanf_s("%f", &s);
    if (s < 250) {
        printf("没有折扣");
    }
    else if (s >= 250 && s < 500) {
        printf("折扣率是2%%");
    }
    else if (s >= 500 && s < 1000) {
        printf("折扣率是5%%");
    }
    else if (s >= 1000 && s < 2000) {
        printf("折扣率是8%%");
    }
    else if (s >= 2000 && s <3000) {
        printf("折扣率是10%%");
    }
    else if (s >= 3000) {
        printf("折扣率是15%%");
    }
}

//6、编写一个C程序,在键盘输入4个整数,要求按从小到大的顺序输出它们。
void practiceOfSix() {
    int a, b, c, d;
    printf("请输入4个整数:");
    scanf_s("%d,%d,%d,%d", &a, &b, &c, &d);

    //先从小到大排好a,b
    if (b < a) {
        int temp = b;
        b = a;
        a = temp;
    }

    //再从小到大排好a,b,c
    if (c <= a) {
        int temp = c;
        c = b;
        b = a;
        a = temp;
    }
    else if (c < b) {
        int temp = c;
        c = b;
        b = temp;
    }

    //最后从小到大排好a,b,c,d
    if (d <= a) {
        int temp = d;
        d = c;
        c = b;
        b = a;
        a = temp;
    }
    else if (d <= b) {
        int temp = d;
        d = c;
        c = b;
        b = temp;
    }
    else if (d <= c) {
        int temp = d;
        d = c;
        c = temp;
    }

    printf("从小到大的顺序为:%d, %d, %d, %d", a, b, c, d);
}

//7、有一个分段函数:
//y=x(x<0);y=x-10(0<=x<10);y=x+10(x>=10)
//编写一个C程序,要求根据输入的x值,输出y的值
void practiceOfSeven() {
    printf("请输入一个数x:");
    double x;
    scanf_s("%lf", &x);
    if (x < 0) {
        printf("y = x = %lf", x);
    }
    else if (x >= 0 && x < 10) {
        printf("y = x - 10 = %lf", x - 10);
    }
    else if (x >= 10 ) {
        printf("y = x + 10 = %lf", x + 10);
    }
}

//8、有4个圆塔,圆心分别为(2,2)、(-2,2)、(-2,-2)、(2,-2),圆的半径为1,这4
//个塔的高度为10m,塔以外无建筑物,编写一个C程序,输入任意一点的坐标后求出该点的
//建筑高度(塔外的高度为0)
void practiceOfEigth() {
    //定义四个圆点(只需定义一个点):
    float x1 = 2, y1 = 2;
    //float x2 = -2, y2 = 2;
    //float x3 = -2, y3 = -2;
    //float x4 = 2, y4 = -2;
    printf("请输入一个点的坐标(x,y):");
    float x, y;
    scanf_s("%f,%f", &x, &y);//输入的时候,逗号也要英文的
    //四个点对称,先将输入的点的坐标转换成绝对值,再计算该点到点(2,2)的距离,并与半径1进行比较
    x = abs(x);
    y = abs(y);
    //与1比较,所以此处也可以不用开方
    //float s = sqrt((x - 2) * (x - 2) + (y - 2) * (y - 2));
    float s = (x - 2) * (x - 2) + (y - 2) * (y - 2);
    if (s <= 1) {
        printf("该点的建筑高度为10m。");
    }
    else {
        printf("该点的建筑高度为0");
    }
}

//9、在全系1000名学生中,征集慈善募捐。编写一个C程序,当总数达到10万元时
//就结束,统计此时捐款的人数,以及平均每人的捐款数
void practiceOfNine() {
    //假设每一位学生捐款不超过300元(0x7fff=32767)
    int sum = 0, stus = 0;
    while (sum < 100000) {
        sum += (int)(rand() / 32767.0 * 300);
        stus++;
    }
    printf("捐款总金额:%d\n捐款人数:%d\n 平均捐款:%.2f", sum, stus, sum * 1.0 / stus);
}

//10、编写一个C程序,输出100~200中不能被3整除的数。
void practiceOfTen() {
    for (int i = 100; i <= 200; i++) {
        if (i % 3 != 0) {
            printf("%d\n", i);
        }
    }
}

//11、编写一个C程序,输出以下4*5的矩阵
//1        2    3    4    5
//2        4    6    8    10
//3        6    9    12    15
//4        8    12    16    20
void practiceOfEleven() {
    for (int i = 1; i <= 4; i++) {
        for (int j = 1; j <= 5; j++) {
            printf("%d\t", j * i);
        }
        printf("\n\n");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路漫漫之编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值