Question

The provided code is my solution, stripped of the details needed to make it work. It...

The provided code is my solution, stripped of the details needed to make it work. It is not a “good” program. It lives in a single file, does not use classes, and it has those evil global variables. That is by design. I want to to craft code. Use my code as a guide to help you put together the needed parts.

#include

#include

#include

// defaults

const int MAX_STEPS = 100; // how long do we run the simulation

const int MAX_DANCERS = 20; // no more than this many dancers allowed

const int INIT_STEPS = 10; // we will run this many steps unless overridden

const int INIT_DANCERS = 5; // start with this many dancers, unless overridden

const int INIT_CARDS = 6; // start with this many cards

// globals are UGLY!

int cards[MAX_DANCERS+1] = {0}; // every dancer starts with no cards

int nsteps = INIT_STEPS;

int ndancers = INIT_DANCERS;

int ncards = INIT_CARDS;

bool debug = false;

bool stable = false;

void usage() {

std::cout << "Usage: "

<< "dancer [-d] [-c cards] [-n dancers] [-s steps]"

<< std::endl;

exit(1);

}

void show_cards() {

// display the current cards

for(int i=0;i

std::cout

<< cards[i]

<< " ";

std::cout << std::endl;

}

std::string show_step(int d) {

std::ostringstream out;

out << "("

<< cards[d]

<< ","

<< cards[d+1]

<< ")";

return out.str();

}

void arg_parse(int argc, char *argv[]) {

// process input parameters

int i=1;

while(true) {

if(i>=argc) break;

if(argv[i][0] == '-')

switch(argv[i][1]) {

case 'c': ncards = std::stoi(argv[++i]); break;

case 'n': ndancers = std::stoi(argv[++i]); break;

case 's': nsteps = std::stoi(argv[++i]); break;

case 'd': debug = true; break;

default: usage();

} else usage();

i++; // look for next arg

}

}

void dance_step(int dancer, int step) {

// this dancer needs to get happy!

if(debug) std::cout << "\tstep: "

<< step << " dancer "

<< dancer << std::endl;

// show current cards

if(debug) std::cout << "\t "

<< show_step(dancer) << " --> ";

// balance the cards

// show the final cards

if(debug) std::cout << show_step(dancer) << std::endl;

}

int main(int argc, char *argv[]) {

// say hello

std::cout << "Dance-Cards (v0.1)" << std::endl;

arg_parse(argc, argv);

if(debug) {

std::cout << ndancers

<< " dancers will dance "

<< nsteps << " steps" << std::endl;

std::cout << "Initial card count is "

<< ncards << std::endl;

}

// let the music begin!

if(debug) std::cout << "Let the dance begin!" << std::endl;

cards[0] = ncards;

show_cards();

for(int step=0; stepfor(int dancer=0; dancer

dance_step(dancer, step);

}

if(stable) {

std::cout << "system is stable" << std::endl;

break;

}

show_cards();

}

}

 
0 0
Add a comment Improve this question Transcribed image text
Answer #1
 

/*
* Parser.h
*/

#define MAX_STEPS 100
#define MAX_DANCERS 20
#define INIT_STEPS 10
#define INIT_DANCERS 5
#define INIT_CARDS 6

class CLASS
{
private:
int nsteps;
int ndancers;
int ncards;
bool debug;
bool stable;

public:
int cards[MAX_DANCERS+1] = {0}; // every dancer starts with no cards

CLASS();

int get_nsteps();

int get_ndancers();

int get_ncards();

bool get_debug();

bool get_stable();

void usage();

void show_cards();

std::string show_step(int d);

void arg_parse(int argc, char *argv[]);


void dance_step(int dancer, int step);

};
/* Parser.cpp */





/*
* Parser.cpp
*/

#include "Parser.h"

CLASS :: CLASS()
{
nsteps = INIT_STEPS;
ndancers = INIT_DANCERS;
ncards = INIT_CARDS;
debug = false;
stable = false;
}

int CLASS :: get_nsteps() { return nsteps; }

int CLASS :: get_ndancers() { return ndancers; }

int CLASS :: get_ncards() { return ncards; }

bool CLASS :: get_debug() { return debug; }

bool CLASS :: get_stable() { return stable; }

void CLASS :: usage()
{
std::cout << "Usage: " << "dancer [-d] [-c cards] [-n dancers] [-s steps]" << std::endl;
exit(1);
}

void CLASS :: show_cards()
{
for(int i = 0; i < MAX_DANCERS; i++)
std::cout << cards[i] << " ";
std::cout << std::endl;
}

std::string CLASS :: show_step(int d)
{
std::ostringstream out;
out << "(" << cards[d] << "," << cards[d+1] << ")";
return out.str();
}

void CLASS :: arg_parse(int argc, char *argv[])
{
// process input parameters
int i=1;

while(true)
{
if(i>=argc)
break;
if(argv[i][0] == '-')
{
switch(argv[i][1])
{
case 'c' : ncards = std::stoi(argv[++i]);
break;
case 'n' : ndancers = std::stoi(argv[++i]);
break;
case 's' : nsteps = std::stoi(argv[++i]);
break;
case 'd' : debug = true;
break;
default : usage();
}
}
else
usage();

i++; // look for next arg
}
}

void CLASS :: dance_step(int dancer, int step)
{

if(debug) std::cout << " step: " << step << " dancer " << dancer << std::endl;

// show current cards
if(debug) std::cout << " " << show_step(dancer) << " --> ";

// balance the cards
// show the final cards
if(debug) std::cout << show_step(dancer) << std::endl;

}
/* Parser.cpp ends here */




/*
* Main.cpp
*/

#include <iostream>
#include <string>
#include <sstream>

#include "Parser.cpp"

int main(int argc, char *argv[])
{
std::cout << "Dance-Cards (v0.1)" << std::endl;

CLASS obj;

obj.arg_parse(argc, argv);

if(obj.get_debug())
{
std::cout << obj.get_ndancers() << " dancers will dance " << obj.get_nsteps() << " steps" << std::endl;
std::cout << "Initial card count is " << obj.get_ncards() << std::endl;
}

// let the music begin!

if(obj.get_debug())
std::cout << "Let the dance begin!" << std::endl;

obj.cards[0] = obj.get_ncards();

obj.show_cards();

for(int step = 0; step < obj.get_nsteps(); step++)
{

for(int dancer = 0; dancer < obj.get_ndancers(); dancer++)
{
obj.dance_step(dancer, step);
}

if(obj.get_stable())
{
std::cout << "system is stable" << std::endl;
break;
}
obj.show_cards();
}
}
/* Main.cpp ends file */

CAUsers TusharkDesktoplnew Parser Main.cpp [Executing] - Dev-C++5.11 File Edit Search View Project Execute Tools AStyle WindowHelp Main.cpp Parser.h Pr.cpp #include <string> 2 3 #include<sstream3 #include Parser.cpp. int mair int argc, char argv 5 Dance-Cards (ve.1) std::cout <<Dance-Cards (ve.l) <<std: CLASS obj.; obj.arg parse(argc, argv); if (obj.get_debugO) std ::cout << obj.get_ndancers() <<-e6 θ θ θ θ θ θ θ θ θ θ θ θ θ θ θ θ θ θ θ std::cout <<Initial card countis Process exited after θ.3888 seconds with return value θ Press any key to continue . . ._ // Let the music beginl if (obj.get_debugO) std::cout <Let the dance begin!< Compiler % Resources ilh compile Log Debug @ -Command: g++ exe C : \Us Compilation results Shorten compiler paths- Errors: o - Warnings: 0 - Output Filename: C:\Use - Output Size: 1.44678211 - Compilation Time: 1.77s Line с01: 33 Sel: 0 Lines: 45 Length: 901 parsing in 0.031 seconds 01:01 AM

Note: Please drop comment, for any issue.

Add a comment
Know the answer?
Add Answer to:
The provided code is my solution, stripped of the details needed to make it work. It...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock...

    #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...

  • #include #include #include #include #include #include // NOLINT (build/c++11) #include class Clock { private: std::chrono::high_resolution_clock::time_point start;...

    #include #include #include #include #include #include // NOLINT (build/c++11) #include class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return type; } }; std::ostream...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • This is c++ programming and here is my code. I am getting an exception thrown on...

    This is c++ programming and here is my code. I am getting an exception thrown on the destructor for permanentworker. Can you tell me why I am getting this exception? #include <iostream> #pragma warning(disable:4996) class PermanentWorker { private:    char *name;    int salary; public:    PermanentWorker(const char* nam, int money) : salary(money)    {        name = new char[strlen(nam) + 1];        strcpy(name, nam);    }    int getPay() const    {        return salary;   ...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT