Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Complete in C + + , practice working with stacks. Your focus in this assignment will be implementing, the processInput.cpp , the functions declared in
Complete in C practice working with stacks.
Your focus in this assignment will be implementing, the processInput.cpp the functions declared in processInput.h
Problem Description: This program takes as input an integer of up to digits. Obviously we cant store this in a C int or even a long long int. It will need to be stored in a string The challenge is to find the smallest integer that can be formed by removing duplicate digits from the input. The final input must contain every digit that can be found in the input including possibly a leading Digits may not be swapped or otherwise rearranged. They may only be removed and may be removed only if the same digit occurs elsewhere in the number.
For example, given the input
the smallest input that can be formed be removing duplicates would be
How did we get that? We use a stack:
The program must run in On time where n is the length of the input string.
The program reads either a long integer from the command line or a path to a file containing a single line of text holding such a string.
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;
Attached is 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
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