设定string型的试机座位号码,可以用顺序存储或链式存储。
顺序存储
#include <iostream>
using namespace std;
struct Node
{
string number;
int computer;
int set;
};
class Student
{
public:
void inputStudent(int n)
{
for(int i = 0 ;i < n ;i++)
{
cin >> a[i].number;
cin >> a[i].computer;
cin >> a[i].set;
}
}
void search(int m, int n)
{
int num;
for (int i = 0; i < m; i++)
{
cin >> num;
for(int j = 0; j < n; j++)
{
if(a[j].computer == num)
{
cout << a[j].number << " " << a[j].set << endl;
break;
}
}
}
return;
}
private:
Node a[100];
};
int main()
{
int n;
cin >> n;
Student s;
s.inputStudent(n);
int m;
cin >> m;
s.search(m, n);
return 0;
}
链式存储
#include <iostream>
using namespace std;
struct Node
{
string number;
int computer;
int set;
Node *next;
};
class Student
{
public:
Student()
{
head = NULL;
}
void inputStudent(int n)
{
Node *p;
Node *q;
for(int i = 0; i < n; i++)
{
p = new Node;
cin >> p->number;
cin >> p->computer;
cin >> p->set;
if(head == NULL)
{
head = p;
q = head;
}
else
{
q->next = p;
q = q->next;
}
}
}
void search(int m, int n)
{
Node *p = head;
int num;
for (int i = 0; i < m; i++)
{
cin >> num;
for(int j = 0; j < n; j++)
{
if(p->computer == num)
{
cout << p->number << " " << p->set << endl;
p = head;
break;
}
p = p->next;
}
}
return;
}
private:
Node *head;
};
int main()
{
int n;
cin >> n;
Student s;
s.inputStudent(n);
int m;
cin >> m;
s.search(m, n);
return 0;
}
2018/1/30