Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. #include "Component.h"
  10. #include "Capacitor.h"
  11. #include "Diode.h"
  12. #include "IntegratedCircuit.h"
  13. #include "Resistor.h"
  14. #include "Transistor.h"
  15.  
  16. /*
  17. * Description: Function that takes a line from the file, splits it at each comma, removes spaces and then returns it
  18. * as a vector (array)
  19. */
  20. vector<string> extractData(string line) {
  21. // Defines array
  22. vector<string> tokenArr;
  23. // Stores in the string
  24. size_t position = 0;
  25. // String that will store each string after split at comma
  26. string token;
  27. // While there is a comma in the string
  28. while ((position = line.find(',')) != std::string::npos) {
  29. // Gets the token before first ','
  30. string token = line.substr(0, position);
  31. // Loops through the token and removes finds all spaces
  32. std::string::iterator end_pos = std::remove(token.begin(), token.end(), ' ');
  33. // Removes spaces
  34. token.erase(end_pos, token.end());
  35. // Adds token to vector
  36. tokenArr.push_back(token);
  37. // Removes the token from the line so don't repeat same item
  38. line.erase(0, position + 1);
  39. }
  40. // Repeats outside loop for final token after last comma
  41. std::string::iterator end_pos = std::remove(line.begin(), line.end(), ' ');
  42. line.erase(end_pos, line.end());
  43. tokenArr.push_back(line);
  44. return tokenArr;
  45. }
  46.  
  47.  
  48. /*
  49. * Description: Simple function that prints a vector out
  50. * @param: vectorPrint - vector<string> you want to print
  51. */
  52. void printVector(vector<string> vectorPrint){
  53. for (int i = 0; i < vectorPrint.size(); i++) {
  54. cout << i << " " << vectorPrint.at(i) << '\n';
  55. }
  56.  
  57. }
  58.  
  59. int main() {
  60. Component componentTest("123",72,1);
  61. Capacitor capacitorTest("CAP_1800nF",191,66,"1800nF");
  62. Diode diodeTest("BY126",118,12);
  63. IntegratedCircuit integratedCircuitTest("NE555",8,17,"Timer");
  64. Resistor resistorTest("RES_220R",40,1,"220R");
  65. Transistor transistorTest("AC125",13,35,"PNP");
  66.  
  67. //File read in
  68. string line;
  69. ifstream inventory("inventory.txt");
  70. if (inventory.is_open())
  71. {
  72. //splitting input by new line
  73. while ( getline (inventory,line, '\n') )
  74. {
  75. vector<string> tokenArr = extractData(line);
  76. cout << tokenArr.at(0) << '\n';
  77. // printVector(tokenArr);
  78. if(tokenArr[0].compare("resistor") == 0){
  79. Resistor resistorTest(tokenArr.at(1),std::stoi(tokenArr.at(2)),std::stoi(tokenArr.at(3)),tokenArr.at(4));
  80. }
  81. if(tokenArr[0].compare("capacitor") == 0){
  82. Capacitor capacitorTest(tokenArr.at(1),std::stoi(tokenArr.at(2)),std::stoi(tokenArr.at(3)),tokenArr.at(4));
  83. }
  84. if(tokenArr.at(0).compare("diode") == 0){
  85. Diode diodeTest(tokenArr.at(1),std::stoi(tokenArr.at(2)),std::stoi(tokenArr.at(3)));
  86. }
  87. if(tokenArr.at(0).compare("transistor") == 0){
  88. Transistor transistorTest(tokenArr.at(1),std::stoi(tokenArr.at(2)),std::stoi(tokenArr.at(3)),tokenArr.at(4));
  89. }
  90. if(tokenArr.at(0).compare("IC") == 0){
  91. IntegratedCircuit integratedCircuitTest(tokenArr.at(1),std::stoi(tokenArr.at(2)),std::stoi(tokenArr.at(3)),tokenArr.at(4));
  92. }
  93. }
  94. inventory.close();
  95. }
  96.  
  97. else cout << "Unable to open file";
  98.  
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement