#define _CRT_SECURE_NO_WSRNINGS
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#define max 200
struct StudentMessage
{
string ClassName;
string sex;
string StudentName;
string StudentID;
};
struct SqQueue
{
StudentMessage* m_Data;
int m_Front;
int m_Rear;
};
int init_sqQueue(SqQueue& sqQueue)
{
sqQueue.m_Data = new StudentMessage[max];
if (!sqQueue.m_Data)
{
cout << "the member is full " << endl;
return 0;
}
sqQueue.m_Front = sqQueue.m_Rear = 0;
return 1;
}
int En_Quenue(SqQueue& sqQueue, StudentMessage data)
{
if ((sqQueue.m_Rear + 1) % max == sqQueue.m_Front)
{
cout << "queue is full" << endl;
return 0;
}
sqQueue.m_Data[sqQueue.m_Rear].ClassName= data.ClassName;
sqQueue.m_Data[sqQueue.m_Rear].sex = data.sex;
sqQueue.m_Data[sqQueue.m_Rear].StudentID = data.StudentID;
sqQueue.m_Data[sqQueue.m_Rear].StudentName = data.StudentName;
sqQueue.m_Rear = (sqQueue.m_Rear + 1) % max;
return 1;
}
void Get_Head(SqQueue& sqQueue)
{
if (sqQueue.m_Front != sqQueue.m_Rear)
{
cout << sqQueue.m_Data[sqQueue.m_Front].ClassName << " " << sqQueue.m_Data[sqQueue.m_Front].sex << " " <<
sqQueue.m_Data[sqQueue.m_Front].StudentID << " " << sqQueue.m_Data[sqQueue.m_Front].StudentName << endl;
}
else
{
cout << "the queue is empty" << endl;
}
}
int De_queue(SqQueue& sqQueue, StudentMessage& newStu)
{
if (sqQueue.m_Front == sqQueue.m_Rear)
{
cout << "queue is empty " << endl;
return 0;
}
else
{
newStu.ClassName = sqQueue.m_Data[sqQueue.m_Front].ClassName;
newStu.sex = sqQueue.m_Data[sqQueue.m_Front].sex;
newStu.StudentID = sqQueue.m_Data[sqQueue.m_Front].StudentID;
newStu.StudentName = sqQueue.m_Data[sqQueue.m_Front].StudentName;
sqQueue.m_Front = (sqQueue.m_Front + 1) % max;
return 1;
}
}
void Sqque_homework()
{
cout << "*****************Squeuce Function *****************" << endl;
cout << "***********1.Init ****************" << endl;
cout << "***********2.Enqueue ****************" << endl;
cout << "***********3.Get Head****************" << endl;
cout << "***********4.Dequeue ****************" << endl;
cout << "***********0.Quit ****************" << endl;
int choose = -1, flag = 0;
SqQueue myQueue;
while (choose != 0)
{
cout << "Please input your choose :" << endl;
cin >> choose;
switch (choose)
{
case 1:
{
if (init_sqQueue(myQueue))
{
flag = 1;
cout << "init success " << endl;
}
else
{
cout << "init fail " << endl;
}
}
break;
case 2:
{
if (flag)
{
flag = 1;
string FileName;
cout << "Please input fileName " << endl;
cin >> FileName;
fstream ifs;
ifs.open(FileName, ios::in);
if (!ifs)
{
cout << "File open Fail" << endl;
}
while (!ifs.eof())
{
StudentMessage newData;
ifs >> newData.ClassName >> newData.sex >> newData.StudentID >> newData.StudentName;
En_Quenue(myQueue, newData);
}
cout << "the message is loaded in" << endl;
cout << "the rear is " << myQueue.m_Data[myQueue.m_Rear].StudentName << endl;
ifs.close();
}
}
break;
case 3:
{
if (flag != -1 && flag != 0)
{
cout << "the head data is " << endl;
Get_Head(myQueue);
}
}
break;
case 4:
{
if (flag == 1 && myQueue.m_Front != myQueue.m_Rear)
{
cout << " the data dequeued is " << endl;
StudentMessage newData;
while (De_queue(myQueue, newData))
{
flag = -1;
cout << " the data dequeue is " << newData.ClassName << " "
<< newData.sex << " " << newData.StudentID << " " << newData.StudentID << " " << newData.StudentName << endl;
}
}
else
{
cout << "the list is empty !" << endl;
}
}
break;
default:
break;
}
}
}
int main()
{
Sqque_homework();
system("pause");
return 0;
}