在C ++中创建患者程序菜单的建议

时间:2014-12-08 18:46:32

标签: c++ visual-c++ menu

所以我基本上已经完成了大部分编程工作。我想添加一个显示输出的菜单。例如,创建记录列表。全部输入后,我希望它只显示从菜单中选择一个数字时吸烟或有高血压或高脂肪饮食的患者。现在我被困在试图解决这个问题,但似乎无法绕过它。有关如何使其工作的任何建议?现在它只设置为一次显示所有记录。

//程序描述:显示患者记录的界面程序。

#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;
}

1 个答案:

答案 0 :(得分: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;
不过,闻起来像家庭作业:)