Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#ifndef TESTER_H #define TESTER_H #include #include #include #include CIS22B.h template class Tester { protected: Class_name *object; std::string command; public: Tester( Class_name *obj = nullptr );

image text in transcribed image text in transcribed image text in transcribed image text in transcribed image text in transcribed #ifndef TESTER_H #define TESTER_H

#include #include #include #include "CIS22B.h"

template class Tester { protected: Class_name *object; std::string command;

public: Tester( Class_name *obj = nullptr ); int run(); virtual void process() = 0; };

template Tester::Tester( Class_name *obj ): object( obj ) { // emtpy constructor }

/**************************************************** * int run() * * Indefinite run of test cases: continue reading console * input until the end of the file. ****************************************************/

template int Tester::run() { // read the flag which indicates the command to be test and // stop if we have reached the end of the file

CIS22B::allocation_table.stop_recording();

const static std::string prompt = " % ";

while ( true ) { // terminate if there is an end-of-file or the user types 'exit'

if ( std::cin.eof() ) { break; }

++CIS22B::count; std::cout

std::cin >> command;

// Remove any comments if ( command.substr( 0, 2 ) == "//" ) { char comment[1024]; std::cin.getline( comment, 1024 );

std::cout

// terminate if there is an end-of-file or the user types 'exit'

if ( std::cin.eof() ) { std::cout

// If the user enters !!, // set the command to be the last command // If the user enters !n where n is a number, (1

if ( command == "!!" ) { if ( CIS22B::count == 1 ) { std::cout

command = CIS22B::history[CIS22B::count - 1]; } else if ( command[0] == '!' ) { int n; std::istringstream number( command.substr( 1, command.length() - 1 ) ); number >> n;

if ( n = CIS22B::count || n >= 1000 ) { std::cout

command = CIS22B::history[n]; }

// only track the first 1001 commands if ( CIS22B::count

// start tracking any memory allocations made CIS22B::allocation_table.start_recording();

// There are five key commands

if ( command == "exit" ) { std::cout

std::cin >> n;

if ( n == CIS22B::allocation_table.memory_alloc() ) { std::cout

std::cin >> n;

CIS22B::allocation_table.memory_change( n ); } else { process(); }

// stop tracking any memory allocations made CIS22B::allocation_table.stop_recording(); }

return 0; }

#endif Here is my code. Please help me make it work: Weighted_graph.h #ifndef WEIGHTED_GRAPH_H #define WEIGHTED_GRAPH_H

#include #include #include #include

class Weighted_graph { public: static const double INF;

Weighted_graph(int n = 50); ~Weighted_graph();

int degree(int n) const; int edge_count() const; double adjacent(int m, int n) const; double distance(int m, int n) const;

void insert(int m, int n, double w);

private: int vertices_count; std::vector<:vector>> adjacency_matrix;

bool isValidVertex(int v) const; void validateVertex(int v) const; double dijkstra(int src, int dest) const; };

#endif // WEIGHTED_GRAPH_H Weighted_graph.cpp #include "Weighted_graph.h" #include

const double Weighted_graph::INF = std::numeric_limits::infinity();

Weighted_graph::Weighted_graph(int n) : vertices_count(std::max(n, 1)), adjacency_matrix(vertices_count, std::vector(vertices_count, INF)) { for (int i = 0; i

Weighted_graph::~Weighted_graph() {}

int Weighted_graph::degree(int n) const { validateVertex(n); return std::count_if(adjacency_matrix[n].begin(), adjacency_matrix[n].end(), [](double weight) { return weight != INF && weight != 0.0; }); }

int Weighted_graph::edge_count() const { int count = 0; for (int i = 0; i

double Weighted_graph::adjacent(int m, int n) const { validateVertex(m); validateVertex(n); return adjacency_matrix[m][n]; }

double Weighted_graph::distance(int m, int n) const { validateVertex(m); validateVertex(n); return dijkstra(m, n); }

void Weighted_graph::insert(int m, int n, double w) { validateVertex(m); validateVertex(n); if (w

bool Weighted_graph::isValidVertex(int v) const { return v >= 0 && v

void Weighted_graph::validateVertex(int v) const { if (!isValidVertex(v)) { throw std::out_of_range("Vertex index out of bounds."); } }

double Weighted_graph::dijkstra(int src, int dest) const { // Implement Dijkstra's algorithm here // This is a placeholder implementation return 0.0; }

Requirements: In this project, you will implement one class implementing the algorithm: A skeleton header file for this class and its associated class is provided. You may modify these files for the project, though you must add your own comments. Do not copy text from the specifications. Testing tools for some of the classes provided in the associated testing directory. You are required to submit the following header file(s): - Weighted_graph.h You may add any other header files (of your own authorship) you need for your project to work. You may use all standard libraries available. This includes the Standard Template Library (STL), cstring, etc. Runtime Ten percent of the grade will be attributed to run-time relative to other students in your class. The class average and standard deviation will be calculated (with outliers removed), and those achieving the class average will get 80%. Other grades will be based on 10 marks per standard deviation in either direction. Memory usage There are no constraints on memory use in this project. sample test The sample test code must be such that passing the sample test code will guarantee a grade of 85% on the project. That is, it will test that member functions are named correctly, and that the basic functionality is present. Students are expected to do their own testing, though testing tools and some minimal tests will be provided. What You May Do You may: - Make multiple submissions-only the last is kept. - Declare and define other classes and other member functions not specified in the project description; however, if these appear in other files, those files must be included with your submission. - Reuse classes from previous projects; however, you must include those files with your submission. - If you reuse previous projects, you are, of course, allowed to make corrections to those projects. - You may even, though you must be very cautious, overload the given member functions or add additional parameters to those member functions so long as those extra parameters have default values and the functions as specified in the project may be called with no issues or ambiguities. Dijkstra's algorithm You may use all standard libraries available. This includes the Standard Template Library (STL), cstring, etc. However, if you want to compete for the fastest implementations that get a prize, you should restrict yourself to using operations like std: :swap and other reasonably straight-forward features. Do not use any of the containers if you want to compete. Requirements In this sub-project, you will implement one class: 1. A weighted graph: Weighted_graph. Class Specification UML Class Diagram \begin{tabular}{|l|} \hline \multicolumn{1}{|c}{ Weighted_graph } \\ \hline + create( in n:Integer =50) :Weighted_graph \\ + degree( in n:Integer ):Integer \\ + edge_count(O-Integer \\ + adjacent( (in m:Integer, in n:Integer ):Real \\ + distance( in m-Integer, in n:Integer ):Real \\ + insert( in m:Integer, in n:Integer, in w:Real ) \\ + destroy( \end{tabular} Description This class allows the user to create and destroy an undirected weighted graph. You will be able to find the shortest distance between two connected vertices. The vertices are numbered 0 through n1 where n is the argument to the constructor. Member Variables You may define whatever member variables you wish. Member Functions Constructors Neighted_graph( int n=50 ) Construct an undirected graph with n vertices (by default, 50). If n0, use n=1. Destructor Clean up any allocated memory. Accessors This class has three accessors: int degree (int n ) const Returns the degree of the vertex n. Throw an illegal argument exception if the argument does not correspond to an existing vertex. (O(1)) int edge_count () const Returns the number of edges in the graph. (O(1)) double adjacent (int m, int n ) const Returns the weight of the edge connecting vertices m and n. If the vertices are the same, return 0 . If the vertices are not adjacent, return infinity. Throw an illegal argument exception if the arguments do not correspond to existing vertices. Mutators This class has two mutators: void insert (int m, int n, double x ) If the weight w0 if it is infinity, throw an illegal argument exception. If the weight w> 0 , add an edge between vertices m and n. If an edge already exists, replace the weight of the edge with the new weight. If the vertices do not exist or are equal, throw an illegal argument exception. double distance (int m, int n ) Return the shortest distance between vertices m and n. Throw an illegal argument exception if the arguments do not correspond to existing vertices. The distance between a vertex and itself is 0.0 . The distance between vertices that are not connected is infinity. \#ifndef EXCEPTION_H \#define EXCEPTION_H / * DO NOT EDIT THIS FILE / /* * All exceptions are derived from the 'exception' class * - consequently, catch( excpetion) \{... \} will catch only these exceptions * exception -- underflow * * * - - overflow - - division_by_zero |- - illegal_argument * / - - out_of_range class exception { // empty class \}; class underflow : public exception \{ // empty class \}; class overflow : public exception \{ // empty class \}; class division_by_zero : public exception \{ // emtpy class \}; class illegal_argument : public exception \{ // emtpy class \}; class out_of_range : public exception \{ // emtpy class \}; \#endif l * Tester * The top-level testing class. Each derived * class inherits the routines: int run() For a derived class to function, it must override the void process() member function. The process function should read in a line from the input file and tests the routine based on those instructions. The member variables are: Class_name *object A pointer to the object being tested. string command The string read in from the input file indicating the next command to be tested. int count The number of the current test being run. The member functions are: int run() Start a test run testing until the end of the file, one test per line. void process() Process and run an individual test. Author: * DO NOT EDIT THIS FILE /

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions

Question

What is an ODS?

Answered: 1 week ago

Question

Can a product line be a product mix?

Answered: 1 week ago

Question

What attracts you about this role?

Answered: 1 week ago

Question

How many states in India?

Answered: 1 week ago

Question

HOW IS MARKETING CHANGING WITH ARTIFITIAL INTELIGENCE

Answered: 1 week ago