Finish assignment 5 with HTML Converter. (This one is actually really neat!)

This commit is contained in:
Charles Descamps 2024-11-12 08:09:55 -07:00
parent d0c2763a23
commit d2d751306e
3 changed files with 372 additions and 0 deletions

Binary file not shown.

View file

@ -12,6 +12,8 @@ Dependencies: ???
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <fstream>
#include <filesystem>
#include <cstring> #include <cstring>
#include <vector> #include <vector>
#include <random> #include <random>
@ -27,14 +29,75 @@ using namespace std;
// main program // main program
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
string fileName;
filesystem::path filePath;
ifstream inputFile;
ofstream outputFile;
char currentChar;
// menu for user // menu for user
cout << "CPP to HTML Converter" << endl;
// ask the user for a cpp file. (check to see if valid?) // ask the user for a cpp file. (check to see if valid?)
cout << "Please enter a CPP source file: ";
cin >> fileName;
// check if the file has the .cpp extension
filePath = fileName;
if(filePath.extension() != ".cpp" && filePath.extension() != ".cp" &&
filePath.extension() != ".C" && filePath.extension() != ".cc")
{
cout << "Invalid File, please enter something else and try again." << endl;
return -1;
}
// check if the file exists
if(!filesystem::exists(fileName))
{
cout << "File is valid, but does not exist." << endl;
return -1;
}
// the file exists, start the parsing according to the comment below.
inputFile.open(fileName);
outputFile.open(filePath.replace_extension(".html"));
// add the <pre> tag, read each character, check to see if the character is one that needs to be // add the <pre> tag, read each character, check to see if the character is one that needs to be
// modified, like '<' and '>' and turned into '&lt;' and '&gt;' once at the end of input file, // modified, like '<' and '>' and turned into '&lt;' and '&gt;' once at the end of input file,
// add the </pre> tag, and output to a new html file that uses the file name // add the </pre> tag, and output to a new html file that uses the file name
// and rids itself of everything after the dot and replaces it with '.html' and writes it out. // and rids itself of everything after the dot and replaces it with '.html' and writes it out.
// add the '<pre>' tag to the output file
outputFile << "<pre>" << endl;
// read in the characters one at a time, check if the character is a '<' or a '>' and replace.
currentChar = inputFile.get();
// outputFile << (char)inputFile.get();
while(!inputFile.eof())
{
if(currentChar == '<' || currentChar == '>')
{
//
if(currentChar == '<')
{
outputFile << "&lt;";
}
else
{
outputFile << "&gt;";
}
}
else
{
outputFile << currentChar;
}
currentChar = inputFile.get();
// outputFile << (char)inputFile.get();
}
// add the '</pre> tag to the output file
outputFile << "</pre>";
// close the input and output files
inputFile.close();
outputFile.close();
// return 0 // return 0
return 0; return 0;

309
assignmentFive/test.cpp Normal file
View file

