我的问题是如何使用FOR循环(或更好的推荐策略)根据用户输入打印出不同的结果。
好的,让我详细说明一下。
由于没有想到,我误解了我的解释。我道歉。
用户输入最小0f 1000 psi,然后最大3000 psi。 然后是要采取的样本量。我们将以100 psi的间隔说出样品。
示例:
代码:
#include <stdio.h>
#include <math.h>
#define N2Denstiy 1.251 //1.251 g/l mole weight
#define ATM 0.0680459639 //Pressure in PSI time ATM
#define R 0.08206 //gas constant
float calculateDensity(float pres, float temp, float moleWeight);
float calculateDensity(float pres, float temp, float moleWeight) {
return moleWeight * (pres * ATM) / (temp * R);
}
int main (int argc, const char * argv[])
{
float result;
float temp = 77.0;
float i, min_pressure, max_pressure, sample_interval;
printf("Enter minimum Pressure, maximum pressure and interval: ");
scanf("%f %f %f", &min_pressure, &max_pressure, &sample_interval);
for(i = min_pressure; i <=max_pressure; i = i + sample_interval)
// result = calculateDensity(i, temp, N2Denstiy);
// printf("%f\n",result);
printf("%f ", i);
printf("\n");
return 0;
}
这里的结果对于基本循环是正确的。但是,我想计算密度,然后显示结果。
输入最小压力,最大压力和间隔:1000 3000 50 1000.000000 1050.000000 1100.000000 1150.000000 1200.000000 1250.000000 1300.000000 1350.000000 1400.000000 1450.000000 1500.000000 1550.000000 1600.000000 1650.000000 1700.000000 1750.000000 1800.000000 1850.000000 1900.000000 1950.000000 2000.000000 2050.000000 2100.000000 2150.000000 2200.000000 2250.000000 2300.000000 2350.000000 2400.000000 2450.000000 2500.000000 2550.000000 2600.000000 2650.000000 2700.000000 2750.000000 2800.000000 2850.000000 2900.000000 2950.000000 3000.000000
答案 0 :(得分:0)
试试这样:
float i;
for(i = min_pressure; i <= max_pressure; + += increment_pressure) {
calculatePressure(i, temp, moleWeight);
}
答案 1 :(得分:0)
如果样本为numSamples
,请说明编号:
for ( int i = 0 ; i < numSamples ; i++ )
{
//get data from user - pres, temp, moleWeight
float result = calculateDensity(pres, temp, moleWeight);
printf("%f\n",result);
}
答案 2 :(得分:0)
据我所知,你想要的问题是:
int i;
for(i = 0; i < sample_interval && increment_pressure * i + min_pressure < max_pressure; i++) {
printf("%f\n", calculatePressure(increment_pressure * i + min_pressure, temp, moleWeight));
}