2016-07-27 73 views
0

我想知道如何記錄變化的變量的以前的值。此問題的一個示例如下:記錄變量的值

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; 
} 

該代碼本身不會返回「是!它工作!」如何記錄先前的距離值,然後將之前的值與當前值進行比較?

編輯: 感謝您的快速評論!真的很感激它。

要澄清實際問題,上面的代碼只是一個快速的模板/示例。由於距離值會在第二個迴路周圍發生變化,因此如何記錄距離的第一個值並將該值設置爲priordistance,然後將距離的當前值與priordistance進行比較(其值實際上只是前一個距離值) 。

+0

定義'for'環路範圍以外的'priordistance'變量? –

+0

您使用相同的參數調用'distanceFormula'函數兩次,所以它會得到相同的結果。爲什麼會輸出'是的!它的工作!'? – Polb

回答

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; 
} 
0

有幾種方法可以做到這一點...您可以在for循環之外定義priordistance,並且一定要重新定義它1次(因爲循環了兩次)。

然而,這不是我會做什麼,我會簡單地創建持有「距離」的n個供大家參考由索引i

int[] or int * 
0

你可以做各種事情整數數組。您需要保留整個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; 
} 
+0

我很高興見到你:) –

+0

而且:-) – doctorlove

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!