Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "components.h"
- #include <iostream>
- #include <vector>
- #include <iomanip>
- #include <string>
- #include <stdexcept>
- #include <stdlib.h>
- void printHeaders(std::vector<Component*> const& vc, int width, int precision) {
- // Start with empty line.
- std::cout << std::endl;
- // Print names.
- for(Component* c : vc) {
- std::cout << std::setw(width*2) << c->getName();
- }
- std::cout << std::endl;
- // Print Columns.
- for(Component* c : vc) {
- std::cout << std::setw(width) << "Volt"
- << std::setw(width) << "Curr";
- }
- std::cout << std::endl;
- std::cout << std::fixed << std::setprecision(precision);
- }
- void printValues(std::vector<Component*> const& vc, int width) {
- for(Component* c : vc) {
- std::cout << std::setw(width) << c->getU()
- << std::setw(width) << c->getI();
- }
- std::cout << std::endl;
- }
- void update(std::vector<Component*> const& vc, double step) {
- for(Component* c : vc) {
- c->update(step);
- }
- }
- void simulate(std::vector<Component*> const& vc,
- int runs, int print, double step) {
- const int WIDTH = 8;
- printHeaders(vc, WIDTH, 2);
- if (print > runs) {
- printValues(vc, WIDTH);
- }
- for (int i {runs}; i>0 ; i--) {
- update(vc, step);
- if (i <= print) {
- printValues(vc, WIDTH);
- }
- }
- }
- void cleanComponentVector(std::vector<Component*>& vc) {
- for(Component* c : vc) {
- delete c;
- }
- }
- void circuit1(int iteration, int rows, double interval, double voltage) {
- std::vector<Component*> vc;
- Connection p;
- Connection n;
- Connection a;
- Connection b;
- vc.push_back(new Battery("Bat", voltage, p, n));
- vc.push_back(new Resistance(6, p, a));
- vc.push_back(new Resistance(4, a, b));
- vc.push_back(new Resistance(8, b, n));
- vc.push_back(new Resistance(12, a, n));
- simulate(vc, iteration, rows, interval);
- cleanComponentVector(vc);
- }
- void circuit2(int iteration, int rows, double interval, double voltage) {
- std::vector<Component*> vc;
- Connection p;
- Connection n;
- Connection l;
- Connection r;
- vc.push_back(new Battery("Bat", voltage, p, n));
- vc.push_back(new Resistance("R1", 150, p, l));
- vc.push_back(new Resistance("R2", 50, p, r));
- vc.push_back(new Resistance("R3", 100, l, r));
- vc.push_back(new Resistance("R4", 300, l, n));
- vc.push_back(new Resistance("R5", 250, r, n));
- simulate(vc, iteration, rows, interval);
- cleanComponentVector(vc);
- }
- void circuit3(int iteration, int rows, double interval, double voltage) {
- std::vector<Component*> vc;
- Connection p;
- Connection n;
- Connection l;
- Connection r;
- vc.push_back(new Battery("Bat", voltage, p, n));
- vc.push_back(new Resistance("R1", 150, p, l));
- vc.push_back(new Resistance("R2", 50, p, r));
- vc.push_back(new Capacitance("C3", 1.0, l, r));
- vc.push_back(new Resistance("R4", 300, l, n));
- vc.push_back(new Capacitance("C5", 0.75, r, n));
- simulate(vc, iteration, rows, interval);
- cleanComponentVector(vc);
- }
- void printCorrectInputError(std::string error, char *argv[]) {
- std::cerr << "ERROR: " << error << std::endl;
- std::cout << "Correct input is:" << std::endl
- << argv[0] << " ITERATIONS ROWS_TO_DISPLAY"
- << " LENGTH_OF_EACH_INTERVAL VOLTAGE" << std::endl;
- }
- // Try-catch ^
- int main(int argc,char *argv[]) {
- if (argc != 5) {
- printCorrectInputError("Incorrect number of parameters.", argv);
- return 1;
- }
- try {
- int iterations {std::stoi(argv[1])};
- int rows {std::stoi(argv[2])};
- double interval {std::stof(argv[3])};
- double voltage {std::stof(argv[4])};
- if (iterations <= 0 || rows <= 0 || interval <= 0 || voltage <= 0) {
- printCorrectInputError("Value 0 or negative.", argv);
- return 1;
- }
- circuit1(iterations, rows, interval, voltage);
- circuit2(iterations, rows, interval, voltage);
- circuit3(iterations, rows, interval, voltage);
- } catch (std::invalid_argument const& ia) {
- printCorrectInputError("Could not find a valid number.", argv);
- } catch (std::out_of_range const& oor) {
- printCorrectInputError("Value out of range.", argv);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement