Somebody had created a physics programme somewhere so I thought i would have a go and add in some other stuff. It is very long. If someone could show me if there is any way to make it shorter I would be pleased. I have put in comments:
Code:
#include <iostream>
#include <string> //included for the use of strings
#include <cstdlib> //included for the use of system ("CLS")
using namespace std;
int main(int argc, char *argv[])
{
float resistance1 = 0; //obviously create a float varibale, same down below
float resistance2 = 0;
float resistancet = 0;
float voltage = 0;
float current = 0;
string type = ""; //create a string, a string is just a line so it can take any number of letters, but there is a limit
string rtype = "";
string end = "";
do //start a do while loop
{
cout<<"Please enter the type of calculation (lower case): "; //obviously enters text, note that i didnt use endl so that the input is on the same line as the writing
getline(cin,type); //gets the input from the string named type
cout<<endl; //this is where the line ends
if (type == "voltage") { //if the calculation type is voltage then the following code is executed
cout<<"Please enter the total resistance (Ohms): "; //once again not that endl is not there
cin>>resistance1; //obtains the input as resistance1
cout<<endl;
cout<<"Please enter the circuit current (A): ";
cin>>current;
voltage = resistance1 * current; //once the current and resistance are set then it will multiply them together to give you the voltage
cout<<endl;
cout<<"The supply voltage = "<<voltage<<"V"<<endl; //shows the answer
cin.get(); //waits for the user to press return, I think
cout<<endl;
cout<<"Would you like to continue? (yes / no): ";
getline(cin,end); //gets the input whether the user wants to end, when the input is yes the do/while loop repeats when no it ends.
system("CLS"); //clears the screen incase the programme is repeating
}
else if (type == "current") { //else if the calculation required is current then do the following
cout<<"Please enter the voltage supply (V): ";
cin>>voltage;
cout<<endl;
cout<<"Please enter the total resistance (Ohms): ";
cin>>resistance1;
current = voltage / resistance1; //divides voltage by resistance to get the current
cout<<endl;
cout<<"The circuit current = "<<current<<"A"<<endl; //gives the answer
cin.get();
cout<<endl;
cout<<"Would you like to continue? (yes / no): ";
getline(cin,end);
system("CLS"); //clear the screen incase the programme is repeating
}
else if (type == "resistance") {
cout<<"How Would you like to work this out? (series, parallel or v=ir): "; //asks you how you would like to calculate resistance
getline(cin,rtype); //this is where the rtype string is used
cout<<endl;
if (rtype == "series") { //nested if staement...if you want to do it by series then do the following
cout<<"NB, This Calculator can only take two resistances"<<endl; //this line of text does not take input
cout<<endl;
cout<<"Please enter the value of resistor 1 (Ohms): ";
cin>>resistance1;
cout<<endl;
cout<<"Please enter the value resistor 2 (Ohms): ";
cin>>resistance2;
resistancet = resistance1 + resistance2; //adds the two resistances together for series circuits
cout<<endl;
cout<<"The total resistance = "<<resistancet<<" Ohms"<<endl; //gives the answer
cin.get();
cout<<endl;
cout<<"Would you like to continue? (yes / no): ";
getline(cin,end);
system("CLS"); //clears screen
}
else if (rtype == "parallel") { //nested else if statement, just the same as if, if you want parallel resistances then do the following
cout<<"NB, This Calculator can only take two resistances"<<endl;
cout<<endl;
cout<<"Please enter the value of resistor 1 (Ohms): ";
cin>>resistance1;
cout<<endl;
cout<<"Please enter the value resistor 2 (Ohms): ";
cin>>resistance2;
resistancet = (resistance1 * resistance2) / (resistance1 + resistance2); //more complicated sum, note brackets because divide is more important than + and *
cout<<endl;
cout<<"The total resistance = "<<resistancet<<" Ohms"<<endl; //gives the answer to the sum
cin.get();
cout<<endl;
cout<<"Would you like to continue? (yes / no): "; //asks the user if they would like to continue
getline(cin,end); //gets the input to string end
system("CLS"); //clears screen
}
else if (rtype == "v=ir") { //I think it is probablyobvious what this piece of code does
cout<<"Please enter the supply voltage (V): ";
cin>>voltage;
cout<<endl;
cout<<"Please enter the circuit current (A) : ";
cin>>current;
resistancet = voltage / current;
cout<<endl;
cout<<"The total resistance = "<<resistancet<<" Ohms"<<endl;
cin.get();
cout<<endl;
cout<<"Would you like to continue? (yes / no): ";
getline(cin,end);
system("CLS");
}
else { //basically if the type of resistance calcultion is entered wrong then it will do the following
cout<<"Sorry this calculator cannot do that!"<<endl; //another no input line
cout<<endl;
cout<<"Would you like to continue? (yes / no): "; //asks the user if they want to end
getline(cin,end); //gets the string end
system("CLS"); //clears screen
}
} //ends all the resistance code
else { //as above just incase the calculation input is wrong, ie ie you type fgakjk instead of voltage
cout<<"Sorry this calculator cannot do that!"<<endl;
cout<<endl;
cout<<"Would you like to continue? (yes / no): ";
getline(cin,end);
system("CLS");
}
}
while (end == "yes"); //end of the do/while loop, as long as the end string is equal tio yes then the programme will stay open
return 0;
} //ends the entire programme