读取另一个函数的变量
Reading the variables of another function
介绍一下我是怎么得到这个问题的...嗯,我会一点FORTRAN,现在正在学习C++。
当我阅读开头部分时,我正在(重新)创建一个我用 FORTRAN 编写的旧程序。
做封闭多边形地形测量的计算部分。
嗯,我正在读 "Programming: Principles and Practice Using C++" 这本书,它说在 C++ 中,我们更愿意让每个函数做最少数量的事情。现在,我有这样的东西(我忽略了所有其他功能):
int main(){
//Starting the program and reading all variables
try
{
//Program beginning
cout << "Hello. Welcome to the 'Topography Program'\n\n\n";
//Reading all the information needed
reading_is_fundamental();
//Testing the angular tolerance
ang_tolerance_test (number_of_points); } ...there's more subroutines' calls
catch (runtime_error& e)
{
cerr << "error:" << e.what()<<'\n';
return 1;
}
return 0;}
"reading_is_fundamental()"是子程序的调用。在那里,我阅读了所有需要的信息。我的问题在于第二个子例程,"ang_tolerance_test (number_of_points)".
在FORTRAN中,我滥用了太多的全局变量,现在,在这本书中,我们说我们必须避免使用全局变量。
故事结束,我有两个问题:
1 - 这个逻辑是正确的还是我误解了这本书?我应该在主函数中进行一些计算(或者至少读取输入变量,因为它们将出现在所有子程序中)还是应该只用它来调用我的子程序?
2 - 如果这确实是我们应该在 C++ 中执行的方式(将每个操作分隔在一个函数中),有人能解释一下如何获取第一个子例程中编写的变量并发送给其他子例程,比如"ang_tolerance_test" 一个?
非常感谢!
编辑
函数"reading_is_fundamental()"是我通过键盘读取所有值的地方。我知道如何通过 FORTRAN 文本文件读取值,在这种情况下,这是一个很大的优势,因为要读取的数字太多了。但是,我还不知道如何在 C++ 中执行此操作,所以请忽略我正在通过键盘读取大量变量,我正在研究它!实际上,我读取变量的方式并不重要,关键是我想要其他子例程中的那些双精度数、整数和向量...
嗯,这是reading_is_fundamental:
//This subroutine reads all the variables
void reading_is_fundamental (){
cout << "What is the linear tolerance?\n";
double lin_tolerance;
cin >> lin_tolerance;
cout << "\nHow many points did you study?\n";
int number_of_points;
cin >> number_of_points;
cout << "\nWhat's the distance between each point?\n";
vector <double> distances;
for (double dist; cin >> dist;)
distances.push_back(dist);
if (distances.size() != number_of_points) error ("The number of distances must be equal to the number of points");
cout << "\nWhat's each horizontal degree angle?\n";
vector <double> horizontal_degrees;
for (double horizontal_degree; cin >> horizontal_degree;)
horizontal_degrees.push_back(horizontal_degree);
if (horizontal_degrees.size() != number_of_points) error ("The number of angles must be equal to the number of points");
cout << "\nWhat's each horizontal minute angle?\n";
vector <double> horizontal_minutes;
for (double horizontal_minute; cin >> horizontal_minute;)
horizontal_minutes.push_back(horizontal_minute);
if (horizontal_minutes.size() != number_of_pointsint) ("The number of angles must be equal to the number of points");
cout << "\nWhat's each horizontal second angle?\n";
vector <double> horizontal_seconds;
for (double horizontal_second; cin >> horizontal_second;)
horizontal_seconds.push_back(horizontal_second);
if (horizontal_seconds.size() != number_of_points) error ("The number of angles must be equal to the number of points");
cout << "\nWhat's the first azimuth degree?\n";
double first_az_degree;
cin >> first_az_degree;
cout << "\nWhat's the first azimuth minute?\n";
double first_az_minute;
cin >> first_az_minute;
cout << "\nWhat's the first azimuth second?\n";
double first_az_second;
cin >> first_az_second;
cin >> number_of_points;}
我在写这篇文章时不知道的一件事是在这个函数中放置 void 是否正确。我的想法是我不想要一个return,但是一些变量被归档了。
第二个函数是"ang_tolerance_test()"。它还没有准备好,所以,我不会在这里复制。问题是当我在 main() 中调用这个函数时,我想把它的参数放在一个从 "reading_is_fundamental()" 发送的值中(即“number_of_points”)。
我希望这能让我的问题更清楚。
尝试 returning 来自 reading_is_fundamental
函数的值,您可以将其传递给其他函数,例如:
int number_of_points = reading_is_fundamental();
//Testing the angular tolerance
ang_tolerance_test (number_of_points);
并定义如下函数:
int reading_is_fundamental() {
....
return number;
}
定义一个 class 如:
class Record
{
private:
double lin_tolerance;
int number_of_points;
public:
int getNumberOfPoints() { return number_of_points; }
void setNumberOfPoints(int v) { number_of_points = v; }
double getLinTolerance() { return lin_tolerance; }
void setLinTolerancedouble v) { lin_tolerance = v; }
};
并且当您读取这些值时,创建 Record 的一个实例,不断填充记录中的值,并在末尾 return 该值,以便您可以将其传递给 ang_tolerance_test。与上面的 int 类似,现在您将从函数中获取 Record 对象。
介绍一下我是怎么得到这个问题的...嗯,我会一点FORTRAN,现在正在学习C++。 当我阅读开头部分时,我正在(重新)创建一个我用 FORTRAN 编写的旧程序。 做封闭多边形地形测量的计算部分。
嗯,我正在读 "Programming: Principles and Practice Using C++" 这本书,它说在 C++ 中,我们更愿意让每个函数做最少数量的事情。现在,我有这样的东西(我忽略了所有其他功能):
int main(){
//Starting the program and reading all variables
try
{
//Program beginning
cout << "Hello. Welcome to the 'Topography Program'\n\n\n";
//Reading all the information needed
reading_is_fundamental();
//Testing the angular tolerance
ang_tolerance_test (number_of_points); } ...there's more subroutines' calls
catch (runtime_error& e)
{
cerr << "error:" << e.what()<<'\n';
return 1;
}
return 0;}
"reading_is_fundamental()"是子程序的调用。在那里,我阅读了所有需要的信息。我的问题在于第二个子例程,"ang_tolerance_test (number_of_points)".
在FORTRAN中,我滥用了太多的全局变量,现在,在这本书中,我们说我们必须避免使用全局变量。
故事结束,我有两个问题:
1 - 这个逻辑是正确的还是我误解了这本书?我应该在主函数中进行一些计算(或者至少读取输入变量,因为它们将出现在所有子程序中)还是应该只用它来调用我的子程序?
2 - 如果这确实是我们应该在 C++ 中执行的方式(将每个操作分隔在一个函数中),有人能解释一下如何获取第一个子例程中编写的变量并发送给其他子例程,比如"ang_tolerance_test" 一个?
非常感谢!
编辑
函数"reading_is_fundamental()"是我通过键盘读取所有值的地方。我知道如何通过 FORTRAN 文本文件读取值,在这种情况下,这是一个很大的优势,因为要读取的数字太多了。但是,我还不知道如何在 C++ 中执行此操作,所以请忽略我正在通过键盘读取大量变量,我正在研究它!实际上,我读取变量的方式并不重要,关键是我想要其他子例程中的那些双精度数、整数和向量...
嗯,这是reading_is_fundamental:
//This subroutine reads all the variables
void reading_is_fundamental (){
cout << "What is the linear tolerance?\n";
double lin_tolerance;
cin >> lin_tolerance;
cout << "\nHow many points did you study?\n";
int number_of_points;
cin >> number_of_points;
cout << "\nWhat's the distance between each point?\n";
vector <double> distances;
for (double dist; cin >> dist;)
distances.push_back(dist);
if (distances.size() != number_of_points) error ("The number of distances must be equal to the number of points");
cout << "\nWhat's each horizontal degree angle?\n";
vector <double> horizontal_degrees;
for (double horizontal_degree; cin >> horizontal_degree;)
horizontal_degrees.push_back(horizontal_degree);
if (horizontal_degrees.size() != number_of_points) error ("The number of angles must be equal to the number of points");
cout << "\nWhat's each horizontal minute angle?\n";
vector <double> horizontal_minutes;
for (double horizontal_minute; cin >> horizontal_minute;)
horizontal_minutes.push_back(horizontal_minute);
if (horizontal_minutes.size() != number_of_pointsint) ("The number of angles must be equal to the number of points");
cout << "\nWhat's each horizontal second angle?\n";
vector <double> horizontal_seconds;
for (double horizontal_second; cin >> horizontal_second;)
horizontal_seconds.push_back(horizontal_second);
if (horizontal_seconds.size() != number_of_points) error ("The number of angles must be equal to the number of points");
cout << "\nWhat's the first azimuth degree?\n";
double first_az_degree;
cin >> first_az_degree;
cout << "\nWhat's the first azimuth minute?\n";
double first_az_minute;
cin >> first_az_minute;
cout << "\nWhat's the first azimuth second?\n";
double first_az_second;
cin >> first_az_second;
cin >> number_of_points;}
我在写这篇文章时不知道的一件事是在这个函数中放置 void 是否正确。我的想法是我不想要一个return,但是一些变量被归档了。
第二个函数是"ang_tolerance_test()"。它还没有准备好,所以,我不会在这里复制。问题是当我在 main() 中调用这个函数时,我想把它的参数放在一个从 "reading_is_fundamental()" 发送的值中(即“number_of_points”)。
我希望这能让我的问题更清楚。
尝试 returning 来自 reading_is_fundamental
函数的值,您可以将其传递给其他函数,例如:
int number_of_points = reading_is_fundamental();
//Testing the angular tolerance
ang_tolerance_test (number_of_points);
并定义如下函数:
int reading_is_fundamental() {
....
return number;
}
定义一个 class 如:
class Record
{
private:
double lin_tolerance;
int number_of_points;
public:
int getNumberOfPoints() { return number_of_points; }
void setNumberOfPoints(int v) { number_of_points = v; }
double getLinTolerance() { return lin_tolerance; }
void setLinTolerancedouble v) { lin_tolerance = v; }
};
并且当您读取这些值时,创建 Record 的一个实例,不断填充记录中的值,并在末尾 return 该值,以便您可以将其传递给 ang_tolerance_test。与上面的 int 类似,现在您将从函数中获取 Record 对象。