记录变量的值
recording the value of a variable
我想知道如何记录一个变量的前一个值。这个问题的一个例子是下面的代码:
int distanceFormula(int x1, int x2, int y1, int y2){
int distance;
distance = sqrt(pow((x1-x2), 2) + pow((y1-y2), 2));
return distance;
}
int main(){
for(int i = 0; i < 2; i++){
int x = rand() % 180;
int y = rand() % 180;
int x2 = rand() % 180;
int y2 = rand() % 180;
int distance = distanceFormula(x, x2, y, y2);
int priordistance = distanceFormula(x, x2, y, y2);
if(priordistance != distance){
cout<<"Yes! It worked!"<<endl;
}
}
return 0;
}
代码本身不会返回"是的!它起作用了!"如何记录以前的距离值,然后将以前的值与当前值进行比较?
编辑:感谢您的快速评论!真的很感激。
为了澄清实际问题,上面的代码只是一个快速模板/示例。由于距离值将在第二次循环时发生变化,如何记录第一个距离值并将该值设置为先验距离,然后将当前距离值与先验距离(其值实际上只是以前的距离值)进行比较。
只需在变量中重做上一个值。
#include <cmath>
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
int distanceFormula(int x1, int x2, int y1, int y2){
int distance;
distance = sqrt(pow((x1-x2), 2) + pow((y1-y2), 2));
return distance;
}
int main(){
int priordistance = 0; // a variable used to record the value
for(int i = 0; i < 2; i++){
int x = rand() % 180;
int y = rand() % 180;
int x2 = rand() % 180;
int y2 = rand() % 180;
int distance = distanceFormula(x, x2, y, y2);
if(i > 0 && priordistance != distance){ // use i > 0 to avoid compareing with meaningless value
cout<<"Yes! It worked!"<<endl;
}
priordistance = distance; // record the value
}
return 0;
}
有几种方法可以做到这一点。。。您可以在for循环之外定义优先级距离,并确保只重新定义一次(因为您循环了两次)。
然而,这不是我要做的,我会简单地创建一个整数数组,其中包含n个"距离",供您通过索引I 参考
int[] or int *
您可以做各种事情。您需要在整个for
循环中保持该值。请注意,在您已经获得一个先前的值之前,进行比较是没有意义的。
int main(){
int priordistance = 0; //lifetime outside for loop
for(int i = 0; i < 2; i++){
int x = rand() % 180;
int y = rand() % 180;
int x2 = rand() % 180;
int y2 = rand() % 180;
int distance = distanceFormula(x, x2, y, y2);
if(i && priordistance != distance){
//^---- have we got a prior value yet?
cout<<"Yes! It worked!"<<endl;
}
priordistance = distance;//remember for next time
}
return 0;
}
你的意思是这样的吗?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
int distanceFormula( int x1, int x2, int y1, int y2 )
{
int distance;
distance = std::sqrt( std::pow( x1 - x2, 2 ) + std::pow( y1 - y2, 2 ) );
return distance;
}
int main()
{
const size_t N = 2;
int distance[N];
std::srand( ( unsigned int )std::time( nullptr ) );
for ( size_t i = 0; i < N; i++ )
{
int x = rand() % 180;
int y = rand() % 180;
int x2 = rand() % 180;
int y2 = rand() % 180;
distance[i] = distanceFormula( x, x2, y, y2 );
}
if ( distance[0] != distance[1] ) std::cout << "Yes! It worked!" << std::endl;
return 0;
}
程序输出为
Yes! It worked!
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- GDB / C++ - 根据循环条件将多个变量记录到输出文件中
- 人造人.NDK.如何记录全局变量的调用析构函数
- 为变量和激活记录保留空间
- 在Android原生ndk中记录变量的值
- 将记录集中的变量(具有货币值)转换为格式正确的字符串
- 使用全局变量和函子实现日志记录
- Netcdf C++|如何为单个变量编写记录
- 记录变量的值
- 如何使用 Doxygen 记录C++函数中的局部变量