1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include <iostream>
#include <string.h>
//PV=nRT
using namespace std;
float celtokel(float cels)
{
return (cels + 273.15); //Celsius to Kelvin
}
float fartokel(float far)
{
float cel = (far-32)/(9/5);
float kel = celtokel(cel); //Farenheit to Kelvin
return kel;
}
float leftsolve(float a, float b, float c, float R)
{
return ( (a*b*R)/c ); //Solve for a variable on the left of the equation.
}
float rightsolve(float a, float b, float c, float R)
{
return( (b*c)/(R*a) ); //Solve for a variable on the right side.
}
int main(int argc, char* argv[])
{
float temp, pressure, volume, moles, R;
float Rp[3] = {.08205746, 62.36367, 8.314472}; //Define R constant.
int control;
control = 0;
if(argc != 4)
{
cout<<"You need to enter: Variable to be solved for (IE: /p, /v, /n, /t)\nThe temp units (IE /f /c /k)\nAnd the pressure units (IE /atm, /torr, /kPa)";
return 1;
}
else
{
if(control == 0) //set R constant depending on user input.
{
if (strcmp(argv[3],"/atm")) { R = Rp[0];}
else if(strcmp(argv[3],"/torr")) { R = Rp[1]; }
else if(strcmp(argv[3],"/kPa")) { R = Rp[2]; }
control++;
}
else if(control == 1) //convert all temp to Kelvin, based on user input.
{
if(!strcmp(argv[1],"/t"))
{
cout<<"Enter temperature: ";
cin>>temp;
if(argv[2] == "/f") { temp = fartokel(temp); }
else if(argv[2] == "/c") { temp = celtokel(temp); }
}
control++;
}
else if(control == 2)//solve equations
{
if(strcmp(argv[1],"/p"))
{
cout<<"\nEnter Volume: ";
cin>>volume;
cout<<"\nEnter number of moles: ";
cin>>moles;
cout<<"\nPressure equals: " << leftsolve(temp, moles, volume, R);
}
else if(strcmp(argv[1],"/v"))
{
cout<<"\nEnter Pressure: ";
cin>>pressure;
cout<<"\nEnter number of moles: ";
cin>>moles;
cout<<"\nVolume equals: "<< leftsolve(temp, moles, pressure, R);
}
else if(strcmp(argv[1],"/n"))
{
cout<<"\nEnter Pressure: ";
cin>>pressure;
cout<<"\nEnter Volume: ";
cin>>volume;
cout<<"\nNumber of moles equals: "<< rightsolve(pressure, volume, temp, R);
}
else if(strcmp(argv[1],"/t"))
{
cout<<"\nEnter Volume: ";
cin>>volume;
cout<<"\nEnter Pressure: ";
cin>>pressure;
cout<<"\nEnter number of moles: ";
cin>>moles;
cout<<"\nTemperature equals: " << rightsolve(pressure, volume, moles, R);
}
}
}
return 0;
}
|