#include <iostream> using std::cout; using std::cin; using std::endl; #include "q2.h" int main() { int inputmax,tmp,check; inputmax=5; bool ok; Queue QueueSort; #ifdef DEBUGs //Debug QueueSort.add(8); QueueSort.add(1); QueueSort.around(); QueueSort.around(); QueueSort.around(); QueueSort.around(); QueueSort.around(); // cout << "del is:"<<QueueSort.del()<<"\n"; cout << "getfront:" << QueueSort.getfront() << "getrear" << QueueSort.getrear(); #endif for (int i=0 ; i<inputmax; i++) { ok=true; cout << "please enter "<< i+1 <<"th number =>"; cin >> tmp; if (i < 2) { QueueSort.add(tmp); QueueSort.pr(); } else { // QueueSort.del(check); check=QueueSort.getfront(); // tmp=QueueSort.getrear(); #ifdef DEBUG //Debug cout << "front:" << check << " rear:" << tmp << " "; QueueSort.pr(); #endif while (ok) { if (check < tmp) { //front < input #ifdef DEBUG //Debug cout << "add-cin check:" << check << " tmp:" << tmp << " "; QueueSort.pr(); #endif QueueSort.add(tmp); QueueSort.pr(); ok=false; } else { QueueSort.around(); // QueueSort.add(tmp); QueueSort.pr(); } check=QueueSort.getfront(); } #ifdef DEBUGs //Debug int aa; cin >> aa; #endif cout << "Sort Number:\n"; do { if (QueueSort.getfront()>QueueSort.getrear()) { QueueSort.pr(); //break; } else { QueueSort.around(); QueueSort.pr(); } } while (QueueSort.getfront()<QueueSort.getrear()); } } return 0; }
http://pastie.org/255656
q2.h
#ifndef QUEUE_H #define QUEUE_H class Queue { public: Queue(); void qFull(); void qEmpty(); void add(const int x); int del(); int getfront(); int getrear(); void pr(); void around(); bool IsFull(); bool IsEmpty(); private: int front,rear; int q[6]; int max; }; Queue::Queue() { front=-1; rear=-1; max=6; for (int i=0;i<max;i++) q[i]=0; } void Queue::qFull() { cout << "\nQueue is Full!!\n";//傳出滿了 } void Queue::qEmpty() { cout << "\nQueue is Empty!!\n";//傳出空的 } inline void Queue::add(const int x) { #ifdef DEBUG //Debug cout << "before add() front:" << front << " rear:" << rear << " "; pr(); #endif int newrear=(rear+1)%max; //測滿了沒 if (front==newrear) qFull(); //如果滿了就不加 else q[rear=newrear]= x; //若沒有就加入 #ifdef DEBUG //Debug cout << " after add() front:" << front << " rear:" << rear << " "; pr(); #endif } inline bool Queue::IsFull() { if (rear==(max-1)) return true; else return false; } inline bool Queue::IsEmpty() { if (front==rear) return true; else return false; } inline int Queue::del() { #ifdef DEBUG //Debug cout << "before del() front:" << front << " rear:" << rear << " "; pr(); #endif int x; if (front==rear) { qEmpty(); return 0; } else { front=(front+1)%max; x = q[front]; //傳回值 q[front]=0; //清除 } #ifdef DEBUG //Debug cout << " after del() front:" << front << " rear:" << rear <<" "; pr(); #endif return x; } int Queue::getfront() { int newfront=(front+1)%max; #ifdef DEBUG //Debug cout << "getfront() newfront:" << newfront << " rear:" << rear << " q:" << q[newfront] <<"\n"; #endif return q[newfront]; } int Queue::getrear() { #ifdef DEBUG //Debug cout << "getrear() front:" << front << " rear:" << rear << " q:"<<q[rear] <<"\n"; #endif return q[rear]; } inline void Queue::pr() { //print from first to rear cout << "List:"; int newfront=(front+1)%max; int newrear=(rear+1)%max; for (int i=0;i<max-1;i++) { cout << " " << q[newfront]; newfront=(newfront+1)%max; // cout << " " <<q[i]; } cout << "\n"; } void Queue::around() { //旋轉 #ifdef DEBUG //Debug cout << "Start Around\n"; #endif add(del()); #ifdef DEBUG //Debug cout << "End Around\n"; #endif } #endif
http://pastie.org/255658
沒有留言:
張貼留言