Entry 3822
main.cpp
Submitted by anonymous
on May 14, 2010 at 5:28 p.m.
Language: C++. Code size: 5.4 KB.
////////////////////////////////////////////////////////// // Filename: main.cpp // Author: Ben Langfeld // Date: 2010-04-20 // // Description: This file contains the main program code // ////////////////////////////////////////////////////////// #include <iostream> #include <string> #include <complex> #include <vector> #include <iostream> #include <fstream> #include "component.h" #include "circuit.h" using namespace std; using namespace langfeld_UoM_cpp_final; int main () { cout << "Welcome! Creating an AC Circuit..." << endl; cout << "What is the angular frequency of the power source (radians/second)? "; double ang_freq(0); cin >> ang_freq; // Declare library of components //map<string, component*> components; //component **components = new component*[num_components]; vector<component*> components; // Set first library element as a wire (resistor of 0 ohms) component* wireobject = new resistor(0); components.push_back(wireobject); // Add components to library cout << "You will now be asked to identify each component as (r)esistor, (i)nductor or (c)apacitor. Choose 's' to stop adding components." << endl; bool cont(true); // Loop continuation flag while (cont) { char component_type; //string label; component* component_object; cout << "Adding a new component. What type is this component? "; cin >> component_type; //cout << "Please specify a unique label for the component (eg 'R1'): "; //cin >> label; if (component_type == 's') { cont = false; break; } double value(0); cout << " "; switch (component_type) { case 'r': cout << "Please specify the resistance in Ohms: "; cin >> value; component_object = new resistor(value); break; case 'i': cout << "Please specify the inductance in Henrys: "; cin >> value; component_object = new inductor(value, ang_freq); break; case 'c': cout << "Please specify the capacitance in Farads: "; cin >> value; component_object = new capacitor(value, ang_freq); break; default: cout << "You provided an invalid input. Please try again." << endl; break; } components.push_back(component_object); //components[i] = component_object; //components.insert(pair<string,component*>(label, component_object)); } // // Print out component library, including values // for (int i(0); i<components.size(); i++) { cout << "Component " << i << ":" << endl; cout << " Type: " << components[i]->get_type() << endl; cout << " Impedance: " << components[i]->get_impedance() << " Ohms" << endl; cout << " Phase Difference: " << components[i]->get_phase_difference() << " radians" << endl << endl; cout << " Reactance: " << components[i]->get_reactance() << " " << endl << endl; } // // Create new circuit and build up // cout << "Creating a new circuit for you...." << endl; circuit current_circuit = circuit(components, ang_freq); cont = true; char orientation; // Series (s) or paralell (p) int component1_id, component2_id; // Component identifiers (keys) within library array int i(1); cout << "You will now be allowed to add components to the circuit in groups of two." << endl; while (cont) { cout << "Section " << i << endl; cout << " Please give the ID of the first component (enter a negative number to stop): "; cin >> component1_id; if (component1_id < 0 ) { // Stop adding components cont = false; break; } cout << " Please give the ID of the second component (enter 0 to add a wire): "; cin >> component2_id; // Determine orientation cout << " Should these components be in (s)eries or (p)aralell? "; cin >> orientation; // Add components to circuit cont = current_circuit.add_component(component1_id, component2_id, orientation); cout << endl; i++; } // // Output circuit specification // cout << endl << "Your circuit now looks like this:" << endl; cout << current_circuit << endl; cout << " Total impedance: " << current_circuit.get_impedance() << " Ohms" << endl; cout << " Total phase difference: " << current_circuit.get_phase_difference() << " Radians" << endl; cout << " Total reactance: " << current_circuit.get_reactance() << " Ohms" << endl; // // Save ASCII representation to file // string output_filename; cout << endl << "Where would you like to save this circuit specification to? "; cin >> output_filename; ofstream out; out.open(output_filename.c_str()); // check output file opened successfully if (!out) { cerr << "Failed to open output file " << output_filename << endl; exit(2); } out << current_circuit << endl; out << " Total impedance: " << current_circuit.get_impedance() << " Ohms" << endl; out << " Total phase difference: " << current_circuit.get_phase_difference() << " Radians" << endl; out << " Total reactance: " << current_circuit.get_reactance() << " Ohms" << endl; cout << "Circuit specification file saved at " << output_filename << endl; cout << "This file includes a netlist that may be used to simulate the circuit. Don't forget to specify the power supply parameters fully." << endl; // // Garbage collection // out.close(); cout << endl << "Destroying your component library." << endl; components.clear(); cout << "Goodbye!" << endl; return 0; }
This snippet took 0.00 seconds to highlight.
Back to the Entry List or Home.