我在C中写了四段代码来计算水的一些物理特性(比热,密度等)作为温度的函数。他们编译和工作正常,但我想把它们放在一个文件中,所以只有一个exe。当我按1时,我希望代码返回特定的热值,当我按2返回密度值时依此类推。
我知道我必须为每个属性定义4个函数,而且我认为这段代码是必要的:
int choice;
printf("Press 1 to compute specific heat.\n");
printf("Press 2 to compute density.\n");
printf("Press 3 to compute thermal conductivity.\n");
printf("Press 4 to compute viscosity.\n");
printf("Press 5 to exit.\n");
printf("Enter your choice\n");
scanf("%d",&choice);
if (choice == 1)
{
execute the specific heat function;
}
if (choice == 2)
{
execute the density function;
}
if (choice == 3)
{
execute the thermal conductivity function;
}
if (choice == 4)
{
execute the viscosity function;
}
if (choice == 5)
exit(0);
以下是我要合并的代码片段。我只附加了两个,因为如果我能用2做,那么我可以用4做。
比热部分:
#include <stdio.h>
#include <math.h>
int main()
{
float T, cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8; /*variables */
printf( "Enter temperature in Kelvin (range 280-900 K)\n" ); /* Asks for temperature */
scanf( "%f", &T );
cp1=1.67662377469456E-11*pow(T,6.)-4.1197828092268E-08*pow(T,5.)+0.0000418869586791152*pow(T,4.)-0.0225003443462658*pow(T,3.)+6.72987826198519*pow(T,2.)-1062.18721205927*T+73195.8387080729;
cp2=0.00525246395569212*pow(T,4.)-12.9485474717992*pow(T,3.)+11970.600895997*pow(T,2.)-4918448.82178862*T+757825839.152518;
cp3=6.63085194968153*pow(T,4.)-17170.4000395659*pow(T,3.)+16673480.6519115*pow(T,2.)-7196031423.69135*T+1164647463146.08;
cp4=-68.0819524480029*pow(T,5.)+222554.556013276*pow(T,4.)-291004129.724188*pow(T,3.)+190252342532.153*pow(T,2.)-62191220562433.3*T+8131793672657510;
cp5=9974.74747475981*pow(T,3.)-19674382.9004572*pow(T,2.)+12935329616.4157*T-2834856258114.32;
cp6=-4.14962557364299E-07*pow(T,6.)+0.000462300031790556*pow(T,5.)-0.0310330512189478*pow(T,4.)+62.8598088326175*pow(T,3.)-189691.032172151*pow(T,2.)+77129922.3433911*T-4056849697.67083;
cp7=1.47773054803578E-08*pow(T,6.)-0.0000239186385308144*pow(T,5.)+0.00563616606939476*pow(T,4.)+1.50391266948934*pow(T,3.)+9118.7111011683*pow(T,2.)-8897122.07353999*T+2172208386.41784;
cp8=-4.6626284146362E-08*pow(T,5.)+0.000192659539297423*pow(T,4.)-0.318404373982708*pow(T,3.)+263.141418952148*pow(T,2.)-108775.736711323*T+18001673.992402;
if (T>=280. && T<=599.9f) { /* 1st temp range*/
printf ("1st range Cp= %.6f J/kgK",cp1); /* returns cp1 */
}
else if (T>599.9 && T<=646.8) { /* 2nd temp range */
printf("2nd range Cp= %.6f J/kgK",cp2); /* returns cp2 */
}
else if (T>646.8 && T<=651.2f) { /* 3rd temp range*/
printf("3rd range Cp= %.6f J/kgK",cp3); /* returns cp3 */
}
else if (T>651.2 && T<=655.8) { /* 4th temp range */
printf("4th range Cp= %.6f J/kgK",cp4); /* returns cp4 */
}
else if (T>655.8 && T<=656.6) { /* 5th temp range */
printf("5th range Cp= %.6f J/kgK",cp5); /* returns cp5 */
}
else if (T>656.6 && T<=662.) { /* 6th temp range */
printf("6th range Cp= %.6f J/kgK",cp6); /* returns cp6 */
}
else if (T>662. && T<=700.) { /* 7th temp range */
printf("7th range Cp= %.6f J/kgK",cp7); /* returns cp7 */
}
else if (T>700. && T<=900.) { /* 8th temp range */
printf("8th range Cp= %.6f J/kgK",cp8); /* returns cp8 */
}
else {
printf("The value entered is NOT in the 280-900 K range. Default Cp= 4180 J/kgK"); /* Executed if no other statement is */
}
printf("\nPress any key to exit.");
getch();
return 0;
}
密度部分:
#include <stdio.h>
#include <math.h>
int main()
{
float T, rho1, rho2, rho3, rho4, rho5; /*variables */
printf( "Enter temperature in Kelvin (range 280-900 K)\n" ); /* Asks for temperature */
scanf( "%f", &T );
rho1=-0.00000266869827*pow(T,3.)+0.001092981832*pow(T,2.)-0.353045781*T+1087.047262;
rho2=-0.00003541921142*pow(T,4.)+0.08730402305*pow(T,3.)-80.71356495*pow(T,2.)+33168.28128*T-5110738.52;
rho3=-0.0133134195680782*pow(T,5.)+43.6908659112031*pow(T,4.)-57351.2250577165*pow(T,3.)+37640535.2854736*pow(T,2.)-12351791332.418*T+1621269348006.22;
rho4=-3.99858316063595E-06*pow(T,5.)+0.0136859140134915*pow(T,4.)-18.7366565748224*pow(T,3.)+12825.4332022085*pow(T,2.)-4389497.59259435*T+600913288.082835;
rho5=-0.000006557464349*pow(T,3.)+0.0168728672*pow(T,2.)-14.60996074*T+4327.503734;
if (T>=280. && T<=599.9f) { /* 1st temp range*/
printf ("1st range Rho= %.6f kg/m3",rho1); /* returns rho1 */
}
else if (T>599.9 && T<=649.9f) { /* 2nd temp range */
printf("2nd range Rho= %.6f kg/m3",rho2); /* returns rho2 */
}
else if (T>649.9 && T<=659.8) { /* 3rd temp range*/
printf("3rd range Rho= %.6f kg/m3",rho3); /* returns rho3 */
}
else if (T>659.8 && T<=700.) { /* 4th temp range */
printf("4th range Rho= %.6f kg/m3",rho4); /* returns rho4 */
}
else if (T>700. && T<=900.) { /* 5th temp range */
printf("5th range Rho= %.6f kg/m3",rho5); /* returns rho5 */
}
else {
printf("The value entered is NOT in the 280-900 K range. Default Rho= 1000 kg/m3"); /* Executed if no other statement is */
}
printf("\nPress any key to exit.");
getch();
return 0;
}
问题是如何制作这四个功能并将它们集成到一起? 这可能是一个微不足道的问题,但我没有编程经验。
答案 0 :(得分:2)
将源代码放在函数中。
#include <stdio.h>
#include <math.h>
// The following line defines a function
void specific_heat(){
// Copy contents of the first main() here
}
void density(){
// Copy contents of the second main() here
}
int main(){
// Ask for choice here
if (choice == 1)
{
// The following line executes a function
specific_heat();
}
if (choice == 2)
{
density();
}
}