@ -0,0 +1,309 @@
/*
Program Name: Date (exception handling)
Process: ???
Function Input/Parameters: ???
Function Output/Parameters: ???
Function Output/Returned: ???
Device Input/Device: ???
Device Output/Monitor: ???
Dependencies: ???
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <vector>
#include <random>
#include <algorithm>
#include <time.h>
using namespace std;
// class definitions
class InvalidDay : public exception
{
public:
virtual const char *what() const noexcept
{
return "InvalidDay: Day should be between 1 and 31 (integers).";
}
};
class InvalidMonth : public exception
{
public:
virtual const char *what() const noexcept
{
return "InvalidMonth: Month should be between 1 and 12 (integers).";
}
};
class Date
{
private:
int month;
int day;
int year;
public:
Date()
{
month = 1;
day = 1;
year = 1970;
}
Date(int inMonth, int inDay, int inYear)
{
try
{
if(inMonth > 1 || inMonth < 12)
{
month = inMonth;
}
else
{
throw InvalidMonth();
}
}
catch(const InvalidMonth& e)
{
std::cerr << e.what() << '\n';
}
// next try statement
try
{
if(inDay > 1 || inDay < 31)
{
day = inDay;
}
else
{
throw InvalidDay();
}
}
catch(const InvalidDay& e)
{
std::cerr << e.what() << '\n';
}
year = inYear;
}
~Date()
{
//
}
// accessor functions
int getDay()
{
return day;
}
int getMonth()
{
return month;
}
int getYear()
{
return year;
}
// mutator functions
void setDay(int inputDay)
{
try
{
if (inputDay > 1 || inputDay < 31)
{
day = inputDay;
}
else
{
throw InvalidDay();
}
}
catch (const InvalidDay &e)
{
std::cerr << e.what() << '\n';
}
}
void setMonth(int inputMonth)
{
try
{
if (inputMonth > 1 || inputMonth < 12)
{
month = inputMonth;
}
else
{
throw InvalidMonth();
}
}
catch (const InvalidMonth &e)
{
std::cerr << e.what() << '\n';
}
}
void setYear(int inputYear)
{
year = inputYear;
}
// date printing functions
void printNumericalMonthDayYear()
{
cout << month << "/" << day << "/" << year << endl;
}
void printStringMonthNumericalDayYear()
{
switch (month)
{
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
case 4:
cout << "April";
break;
case 5:
cout << "May";
break;
case 6:
cout << "June";
break;
case 7:
cout << "July";
break;
case 8:
cout << "August";
break;
case 9:
cout << "September";
break;
case 10:
cout << "October";
break;
case 11:
cout << "November";
break;
case 12:
cout << "December";
break;
default:
break;
}
cout << " " << day << ", " << year << endl;
}
void printNumericalDayStringMonthYear()
{
cout << day << " ";
switch (month)
{
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
case 4:
cout << "April";
break;
case 5:
cout << "May";
break;
case 6:
cout << "June";
break;
case 7:
cout << "July";
break;
case 8:
cout << "August";
break;
case 9:
cout << "September";
break;
case 10:
cout << "October";
break;
case 11:
cout << "November";
break;
case 12:
cout << "December";
break;
default:
break;
}
cout << " " << year << endl;
}
// shorthand print functions
void printNMDY()
{
printNumericalMonthDayYear();
}
void printSMNDY()
{
printStringMonthNumericalDayYear();
}
void printNDSMY()
{
printNumericalDayStringMonthYear();
}
};
// function prototypes
// main program
int main(int argc, char **argv)
{
int tempDay, tempMonth, tempYear;
Date linuxEpoch = Date();
Date steveBirthday = Date(2, 24, 1955);
Date christmasThisYear = Date(12, 25, 2024);
Date emptyDate = Date(); // values will be set be accessor/mutators
// ask the user for the values for emptyDate.
cout << "Start date entry:" << endl;
cout << "\tPlease enter in a month: ";
cin >> tempDay;
cout << "\tPlease enter in a day: ";
cin >> tempMonth;
cout << "\tPlease enter in a year: ";
cin >> tempYear;
// finish the user entry.
cout << endl;
// attempt to enter in the date into empty date.
emptyDate.setDay(tempDay);
emptyDate.setMonth(tempMonth);
emptyDate.setYear(tempYear);
// print out each date in different formats.
cout << "Date of Linux Epoch: ";
linuxEpoch.printNumericalDayStringMonthYear();
cout << endl;
cout << "Date of Steve Job's Birthday: ";
steveBirthday.printNumericalMonthDayYear();
cout << endl;
cout << "Date of Christmas this year: ";
christmasThisYear.printStringMonthNumericalDayYear();
cout << endl;
cout << "User inputted date in all formats: " << endl;
emptyDate.printNumericalDayStringMonthYear();
emptyDate.printNumericalMonthDayYear();
emptyDate.printStringMonthNumericalDayYear();
cout << endl;
// return 0
return 0;
}