0

我使用链表的概念在 C++ 中制作了一个学生记录系统。该计划将采用学生证、姓名和分数进行 3 次不同的考试。然后它将根据这些考试分数计算总百分比,并根据总百分比显示相关消息。该程序正在运行,没有任何错误。

但我希望学生证是独一无二的。例如,如果用户输入了已经分配给前一个学生的学生 ID,那么它将显示“请输入不同的 ID”的消息。

我该怎么做。任何帮助将不胜感激。

struct student
{
int id;
char name[MAX];
string status;
double aggr;
int matric_marks, inter_marks, entryTest_marks;
};

struct node
{
struct student *data;
struct node *next;


node()                  
{                       
 data = 0;              
 next = NULL;            
}    
};

struct student *readStudent()
{
clearWindow();
struct student *stdnt = new student;
gotoxy(33,8);cout<<"Student ID: ";
while(!(cin >> stdnt->id) || cin.peek() != '\n')
{
     char ch;
     cin.clear();
     cout << "sorry";
     while(cin.get(ch) && ch != '\n');
}
cin.ignore();
gotoxy(33,10);cout<<"Student Name: ";
cin.getline(stdnt->name, 50);
gotoxy(33,12);cout<<"Enter Matriculation Marks: ";
cin>>(stdnt->matric_marks);


gotoxy(33,14);cout<<"Enter Intermediate Marks: ";
cin>>(stdnt->inter_marks);


gotoxy(33,16);cout<<"Enter Entry Test Marks: ";
cin>>(stdnt->entryTest_marks);
stdnt->aggr = calculate_aggregiate(stdnt);
gotoxy(33,18);cout<<"Student Aggregate Marks: "<< stdnt->aggr;
if (stdnt->aggr >= 70)
{
    gotoxy(33,20);cout<<"Student Registered In Electrical Engg";

}
else if (stdnt->aggr >= 60)
{
    gotoxy(33,22);cout<<"Student Registered In Mechanical Engg";

}
else if (stdnt->aggr >=50)
{
    gotoxy(33,24);cout<<"Student Registered In Computer Science";

}
else
{
    gotoxy(33,20);cout<<"Sorry! The Student Doesnt Qualify";

}

return stdnt;
}


struct node *createDatabase(int size)
{
int i = 0;

struct node *head = NULL;
struct node *last = NULL;


for(i = 0; i < size; i++)
{
    struct student *stdnt = readStudent();


    struct node * current = new node;
    current->data = stdnt;
    current->next = NULL;


    if(last)
        last->next = current;
    else
        head = current;


    last = current;
}
return head;
}


struct node  *InsertRecord(struct node *head)
{
struct  node *record = new node;

struct student *stdnt = readStudent();
record->data=stdnt;

record->next=head;

return record;
}

double calculate_aggregiate(struct student *stud)
{    
student *stdnt = stud;
double aggr;
aggr = stdnt->matric_marks * 10/100  + stdnt->inter_marks * 50/100 + 
    stdnt->entryTest_marks * 40/100;

return aggr;
}

struct node *DeleteRecord(struct node *head)
{
clearWindow();
struct node *curr,*prev, *temp;
int tdata;
if(head==NULL)
{
gotoxy(33,10);cout<<"NO RECORDS TO DELETE......!";
}
else
{
gotoxy(33,10);cout<<"Enter Student ID To Be Deleted: ";
cin>>tdata;
prev=curr=head;
while((curr!=NULL)&&(curr->data->id!=tdata))
{
prev=curr;
curr=curr->next;
}
if(curr==NULL)
{
gotoxy(33,12);cout<<"The Requested ID Is Not Found...!";
}
else if(curr==head)
{
head=head->next;
gotoxy(33,12);cout<<"DATA DELETED....!";
}
else
{
prev->next=curr->next;
if(curr->next==NULL)
{
  temp=prev;
}
gotoxy(33,12);cout<<"DATA DELETED....!"<<tdata;
}
delete(curr);
}    
return head;
}

