0

我已经尝试了我所知道的一切以使其正常工作,但我不知道如何使该程序在按下转义键时终止任何帮助我知道我可以使用 getch 或 getchar 来获取 esc 键的击键但是我也希望它也能与我的其他输入一起工作......

我是初学者。有任何想法吗?如何用这个程序完成?

#include <bits/stdc++.h>
#include <conio.h>

using namespace std;

class Node {
public:
        int roll;
        string Name;
        string address;
        int contact;
        string email;
        Node* next;
};

Node* head = new Node();

bool check(int x)
{
        if (head == NULL)
                return false;

        Node* t = new Node;
        t = head;

        while (t != NULL) {
                if (t->roll == x)
                        return true;
                t = t->next;
        }

        return false;
}

void Insert_Record(int roll, string Name,
                   string address, int contact, string email)
{
        if (check(roll)) {
                cout << "Student with this "
                     << "record Already Exists\n";
                return;
        }
        Node* t = new Node();
        t->roll = roll;
        t->Name = Name;
        t->address = address;
        t->contact = contact;
        t->email = email;
        t->next = NULL;

        if (head == NULL|| (head->roll >= t->roll)) {
                t->next = head;
                head = t;
        }

        else {
                Node* c = head;
                while (c->next != NULL
                        && c->next->roll < t->roll) {
                        c = c->next;
                }
                t->next = c->next;
                c->next = t;
        }

        cout << "Record Inserted "
                << "Successfully\n";
}

void Search_Record(int roll)
{
        if (!head) {
                cout << "No such Record "
                        << "Avialable\n";
                return;
        }

        else {
                Node* p = head;
                while (p) {
                        if (p->roll == roll) {
                                cout << "Roll Nmuber\t"
                                        << p->roll << endl;
                                cout << "Name\t\t"
                                        << p->Name << endl;
                                cout << "Address\t"
                                        << p->address << endl;
                                cout << "Contact\t\t"
                                        << p->contact << endl;
                                cout << "Email\t\t"
                                        << p->email << endl;
                                return;
                        }
                        p = p->next;
                }

                if (p == NULL)
                        cout << "No such Record "
                             << "Avialable\n";
        }
}

int Delete_Record(int roll)
{
        Node* t = head;
        Node* p = NULL;

        // Deletion at Begin
        if (t != NULL && t->roll == roll) {
                head = t->next;
                delete t;

                cout << "Record Deleted "
                        << "Successfully\n";
                return 0;
        }

        // Deletion Other than Begin
        while (t != NULL && t->roll != roll) {
                p = t;
                t = t->next;
        }

        if (t == NULL) {
                cout << "Record does not Exist\n";
                return -1;
                p->next = t->next;

                delete t;
                cout << "Record Deleted "
                     << "Successfully\n";

                return 0;
        }
}

void Show_Record()
{
        Node* p = head;
        if (p == NULL) {
                cout << "No Record "
                        << "Available\n";
        }
        else {
                cout << "Roll-No\tName\tAddress\tContact\tEmail \n";

                while (p != NULL) {
                        cout<<""<< p->roll <<"\t\t"<< p->Name << "\t\t"<< p->address << "\t\t"<< p->contact << "\t\t"<< p->email << endl;
                        p = p->next;
                }
        }
}

int main()
{
        head = NULL;
        string Name, address, email;
        int Roll, contact;

        while (true) {
                cout << "\n\t\tWelcome "
                                "\n\n\tPress\n\t1 to Store New Data\n\t2 To Display the Record\n\t4 To delete a Data\n\t5 Press Escape Key to Exit\n";
                cout << "\nEnter your Choice\n";

                int Choice;
                
                cin>>Choice;
                if (Choice == 1) {
                        cout << "Enter Roll Number of Student\n";
                        cin >> Roll;
                        cout << "Enter Name of Student\n";
                        cin >> Name;
                        cout << "Enter The Address of Student \n";
                        cin >> address;
                        cout << "Enter  Contact of Student\n";
                        cin >> contact;
                        cout<< "Enter Email Address of the Student\n";
                        cin>> email;
                        Insert_Record(Roll, Name, address, contact, email);
                }
                else if (Choice == 2) {
                        cout << "Enter Roll Number of Student whose "
                                        "record you want to Search\n";
                        cin >> Roll;
                        Search_Record(Roll);
                }
                else if (Choice == 3) {
                        cout << "Enter Roll Number of Student whose "
                                        "record is to be deleted\n";
                        cin >> Roll;
                        Delete_Record(Roll);
                }
                else if (Choice == 0) {
                        exit (0);
                }
                else {
                        cout << "Invalid Choice "
                                << "Try Again\n";
                }
        }
        return 0;
}

4

1 回答 1

0

由于您已经在使用 conio.h,因此这个答案可能满足您的所有需求。
如何检测C中的ESC键?

请注意,conio/_getch 很可能会从 cin 流中“窃取”字符,因此您可能更简单地删除 cin,并且只使用 _getch/_kbhit()。

编辑:回应评论

这里几乎没有设计决定。通过试图绕过 C++ 模型的限制,您正在进入操作系统特定字符代码的稍微晦涩难懂的世界。您将需要决定要支持多少这些代码。这真的归结为可用性。
你想支持退格/删除吗?
光标键呢?
标签呢?此处
的表格给出了一些想法,这可能会变得多么复杂。但是,也许您不需要那么复杂?由你决定。

如果是我,我的目标是一次存储一行,在 std::string 中。在换行符上,创建一个std::istringstream来进行复杂的输入解析。

像所有用户界面设计一样,玩起来很有趣,但通常需要大量工作才能做到完美。

于 2021-08-19T11:56:38.940 回答