Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void elements(float*,float*);
int Finish();
float equation(double p, double v, double a, double b, double n);
int main()
{
do
{
FILE *output = NULL;
float p, /*Pressure*/
v, /*Volume of Container*/
n, /*Number of Moles*/
a, /*Inputted by user defined element*/
b, /*Inputted by user defined element*/
eqfinal; /*Van der Waals equation*/
if((output=fopen("c:answer.txt","w")) == NULL)
printf("Error opening data file.\n");
else
{
printf("Van der Waals Equation:\n\n");
printf(" an^2\n"
"(P + -----)(V-nb)=nRT\n"
" V^2\n\n");
elements(&a,&b);
printf("\nElement has a and b defined as:"
"\na=%f b=%f\n\n",a,b);
printf("Please enter the amount of pressure (atm), volume (L), and "
"moles (mol):\n(Separated by a Space)\n\n");
scanf("%f %f %f",&p,&v,&n);
printf("\nPressure=%.3f Volume=%.3f Moles=%.3f\n\n",p,v,n);
printf("Temperature is equal to %.3f Kelvin\n\n",equation(p,v,a,b,n));
fprintf(output,"AC\nKA\n\n"
"Since a=%f and b=%f\n"
"Pressure=%f atm Volume=%f L and Moles=%f mol\n\n"
"Temperature is equal to %f Kelvin\n\n",a,b,
p,v,n,equation(p,v,a,b,n));
fclose(output);
}
}while ( Finish() == 0 );
system("PAUSE");
return 0;
}
void elements(float *a, float *b)
{
int user_element;
float constants[6][2]={
{3.640, .04267},
{.03457, .0237},
{3.716, .04081},
{1.378, .03183},
{6.803, .05636},
{5.536, .03049}
};
printf("Which element do you want to solve for T?\n\n");
printf("1-Carbon Dioxide\n2-Helium\n3-Hydrogen Chloride\n"
"4-Oxygen\n5-Sulfur Dioxide\n6-Water\n\n");
scanf("%d",&user_element);
if(user_element<1 || user_element>6)
{
printf("\nError, Rerunning Choices!\n\n");
elements(a,b);
}
else
{
switch(user_element)
{
case 1:
*a=constants[0][0];
*b=constants[0][1];
break;
case 2:
*a=constants[1][0];
*b=constants[1][1];
break;
case 3:
*a=constants[2][0];
*b=constants[2][1];
break;
case 4:
*a=constants[3][0];
*b=constants[3][1];
break;
case 5:
*a=constants[4][0];
*b=constants[4][1];
break;
case 6:
*a=constants[5][0];
*b=constants[5][1];
break;
}
}
}
float equation(double p, double v, double a, double b, double n)
{
float eqfinal,
eq1,
eq2;
const float R=8.314472;
eq1=p+a*((n*n)/(v*v));
eq2=v-n*b;
eqfinal=(eq1*eq2)/(n*R);
return eqfinal;
}
int Finish()
{
char cAnswer = 0;
while (cAnswer != 'Y' && cAnswer != 'N')
{
printf("Want to quit ([Y]es/[N]o)?\n");
scanf("%c", &cAnswer);
cAnswer = toupper(cAnswer);
}
return (cAnswer == 'Y');
}