void ModifyRecord(struct node *head)
{

 clearWindow();
 int ch, sid;
 struct node *current;
 struct student *stdnt;
 if (head==NULL)
 {
     gotoxy(33,8);cout<<"NO RECORD TO MODIFY..!";
 }
 else
 {
     gotoxy(33,8);cout<<"Enter Student ID To Modify: ";
     cin>>sid;
     current=head;
 while((current!=NULL) && (current->data->id!=sid))
 {
 current=current->next;
 }
 if (current==NULL) 
 {
 gotoxy(33,10);cout<<"The Requested ID is Not Found";
 }

 else if(current->data->id==sid)
 {
 gotoxy(33,10);cout<<"What Do You Want To Modify";
 gotoxy(33,12);cout<<"1. Student's Name";
 gotoxy(33,14);cout<<"2. Student's Matric Marks";
 gotoxy(33,16);cout<<"3. Student's Intermediate Marks";
 gotoxy(33,18);cout<<"4. Student's Entry Test Marks";
 gotoxy(33,20);cout<<"Enter Your Choice: ";
 cin>>ch;
 switch(ch)
 {
 case 1 :  gotoxy(33,22);cout<<"Enter New Name: ";
      cin.getline(current->data->name, 50);break;
 case 2 :  gotoxy(33,22);cout<<"Enter New Matric Marks: ";
      cin>>(current->data->matric_marks);break;
 case 3 :  gotoxy(33,22);cout<<"Enter New Intermediate Marks: ";
      cin>>(current->data->inter_marks);break;
 case 4 :  gotoxy(33,22);cout<<"Enter New Entry Test Marks: ";
      cin>>(current->data->entryTest_marks);break;
      current->data->aggr = current->data->matric_marks * 10/100  + current->data-               >inter_marks * 50/100 + 
    current->data->entryTest_marks * 40/100;
 }
 gotoxy(33,24);cout<<"RECORD MODIFIED....!";

 }


 }


 }


void SearchRecord(struct node *head)
{
clearWindow();
int s_id;
 struct node *current;
 if (head==NULL)
 {
     gotoxy(33,8);cout<<"NO RECORD TO SEARCH..!";
 }
 else
 {
     gotoxy(33,8);cout<<"Enter Student ID To Be Searched: ";
     cin>>s_id;
     current=head;
 while ((current!=NULL) && (current->data->id!=s_id))
{
 current=current->next;
}
 if (current==NULL) 
{
gotoxy(33,10);cout<<"The Requested ID is Not Found";
}

else     if (current->data->id==s_id)
    {
         gotoxy(33,10);cout<<"Student ID: "<<current->data->id;
         gotoxy(33,12);cout<<"Student Name: "<<current->data->name;
         gotoxy(33,14);cout<<"Student Matric Marks: "<<current->data->matric_marks;
         gotoxy(33,16);cout<<"Student Intermediate Marks: "<<current->data - >inter_marks;
         gotoxy(33,18);cout<<"Student Entry Test  Marks: "<<current->data- >entryTest_marks;
         gotoxy(33,20);cout<<"Student Aggregate Marks: "<<current->data->aggr;
    if (current->data->aggr >= 70)
{
    gotoxy(33,22);cout<<"Student Registered In Electrical Engg";

}
else if (current->data->aggr >= 60)
{
    gotoxy(33,22);cout<<"Student Registered In Mechanical Engg";

}
else if (current->data->aggr >=50)
{
    gotoxy(33,22);cout<<"Student Registered In Computer Science";

}
else
{
    gotoxy(33,22);cout<<"Sorry! The Student Doesnt Qualify";

}
     }     


 }

 }


void print(struct node *head)
{
clearWindow_p();

struct node *current = head;
if (head == NULL)
{
    gotoxy(33,8);cout<<"No Student Registered Yet......!";
}
else
{

    cout<<"\n\t\t\t\t\tSTUDENTS STATISTICS";
while(current)
{
    struct student *stdnt = current->data;
    cout<<"\n\t\t\t\t\t--------------------------------  ";

    cout<<"\n\t\t\t\t\tStudent ID   :"<<stdnt->id;
    cout<<"\n\t\t\t\t\tStudent Name   :"<<stdnt->name;
    cout<<"\n\t\t\t\t\tMatric Marks :"<<stdnt->matric_marks;
    cout<<"\n\t\t\t\t\tIntermediate Marks :"<<stdnt->inter_marks;
    cout<<"\n\t\t\t\t\tEntry Test Marks: "<<stdnt->entryTest_marks;
    cout<<"\n\t\t\t\t\tAggregate: "<<stdnt->aggr;
    if (stdnt->aggr >= 70)
{
    cout<<"\n\t\t\t\t\tStudent Registered In Electrical Engg";

}
else if (stdnt->aggr >= 60)
{
    cout<<"\n\t\t\t\t\tStudent Registered In Mechanical Engg";

}
else if (stdnt->aggr >=50)
{
    cout<<"\n\t\t\t\t\tStudent Registered In Computer Science";

}
else
{
    cout<<"\n\t\t\t\t\tSorry! The Student Doesnt Qualify";

}


    current=current->next;
}
    cout<<"\n\t\t\t\t\t--------------------------------";


}
}




int compareStudents(struct student *left, struct student *right)
{
    return strcmp(left->name, right->name);
}




struct node *sort(struct node *head)
{


    //using bubble sort


    int swapped = 0;


    do
    {    
        swapped = 0;
        struct node *current = head;
        struct node *previous = NULL;


        while(current && current->next)
        {
            if(compareStudents(current->data, current->next->data) > 0)
            {
                //swap here

                struct node *next = current->next;

                if(previous)
                {
                    previous->next = next;        
                }
                else
                {
                    head = next;
                }


                current->next = next->next;
                previous = next;
                previous->next = current;    
                swapped = 1;
            }
            else
            {
                previous = current;
                current = current->next;
            }


        }


    } while(swapped);    


    return head;
}


int main()
{
    system("color f0");
    window();
    SetColor(28);
        int total,i,choice;
        int x = 2;
        struct node *head;
        head=NULL;
        do
        {
            menu:
            gotoxy(x, 8);cout<<"1.Create a Record File";
            gotoxy(x, 10);cout<<"2.Insert Student's Record";
            gotoxy(x, 12);cout<<"3.Modify Student's Record";
            gotoxy(x, 14);cout<<"4.Delete Student's Record";
            gotoxy(x, 16);cout<<"5.Search Student's Record";
            gotoxy(x, 18);cout<<"6.Print All Records";
            gotoxy(x, 20);cout<<"7.Sort According To Names";
            gotoxy(x, 22);cout<<"8.Clear The Screen";
            gotoxy(x, 24);cout<<"9.Exit";
            gotoxy(x, 26);cout<<"Enter your choice";
            cout<<" [ ]\b\b";
            cin>>choice;
            switch(choice)
            {
                case 1:
                    clearWindow();
                    gotoxy(33, 8);cout<<"How Many Students Do You Want To Register: ";
                    cin>>total;
                    head=createDatabase(total);
                    break;
                case 2:
                    head=InsertRecord(head);
                    break;
                case 3:
                    ModifyRecord(head);
                    break;

                case 4:
                    DeleteRecord(head);
                    break;
                case 5:
                    SearchRecord(head);
                    break;
                case 6:
                    print(head);
                    break;
                case 7:
                    sort(head);
                    print(head);
                    break;
                case 8:
                    main();
                    break;
                default: gotoxy(33,8);cout<<"INVALID OPTION";
        }
        }while(choice!=9);
    getch();
    return 0;
} 
4

3 回答 3

2

由于您使用的是 C++,因此您应该利用内置类型。如果只有一个学生具有给定的 ID,请使用std::map<>模板。例如:

struct student
{
  char name[MAX];
  string status;
  double aggr;
  int matric_marks, inter_marks, entryTest_marks;
};

map<int, student> students;

然后,遍历学生:

for (auto i = students.begin(); i != students.end(); ++i) {
    int studentId = i->first;
    student& student = i->second;

    student.aggr = ...
}

auto是 C++11,并非所有编译器都默认启用 IIRC。请参见此处。)

于 2013-05-27T19:33:28.017 回答
0

如果您知道学生 id 的范围,并且在内存的可行范围内,则可以使用 UsedId[] 数组。

如果范围太大而无法容纳,请使用 Hashmap ,(C++ 中的 MAP)。

于 2013-05-27T19:28:27.827 回答
0

我可能会实现一个名为FindRecordByID.

struct student *FindRecordByID(struct node *head, int id) {
    for(; head != NULL; head = head->next)
        if (head->data->id == id)
            return head->data;
    return NULL;
}

然后,当您执行create_new_student例行程序时,您可以使用它来验证给定的 ID 是否尚未使用。

于 2013-05-27T19:48:05.677 回答