繁体   English   中英

使用C ++创建患者程序菜单的建议

[英]Suggestions with creating a Menu for Patient Program in C++

因此,我基本上已经完成了大部分编程工作。 我想添加一个显示输出的菜单。 例如,创建记录列表。 输入所有内容后,我希望它在从菜单中选择一个数字时仅显示吸烟,高血压或高脂饮食的患者。 现在,我一直试图找出答案,但似乎无法绕开它。 关于如何使它工作的任何建议? 现在,它仅设置为一次显示所有记录。

//程序说明:显示患者记录的界面程序。

#include <iostream>
#include <string>
using namespace std;

struct Node
{
    char name[20];
    int age;
    string smoker;
    string hbp;
    string hfd;
    int points;
    Node *ptr;
};

Node *startPtr = NULL;

void getInfo()
{
    Node *p, *p2;
    p = new Node;

    int agePoints;
    int smkPoints;
    int hbpPoints;
    int hfdPoints;

    cout << "   Please enter the name of the patient : ";
    cin >> p->name;
    cout << "   Please enter the age of the patient : ";
    cin >> p->age;
    cout << "   Is he/she a smoker (y/n) : ";
    cin >> p->smoker;
    cout << "   Does he/she have high blood pressure? (y/n) : ";
    cin >> p->hbp;
    cout << "   Does he/she have a high fat diet? (y/n) : ";
    cin >> p->hfd;
    cout << endl;
    p->ptr = NULL;

    // Age point system

    if (p-> age > 30) {
        agePoints = 3;
    }

    else if (p->age > 20)
    {
        agePoints = 2;
    }

    else
    {
        agePoints = 1;
    }

    // Habits Points System
    if (p->smoker == "Y" || p->smoker == "y")
    {
        p->smoker = "Yes";
        smkPoints = 4;
    }
    else
    {
        p->smoker = "non smoker";
        smkPoints = 0;
    }

    if (p->hbp == "Y" || p->hbp == "y")
    {
        p->hbp = "High blood pressure";
        hbpPoints = 2;
    }
    else
    {
        p->hbp = "Normal blood pressure";
        hbpPoints = 0;
    }

    if (p->hfd == "Y" || p->hfd == "y")
    {
        p->hfd = "High fat diet";
        hfdPoints = 1;
    }
    else
    {
        p->hfd = "Low fat diet";
        hfdPoints = 0;
    }

    p->points = agePoints + smkPoints + hbpPoints + hfdPoints;

    // Set up link to this node
    if (startPtr == NULL) {
        startPtr = p;
    }
    else
    {
        p2 = startPtr;
        while (p2->ptr != NULL)
            p2 = p2->ptr;
        p2->ptr = p;
    }
}

void delete_start_node()
{
    Node *p;
    p = startPtr;
    startPtr = startPtr->ptr;
    delete p;
}

void delete_end_node()
{
    Node *p1, *p2;
    if (startPtr == NULL)
        cout << "The List is empty!\n";
    else
    {
        p1 = startPtr;
        if (p1->ptr == NULL)
        {
            delete p1;
            startPtr = NULL;
        }
        else
        {
            while (p1->ptr != NULL)
            {
                p2 = p1;
                p1 = p1->ptr;
            }
            delete p1;
            p1->ptr = NULL;
        }
    }
}
void disp()
{
    Node *p;
    p = startPtr;

    if (p == NULL)
        cout << "Empty List!\n";

    while (p != NULL)
    {
        if (p == NULL)
            cout << "Empty List!\n";
        cout << "   Name               : " << p->name << endl;
        cout << "   Age                : " << p->age << endl;
        cout << "   Smoker             : " << p->smoker << endl;
        cout << "   Blood Pressure     : " << p->hbp << endl;
        cout << "   Fat Diet           : " << p->hfd << endl;
        cout << "   Points             : " << p->points << endl;
        cout << endl;
        p = p->ptr;
    }
}

Node* removeptr(Node* prev)
{
    if (prev)
    {
        if (prev->ptr)
        {
            Node* p = prev->ptr;
            prev->ptr = p->ptr;
            return p;
        }
    }
    else if (startPtr)
    {
        Node* p = startPtr;
        startPtr = startPtr->ptr;
        return p;
    }
    return NULL;
}

// sort by age in ascending order
void sortAge()
{
    Node* startPtr2 = NULL;

    while (startPtr)
    {
        Node* prev = NULL;
        Node* curr = startPtr;
        Node* prevMax = NULL;
        int max = startPtr->age;

        while (curr)
        {
            if (curr->age > max)
            {
                max = curr->age;
                prevMax = prev;
            }
            prev = curr;
            curr = curr->ptr;
        }

        // Node with the highest age found pass throug the list.
        Node* xferNode = removeptr(prevMax);

        if (xferNode)
        {
            xferNode->ptr = startPtr2;
            startPtr2 = xferNode;
        }
    }
    startPtr = startPtr2;
}

int main()
{
    Node *p;
    p = startPtr;

    char selection;

    do
    {
        cout << "               Patient Menu\n";
        cout << "   =============================================\n";
        cout << "   1. Insert a new record\n";
        cout << "   2. List smoker patients\n"; // smoker patient
        cout << "   3. List HBP patients\n"; // high blood pressure patient
        cout << "   4. List HFD patients\n"; // high fat diet patient
        cout << "   5. Delete a patient record by given name\n";
        cout << "   6. Exit the program\n";
        cout << "   =============================================\n";
        cout << "   Enter your selection: ";
        cin >> selection;
        cout << endl;

        switch (selection)
        {
            case '1':
                getInfo();
                break;

            case '2':
                disp();
                break;

            case '3':
                break;

            case '4':
                break;

            case '5':
                break;

            case '6':
                cout << "   Goodbye!\n";
                return 0;
                break;

            default:
                break;
        }
    }while (selection != 6);
    /*
    disp();
    cout << "________________________________________________\n";
    sortAge();
    disp();*/

    system("pause");
    return 0;
}
Node *smokers, *hbp, *hfd = NULL;

您可以为每种类型保留单独的链接列表-吸烟者,hbp,hfd和更改

void disp()
// to
void disp(Node* linkedList)

并给它打印清单

case '2':
disp(smokers);
break;

case '3':
disp(hbp);
break;

case '4':
disp(hfd);
break;

顺便说一句,闻起来像是作业:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM