查找名字中有某个字

 

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
class Student
{
private:
    string name;

    int num;
    double ch;
    double mat;
    double average;

public:
    Student() {}
    Student(int nu, string na);
    friend void findStudent(Student s[],int nn, string sea);
    bool setChinese(double c)
    {
        if(c>=0&&c<=100)
        {
            ch=c;
            return 1;
        }
        else
        {
            ch=0;
            return 0;
        }
    }
    bool setMath(double math)
    {
        if(math>=0&&math<=100)
        {
            mat=math;
            return 1;
        }
        else
        {
            mat=0;
            return 0;
        }
    }
};
Student::Student(int nu, string na):num(nu),name(na)
{
}
void findStudent(Student s[],int nn, string sea)
{
    int i=sea.size();
    int j,k,f=0,u,w;
    for(k=0; k<nn; k++)
    {
        u=s[k].name.size();
        for(w=0; w<=u-1; w++)
        {
            for(j=0; j<=i-1; j++)
            {
                if(sea[j]!=s[k].name[j+w])
                {
                    break;
                }
            }
            if(j==i){
                break;
            }
        }
        if(j==i)
        {
            f++;
            cout<<s[k].num<<' '<<s[k].name<<' '<<s[k].ch<<' '<<s[k].mat<<' '<<(s[k].ch+s[k].mat)/2.0<<endl;
        }

    }
    if(f==0)
    {
        cout<<"Not found.";
    }

}
int main()
{
    int n,i;
    int number;
    string name, searchName;
    double math,chinese;
    cin>>n;
    Student stu[n];
    for(i=0; i<n; i++)
    {
        cin>>number>>name>>chinese>>math;
        stu[i] = Student(number, name);
        stu[i].setChinese(chinese);
        stu[i].setMath(math);

    }

    cin>>searchName;  //输入需要查找的姓名

    findStudent(stu, n, searchName);  //调用函数,查找学生,输出信息
    return 0;
}
 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值