Я потратил бесчисленные часы в этой программе, но безрезультатно.Я считаю, что перечисление не работает в мой последний день.Моя программа ниже.
Она имеет дело с Dual Stacks и имеет много комментариев, чтобы помочь прочитать.Перечисление не работает должным образом, потому что, когда я «выталкиваю» из стека, это не выдает правильное перечисление / имя.Кроме того, время указывается в формате ЧЧ: ММ: СС и должно быть в формате СС: миллисекунды.Формат часов не имеет большого значения, так что если я хотел бы знать, ПОЧЕМУ перечисление не работает, и исправляет это.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <chrono>
#include <thread>
#include <fstream>
using namespace std;
const int MAX = 20;
typedef std::chrono::high_resolution_clock Clock;
int seed;
int upper;
int lower;
float randNum;
float randNumArr;
float randNumTie;
float randNumStar;
int dagabacount = 0;
int numberArrivals;
clock_t start;
double duration;
int fixedvehicles = 0;
enum alphabet {A =0,B, C, D, E, F, G, H,I, J, K,
L , M , N , O , P ,Q ,R, S, T , U, V , W , X , Y , Z};
alphabet alphS;
alphabet alphT;
string TieName;
string StarName;
//function for randomly generating number for delay time for ties
int randTieDelay(){
randNumTie = float(rand()) / (float(RAND_MAX) + 1.0);
if(randNumTie < 0.333){
return 2;
}
else if(.333 <= randNumTie < .66){
return 4;
}
else{
return 6;
}
}
//function for randomly generating number for delay time for stars
int randStarDelay(){
randNumStar = float(rand()) / (float(RAND_MAX) + 1.0);
if(randNumStar < 0.25){
return 3;
}
else if(.25 <= randNumStar < .50){
return 4;
}
else if(.50 <= randNumStar < .75){
return 7;
}
else{
return 10;
}
}
//struct for vmr
struct vmr{
char type;
string name;
int repairTime;
};
vmr item;
//declaring dualstack
class repairStack
{
private:
int topT;
int topS;
vmr ele[MAX];
public:
repairStack();
void pushTie (struct vmr);
void pushStar (struct vmr);
int popTie (vmr *item);
int popStar (vmr *item);
};
//initializing dualstack
repairStack::repairStack()
{
topT = -1;
topS = MAX;
}
//pushing to tiestack
void repairStack::pushTie( vmr v)
{
if( topS == topT + 1 )
{
cout<<"\nStack Overflow Tie Stack: Pushing to Dagaba"<<endl;
if(dagabacount == 6){
return;
}
else{
cout<< "Total ships sent to Dagaba: " << dagabacount <<endl;
dagabacount++;
return;}
}
topT++;
ele[topT] = v;
}
//pushing to starstack
void repairStack::pushStar( vmr v)
{
if( topS == topT + 1 )
{
cout<<"\nStack Overflow on Star Stack: Pushing to Dagaba" <<endl;
if(dagabacount == 6){
return;
}
else{
cout<< "Total ships sent to Dagaba: " << dagabacount <<endl;
dagabacount++;
return;}
}
topS--;
ele[topS] = v;
}
//popping to tiestack,checking for underflow, using system clock to start time of repair
//uses chrono clock and this_thread::sleep_for(); that is defined in c++11
int repairStack::popTie( vmr *item )
{
if( topT == -1 )
{
//cout<<"\nStack Underflow tStack";
return -1;
}
using std::chrono::system_clock;
std::time_t tt = system_clock::to_time_t (system_clock::now());
struct std::tm * ptm = std::localtime(&tt);
cout << "\nName: Tie" << item->name <<endl;
std::cout << "Start time: " << std::put_time(ptm,"%X") << '\n';
int delay = randTieDelay();
std::this_thread::sleep_for (std::chrono::seconds(delay));
cout << "Type: Tie Fighter" << endl;
cout << "Repair Time: " << delay << endl;
*item = ele[topT--];
return 0;
}
//popping to starstack, checking for underflow, using system clock to start time of repair
//uses chrono clock and this_thread::sleep_for(); that is defined in c++11
int repairStack::popStar( vmr *item )
{
if( topS == MAX )
{
//cout<<"\nStack Underflow sStack";
return -1;
}
using std::chrono::system_clock;
std::time_t tt = system_clock::to_time_t (system_clock::now());
struct std::tm * ptm = std::localtime(&tt);
cout << "\nName: Star"<< item->name <<endl;
std::cout << "Start time: " << std::put_time(ptm,"%X") << '\n';
int delay = randStarDelay();
std::this_thread::sleep_for (std::chrono::seconds(delay));
cout << "Type: Star Destroyer" << endl;
cout << "Repair Time: " << delay << endl;
*item = ele[topS++];
return 0;
}
//randomly generating how many arrivals come in each iteration
int getNumArrivals(){
randNumArr = float(rand()) / (float(RAND_MAX) + 1.0);
if(randNumArr < 0.25){
return 1;
}
else if(.25 <= randNumArr < .50){
return 2;
}
else if(.50 <= randNumArr < .75){
return 3;
}
else{
return 4;
}
}
int main()
{
//asking user for bounds
cout << "Please give seed value: ";
cin >> seed;
cout << "Please give lower bound of stack: ";
cin >> lower;
cout << "Please give upper bound of stack: ";
cin >> upper;
cout << "\n";
srand(seed);
//initalizing stack 's'
repairStack s = repairStack();
//initializing clock and entire duration of program
start = clock();
duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
//while rejected vehicles is under 5
while(dagabacount < 5){
//getting numberArrivals either 1,2,3,4
numberArrivals = getNumArrivals();
cout << "Number of Arrivals: " << numberArrivals;
int tfperloop = 0;
int sdperloop = 0;
for(int i = 0; i < numberArrivals; i++){
//getting random number between 0 and 1 to see if its T or S
randNum = float(rand()) / (float(RAND_MAX) + 1.0);
//if tie then creates and adds struct and pushes to tie stack
//gets next enumeration for ties
if(randNum < 0.75){
TieName = (char)(alphT + 65);
struct vmr Tie;
Tie.type='T';
Tie.repairTime = 3;
Tie.name = TieName;
s.pushTie(Tie);
int t = (int)alphT;
t= (t+1)%26;
alphT = (alphabet)t;
tfperloop++;
}
//if star then creates and adds struct and pushes to star stack
//gets next enumeration stars
else{
StarName = (char)(alphS + 65);
struct vmr Star;
Star.type='S';
Star.repairTime = 7;
Star.name = StarName;
s.pushStar(Star);
int g = (int)alphS;
g= (g+1)%26;
alphS = (alphabet)g;
sdperloop++;
}
}
//cout << "\n# of Tie Fighters in this Arrival: " << tfperloop <<endl;
//cout << "# of Star Destroyers in this Arrival: " << sdperloop <<endl;
//fixing star if star stack is not empty, if so, fix a tiefighter if theres one in stack
//uses chrono clock to get system time at that struct and localtime are libraries within C++
if(s.popStar(&item) == 0){
cout << "Status: Fixed" << endl;
using std::chrono::system_clock;
std::time_t tt = system_clock::to_time_t (system_clock::now());
struct std::tm * ptm = std::localtime(&tt);
std::cout << "Finish time: " << std::put_time(ptm,"%X") << endl << endl;
fixedvehicles++;
std::this_thread::sleep_for (std::chrono::seconds(2));
}
else{
if(s.popTie(&item) == 0){
cout << "Status: Fixed" << endl;
using std::chrono::system_clock;
std::time_t tt = system_clock::to_time_t (system_clock::now());
struct std::tm * ptm = std::localtime(&tt);
std::cout << "Finish time: " << std::put_time(ptm,"%X") << endl << endl;
fixedvehicles++;
std::this_thread::sleep_for (std::chrono::seconds(2));
}
else{
cout << "Underflow" << endl;
}
}
}
//print out some results of the program for user benefit
cout << "5 Vehicles Sent To Dagaba" << endl;
cout << "# of Vehicles Serviced: " << fixedvehicles << endl;
cout << "Average Time of Repair: " << duration / fixedvehicles << endl;
return 0;
}