Помощь с stack.h и stack.cpp и еще prog4.cpp
Код CursorCntl.h:
#ifndef CURSORCNTL_H #define CURSORCNTL_H /*--------------- C u r s o r C n t l . h --------------- PURPOSE This is the interface to CursorCntl.cpp. */ // To simulate not running in Windows, include the following line. //#define NoGraphics // 03-24-2016 gpc - Correct conditional compile order #ifndef _WIN32 #define NoGraphics #endif #ifdef NoGraphics void Sleep(unsigned ms); #endif //----- f u n c t i o n p r o t o t y p e s void getxy(int &x, int &y); // Return the column (x) and row (y) positions of the cursor. void gotoxy(int x, int y); // Move the cursor to column "x", row "y". void clrscr(); // Clear the entire screen. void clreol(); // Clear from the cursor to the end of line. void SaveXY(); // Save the current cursor position. void RestoreXY(); // Restore the current cursor position. #endif CursorCntl.cpp code: /*--------------- C u r s o r C n t l . c p p --------------- PURPOSE This module defines a collection of console window cursor control routines NOTE: Type int is used instead of unsigned to be consistent with the original gotoxy(). */ #include <iostream> using namespace std; #include "CursorCntl.h" /*--------------- S l e e p ( ) -------------- PURPOSE Sleep for "speed" ms. This function is for non-windows environments. INPUT PARAMETERS ms -- the number of ms. to sleep */ #if defined NoGraphics #include <ctime> void Sleep(unsigned ms) { #ifdef __LINUX__ #include <unistd.h> // Linux, use usleep() usleep(1000 * ms); #else // 03-24-2016 gpc - Fix non-windows Sleep() function. // Not Windows or Linux, use time.h to delay. // NOTE: This code is a CPU hog. const unsigned MsPerSec = 1000; // Clock ticks per ms. clock_t clocksPerMs = CLOCKS_PER_SEC / MsPerSec; // In case of insufficient clock resolution (i.e. CLOCKS_PER_SEC < 1000) if (clocksPerMs < 1) clocksPerMs = 1; clock_t tDone = clock() + ms * clocksPerMs; while (clock() < tDone) ; #endif } /*--------------- g e t x y ( ) -------------- PURPOSE Obtain the cursor location from the screen. OUTPUT PARAMETERS x -- the x (column) location of the cursor y -- the y (row) location of the cursor */ void getxy(int &x, int &y) { x = 0; y = 0; } /*--------------- g o t o x y ( ) -------------- PURPOSE Move the cursor to a specific screen position. INPUT PARAMETERS x -- the x (column) location of the cursor y -- the y (row) location of the cursor */ void gotoxy(int x, int y) { } /*--------------- c l r s c r ( ) -------------- PURPOSE Erase the console window and move the cursor home. */ void clrscr(void) { } /*--------------- c l r e o l ( ) -------------- PURPOSE Erase from the cursor to the end of the line. */ void clreol(void) { } /*--------------- S a v e C u r s o r ( ) -------------- PURPOSE Save the current screen cursor location. */ void SaveXY(void) { } /*--------------- R e s t o r e C u r s o r ( ) -------------- PURPOSE Restore the screen cursor location */ void RestoreXY(void) { } #else #include <windows.h> /*--------------- g l o b a l s ---------------*/ // Console window colors enum COLORS { BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY, DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE }; // Saved cursor location static int cursorSaveX; static int cursorSaveY; // Background and foreground colors. static int BACKGROUND = BLACK; static int FOREGROUND = LIGHTGRAY; /*--------------- g e t x y ( ) -------------- PURPOSE Obtain the cursor location from the screen. OUTPUT PARAMETERS x -- the x (column) location of the cursor y -- the y (row) location of the cursor */ void getxy(int &x, int &y) { CONSOLE_SCREEN_BUFFER_INFO screenBfr; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &screenBfr); x = screenBfr.dwCursorPosition.X; y = screenBfr.dwCursorPosition.Y; } /*--------------- g o t o x y ( ) -------------- PURPOSE Move the cursor to a specific screen position. INPUT PARAMETERS x -- the x (column) location of the cursor y -- the y (row) location of the cursor */ void gotoxy(int x, int y) { COORD point; point.X = (SHORT)x; point.Y = (SHORT)y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point); } /*--------------- c l r s c r ( ) -------------- PURPOSE Erase the console window and move the cursor home. */ void clrscr() { /* COORD home = {0, 0}; DWORD written; CONSOLE_SCREEN_BUFFER_INFO screenBfr; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &screenBfr); int screenChars = screenBfr.dwSize.X * screenBfr.dwSize.Y; FillConsoleOutputAttribute ( GetStdHandle (STD_OUTPUT_HANDLE), FOREGROUND + (BACKGROUND << 4), screenChars, home, &written); FillConsoleOutputCharacter ( GetStdHandle (STD_OUTPUT_HANDLE), ' ', screenChars, home, &written); */ system("CLS"); gotoxy(0, 0); } /*--------------- c l r e o l ( ) -------------- PURPOSE Erase from the cursor to the end of the line. */ void clreol() { COORD start; DWORD written; CONSOLE_SCREEN_BUFFER_INFO screenBfr; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &screenBfr); int x; // Current column int y; // Current row. // Find the cursor location getxy(x, y); start.X = (SHORT)x; start.Y = (SHORT)y; // How many characters from the cursor to the end of line? int numChars = screenBfr.dwSize.X - start.X; // Erase FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND + (BACKGROUND << 4), numChars, start, &written); FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), ' ', numChars, start, &written); } /*--------------- S a v e C u r s o r ( ) -------------- PURPOSE Save the current screen cursor location. */ void SaveXY() { getxy(cursorSaveX, cursorSaveY); } /*--------------- R e s t o r e C u r s o r ( ) -------------- PURPOSE Restore the screen cursor location */ void RestoreXY() { gotoxy(cursorSaveX, cursorSaveY); } #endif Maze.h code : #ifndef POSITION_H #define POSITION_H #ifndef MAZE_H #define MAZE_H #include <fstream> #include <string> using namespace std; #include "Position.h" //#include "Stack.h" //----- c o n s t a n t d e f i n i t i o n s const unsigned GridSize = 10; // Number of rows and columns in the grid. //----- t y p e d e f i n i t o n s ----- // Cell states are ASCII characters. typedef char CellState; // Define the possible states for cells in the grid. const char Open = ' '; const char Obstacle = '-'; const char Visited = 'V'; const char Rejected = 'R'; const char StartCell = 'S'; const char GoalCell = 'G'; const char PathCell = 'P'; //----- c l a s s M a z e ----- class Maze { public: // Constructor Maze(); // Accessors bool IsOpen(const Position &cellPos) const; bool IsVisited(const Position &cellPos) const; Position Start() { return start; } Position Goal() { return goal; } // Mutators void Visit(const Position &p); void Reject(const Position &p); void MarkPathCell(const Position &p); private: // The square grid of maze cells CellState cell[GridSize][GridSize]; // The starting position in the maze. Position start; // The goal position in the maze. Position goal; // Maze File Name string mazeFileName; // Log File Output Stream ofstream logFile; // The number of moves per second int speed; // Display the maze on the screen. void Show() const; // Display the state of one cell on the screen. void ShowCell(const Position &p, const CellState state) const; // Load the maze definition from a file. void OpenMazeFile(string &mazeFileName, ifstream &mazeFile); void StoreCell(char c, int rowNum, int colNum); void LoadMazeFile(); // Set the speed of travel. void SetSpeed(); }; #endif Position.h code: #include <iostream> #include <string> //----- c l a s s P o s i t i o n ----- class Position { public: // Constructors Position() { defined = false; } Position(const int theRow, const int theCol) : row(theRow), col(theCol), defined(true) { } // Accessors int Row() const { return row; } int Col() const { return col; } bool Defined() { return defined; } // Overloaded operators Position operator+(const Position &b) const; Position operator+=(const Position &b); bool operator!=(const Position &b) const { return (row != b.row) || (col != b.col); } bool operator==(const Position &b) const { return (row == b.row) && (col == b.col); } private: int row; // the row (y) location of a position int col; // the column (x) location of a position bool defined; // True if the position has been defined }; //----- c o n s t a n t d e f i n i t i o n s ----- // Offsets to neighboring cells (relative positions) const Position StepEast = Position(0, +1); // One step east const Position StepSouth = Position(+1, 0); // One step south const Position StepWest = Position(0, -1); // One step west const Position StepNorth = Position(-1, 0); // One step north #endif position.cpp code: #include <iostream> #include <cassert> using namespace std; #include "Position.h" /*----- P o s i t i o n : : o p e r a t o r + ( ) ----- PURPOSE Add two positions by adding the row numbers and adding the column numbers. INPUT PARAMETERS b -- the second operand of "+" */ Position Position::operator+(const Position &b) const { Position result; result.row = row + b.row; result.col = col + b.col; result.defined = true; return result; } /*----- P o s i t i o n : : o p e r a t o r + = ( ) ----- PURPOSE Add position "b" to this position. INPUT PARAMETERS b -- the second operand of "+=" */ Position Position::operator+=(const Position &b) { row = row + b.row; col = col + b.col; defined = true; return *this; } stack.h code: #ifndef STACK_H #define STACK_H #include <cassert> using namespace std; #include "Position.h" const int StackCapacity = 100; // Make the stack element type be a grid position. typedef Position StackElement; //---------- c l a s s S t a c k ---------- // Define an array based stack class. class Stack { struct Node { StackElement data; // the "contents" of the node Node *next; // Link to the next node // Node constructor Functions Node(){} Node(const StackElement &theData, Node *const theNext = 0): data(theData), next(theNext) {} }; public: // Contruct an empty stack. Stack() { tos = -1; } // Test for an empty stack. bool Empty() const { return tos < 0; } // Test for a full stack. bool Full() const { return tos >= StackCapacity - 1; } // Push a new element onto the top of the stack. void Push(const StackElement &elem); // Retrieve the top element and pop it off of the stack. StackElement Pop(); // Retrieve the top element, but do not remove it from the stack. StackElement Top() const; private: int tos; // Index (subscript) of the top stack element Node *top; StackElement stack[StackCapacity]; // Storage for the stack elements }; #endif stack.cpp code: #include "stack.h" #include"Maze.h" /*--------------- P u s h ( ) --------------- Push a new element onto the top of the stack. INPUT PARAMETERS elem -- the element to add to the top of stack */ void Stack::Push(const StackElement &elem) { assert(!Full()); stack[++tos] = elem; } /*--------------- P o p ( ) --------------- Pop the top element off of the stack and return its value. RETURN VALUE The old top of stack */ StackElement Stack::Pop() { assert(!Empty()); tos = Rejected; return stack[tos--]; } main program code: #include <stdlib.h> #include <iostream> using namespace std; #include "CursorCntl.h" #include "Maze.h" #include "Stack.h" /*----- M a z e : : S o l v e ( ) ----- PURPOSE Naive maze traversal algorithm. Try all possible next positions, but give up at a dead end. PARAMETERS maze -- the maze object to solve stack -- the stack of traversed positions RETURN VALUE true -- a solution was found. false -- failed to find a solution. */ bool Solve(Maze &maze, Stack &stack) { Position curPos; // The current position // Move to the start cell. curPos = maze.Start(); maze.Visit(curPos); // Repeatedly find a next move until the goal is reached. while (curPos != maze.Goal()) { if (maze.IsOpen(curPos + StepEast)) curPos += StepEast; else if (maze.IsOpen(curPos + StepSouth)) curPos = curPos + StepSouth; else if (maze.IsOpen(curPos + StepWest)) curPos = curPos + StepWest; else if (maze.IsOpen(curPos + StepNorth)) curPos = curPos + StepNorth; else return false; maze.Visit(curPos); } // Found a solution. return true; } /*----- M a z e : : R e t r a c e P a t h ( ) ----- PURPOSE Find the way back from the goal position to the start position PARAMETERS maze -- the maze object to retrace stack -- the stack of traversed positions */ void RetracePath(Maze &maze, Stack &stack) { Stack object; // object of stack object.Pop(); // calling pop function to distroy top of stack (tos) // F I L L I N M I S S I N G D E F I N I T I O N } /*--------------- m a i n ( ) ---------------*/ int main(void) { // Screen positions const unsigned XResult = 15; const unsigned YResult = 5; const unsigned XFinish = 0; const unsigned YFinish = 20; // Position stack remembers visited positions. Stack posStack; // Construct a maze from a maze definition file. Maze maze; // Traverse the maze. bool success = Solve(maze, posStack); // Indicate success or failure. gotoxy(XResult, YResult); if (!success) cout << "Failed: No path from start to goal exists." << endl; else { cout << "Success: Found a path. Press <enter> to retrace." << endl; cin.get(); // Wait for a ENTER key. // Retrace the path back from the goal position back to the start position. RetracePath(maze, posStack); } // Done gotoxy(XFinish, YFinish); return 0; } maze.cpp code: #include <limits.h>#include <cassert> #include <iostream> #include <fstream> #include <string> #include <cctype> #include <ctime> using namespace std; // 03-24-206 gpc - Include CursorCntl.h first. #include "CursorCntl.h" #include "Maze.h" #include "Stack.h" // 03-24-206 gpc Include Windows.h if running in Windows and NoGraphics mode is not enabled. #if (defined _WIN32) && (!defined NoGraphics) #include <windows.h> #endif //----- c o n s t a n t d e f i n t i o n s ----- const unsigned DefaultSpeed = 6; // Default speed to travel the maze const unsigned MsPerSec = 1000; // Number of ms. in one second //------ f u n c t i o n s ----- /*----- M a z e : : O p e n M a z e F i l e ( ) ----- PURPOSE Open a maze definition file. OUTPUT PARAMETERS mazeFileName -- a string giving the name of the opened file mazeFile -- the opened stream */ void Maze::OpenMazeFile(string &fileName, ifstream &mazeFile) { const char DefFileName[] = "maze"; // Default maze definition file name const string ext = ".txt"; // Maze definition file extension // Repeatedly ask for a file name and try to open it. for (;;) { // Get the file name. If empty, use the default. cout << "Maze file name [default = \"" << DefFileName << "\", ctrl-C quits]: "; if (cin.peek() == '\n') { cin.ignore(INT_MAX, '\n'); fileName = DefFileName; } else getline(cin, fileName); // Insure that the file extension is correct. if (fileName.length() >= ext.length()) { if (fileName.substr(fileName.length() - ext.length()) != ext) fileName += ext; } else fileName += ext; // Try to open the file. mazeFile.open(fileName.c_str()); // If success, open a log file. if (mazeFile.is_open()) { const string LogFileExt = ".log"; // Log file extension // The log file name is the same as the maze file name // with the extension changed to ".log." string logFileName = mazeFileName; logFileName.erase(logFileName.length() - ext.length(), ext.length()); logFileName += LogFileExt; logFile.open(logFileName.c_str()); // Make sure the log file opened. assert(logFile.is_open()); break; } // Open failed: clear the stream state, give an error message, and try again. mazeFile.clear(); cout << "*** ERROR: No such file: " << fileName << endl; } } /*----- M a z e : : S t o r e C e l l ( ) ----- PURPOSE Initialize one cell in the maze. INPUT PARAMETERS c -- a character indicating the initial cell state. rowNum -- the cell's row number colNum -- the cell's column number ERRORS Abort if more than one start or goal position are defined. */ void Maze::StoreCell(char cellStateChar, int rowNum, int colNum) { // Use the character to set the initial state of the next maze cell. switch (toupper(cellStateChar)) { // Cell is open. case '0': case ' ': case 'O': cell[rowNum][colNum] = Open; break; // Cell is the start cell. case StartCell: // If start already found, quit. if (start.Defined()) { cout << "*** ERROR: More than one start position specified." << endl; exit(EXIT_FAILURE); } // Mark the cell and record the start position. cell[rowNum][colNum] = StartCell; // 10-16-2003 gpc start = Position(rowNum, colNum); break; // Cell is the goal cell. case GoalCell: // If goal already found, quit. if (goal.Defined()) { cout << "*** ERROR: More than one goal position specified." << endl; exit(EXIT_FAILURE); } // Mark the cell and record the goal position. cell[rowNum][colNum] = GoalCell; //0-16-2003 - gpc goal = Position(rowNum, colNum); break; // Any other character represents an obstacle. default: cell[rowNum][colNum] = Obstacle; break; } } /*----- M a z e : : L o a d M a z e F i l e ( ) ----- PURPOSE Initialize the maze from a maze definition file. ERRORS Abort if incomplete maze, or no start or goal position are defined. */ void Maze::LoadMazeFile() { // The stream from which the maze is loaded ifstream mazeFile; // Maze file input stream // Open the maze definition file. OpenMazeFile(mazeFileName, mazeFile); // The file is open; load in the maze. // Read "Gridsize" lines from the file. for (int rowNum = 0; rowNum < GridSize; rowNum++) { // Read "GridSize" columns from each line. for (int colNum = 0; colNum < GridSize; colNum++) { char cellStateChar; // the next character from the file // Get the next character. mazeFile.get(cellStateChar); // If early end-of-file reached, abort. if (mazeFile.eof()) { cout << "*** ERROR: Unexpected end of file on " << mazeFileName << endl; exit(EXIT_FAILURE); } // Set the cell state. StoreCell(cellStateChar, rowNum, colNum); } // Flush newline before reading the next line from the file. mazeFile.ignore(INT_MAX, '\n'); } // Done with the file, close it. mazeFile.close(); // Make sure that the file contained start and goal positions. if (!start.Defined()) { cout << "*** ERROR: No start positon specified." << endl; exit(EXIT_FAILURE); } if (!goal.Defined()) { cout << "*** ERROR: No goal positon specified." << endl; exit(EXIT_FAILURE); } } /*----- M a z e : : S e t S p e e d ( ) ----- PURPOSE Get the maze travel speed. */ void Maze::SetSpeed() { const unsigned MinSpeed = 1; // Speed must be bigger than zero. const unsigned DefSpeed = 6; // Default speed setting const unsigned XPrompt = 0; // Screen column for speed prompt const unsigned YPrompt = 15; // Screen row for speed prompt bool needSpeed; // True until a valid speed is entered. // Repeatedly ask for a speed until a valid value is given. do { // Read the speed. needSpeed = true; gotoxy(XPrompt, YPrompt); clreol(); cout << "Speed [minimum = " << MinSpeed << ", default = " << DefSpeed << ", ctrl-C quits]: "; if (cin.peek() == '\n') { // If empty, use the default. speed = DefaultSpeed; needSpeed = false; } else { // Not empty, read in the speed number. cin >> speed; // Make sure the speed is valid. if (cin.fail()) { // A bad integer was entered. clreol(); cout << "***ERROR: Speed must be a positive integer." << endl; cin.clear(); } else if (speed < MinSpeed) { // The speed is too low. clreol(); cout << "***ERROR: Speed must be at least " << MinSpeed << "." << endl; } else // Entered speed was valid. needSpeed = false; } // Flush the newline. cin.ignore(INT_MAX, '\n'); } while (needSpeed); // Clean up any remaining error messages. gotoxy(XPrompt, YPrompt + 1); clreol(); } /*----- M a z e : : M a z e ( ) ----- PURPOSE Construct a Maze object from a maze definition file. */ Maze::Maze() { // Load the maze from the maze definition file. LoadMazeFile(); // Display the maze on the screen. Show(); // Set the speed of travel. SetSpeed(); } /*----- M a z e : : S h o w ( ) ----- PURPOSE Display the maze on the screen. */ void Maze::Show(void) const { // Column numbers const char ColHeadings[] = " 0123456789"; // Erase the console window. clrscr(); // Show column numbers above the grid. cout << ColHeadings << " " << mazeFileName << endl; // Show the grid, one row at a time. for (int rowNum = 0; rowNum < GridSize; rowNum++) { // Give the row number to the left of the grid. cout << rowNum; // Show the next row. for (int colNum = 0; colNum < GridSize; colNum++) cout << cell[rowNum][colNum]; // Give the row number to the left of the grid. cout << rowNum << endl; } // Show column numbers above the grid. cout << ColHeadings << endl; } /*----- M a z e : : I s O p e n ( P o s i t i o n & ) ----- PURPOSE Determine the whether a given maze cell is open. INPUT PARAMETERS cellPos -- the position of the cell whose state is to be obtained RETURN VALUE true if the cell is open; otherwise false */ bool Maze::IsOpen(const Position &cellPos) const { // If the position is off the grid, it is illegal. if (cellPos.Row() < 0 || cellPos.Row() >= GridSize) return false; if (cellPos.Col() < 0 || cellPos.Col() >= GridSize) return false; // The start and goal cells are open until visited. if (cell[cellPos.Row()][cellPos.Col()] == StartCell) // 10-16-2003 - gpc return true; if (cell[cellPos.Row()][cellPos.Col()] == GoalCell) // 10-16-2003 - gpc return true; // Use the stored cell state. return cell[cellPos.Row()][cellPos.Col()] == Open; } /*----- M a z e : : Is V i s i t e d ( P o s i t i o n & ) ----- PURPOSE Determine the whether a given maze cell is marked "Visited". INPUT PARAMETERS cellPos -- the position of the cell whose state is to be obtained RETURN VALUE true if the cell is marked Visited; otherwise falseThe cell state. */ bool Maze::IsVisited(const Position &cellPos) const { // If the position is off the grid, it is illegal. if (cellPos.Row() < 0 || cellPos.Row() >= GridSize) return false; if (cellPos.Col() < 0 || cellPos.Col() >= GridSize) return false; // Use the stored cell state. return cell[cellPos.Row()][cellPos.Col()] == Visited; } /*----- M a z e : : S h o w C e l l ( ) ----- PURPOSE Display the state of on cell on the screen. INPUT PARAMETERS p -- the position of the cell whose state is to be displayed. state -- the state to be displayed */ void Maze::ShowCell(const Position &p, const CellState state) const { // 03-01-2018 gpc const unsigned MsPerSec = 1000; // Number of ms. in one second const char CurPosChar = '+'; // Current position display character const unsigned curPosX = 15; // X location to display current position const unsigned curPosY = 2; // Y location to display current position // Display the numeric current position (x, y). gotoxy(curPosX, curPosY); cout << "Position: (" << p.Col() << ", " << p.Row() << ")"; // Display the new state. gotoxy(p.Col() + 1, p.Row() + 1); // Wait and then change and display the new state character. // Delay(MsPerSec/speed); cout << state; } /*----- M a z e : : V i s i t ( ) ----- PURPOSE Mark a maze cell visited. INPUT PARAMETERS p -- the position of the visited cell */ void Maze::Visit(const Position &p) { // Record move to log file. logFile << "Visit (" << p.Row() << ", " << p.Col() << ")" << endl; cell[p.Row()][p.Col()] = Visited; #if (defined NoGraphics) Show(); #else ShowCell(p, Visited); #endif Sleep(MsPerSec / speed); } /*----- M a z e : : R e j e c t ( ) ----- PURPOSE Mark a maze cell rejected. INPUT PARAMETERS p -- the position of the rejected cell */ void Maze::Reject(const Position &p) { // Record move to log file. logFile << "Reject (" << p.Row() << ", " << p.Col() << ")" << endl; cell[p.Row()][p.Col()] = Rejected; #if (defined NoGraphics) Show(); #else ShowCell(p, Rejected); #endif Sleep(MsPerSec / speed); } /*----- M a z e : : M a r k P a t h C e l l ( ) ----- PURPOSE Mark a maze cell rejected. INPUT PARAMETERS p -- the position of the rejected cell */ void Maze::MarkPathCell(const Position &p) { // Record move to log file. logFile << "Retrace (" << p.Row() << ", " << p.Col() << ")" << endl; cell[p.Row()][p.Col()] = PathCell; #if (defined NoGraphics) Show(); #else ShowCell(p, PathCell); #endif Sleep(MsPerSec / speed); }
Что я уже пробовал:
Я попытался изменить эти коды (stack .h и stack.cpp и еще prog4.cpp )
вопросы можно найти здесь:
Стек.ч , Stack.cpp и еще Prog4.cpp нужно быть модифицированным... | Chegg.com[^]