|
|
- //����2-2
- #include <iostream>
- #include <ctime>
- #include <stdlib.h>
- #include <time.h>
- #include <windows.h>
-
- using namespace std;
-
- #define Status int
- #define OVERFLOW 2
- #define ERROR 1
- #define OK 0;
- typedef int QElemType;
-
- int queue_number[10] = { 0 };//queue_number��¼��������
- int i, j = 0;//i���ڿͻ�������j���ڴ��ڼ���
- int no = 0;//no���ڼ�¼�ͻ����ţ����е�һλ�ͻ�������Ϊ1
- int time_length = 0;//time_length��¼ģ����������ʱ��
- int window_number = 0;
-
- typedef struct QNode
- {
- int no;//����
- double amount;//�ܽ���
- double remain_amount;//δȡ������Ƶ�ǰ�֣�
- struct QNode* next;
- }QNode, * QueuePtr;
-
- typedef struct
- {
- QueuePtr front; //��ͷָ��
- QueuePtr rear; //��βָ��
- }LinkQueue;
-
- Status InitQueue(LinkQueue& Q)
- {
- Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
- if (!Q.front) exit(OVERFLOW);
- Q.front->next = NULL;
- return OK;
- }
-
- Status DestroyQueue(LinkQueue& Q)
- {
- while (Q.front)
- {
- Q.rear = Q.front->next;
- free(Q.front);
- Q.front = Q.rear;
- }
- return OK;
- }
-
- Status QueueEmpty(LinkQueue Q)
- {
- return (Q.front == Q.rear);
- }
-
- Status GetHead(LinkQueue Q, int& e1, double& e2, double& e3)
- {
- if (Q.front == Q.rear) return ERROR;
- e1 = Q.front->next->no;
- e2 = Q.front->next->amount;
- e3 = Q.front->next->remain_amount;
- return OK;
- }
-
- Status EnQueue(LinkQueue& Q, int& e1, double& e2, double& e3)
- {
- QueuePtr p;
- p = (QueuePtr)malloc(sizeof(QNode));
- if (!p) exit(OVERFLOW);
- p->no = e1;
- p->amount = e2;
- p->remain_amount = e3;
- p->next = NULL;
- Q.rear->next = p;
- Q.rear = p;
- return OK;
- }
-
- Status DeQueue(LinkQueue& Q, int& e1, double& e2, double& e3)
- {
- if (Q.front == Q.rear) return ERROR;
- QueuePtr p;
- p = Q.front->next;
- e1 = p->no;
- e2 = p->amount;
- e3 = p->remain_amount;
- Q.front->next = p->next;
- if (Q.rear == p) Q.rear = Q.front;
- delete p;
- return OK;
- }
-
- int MinQueue()
- {
- int s = 10000, k = 0, temp;
- for (k = 0;k < window_number;k++)
- {
- if (queue_number[k] < s)
- {
- temp = k;
- s = queue_number[k];
- }
- }
- return temp;
- }
-
- int MaxQueue()
- {
- int s = -1, k = 0, temp;
- for (k = 0;k < window_number;k++)
- {
- if (queue_number[k] > s)
- {
- temp = k;
- s = queue_number[k];
- }
- }
- return temp;
- }
-
- void Remove(LinkQueue& Q)//������һ�����Ƿ���ȡ�ꡣע���ж϶����Ƿ�Ϊ�ա�
- {
- if (!QueueEmpty(Q))
- {
- if (Q.front->next->remain_amount <= 1e-8)
- {
- int e1 = -1;
- double e2 = -1, e3 = -1;
- DeQueue(Q, e1, e2, e3);
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
- FOREGROUND_INTENSITY | FOREGROUND_GREEN);//������ɫ
- cout << "��" << (double)i / 10 + 0.1 << "��ʱ" << j + 1 << "�Ŵ���"
- << "��" << e1 << "λ�ͻ�����ҵ����ȡ�߹�" << e2 << "��Ԫ��" << endl;
- queue_number[j]--;
- }
- }
- }
-
- void Join(LinkQueue* q)
- {
- int a = rand() % 60;
- //a��ֵ��0-60֮�������ֲ���ֻ�е�a=0��window_number-1ʱ�Ų����¿ͻ�
- //ƽ��ÿ6������window_number���ͻ����봰��������Ӧ
- if (a < window_number)
- {
- no++;
- double b = double(rand() % 191) / 10 + 1;//b��ֵ��1-20֮�������ֲ�(����0.1)�������û�ȡǮ�Ľ���������λ����Ԫ��
- int m = MinQueue();
- EnQueue(q[m], no, b, b);
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
- FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);//���ú�ɫ����ɫ����
- cout << "��" << (double)i / 10 + 0.1 << "��ʱ��" << no << "λ�ͻ����봰��"
- << m + 1 << "���У���ȡ" << b << "��Ԫ��" << endl;
- queue_number[m]++;
- }
- }
-
- void ChangeQuene(LinkQueue* q)
- {
- int min = MinQueue(), max = MaxQueue();
- while (queue_number[max] - queue_number[min] >= 2)
- {
- int e1;
- double e2, e3;
- DeQueue(q[max], e1, e2, e3);
- EnQueue(q[min], e1, e2, e3);
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
- FOREGROUND_INTENSITY | FOREGROUND_BLUE);//������ɫ
- cout << "��" << e1 << "λ�ͻ��Ӵ���" << max + 1 << "���е���������" << min + 1 << "����" << endl;
-
- queue_number[min]++;
- queue_number[max]--;
- min = MinQueue();
- max = MaxQueue();
- }
- }
-
- void Withdraw(LinkQueue& Q)
- {
- if (!QueueEmpty(Q) && i != time_length)
- {
- Q.front->next->remain_amount -= 0.2;//ÿ0.1��ȡ��0.2��
- }
- }
-
- void Undone(LinkQueue& Q)
- {
- while (!QueueEmpty(Q))
- {
- int e1 = -1;
- double e2 = -1, e3 = -1;
- DeQueue(Q, e1, e2, e3);
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
- FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);//������ɫ����ɫ����
- cout << "����" << j + 1 << "���е�" << e1 <<
- "λ�ͻ�δ����ҵ����δȡ��" << e2 << "��Ԫ��" << endl;
- }
- }
-
- int main()
- {
- cout << "������ģ����������ʱ������λ���룩��1-300֮������������" << endl;
- //time_length = 120;
- cin >> time_length;
- while (time_length <= 0 || time_length > 300)
- {
- cout << "��������ʱ�䲻����Ҫ�������������룺";
- cin >> time_length;
- }
-
- cout << "���������д�������1-10֮������������" << endl;
- cin >> window_number;
- while (window_number <= 0 || window_number > 10)
- {
- cout << "�������Ĵ�����������Ҫ�������������룺";
- cin >> window_number;
- }
-
- LinkQueue* q = (LinkQueue*)malloc(window_number * sizeof(LinkQueue));
-
- //���г�ʼ��
- for (j = 0;j < window_number;j++)
- {
- InitQueue(q[j]);
- }
-
- srand(time(nullptr));//��������������
-
- //���Ĵ���
- for (i = 0;i <= time_length * 10;i++)//ÿ0.1��ѭ��һ�Σ�ֱ������Ԥ��ʱ��
- {
- //�ȼ�����һ�����Ƿ���ȡ�ꡣע���ж϶����Ƿ�Ϊ��
- for (j = 0;j < window_number;j++)
- {
- Remove(q[j]);
- }
-
- //�ٰ��¿ͻ��ӵ������ٵĶ���
- Join(q);
-
- //���ж��Ƿ��пͻ���Ҫ����ѡ������
- ChangeQuene(q);
-
- //�ٸ���ǰ������һλȡǮ��ע���ж϶����Ƿ�Ϊ��
- //ע��i=time_lengthʱȡǮҵ���Ѿ�ֹͣ
- for (j = 0;j < window_number;j++)
- {
- Withdraw(q[j]);
- }
-
- Sleep(93);
- //100��λΪ���룬��0.1�롣���ǵ���������ʱ�䣬��100��Ϊ93
- //����Sleep()�����ľ��Ȳ��ߣ�����ѭ����������̫С
- }
-
- //��������ǰ������δȡ��Ǯ���˵�����
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
- FOREGROUND_INTENSITY);//������ɫ��û��������ɫ����Ϊԭɫ
- cout << endl << "ȡǮҵ��������" << endl << endl;
- for (j = 0;j < window_number;j++)
- {
- Undone(q[j]);
- }
-
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
- FOREGROUND_INTENSITY);//������ɫ��û��������ɫ����Ϊԭɫ
- return 0;
- }
|