江月何年初照人… 2022-04-30 01:14 采纳率: 100%
浏览 55
已结题

按要求补充编程片段 只要符合标准 秒采纳

img

img


类与对象题不要用太高级句子(最好多些注释) 必须按照他的提示填写 最好附上运行结果 只要测试后没问题 秒通过!

  • 写回答

2条回答 默认 最新

  • 下坠丷 2022-04-30 08:09
    关注
    
    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    class Patient {
    private:
        char name[10];
        char sex;        //m男,f女
        int age;    
        int weith;        
        char gm[5];        //yes or no
    public:
        //无参构造函数
        Patient()
        {
    
        }
    
        //带参构造函数
        Patient(char* name, char sex, int age, int weith, char* gm)
        {
            strcpy(this->name,name);
            this->sex = sex;
            this->age = age;
            this->weith = weith;
            strcpy(this->gm, gm);
        }
    
        //set函数
        void setName(char* name) { strcpy(this->name, name);}
        void setSex(char sex) { this->sex = sex; }
        void setAge(int age) { this->age = age; }
        void setWeith(int weith) { this->weith = weith; }
        void setGm(char* gm) { strcpy(this->gm, gm); }
    
        //get函数
        char* getName() { return name; }
        char getSex() { return sex; }
        int getAge() { return age; }
        int getWeith() { return weith; }
        char* getGm() { return gm; }
    
        //匹配函数
        bool isSame(char* target)
        {
            return strcmp(name, target) == 0;
        }
    
        //输出信息
        void display()
        {
            cout << name << ' ' << sex << ' ' << age << ' ' << weith << ' ' << gm << endl;
        }
    };
    
    int main()
    {
        int n;
        cin >> n;
        Patient* arr = new Patient[n];
        for (int i = 0; i < n; ++i)
        {
            char name[10],gm[5];
            char sex;
            int age, weith;
            cin >> name >> sex >> age >> weith >> gm;
            arr[i] = Patient(name, sex, age, weith, gm);
        }
    
        char target[10];
        cin >> target;
        //找到名字一样的就输出并置find为true 
        bool find = false;
        for (int i = 0; i < n; ++i)
        {
            if (arr[i].isSame(target))
            {
                arr[i].display();
                find = true;
            }
        }
    
        //未找到 
        if (!find)
        {
            cout << "not found\n";
        }
        return 0;
    }
    
    

    img

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月8日
  • 已采纳回答 4月30日
  • 创建了问题 4月30日