该程序的目的是从文件中读取数据,然后使用该数据使用六种不同温度下的 a 和 b 值计算气体压力。目前在打印输出时,我认为该程序仅使用五个值中的第一个 a 和 b 值,因此为每个 a 和 b 值打印出相同的信息。任何帮助,将不胜感激!我不知道如何上传文件,所以这里是数据:
数据:
0.0341 0.0237
0.244 0.0266
1.36 0.0318
5.46 0.0305
20.4 0.1383
代码:
#include <stdio.h>
#include <math.h>
#define R 0.08314472
#define MAXNUMBERGASES 10
#define NUMBERTEMPS 6
#define FILENAME "gasValues.txt"
//prototype functions
int getGasValues (double a[], double b []);
void printHeaders (double tempF[]);
void computePressure (double tempF[], double pressure[], double moles,
double volume, double a, double b);
void printGasInfo (double a, double b, double pressure[]);
int main()
{
int moles = 2; //mol (n)
int volume = 1; //Liters (V)
double a[MAXNUMBERGASES]; //L^2bar/mol^2
double b[MAXNUMBERGASES]; //L/mol
int numberGases, g;
double tempF[] = {0, 20, 40, 60, 80, 100};
double pressure[NUMBERTEMPS];
numberGases = getGasValues (a, b);
if (numberGases < 1) printf ("Error. No data read from file\n");
else
{
printHeaders(tempF);
for (g = 0; g < numberGases; g++)
{
computePressure (&tempF[g], &pressure[g], moles, volume, a[g], b[g]);
printGasInfo (a[g], b[g], &pressure[g]);
}
}
return 0;
}
int getGasValues (double a[], double b[])
{
FILE *gasFile; //file pointer
int g = 0; //counter for number of gases
gasFile = fopen("gasValues.txt", "r");
if (gasFile == NULL){
printf("File could not be opened. Program terminated.\n");
return 0; //end program if file cannot be opened or found
}
else
while ((fscanf (gasFile, "%lf" "%lf", &a[g], &b[g])) != EOF) g++;
return g;
}
void printHeaders (double tempF[NUMBERTEMPS])
{
printf ("\t\t\t Pressure (atm) using Waals' Ideal Gas Law\n\n");
printf ("L2atm/mol2 \tL/mol");
int t = 0;
for (t = 0; t < NUMBERTEMPS; t++)
{
printf (" %10.0lfF" ,tempF[t]);
}
printf ("\n");
}
void computePressure (double tempF[NUMBERTEMPS], double pressure[NUMBERTEMPS], double moles, double volume, double a, double b)
{
int t=0;
for (t = 0; t < 6; t++)
{
double tempK = (5/9) * (tempF[t]-32) + 273;
double part1 = (moles * R * tempK);
double part2 = volume - (moles*b);
double part3 = (a*(pow(moles,2)))/(volume*volume);
double part4 = part1/part2;
pressure[t] = part4 - part3;
}
}
void printGasInfo (double a, double b, double pressure[NUMBERTEMPS])
{
int p = 0;
printf("%8.4lf" "%13.4lf", a, b);
for (p = 0; p < NUMBERTEMPS; p++)
{
printf (" %8.4lf", pressure[p]);
}
printf ("\n");
}