C语言经典例题(22) --- 学生基本信息输入输出、判断字母、字符金字塔、ASCII码、出生日期输入输出

1.学生基本信息输入输出

题目描述:

依次输入一个学生的学号,以及3科(C语言、数学、英语)成绩,在屏幕上输出该学生的学号,3科成绩。

输入描述:

学号以及3科成绩,学号和成绩之间用英文分号隔开,成绩之间用英文逗号隔开。

输出描述:

学号,3科成绩,输出格式详见输出样例。

输入:

17140216;80.845,90.55,100.00

输出:

The each subject score of of No. 17140216 is 80.85,90.55,100.00

参考代码:

#include <stdio.h>

int main()
{
    int id = 0;
    float c_score = 0;
    float math_score = 0;
    float eng_score = 0;

    scanf("%d;%f,%f,%f", &id, &c_score, &math_score, &eng_score);
    printf("The each subject score of of No. %d is %.2f, %.2f, %.2f.", id, c_score, math_score, eng_score);
    return 0;
}

2.判断字母

题目描述

从键盘任意输入一个字符,编程判断是否是字母(包括大小写)。

输入描述:

多组输入,每行输入包括一个字符。

输出描述:

针对每行输入,输出该字符是字母(YES)或不是(NO)。

输入:

H

9

输出:

YES

NO

参考代码:

#include <stdio.h>
#include <ctype.h>

int main()
{
    int ch = 0;
    while ((ch = getchar()) != EOF)
    {
        if (isalpha(ch))
        {
            printf("YES");
        }
        else
        {
            printf("NO");
        }
        getchar();
        printf("\n");
    }
    return 0;
}

3.字符金字塔

题目描述

输入一个字符,用它构造一个三角形金字塔。

输入描述:

输入只有一行,一个字符。

输出描述:

该字符构成的三角形金字塔。

输入:

1

输出:

    1
   1 1
  1 1 1
 1 1 1 1
1 1 1 1 1

参考代码:

#include <stdio.h>

int main()
{
    int i = 0;
    char n = 0;
    scanf("%c", &n);
    for (i = 0; i < 5; i++)
    {
        int j = 0;
        for (j = 0; j < 4-i; j++)
        {
            printf(" ");
         }
        for (int j = 0; j <= i; j++)
        {
            printf("%c ",n);
        }
        printf("\n");
    }
}

4.ASCII码

题目描述:

BoBo教KiKi字符常量或字符变量表示的字符在内存中以ASCII码形式存储。BoBo出了一个问题给KK,转换以下ASCII码为对应字符并输出他们。

73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33

输入描述:

输出描述:

转换输出题目中给出的所有ASCII到对应的字符。

参考代码:

#include <stdio.h>

int main()
{
    int i = 0;
    char arr[] = { 73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33 };
    int sz = sizeof(arr) / sizeof(arr[0]);
    for (i = 0; i < sz; i++)
    {
        printf("%c",arr[i]);
    }
    return 0;
}

5.出生日期输入输出

题目描述

输入一个人的出生日期(包括年月日),将该生日中的年、月、日分别输出。

输入描述:

输入只有一行,出生日期,包括年月日,年月日之间的数字没有分隔符。

输出描述:

三行,第一行为出生年份,第二行为出生月份,第三行为出生日期。输出时如果月份或天数为1位数,需要在1位数前面补0。

输入:

20130225

输出:

year=2013

month=02

date=25

参考代码:

#include <stdio.h>

int main()
{
    int year = 0;
    int month = 0;
    int day = 0;
    scanf("%4d%2d%2d",&year, &month, &day);
    printf("year=%d\n", year);
    printf("month=%02d\n", month);
    printf("day=%02d\n", day);
    return 0;
}

参考代码

#include <stdio.h>

int main()
{
    int weight = 0.0;
    int high = 0.0;
    double bmi = 0.0;
    scanf("%d %d",&weight, &high);
    bmi = weight / ((high / 100.0) * (high / 100.0));
    printf("%.2lf\n", bmi);
    return 0;
}

相关推荐

  1. C语言学习(8)—— 输入输出

    2024-04-12 07:18:05       45 阅读
  2. C语言代码 判断输入字符是不是字母

    2024-04-12 07:18:05       40 阅读
  3. C语言】(7)输入输出

    2024-04-12 07:18:05       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 07:18:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 07:18:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 07:18:05       82 阅读
  4. Python语言-面向对象

    2024-04-12 07:18:05       91 阅读

热门阅读

  1. 自动驾驶感知场景挖掘

    2024-04-12 07:18:05       37 阅读
  2. LiveData和ViewModel源码学习

    2024-04-12 07:18:05       33 阅读
  3. * ./cptable in ./node_modules/xlsx-style/dist/cpexcel.js

    2024-04-12 07:18:05       32 阅读
  4. Android中的drawable

    2024-04-12 07:18:05       28 阅读
  5. DA转换(数模转换)

    2024-04-12 07:18:05       37 阅读
  6. 18. 【Android教程】图片控件 ImageView

    2024-04-12 07:18:05       35 阅读
  7. Redis部署之哨兵

    2024-04-12 07:18:05       42 阅读
  8. 三维情况下的UWB定位原理和matlab原创函数源码

    2024-04-12 07:18:05       36 阅读
  9. Spring Boot中@KafkaListener使用${}动态指定topic

    2024-04-12 07:18:05       166 阅读
  10. 【SpringBoot】面试题汇总

    2024-04-12 07:18:05       39 阅读