Advertisement
IceJade

lab3

Feb 20th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. #include "components.h"
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <iomanip>
  6. #include <string>
  7. #include <stdexcept>
  8.  
  9. #include <stdlib.h>
  10.  
  11. void printHeaders(std::vector<Component*> const& vc, int width, int precision) {
  12. // Start with empty line.
  13. std::cout << std::endl;
  14.  
  15. // Print names.
  16. for(Component* c : vc) {
  17. std::cout << std::setw(width*2) << c->getName();
  18. }
  19. std::cout << std::endl;
  20.  
  21. // Print Columns.
  22. for(Component* c : vc) {
  23. std::cout << std::setw(width) << "Volt"
  24. << std::setw(width) << "Curr";
  25. }
  26. std::cout << std::endl;
  27.  
  28. std::cout << std::fixed << std::setprecision(precision);
  29. }
  30.  
  31. void printValues(std::vector<Component*> const& vc, int width) {
  32. for(Component* c : vc) {
  33. std::cout << std::setw(width) << c->getU()
  34. << std::setw(width) << c->getI();
  35. }
  36. std::cout << std::endl;
  37. }
  38.  
  39. void update(std::vector<Component*> const& vc, double step) {
  40. for(Component* c : vc) {
  41. c->update(step);
  42. }
  43. }
  44.  
  45. void simulate(std::vector<Component*> const& vc,
  46. int runs, int print, double step) {
  47. const int WIDTH = 8;
  48. printHeaders(vc, WIDTH, 2);
  49.  
  50. if (print > runs) {
  51. printValues(vc, WIDTH);
  52. }
  53.  
  54. for (int i {runs}; i>0 ; i--) {
  55. update(vc, step);
  56.  
  57. if (i <= print) {
  58. printValues(vc, WIDTH);
  59. }
  60. }
  61. }
  62.  
  63. void cleanComponentVector(std::vector<Component*>& vc) {
  64. for(Component* c : vc) {
  65. delete c;
  66. }
  67. }
  68.  
  69. void circuit1(int iteration, int rows, double interval, double voltage) {
  70. std::vector<Component*> vc;
  71.  
  72. Connection p;
  73. Connection n;
  74. Connection a;
  75. Connection b;
  76.  
  77. vc.push_back(new Battery("Bat", voltage, p, n));
  78. vc.push_back(new Resistance(6, p, a));
  79. vc.push_back(new Resistance(4, a, b));
  80. vc.push_back(new Resistance(8, b, n));
  81. vc.push_back(new Resistance(12, a, n));
  82.  
  83. simulate(vc, iteration, rows, interval);
  84. cleanComponentVector(vc);
  85. }
  86.  
  87. void circuit2(int iteration, int rows, double interval, double voltage) {
  88. std::vector<Component*> vc;
  89.  
  90. Connection p;
  91. Connection n;
  92. Connection l;
  93. Connection r;
  94.  
  95. vc.push_back(new Battery("Bat", voltage, p, n));
  96. vc.push_back(new Resistance("R1", 150, p, l));
  97. vc.push_back(new Resistance("R2", 50, p, r));
  98. vc.push_back(new Resistance("R3", 100, l, r));
  99. vc.push_back(new Resistance("R4", 300, l, n));
  100. vc.push_back(new Resistance("R5", 250, r, n));
  101.  
  102. simulate(vc, iteration, rows, interval);
  103. cleanComponentVector(vc);
  104. }
  105.  
  106. void circuit3(int iteration, int rows, double interval, double voltage) {
  107. std::vector<Component*> vc;
  108.  
  109. Connection p;
  110. Connection n;
  111. Connection l;
  112. Connection r;
  113.  
  114. vc.push_back(new Battery("Bat", voltage, p, n));
  115. vc.push_back(new Resistance("R1", 150, p, l));
  116. vc.push_back(new Resistance("R2", 50, p, r));
  117. vc.push_back(new Capacitance("C3", 1.0, l, r));
  118. vc.push_back(new Resistance("R4", 300, l, n));
  119. vc.push_back(new Capacitance("C5", 0.75, r, n));
  120.  
  121. simulate(vc, iteration, rows, interval);
  122. cleanComponentVector(vc);
  123. }
  124.  
  125. void printCorrectInputError(std::string error, char *argv[]) {
  126. std::cerr << "ERROR: " << error << std::endl;
  127. std::cout << "Correct input is:" << std::endl
  128. << argv[0] << " ITERATIONS ROWS_TO_DISPLAY"
  129. << " LENGTH_OF_EACH_INTERVAL VOLTAGE" << std::endl;
  130. }
  131.  
  132. // Try-catch ^
  133.  
  134. int main(int argc,char *argv[]) {
  135. if (argc != 5) {
  136. printCorrectInputError("Incorrect number of parameters.", argv);
  137. return 1;
  138. }
  139.  
  140. try {
  141. int iterations {std::stoi(argv[1])};
  142. int rows {std::stoi(argv[2])};
  143. double interval {std::stof(argv[3])};
  144. double voltage {std::stof(argv[4])};
  145.  
  146. if (iterations <= 0 || rows <= 0 || interval <= 0 || voltage <= 0) {
  147. printCorrectInputError("Value 0 or negative.", argv);
  148. return 1;
  149. }
  150.  
  151. circuit1(iterations, rows, interval, voltage);
  152. circuit2(iterations, rows, interval, voltage);
  153. circuit3(iterations, rows, interval, voltage);
  154. } catch (std::invalid_argument const& ia) {
  155. printCorrectInputError("Could not find a valid number.", argv);
  156. } catch (std::out_of_range const& oor) {
  157. printCorrectInputError("Value out of range.", argv);
  158. }
  159.  
  160. return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement