This is C++. I would like to know the TODO list comments. #include <cstdlib> #in
ID: 3793834 • Letter: T
Question
This is C++. I would like to know the TODO list comments.
#include <cstdlib>
#include <iostream>
// TODO: 2. (1 point) Declare your Acker function prototype below:
// When done, commit with the commit message that follows, being
// sure to delete all these TODO instructions before actuallly
// executing the git commit command.
//
// CSC232-HW02 - Added Aker function prototype.
//
// Don't forget to fully document the function prototype using
// all the appropriate doxygen elements.
/**
* @brief Entry point to this application.
* @remark You are encouraged to modify this file as you see fit to gain
* practice in using objects.
*
* @param argc the number of command line arguments
* @param argv an array of the command line arguments
* @return EXIT_SUCCESS upon successful completion
*/
int main(int argc, char **argv) {
// TODO: 4. (0.5 points) Replace *** text *** with actual call to Acker
// function. When done, commit with the commit message that follows,
// being sure to delete all these TODO instructions before actuallly
// executing the git commit command.
//
// CSC232-HW02 - Display calculated Acker value.
//
std::cout << "Acker(1, 2) = " << "*** replace me with call to Acker***"
<< std::endl;
return EXIT_SUCCESS;
}
// TODO: 3. (2 points) Define the Acker recursive function below. When done,
// commit with the commit message that follows, being sure to delete
// all these TODO instructions before actually executing the git
// commit command.
//
// CSC232-HW02 - Implemented Acker function.
// TODO: 5. (1 point) Do a box trace of Acker(1, 2) using pen and paper.
// Hand this in at the beginning of class on Friday, 17 February 2017.
// Your solution MUST be legible; no points are assigned to illegible
// papers.
This is testRunner.
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/Test.h>
#include <cppunit/TestFailure.h>
#include <cppunit/portability/Stream.h>
/**
* @brief Main test driver for CPPUNIT test suite.
* @remark DO NOT MODIFY THE SPECIFICATION OR IMPLEMENTATION OF THIS CLASS! ANY
* MODIFICATION TO THIS CLASS WILL RESULT IN A GRADE OF 0 FOR THIS LAB!
*/
class ProgressListener : public CPPUNIT_NS::TestListener {
public:
ProgressListener()
: m_lastTestFailed(false) {
}
~ProgressListener() {
}
void startTest(CPPUNIT_NS::Test *test) {
CPPUNIT_NS::stdCOut()<< test->getName();
CPPUNIT_NS::stdCOut()<< " ";
CPPUNIT_NS::stdCOut().flush();
m_lastTestFailed = false;
}
void addFailure(const CPPUNIT_NS::TestFailure& failure) {
CPPUNIT_NS::stdCOut()<< " : " << (failure.isError() ? "error" : "assertion");
m_lastTestFailed = true;
}
void endTest(CPPUNIT_NS::Test *test) {
if (!m_lastTestFailed)
CPPUNIT_NS::stdCOut() << " : OK";
CPPUNIT_NS::stdCOut()<< " ";
}
private:
/// Prevents the use of the copy constructor.
ProgressListener(const ProgressListener& copy);
/// Prevents the use of the copy operator.
void operator=(const ProgressListener& copy);
private:
bool m_lastTestFailed;
};
int main() {
// Create the event manager and test controller
CPPUNIT_NS::TestResult controller;
// Add a listener that colllects test result
CPPUNIT_NS::TestResultCollector result;
controller.addListener(&result);
// Add a listener that print dots as test run.
ProgressListener progress;
controller.addListener(&progress);
// Add the top suite to the test runner
CPPUNIT_NS::TestRunner runner;
runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
runner.run(controller);
// Print test in a compiler compatible format.
CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
outputter.write();
return result.wasSuccessful() ? 0 : 1;
}
Explanation / Answer
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector<string> pullFile(string fileName);
void pushFile(vector<string> totalList, string fileName);
bool ifExists(string fileName);
void printList(vector<string> totalList, string fileName);
int main(){
string trash;
cout << "[2J[1;1H";
string fileName;
vector<string> totalList;
vector<string> names;
if (ifExists("lists.txt")){
names = pullFile("lists.txt");
}
cout<<"File List: ";
if(names.size() <= 1){
cout<<"No lists found. ";
}
else{
for(int c = 0; c < names.size(); c++){
cout<<(c+1)<<". "<<names[c]<<" ";
}
}
bool flag = true;
while (flag){
cout<<" Enter the number of the list you would like to load or Enter file name for a new list. >", cin>>fileName, cout<<" ";
if(isdigit(fileName[0])){
int x = fileName[0] - '0';
if(x > 0 && x < names.size()){
fileName = names[x-1];
flag = false;
}
else{
cout<<"Not a list number. ";
}
}
else{
fileName = fileName + ".txt";
names.push_back(fileName);
flag = false;
}
}
pushFile(names, "lists.txt");
if (ifExists(fileName)){
totalList = pullFile(fileName);
}
bool cont = true;
while (cont){
printList(totalList, fileName);
int choice = 0;
cout<<"What would you like to do? ";
cout<<"1. Add item. ";
cout<<"2. Remove item. ";
cout<<"3. Quit. ";
cout<<">", cin>>choice, cout<<" ";
while(cin.fail()){
cin.ignore();
cin.clear();
rewind(stdin);
cout<<">", cin>>choice, cout<<" ";
}
if(choice == 1){
string newItem;
cout<<"Enter new item: ", cin>>newItem, cout<<" ";
if(cin.fail()){
cin.ignore();
cin.clear();
rewind(stdin);
cout<<"Enter new item: ", cin>>newItem, cout<<" ";
}
totalList.push_back(newItem);
}
else if(choice == 2){
int itemNumber = 0;
cout<<"Enter item number to delete: ", cin>>itemNumber, cout<<" ";
while(cin.fail() || itemNumber < 1 || itemNumber > totalList.size()){
cin.ignore();
cin.clear();
rewind(stdin);
cout<<"Enter item number to delete: ", cin>>itemNumber, cout<<" ";
}
totalList.erase(totalList.begin() + (itemNumber-1));
}
else if(choice == 3){
cont = false;
pushFile(totalList, fileName);
cout<<"Exiting. ";
}
else{
cout<<"Invalid Input! ";
cin.clear();
cin.ignore();
}
}
}
vector<string> pullFile(string fileName){
vector<string> totalList;
fstream ioFile;
ioFile.open(fileName.c_str(), fstream::in);
string bullet;
while (!ioFile.eof()){
getline(ioFile, bullet);
if(!bullet.empty()){
totalList.push_back(bullet);
}
}
ioFile.close();
return totalList;
}
void pushFile(vector<string> totalList, string fileName){
fstream ioFile;
ioFile.open(fileName.c_str(), fstream::out);
for(int c = 0; c < totalList.size(); c++){
if(!totalList[c].empty()){
ioFile<<totalList[c]<<" ";
}
}
ioFile.close();
}
bool ifExists(string fileName){
ifstream infile(fileName.c_str());
return infile.good();
}
void printList(vector<string> totalList, string fileName){
cout << "[2J[1;1H";
cout<<"List: "<<fileName<<"... ";
for(int c = 0; c < totalList.size(); c++){
cout<<(c+1)<<". "<<totalList[c]<<" ";
}
cout<<" ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.