基本上在一定温度下的水具有不同的比重,我根据你使用的SI单位系统和英制单位系统设置为2个数组。我试图找出如何做一个函数来使用水的用户输入温度来确定其相应的比重或如何让用户重新输入一个值而不重新开始。现在如果你输入一个标准温度,这个东西就可以工作,但是我需要弄清楚如何让用户重新输入一个温度,如果他们以前有过一个非标准值的话。故障与数组和功能C++
例如: 如果您经历过程并选择Y和SI和T并得到结果“什么是水的温度”? cin < < T [1]; //也不知道为什么我把它放到一个数组中如果你输入可以说88 因为那个温度数组中没有数值计算为零,即使这个数值显然不是假设为零。有没有什么办法可以让用户在不关闭的情况下再次尝试,或者在进程中重启或以某种方式使用某个函数,或者甚至将温度调整到最接近的标准值并使用该特定的重量? ps我的编程技巧是超级基础(如果你不能告诉我的代码)
任何帮助将不胜感激,谢谢!
// this program will calculate the specific weight of water at any given temperature in either metirc or imperial and the reslutant force of the water in in tank
#include<iostream>
#include<math.h>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
// my sad attempt at trying to write a function
float match_temp_to_sw(float T[1]);
/*float match_temp_to_sw{
for(i=o; T[1]!=SI[i]; i++){
c = i+1;
x = SWSI[c];
}
}
*/
int main() {
string units_system, units_sw, units_p, units_FR, a;
string fluid;
char pressure, pressure1;
int calculate_specific_weight;
char calculate_SW, calculate_FR;
float specific_gravity, specific_weight, temperature;
float pressureA;
int i,c;
// these are count variables for the for loops to determine the specific weight of water at a specific temperature
float x, area, height, FR;
// these are the values of the temperature and specific weight of water in metric and imperial units
int SI[21] = {0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100};
float SWSI[21]= {9.81,9.81,9.81,9.81,9.79,9.78,9.77,9.75,9.73,9.71,9.69,9.67,9.65,9.62,9.59,9.56,9.53,9.50,9.47,9.44,9.40};
int I[19] = {32,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,212};
float SWI[19] = {62.4,62.4,62.4,62.4,62.3,62.1,62.0,61.9,61.7,61.5,61.4,61.2,61.0,60.8,60.6,60.4,60.1,59.8};
int T[1];
cout << fixed << showpoint;// setting the precision of the number of sigfigs
cout << setprecision(2);
cout << "Do you want to calculate the resultant force of water acting on a tanks surface?(Y or N)\n";
cin >> calculate_FR;
while(calculate_FR == 'Y') { // the outer most loop
// initialzing the string variable to zero so that way when you repeat this whole process they dont remember your previous input
a = "NULL";
units_system ="NULL";
// initializing the standard set of units that are going to be used throughout the calculations
while(units_system !="Imperial"&& units_system!="SI") {
cout<< "what units system are you using Imperial or SI\n";
cin>> units_system;
cout<< "\nTherefore, you are using the "<<units_system<< " system\n" ;
}
fluid = "water";
while (a!="SG" && a!="T") {
cout << "\nDo you know the specific gravity(SG) or the temperature(T)?\n";
cin >> a;
}
if(a == "SG") {
cout << "\nWhat is the specific gravity of the water?\n";
cin >> specific_gravity;
}
else if (a == "T") {
cout << "\nWhat is the temperature of water\n";
cin >> T[1];
}
// if you are using the SI system the units for specific wieght will be kN/m^3
if(units_system == "SI") {
units_sw = " kN/m^3";
units_p = " kPa";
specific_weight = (specific_gravity *9.81);
units_FR = "kN";
if(a == "T") {
if (T[1]== SI[0]) {
specific_weight =SWSI[0];
}
for(i=0; T[1]!=SI[i]; i++) {
c = (i+1);
specific_weight= SWSI[c];
}
if(T[1]!=SI[21]) {
cout << "You did not enter a standard value, and i dont know how to get you to reinput a proper value without restarting this program so please try again";
return 0;
}
}
}
// if you are using the imperial system the units for specific weight will be lb/ft^3
else if(units_system == "Imperial") {
units_sw = " lb/ft^3";
units_p = " lb/ft^2";
specific_weight = (specific_gravity *62.4);
units_FR = "lb";
if(a == "T") {
if(T[1]== I[0]) {
specific_weight=SWI[0];
}
for(i=0; T[1]!=I[i]; i++) { // a for loop to determine the corresponding specific wieght to the user input temperature
specific_weight= SWI[i];
}
}
}
cout <<"\nTherefore, the specific weight of "<<fluid<<" is ";
cout << specific_weight<< units_sw;
// i know this isnt in the right spot lol
//cout << "\n\nDo you want to calculate the specific weight of water at another temperature(Y or N)?\n";
//cin >> calculate_SW;
cout << "\n\nWhat is the vertical height to the centroid of the shape you are analyzing\n";
cin >> height;
cout << "What is the area of the shape you are analyzing\n";
cin >> area;
FR = (specific_weight * height * area);
cout << "\nThe resultant force acting on the tank you are analyzing is " << FR <<" " << units_FR;
cout << "\n\nDo you want to calculate the resultant force of water at another temperature(Y or N)?\n";
cin >> calculate_SW;
while (calculate_FR == 'N') {
cout << "\nYou do not want to complete anymore calculations";
return 0;
}
}
}
请修正所示代码的缩进。随机缩进使得所显示的代码几乎不可读。 –
'cin >> a;' - 给你的变量1个字母的名字使得代码更难阅读,更难理解,难以调试,并且更容易犯错误。使用更多描述性名称。 – PaulMcKenzie
对于不在数组中的值,可以*插入*。最简单的方法是假设你的数据点是通过直线段连接的;这被称为线性插值。它看起来像你有*并行数组*。使用每个项目都包含一组相关值的项目数组通常更简单,更简单。您可以使用'struct'来定义合适的项目类型。 –