16考研西交915编程题

一、剔除数字字符

问题描述

编写一个函数,其功能是将字符串 S S S 中所有的数字字符去掉,保留其余字符,并且将形成的新字符串存储在原 S S S 的空间中。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 100010;
char s[N];

bool isdigit(char c)
{
    return c >= '0' && c <= '9';
}

int main()
{
    scanf("%s", s);
    int n = strlen(s);
    for (int i = n; i >= 0; i --)
    {
        int j = i, k = i;
        while (j - 1 >= 0 && isdigit(s[j - 1])) j --;
        while (s[k]) s[j ++] = s[k ++];
        s[j] = '\0';
    }
    printf("%s\n", s);
    
    return 0;
}

在这里插入图片描述

二、统计各位上等于n的数字数目

问题描述

编写一个函数,其功能是从一个整数 m m m 中,统计其中各位上等于 n n n 的数字数目,并返回。
其中 0 < = n < = 9 0<=n<=9 0<=n<=9 ,若 n n n 越界,则返回 − 1 -1 1,并提示“第二个参数越界”。
例如: 0123456789 0123456789 0123456789 中有 0 0 0 2 2 2 个,编写主函数并调试。

#include <iostream>

using namespace std;

int func(int m, int n)
{
    if (n < 0 || n > 9)
    {
        puts("第二个参数越界");
        return -1;
    }
    
    int res = 0;
    while (m)
    {
        if (m % 10 == n) res ++;
        m /= 10;
    }
    return res;
}

int main()
{
    int n;
    cin >> n;
    while (n -- )
    {
        int a, b;
        cin >> a >> b;
        if (func(a, b) != -1)
            cout << func(a, b) << endl;
    }
    
    return 0;
}

在这里插入图片描述

三、学生出勤情况管理系统

问题描述

建立一个学生在某一个课程到课情况统计程序。
功能要求:
(1)可一次性输入所有学生的到课情况。输入学生总人数,该课程总课时,学生学号及其到课情况,分为正常,迟到,请假,旷课;
(2)可统计某一个学生的到课情况的上课率(包括正常,迟到)、旷课率,并输出;
(3)可统计所有学生的上课率、旷课率,并输出。
问题:
1)写出所用的数据结构;
2)写出算法描述;
3)写出源程序。

#include <iostream>
#include <cstring>

using namespace std;

const int N = 10;

int n;  // 学生总人数
int course; // 总课时

struct Stu
{
    char id[20];// 学号
    int st[4];  // 学生的到课情况,st[i](i = 0, 1, 2, 3)分别表示正常,迟到,请假,旷课
    double attend; // 上课率
    double pass;   // 旷课率
} s[N];

void show_one_stu(char id[20])  // 统计一个学生的到课情况
{
    for (int i = 0; i < n; i ++)
        if (strcmp(id, s[i].id) == 0)
        {
            puts("查找成功!");
            printf("学号:%s, 上课率:%.2lf, 旷课率:%.2lf\n\n", id, s[i].attend, s[i].pass);
            break;
        }
}

void show_all_stu() // 统计所有学生的到课情况
{
    for (int i = 0; i < n; i ++)
        printf("学号:%s, 上课率:%.2lf, 旷课率:%.2lf\n", s[i].id, s[i].attend, s[i].pass);
}

int main()
{
    cout << "学生总人数:";
    cin >> n;
    cout << "总课时:";
    cin >> course;

    puts("请依次输入所有学生的到课情况...");
    for (int i = 0; i < n; i ++)
    {
        cout << "学生学号:";
        cin >> s[i].id;
        cout << "正常,迟到,请假,旷课次数:";
        for (int &j : s[i].st) cin >> j;
        s[i].attend = (double) (s[i].st[0] + s[i].st[1]) / course;
        s[i].pass = (double) (s[i].st[2] + s[i].st[3]) / course;
    }

    char id[20];
    cout << "请输入要查询的学生的学号:";
    cin >> id;
    show_one_stu(id);

    puts("所有学生的到课情况:");
    show_all_stu();

    return 0;
}

请添加图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值