Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vector>
- using namespace std;
- #include "Component.h"
- #include "Capacitor.h"
- #include "Diode.h"
- #include "IntegratedCircuit.h"
- #include "Resistor.h"
- #include "Transistor.h"
- /*
- * Description: Function that takes a line from the file, splits it at each comma, removes spaces and then returns it
- * as a vector (array)
- */
- vector<string> extractData(string line) {
- // Defines array
- vector<string> tokenArr;
- // Stores in the string
- size_t position = 0;
- // String that will store each string after split at comma
- string token;
- // While there is a comma in the string
- while ((position = line.find(',')) != std::string::npos) {
- // Gets the token before first ','
- string token = line.substr(0, position);
- // Loops through the token and removes finds all spaces
- std::string::iterator end_pos = std::remove(token.begin(), token.end(), ' ');
- // Removes spaces
- token.erase(end_pos, token.end());
- // Adds token to vector
- tokenArr.push_back(token);
- // Removes the token from the line so don't repeat same item
- line.erase(0, position + 1);
- }
- // Repeats outside loop for final token after last comma
- std::string::iterator end_pos = std::remove(line.begin(), line.end(), ' ');
- line.erase(end_pos, line.end());
- tokenArr.push_back(line);
- return tokenArr;
- }
- /*
- * Description: Simple function that prints a vector out
- * @param: vectorPrint - vector<string> you want to print
- */
- void printVector(vector<string> vectorPrint){
- for (int i = 0; i < vectorPrint.size(); i++) {
- cout << i << " " << vectorPrint.at(i) << '\n';
- }
- }
- int main() {
- Component componentTest("123",72,1);
- Capacitor capacitorTest("CAP_1800nF",191,66,"1800nF");
- Diode diodeTest("BY126",118,12);
- IntegratedCircuit integratedCircuitTest("NE555",8,17,"Timer");
- Resistor resistorTest("RES_220R",40,1,"220R");
- Transistor transistorTest("AC125",13,35,"PNP");
- //File read in
- string line;
- ifstream inventory("inventory.txt");
- if (inventory.is_open())
- {
- //splitting input by new line
- while ( getline (inventory,line, '\n') )
- {
- vector<string> tokenArr = extractData(line);
- cout << tokenArr.at(0) << '\n';
- // printVector(tokenArr);
- if(tokenArr[0].compare("resistor") == 0){
- Resistor resistorTest(tokenArr.at(1),std::stoi(tokenArr.at(2)),std::stoi(tokenArr.at(3)),tokenArr.at(4));
- }
- if(tokenArr[0].compare("capacitor") == 0){
- Capacitor capacitorTest(tokenArr.at(1),std::stoi(tokenArr.at(2)),std::stoi(tokenArr.at(3)),tokenArr.at(4));
- }
- if(tokenArr.at(0).compare("diode") == 0){
- Diode diodeTest(tokenArr.at(1),std::stoi(tokenArr.at(2)),std::stoi(tokenArr.at(3)));
- }
- if(tokenArr.at(0).compare("transistor") == 0){
- Transistor transistorTest(tokenArr.at(1),std::stoi(tokenArr.at(2)),std::stoi(tokenArr.at(3)),tokenArr.at(4));
- }
- if(tokenArr.at(0).compare("IC") == 0){
- IntegratedCircuit integratedCircuitTest(tokenArr.at(1),std::stoi(tokenArr.at(2)),std::stoi(tokenArr.at(3)),tokenArr.at(4));
- }
- }
- inventory.close();
- }
- else cout << "Unable to open file";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement