Answered step by step
Verified Expert Solution
Question
1 Approved Answer
NoDupes.cpp #include processInput.h #include #include #include #include #include using namespace std; void noDuplicates ( const string& input ) { bool isInStack [ 1 0
NoDupes.cpp
#include "processInput.h
#include
#include
#include
#include
#include
using namespace std;
void noDuplicatesconst string& input
bool isInStack; inInStacki true iff digit is already on the stack
int remainingInInput; how many occurrences of each digit remain unprocessed in the input?
filln isInStack false;
filln remainingInInput;
Count the number of times each digit occurs in the input
for char c: input
int digit c ;
remainingInInputdigit;
stack digitStack;
Process each character of the input
for char c: input
int digit c ;
remainingInInputdigit;
processInputDigit digit digitStack, isInStack, remainingInInput;
Print the answer
print cout digitStack;
cout endl;
int main int argc, char argv
if argc
cerr "Usage: argv number
or argv filepath" endl;
return ;
string input argv;
if inputfindfirstnotof string::npos
Input is not a number assume it is a file name and
read the number from there
ifstream ininput;
in input;
inclose;
noDuplicatesinput;
processInput.h
#ifndef PROCESSINPUTH
#define PROCESSINPUTH
#include
#include
@brief Process a digit of input.
Process a single digit of input in the search for the smallest
number that can be formed by removing duplicates.
@param digit the input digit, a number in the range
@param digitStack a stack of digits representign the best number found so far,
with the most significant digit of the number on the bottom
of the stack.
@param digitIsInStack an array indicating which digits are on the stack.
Will be updated by this function.
@param remainingDigitsInInput a count of the number of times each
digit occurs in the remaining output.
void processInputDigit int digit, std::stack& digitStack,
bool digitIsInStack, const int remainingDigitsInInput;
@brief Print the number on the stack to out.
@param out the stream to which to print.
@param digitStack a stack of digits, most significant on the bottom.
void print std::ostream& out, std::stack& digitStack;
#endif
These are the two files that coincide with creating processInput.cpp
focus in this assignment will be implementing, in processInput.cpp the functions declared in processInput.h
ability to function correctly within the noDupes application
In addition to the stack, you may need structures to keep track of how many occurrences of each digit remain in the part of the input to be processed and to keep track of which digits are already in the stack.
You could determine this by repeatedly scanning the input and stack, but that wont achieve the desired On
complexity if you have to do an On
scan for each of the n
digits.
But since there are only possible digits, you can track these with simple arrays:
int unprocessedDigits;
bool digitInStack;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started