C++实验一
面向过程的整形队列编程
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include<malloc.h>
struct Queue {
int* const elems;
const int max;
int head, tail;
};
void initQueue(Queue* const p, int m);
void initQueue(Queue* const p, const Queue& s);
void initQueue(Queue* const p, Queue&& s);
int number(const Queue* const p);
int size(const Queue* const p);
Queue* const enter(Queue* const p, int e);
Queue* const leave(Queue* const p, int& e);
Queue* const assign(Queue* const p, const Queue& q);
Queue* const assign(Queue* const p, Queue&& q);
char* print(const Queue* const p, char* s);
void destroyQueue(Queue* const p);
void initQueue(Queue* const p, int m) {
if (p == nullptr){
throw"The pointer must have allocated memory!";
return;
}
(int&)p->max = m;
(int*&)p->elems = new int[m];
p->head = 0;
p->tail = 0;
}
void initQueue(Queue* const p, const Queue& s) {
if (p == nullptr){
throw"The pointer must have allocated memory!";
return;
}
p->head = s.head;
p->tail = s.tail;
(int&)p->max = s.max;
(int*&)p->elems = new int[s.max];
int i = s.head;
while (i != s.tail) {
p->elems[i] = s.elems[i];
i++;
i = i % p->max;
}
}
void initQueue(Queue* const p, Queue&& s) {
if (p == nullptr){
throw"The pointer must have allocated memory!";
return;
}
if (p == &s) {
throw"Can't init a queue which is already inited!";
return;
}
p->head = s.head;
p->tail = s.tail;
(int&)p->max = s.max;
(int*&)p->elems = s.elems;
(int *&)s.elems = nullptr;
s.head = 0;
s.tail = 0;
(int&)s.max = 0;
}
int number(const Queue* const p) {
if (p->elems == nullptr)return NULL;
int head = p->head;
int tail = p->tail;
int max = p->max;
return (tail - head + max) % max;
}
int size(const Queue* const p) {
return p->max;
}
Queue* const enter(Queue* const p, int e) {
int head = p->head;
int tail = p->tail;
if ((tail+1) % p->max == head) {
throw"Queue is full!";
return p;
}
p->elems[tail] = e;
p->tail = (p->tail+1) % (p->max);
return p;
}
Queue* const leave(Queue* const p, int& e) {
if (p->head == p->tail) {
throw"Queue is empty!";
return p;
}
e = p->elems[p->head];
p->head = (p->head+1) % (p->max);
return p;
}
Queue* const assign(Queue* const p, const Queue& q) {
p->head = q.head;
p->tail = q.tail;
(int &)p->max = q.max;
for (int i = 0; i < p->max; i++) {
p->elems[i] = q.elems[i];
}
return p;
}
Queue* const assign(Queue* const p, Queue&& q) {
if (p == &q)return p;
if (p->elems!=nullptr)delete []p->elems;
(int*&)(p->elems) = q.elems;
(int&)(p->max) = q.max;
p->head = q.head;
p->tail = q.tail;
(int*&)(q.elems) = nullptr;
(int&)(q.max) = 0;
q.head = q.tail = 0;
return p;
}
char* print(const Queue* const p, char* s) {
if (p->max == 0) {
throw"Can't print a nonexistent Queue!";
return nullptr;
}
char* q = s;
int i = p->head;
while (i != p->tail) {
if(i==p->head)sprintf(q, "%d", p->elems[i]);
else sprintf(q+strlen(q), "%d", p->elems[i]);
if (i!=(p->tail-1)%p->max)
sprintf(q+strlen(q), ",");
i++;
if (i == p->max) i = 0;
}
return s;
}
void destroyQueue(Queue* const p) {
if (p->elems != nullptr) {
delete[]p->elems;
(int*&)p->elems = nullptr;
(int &)p->max = 0;
}
else throw"Can't deconstruct an invild queue!";
}