我已经尝试了我所知道的一切以使其正常工作,但我不知道如何使该程序在按下转义键时终止任何帮助我知道我可以使用 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;
}