added old projects
This commit is contained in:
+28
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"tasks": [
|
||||
{
|
||||
"type": "cppbuild",
|
||||
"label": "C/C++: g++ build active file",
|
||||
"command": "/usr/bin/g++",
|
||||
"args": [
|
||||
"-fdiagnostics-color=always",
|
||||
"-g",
|
||||
"${file}",
|
||||
"-o",
|
||||
"${fileDirname}/${fileBasenameNoExtension}"
|
||||
],
|
||||
"options": {
|
||||
"cwd": "${fileDirname}"
|
||||
},
|
||||
"problemMatcher": [
|
||||
"$gcc"
|
||||
],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
},
|
||||
"detail": "Task generated by Debugger."
|
||||
}
|
||||
],
|
||||
"version": "2.0.0"
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"tasks": [
|
||||
{
|
||||
"type": "cppbuild",
|
||||
"label": "C/C++: g++ build active file",
|
||||
"command": "/usr/bin/g++",
|
||||
"args": [
|
||||
"-fdiagnostics-color=always",
|
||||
"-g",
|
||||
"${file}",
|
||||
"-o",
|
||||
"${fileDirname}/${fileBasenameNoExtension}"
|
||||
],
|
||||
"options": {
|
||||
"cwd": "${fileDirname}"
|
||||
},
|
||||
"problemMatcher": [
|
||||
"$gcc"
|
||||
],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
},
|
||||
"detail": "Task generated by Debugger."
|
||||
}
|
||||
],
|
||||
"version": "2.0.0"
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
#include <string>
|
||||
|
||||
template<typename inputType>
|
||||
|
||||
inputType ReadValue(std::string prompt)
|
||||
{
|
||||
inputType returnValue;
|
||||
std::cout << prompt;
|
||||
std::cin >> returnValue;
|
||||
while (std::cin.fail()) {
|
||||
std::cout << "Error! Cannot read input.\n";
|
||||
std::cin.clear();
|
||||
std::cin.ignore(INT_MAX,'\n');
|
||||
std::cout << prompt;
|
||||
std::cin >> returnValue;
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
template<typename inputType>
|
||||
inputType ReadValue(std::string prompt, inputType minValue)
|
||||
{
|
||||
inputType returnValue=0;
|
||||
returnValue=ReadValue<inputType>(prompt);
|
||||
while (returnValue < minValue) {
|
||||
std::cout << "Error! Value must be >= " << minValue << std::endl;
|
||||
returnValue=ReadValue<inputType>(prompt);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
template<typename inputType>
|
||||
inputType ReadValue(std::string prompt, inputType minValue, inputType maxValue)
|
||||
{
|
||||
inputType returnValue=0;
|
||||
returnValue=ReadValue<inputType>(prompt,minValue);
|
||||
while (returnValue > maxValue) {
|
||||
std::cout << "Error! Value must be <= " << maxValue << std::endl;
|
||||
returnValue=ReadValue<inputType>(prompt,minValue);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
#include "list.h"
|
||||
#include "input.h"
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
void List::Clear()
|
||||
{
|
||||
if (size > 0)
|
||||
{
|
||||
delete[] data;
|
||||
size = 0;
|
||||
data = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::string List::Display()
|
||||
{
|
||||
std::string result;
|
||||
if (size > 0)
|
||||
{
|
||||
std::cout << "List values:\n";
|
||||
for (std::string *ptr = data; ptr < data + size; ptr++)
|
||||
{
|
||||
result = result + *ptr + " ";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = "List is empty.\n";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// There must be a function to allow the list size to be increased without losing data that has been previously stored in the list.
|
||||
void List::Resize(long numValues)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (numValues < 1 || numValues > MAX_SIZE)
|
||||
{
|
||||
throw "Error! Invalid list size specified.";
|
||||
}
|
||||
|
||||
if (size == 0)
|
||||
{
|
||||
Clear();
|
||||
data = new std::string[numValues];
|
||||
|
||||
if (data == nullptr)
|
||||
{
|
||||
throw "Error! Could not allocate memory for list.";
|
||||
}
|
||||
|
||||
size = numValues;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp = new std::string[size + numValues];
|
||||
|
||||
copy(data, data + size, temp);
|
||||
|
||||
Clear();
|
||||
data = new std::string[size + numValues];
|
||||
|
||||
std::string *tempPtr = temp;
|
||||
|
||||
for (std::string *ptr=data; ptr<data+size+numValues; ptr++) {
|
||||
*ptr= *tempPtr;
|
||||
}
|
||||
|
||||
size = numValues + size;
|
||||
|
||||
delete[] temp;
|
||||
}
|
||||
}
|
||||
catch (std::exception &e)
|
||||
{
|
||||
throw "Error! Could not allocate memory for list.";
|
||||
}
|
||||
}
|
||||
|
||||
void List::SetValue(std::string value, long pos)
|
||||
{
|
||||
if (pos < 0 || pos >= size)
|
||||
{
|
||||
throw "Error! Invalid position in list specified.";
|
||||
}
|
||||
data[pos] = value;
|
||||
}
|
||||
|
||||
std::string List::GetValue(long pos)
|
||||
{
|
||||
if (pos < 0 || pos >= size)
|
||||
{
|
||||
throw "Error! Invalid position in list specified.";
|
||||
}
|
||||
return data[pos];
|
||||
}
|
||||
|
||||
void List::Remove(std::string value)
|
||||
{
|
||||
int index;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
if (data[i] == value)
|
||||
{
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = index; i < size; i++)
|
||||
data[i] = data[i + 1];
|
||||
}
|
||||
|
||||
void List::Add()
|
||||
{
|
||||
try {
|
||||
long x=ReadValue<long>("Number of values? ");
|
||||
Resize(x);
|
||||
if (size == 0) {
|
||||
for (std::string *ptr=data; ptr<data+size; ptr++) {
|
||||
*ptr=ReadValue<std::string>("Value? ");
|
||||
}
|
||||
} else {
|
||||
for (std::string *ptr=data; ptr<data+size+x; ptr++) {
|
||||
*ptr=ReadValue<std::string>("Value? ");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const char* s) {
|
||||
throw s;
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, List &l)
|
||||
{
|
||||
out << l.Display();
|
||||
return out;
|
||||
}
|
||||
|
||||
List::List()
|
||||
{
|
||||
data = nullptr;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
List::~List()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
List::List(const List &x)
|
||||
{
|
||||
size = x.size;
|
||||
data = new std::string[size];
|
||||
for (long i = 0; i < x.size; i++)
|
||||
{
|
||||
data[i] = x.data[i];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef LIST_H
|
||||
#define LIST_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class List
|
||||
{
|
||||
private:
|
||||
|
||||
|
||||
std::string* temp;
|
||||
|
||||
public:
|
||||
std::string* data;
|
||||
long size;
|
||||
enum {MAX_SIZE=1000};
|
||||
|
||||
void Remove(std::string value);
|
||||
void Add();
|
||||
void Resize(long numValues);
|
||||
void Clear();
|
||||
void SetValue(std::string value, long pos);
|
||||
std::string GetValue(long pos);
|
||||
long GetSize();
|
||||
void Input(long newSize);
|
||||
std::string Display();
|
||||
|
||||
|
||||
List();
|
||||
List(const List& l);
|
||||
~List();
|
||||
};
|
||||
|
||||
#endif
|
||||
std::istream& operator>> (std::istream& in, List& l);
|
||||
std::ostream& operator<< (std::ostream& out, List& l);
|
||||
@@ -0,0 +1,72 @@
|
||||
//
|
||||
// main.cpp
|
||||
// BookList
|
||||
//
|
||||
// Created by Austin Bennett on 6/22/22.
|
||||
//
|
||||
|
||||
/*
|
||||
Create a C++ program that will keep a list of books. Your program must meet the following criteria:
|
||||
ø The program will provide the user with a menu to add names to the list, delete a name from the list (searching by title), display the list in it's entirety
|
||||
√ The list of books will be stored as a dynamically allocated array of strings. The strings can be either STL strings or C style strings
|
||||
√ The list must be stored in a class
|
||||
√ There must be an operator overload created for the stream extraction operator that will handle the output of the list
|
||||
√ You must provide a copy constructor for the class that will allow the programmer to set one class object equal to another
|
||||
√ There must be a function to allow the list size to be increased without losing data that has been previously stored in the list.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "list.h"
|
||||
#include "input.h"
|
||||
|
||||
// The program will provide the user with a menu to add names to the list, delete a name from the list (searching by title), display the list in it's entirety
|
||||
void menu(List books) {
|
||||
int choice;
|
||||
bool inProgram = true;
|
||||
while (inProgram != false){
|
||||
std::cout << "*******************************\n";
|
||||
std::cout << " 1 - Add Item.\n";
|
||||
std::cout << " 2 - Delete Item.\n";
|
||||
std::cout << " 3 - Display List.\n";
|
||||
std::cout << " 4 - Exit.\n";
|
||||
|
||||
choice = ReadValue<int>("Enter your choice and press return: ");
|
||||
|
||||
switch (choice) {
|
||||
case 1: // Add Item
|
||||
books.Add();
|
||||
break;
|
||||
case 2: // Delete Item
|
||||
books.Remove(ReadValue<std::string>("Enter Book to Remove: "));
|
||||
break;
|
||||
case 3: // Display List
|
||||
// There must be an operator overload created for the stream extraction operator that will handle the output of the list
|
||||
std::cout << books;
|
||||
break;
|
||||
case 4:
|
||||
std::cout << "End of Program.\n";
|
||||
inProgram = false;
|
||||
break;
|
||||
default:
|
||||
std::cout << "Not a Valid Choice. \n";
|
||||
std::cout << "Choose again.\n";
|
||||
std::cin >> choice;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
// The list must be stored in a class
|
||||
List books;
|
||||
|
||||
menu(books);
|
||||
|
||||
// You must provide a copy constructor for the class that will allow the programmer to set one class object equal to another
|
||||
List books2 = List(books);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user