Answered step by step
Verified Expert Solution
Link Copied!

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 noDuplicates(const string& input)
{
bool isInStack[10]; // inInStack[i]== true iff digit is already on the stack
int remainingInInput[10]; // how many occurrences of each digit remain unprocessed in the input?
fill_n (isInStack,10, false);
fill_n (remainingInInput,10,0);
// Count the number of times each digit occurs in the input
for (char c: input){
int digit = c -'0';
++remainingInInput[digit];
}
stack digitStack;
// Process each character of the input
for (char c: input){
int digit = c -'0';
--remainingInInput[digit];
processInputDigit (digit, digitStack, isInStack, remainingInInput);
}
// Print the answer
print (cout, digitStack);
cout endl;
}
int main (int argc, char** argv)
{
if (argc 2){
cerr "Usage: " argv[0]" number
"
" or " argv[0]" filepath" endl;
return -1;
}
string input (argv[1]);
if (input.find_first_not_of("0123456789")!= string::npos)
{
// Input is not a number - assume it is a file name and
// read the number from there
ifstream in(input);
in >> input;
in.close();
}
noDuplicates(input);
}
processInput.h
#ifndef PROCESSINPUT_H
#define PROCESSINPUT_H
#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 0..9
* @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 O(n)
complexity if you have to do an O(n)
scan for each of the n
digits.
But since there are only 10 possible digits, you can track these with simple arrays:
int unprocessedDigits[10];
bool digitInStack[10];
image text in transcribed

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions