2014-02-20 56 views
0

我正在嘗試編寫一個C++代碼,用戶輸入一個日期,並輸出使用John Conway的Doomsday算法登陸的日期。如何在另一個函數中訪問一個函數中的變量?

我想訪問變量z是在函數dayofthemonth中聲明和定義的,並在稱爲dayoftheweek的函數中使用它。請記住,我對C++非常陌生,所以如果你能儘可能簡單地回答,那將會有很大的幫助。下面的代碼:

#include <iostream> 
#include <string> 
using namespace std; 



int dayofthemonth (int year, int month){   
    int z; 

    if ((year % 400 == 0 || year % 100 !=0) && (year % 4 == 0)){ //reference day of the month for leap years 
    cout << year << " is a leap year." << endl; 

    if (month == 1){ 
     cout << "The doomsday for month " << month << " is 4." << endl; 
     z = 4; 
    } 
    if (month == 2){ 
     cout << "The doomsday for month " << month << " is 1." << endl; 
     z = 1; 
    } 
    if (month == 3){ 
     cout << "The doomsday for month " << month << " is 0." << endl; 
     z = 0; 
    } 
    if (month == 4){ 
     cout << "The doomsday for month " << month << " is 4." << endl; 
     z = 4; 
    } 
    if (month == 5){ 
     cout << "The doomsday for month " << month << " is 9." << endl; 
     z = 9; 
    } 
    if (month == 6){ 
     cout << "The doomsday for month " << month << " is 6." << endl; 
     z = 6; 
    } 
    if (month == 7){ 
     cout << "The doomsday for month " << month << " is 11." << endl; 
     z = 11; 
    } 
    if (month == 8){ 
     cout << "The doomsday for month " << month << " is 8." << endl; 
     z = 8; 
    } 
    if (month == 9){ 
     cout << "The doomsday for month " << month << " is 5." << endl; 
     z = 5; 
    } 
    if (month == 10){ 
     cout << "The doomsday for month " << month << " is 10." << endl; 
     z = 10; 
    } 
    if (month == 11){ 
     cout << "The doomsday for month " << month << " is 7." << endl; 
     z = 7; 
    } 
    if (month == 12){ 
     cout << "The doomsday for month " << month << " is 12." << endl; 
     z = 12; 
    }  

} 

else{              //reference day of the month for non-leap years 
    cout << year << " is not a leap year." << endl; 

    if (month == 1){ 
     cout << "The doomsday for month " << month << " is 3." << endl; 
     z = 3; 
    } 
    if (month == 2){ 
     cout << "The doomsday for month " << month << " is 0." << endl; 
     z = 0; 
    } 
    if (month == 3){ 
     cout << "The doomsday for month " << month << " is 0." << endl; 
     z = 0; 
    } 
    if (month == 4){ 
     cout << "The doomsday for month " << month << " is 4." << endl; 
     z = 4; 
    } 
    if (month == 5){ 
     cout << "The doomsday for month " << month << " is 9." << endl; 
     z = 9; 
    } 
    if (month == 6){ 
     cout << "The doomsday for month " << month << " is 6." << endl; 
     z = 6; 
    } 
    if (month == 7){ 
     cout << "The doomsday for month " << month << " is 11." << endl; 
     z = 11; 
    } 
    if (month == 8){ 
     cout << "The doomsday for month " << month << " is 8." << endl; 
     z = 8; 
    } 
    if (month == 9){ 
     cout << "The doomsday for month " << month << " is 5." << endl; 
     z = 5; 
    } 
    if (month == 10){ 
     cout << "The doomsday for month " << month << " is 10." << endl; 
     z = 10; 
    } 
    if (month == 11){ 
     cout << "The doomsday for month " << month << " is 7." << endl; 
     z = 7; 
    } 
    if (month == 12){ 
     cout << "The doomsday for month " << month << " is 12." << endl; 
     z = 12; 
    }  

}  



} 

int doomsday (int year){   //reference day of the week 
    int a, b, c , d, e, x; 


    a = ((year/100) % 4); 
    b = (year % 100); 
    c = (b/12); 
    d = (b % 12); 
    e = (d/4); 

    x = ((c + d + e + (5*a) + 2) % 7); 

    if (x == 0){ 
    cout << "The doomsday for " << year << " is Sunday." << endl; 
    } 
    else if (x == 1){ 
    cout << "The doomsday for " << year << " is Monday." << endl; 
    } 
    else if (x == 2){ 
    cout << "The doomsday for " << year << " is Tuesday." << endl; 
    } 
    else if (x == 3){ 
    cout << "The doomsday for " << year << " is Wednesday." << endl; 
    } 
    else if (x == 4){ 
    cout << "The doomsday for " << year << " is Thursday." << endl; 
    } 
    else if (x == 5){ 
    cout << "The doomsday for " << year << " is Friday." << endl; 
    } 
    else if (x == 6){ 
    cout << "The doomsday for " << year << " is Saturday." << endl; 
    } 



} 

void dayoftheweek(int month, int day, int year}(

int r; 

cout << "You want to find out what day of the week " << month << "/" << day << "/" << year << " lies on." << endl; 

    doomsday(year); 


    r = (day - z) + 14;   //offset between the given day and the result of dayofthemonth function. 
    cout << r << endl; 


} 


int main(){ 

int year, month, day;  


cout << "Enter the year." << endl; 
cin >> year; 

cout << "Enter the month using numbers 1-12." << endl; 
cin >> month; 

cout << "Enter the day." << endl; 
cin >> day; 

dayoftheweek(month, day, year); 



system ("PAUSE"); 
return 0; 


} 
+0

int z;超出範圍......你不能。 你必須改變執行:) – 2014-02-20 20:59:09

+0

爲什麼你不只是'返回z'? –

回答

1

你的函數有返回類型int但實際上並不返回任何值。返回z,然後將它作爲參數傳遞給下一個函數。

+0

非常感謝!這有幫助! – Christie

1

另一種觀察,這個函數int dayofthemonth (int year, int month)沒有調用者。

可能是你應該改變的,

r = (day - z) + 14;

r = (day - dayofthemonth(year, month)) + 14; //提供這個函數返回z

+0

非常感謝!這工作! – Christie

+0

@ user3308118不客氣。 –

0

如果您想在代碼中的所有範圍中使用一個變量,可以在主函數之前和「#include < ...>」和「using namespace ...」之後聲明它。 但在使用這種方式時要小心,因爲如果在代碼中使用該變量2次或更多時間並且不使其成爲0,您將看到錯誤的答案。因爲如果你想在linux下編譯你的代碼「system(」pause「)」會給你一個錯誤。「我的代碼在系統(」暫停「)中使用了」cin.get()「。

#include <iostream> 
#include <string> 
using namespace std; 

int z; 

void dayofthemonth (int year, int month) 
{ 

    z = 0; 
    if ((year % 400 == 0 || year % 100 !=0) && (year % 4 == 0)){ //reference day of the month for leap years 
    cout << year << " is a leap year." << endl; 

    if (month == 1){ 
     cout << "The doomsday for month " << month << " is 4." << endl; 
     z = 4; 
    } 
    if (month == 2){ 
     cout << "The doomsday for month " << month << " is 1." << endl; 
     z = 1; 
    } 
    if (month == 3){ 
     cout << "The doomsday for month " << month << " is 0." << endl; 
     z = 0; 
    } 
    if (month == 4){ 
     cout << "The doomsday for month " << month << " is 4." << endl; 
     z = 4; 
    } 
    if (month == 5){ 
     cout << "The doomsday for month " << month << " is 9." << endl; 
     z = 9; 
    } 
    if (month == 6){ 
     cout << "The doomsday for month " << month << " is 6." << endl; 
     z = 6; 
    } 
    if (month == 7){ 
     cout << "The doomsday for month " << month << " is 11." << endl; 
     z = 11; 
    } 
    if (month == 8){ 
     cout << "The doomsday for month " << month << " is 8." << endl; 
     z = 8; 
    } 
     if (month == 9){ 
     cout << "The doomsday for month " << month << " is 5." << endl; 
     z = 5; 
    } 
    if (month == 10){ 
     cout << "The doomsday for month " << month << " is 10." << endl; 
     z = 10; 
    } 
    if (month == 11){ 
     cout << "The doomsday for month " << month << " is 7." << endl; 
     z = 7; 
    } 
    if (month == 12){ 
     cout << "The doomsday for month " << month << " is 12." << endl; 
     z = 12; 
    } 

} 

else{              //reference day of the month for non-leap years 
    cout << year << " is not a leap year." << endl; 

    if (month == 1){ 
     cout << "The doomsday for month " << month << " is 3." << endl; 
     z = 3; 
    } 
    if(month == 2){ 
     cout << "The doomsday for month " << month << " is 0." << endl; 
    z = 0; 
    } 
    if (month == 3){ 
     cout << "The doomsday for month " << month << " is 0." << endl; 
     z = 0; 
    } 
    if (month == 4){ 
     cout << "The doomsday for month " << month << " is 4." << endl; 
     z = 4; 
    } 
    if (month == 5){ 
     cout << "The doomsday for month " << month << " is 9." << endl; 
     z = 9; 
    } 
    if (month == 6){ 
     cout << "The doomsday for month " << month << " is 6." << endl; 
     z = 6; 
    } 
    if (month == 7){ 
     cout << "The doomsday for month " << month << " is 11." << endl; 
     z = 11; 
    } 
    if (month == 8){ 
     cout << "The doomsday for month " << month << " is 8." << endl; 
     z = 8; 
    } 
    if (month == 9){ 
     cout << "The doomsday for month " << month << " is 5." << endl; 
     z = 5; 
    } 
    if (month == 10){ 
     cout << "The doomsday for month " << month << " is 10." << endl; 
     z = 10; 
    } 
    if (month == 11){ 
     cout << "The doomsday for month " << month << " is 7." << endl; 
     z = 7; 
    } 
    if (month == 12){ 
     cout << "The doomsday for month " << month << " is 12." << endl; 
     z = 12; 
    } 

} 



} 

void doomsday (int year) 
{   //reference day of the week 
    int a, b, c , d, e, x; 


    a = ((year/100) % 4); 
    b = (year % 100); 
    c = (b/12); 
    d = (b % 12); 
    e = (d/4); 

    x = ((c + d + e + (5*a) + 2) % 7); 

    if (x == 0) 
    cout << "The doomsday for " << year << " is Sunday." << endl; 
    else if (x == 1) 
    cout << "The doomsday for " << year << " is Monday." << endl; 
    else if (x == 2) 
    cout << "The doomsday for " << year << " is Tuesday." << endl; 
    else if (x == 3) 
    cout << "The doomsday for " << year << " is Wednesday." << endl; 
    else if (x == 4) 
    cout << "The doomsday for " << year << " is Thursday." << endl; 
    else if (x == 5) 
    cout << "The doomsday for " << year << " is Friday." << endl; 
    else if (x == 6) 
    cout << "The doomsday for " << year << " is Saturday." << endl; 
} 

void dayoftheweek(int month, int day, int year) 
{ 
int r; 
cout << "You want to find out what day of the week " << month << "/" << day << "/" << year << " lies on." << endl; 
doomsday(year); 
r = (day - z) + 14;   //offset between the given day and the result of  dayofthemonth function. 
cout << r << endl; 
} 


int main(){ 

int year, month, day; 


cout << "Enter the year." << endl; 
cin >> year; 

cout << "Enter the month using numbers 1-12." << endl; 
cin >> month; 

cout << "Enter the day." << endl; 
cin >> day; 

dayoftheweek(month, day, year); 


cin.get(); 
return 0; 


} 
相關問題