0

该程序的目的是从文件中读取数据,然后使用该数据使用六种不同温度下的 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");
}
4

2 回答 2

1

你有一个常见的错误:在

        double tempK = (5/9) * (tempF[t]-32) + 273;

5/9总是零,因为它是整数除法。使用5.0/9.0,这样的结果是

                Pressure (atm) using Waals' Ideal Gas Law

L2atm/mol2  L/mol           0F          20F          40F          60F          80F         100F
  0.0341       0.0237      44.4162      46.3557      48.2953      50.2349      52.1745      54.1141
  0.2440       0.0266      45.8010      47.7524      49.7039      51.6554      53.6069      43.8495
  1.3600       0.0318      43.8296      45.8028      47.7759      49.7491      39.8833      39.8833
  5.4600       0.0305      29.2609      31.2286      33.1963      23.3578      23.3578      23.3602
 20.4000       0.1383     -12.7150     -10.1609     -22.9315     -22.9315     -22.9285     -22.9281

现在结果有所不同,尽管我不确定负压是否应该在最后一行中实际看到。


我看到的第二个问题是您将指向特定数组项的指针传递给pressure,但正如您可能看到的那样,您打算将指针传递给数组。也有可能获得越界数组访问,因为(and ) 不必包含最多 MAXNUMBERGASES 个项目,并且您使用(and ) where is limited to the number of lines in the file with the number of lines in the file limited到 MAXNUMBERGAS 个。tempFcomputePressurecomputePressuretempFpressure&tempF[g]&pressure[g]g

即将这些行更改为

        computePressure (tempF, pressure, moles, volume, a[g], b[g]);

        printGasInfo (a[g], b[g], pressure);

. 结果:

                Pressure (atm) using Waals' Ideal Gas Law

L2atm/mol2  L/mol           0F          20F          40F          60F          80F         100F
  0.0341       0.0237      44.4162      46.3557      48.2953      50.2349      52.1745      54.1141
  0.2440       0.0266      43.8495      45.8010      47.7524      49.7039      51.6554      53.6069
  1.3600       0.0318      39.8833      41.8565      43.8296      45.8028      47.7759      49.7491
  5.4600       0.0305      23.3578      25.3255      27.2932      29.2609      31.2286      33.1963
 20.4000       0.1383     -22.9315     -20.3774     -17.8233     -15.2691     -12.7150     -10.1609

注意:不再有 valgrind 错误。

于 2013-11-06T17:50:08.280 回答
1

您的代码中有一些错误。

当您将 tempF 和压力数组传递给 computePressure 时,我认为您不想传递第 gth 元素的地址。看起来您的代码需要数组开头的地址。它应该看起来像这样:

computePressure (tempF, pressure, moles, volume, a[g], b[g]);

您在调用 printGasInfo 时遇到了同样的问题,您在其中传递了第 g 个压力元素的地址。它应该更改为类似于以下内容:

printGasInfo (a[g], b[g], pressure);

我看到的另一个问题是在 computePressure 中计算 tempK。比率 5/9 将使用整数计算并返回 0,因此 tempK 将始终导致 273 的值,这不是您想要的。您需要将其更改为使用浮点/双精度常量。此外,正确转换为开尔文应该使用 273.15 的偏移量。tempK 计算应如下所示:

double tempK = (5.0/9.0) * (tempF[t]-32) + 273.15;

我通过这些更改得到的输出是:

                            Pressure (atm) using Waals' Ideal Gas Law

L2atm/mol2      L/mol           0F          20F          40F          60F          80F         100F
  0.0341       0.0237      44.4423      46.3819      48.3215      50.2611      52.2007      54.1403
  0.2440       0.0266      43.8758      45.8273      47.7788      49.7303      51.6817      53.6332
  1.3600       0.0318      39.9100      41.8831      43.8563      45.8294      47.8026      49.7757
  5.4600       0.0305      23.3844      25.3521      27.3198      29.2875      31.2551      33.2228
 20.4000       0.1383     -22.8971     -20.3429     -17.7888     -15.2347     -12.6805     -10.1264
于 2013-11-06T18:07:42.497 回答