Attempting in c++

This commit is contained in:
Ada Werefox 2025-12-06 14:38:09 -08:00
parent 74cb00b0ae
commit 3f854b96e1
6 changed files with 134 additions and 0 deletions

36
05/Makefile Normal file
View file

@ -0,0 +1,36 @@
# Compiler
CXX = g++
# Compiler flags
CXXFLAGS = -Wall -g
# Target executable
TARGET = main
# For deleting the target
TARGET_DEL = main
# Source files
SRCS = src/main.cpp src/produce_database.cpp src/read_input.cpp
# Object files
OBJS = $(SRCS:.cpp=.o)
# Default rule to build and run the executable
all: $(TARGET) run
# Rule to link object files into the target executable
$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJS)
# Rule to compile .cpp files into .o files
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
# Rule to run the executable
run: $(TARGET)
$(TARGET)
# Clean rule to remove generated files
clean:
del $(TARGET_DEL) $(OBJS)

24
05/src/main.cpp Normal file
View file

@ -0,0 +1,24 @@
/* Filename: main.cpp
* Author: Alice Winters
* Created: 05/12/2025
*/
#include <fstream>
#include <iostream>
#include <vector>
#include "produce_database.h"
#include "read_input.h"
const std::string INPUT_FILEPATH = "input/test_ingredients.txt";
int main()
{
produce_database produce_db = read_input_from_file(INPUT_FILEPATH);
for (int i = 0; i < produce_db.get_fresh_ranges_size(); i++)
{
std::cout << produce_db.get_fresh_range_from_index(i) << std::endl;
}
return 0;
}

View file

@ -0,0 +1,19 @@
#include "produce_database.h"
#include <vector>
#include <string>
int produce_database::get_fresh_ranges_size()
{
return this->fresh_ingredient_id_ranges.size();
}
std::string produce_database::get_fresh_range_from_index(int index)
{
return this->fresh_ingredient_id_ranges[index];
}
void produce_database::add_fresh_range(std::string range)
{
this->fresh_ingredient_id_ranges.push_back(range);
return;
}

20
05/src/produce_database.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef PRODUCE_DATABASE_H
#define PRODUCE_DATABASE_H
#include <vector>
#include <string>
class produce_database
{
private:
std::vector<std::string> fresh_ingredient_id_ranges;
std::vector<std::string> available_ingredient_ids;
public:
int get_fresh_ranges_size();
std::string get_fresh_range_from_index(int index);
void add_fresh_range(std::string range);
};
#endif

22
05/src/read_input.cpp Normal file
View file

@ -0,0 +1,22 @@
#include "read_input.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include "produce_database.h"
using namespace std;
produce_database read_input_from_file(string filepath)
{
produce_database produce_db;
ifstream input_file(filepath);
string input_buffer = " ";
while (input_buffer != "")
{
getline(input_file, input_buffer);
produce_db.add_fresh_range(input_buffer);
}
input_file.close();
return produce_db;
}

13
05/src/read_input.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef READ_INPUT_H
#define READ_INPUT_H
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include "produce_database.h"
produce_database read_input_from_file(std::string filepath);
#endif