gpt4 book ai didi

c++ - 如何在一个函数中访问另一个函数中的变量?

转载 作者:行者123 更新时间:2023-11-28 02:56:14 25 4
gpt4 key购买 nike

<分区>

我正在尝试编写一个 C++ 代码,用户在其中输入一个日期,然后它会使用 John Conway 的世界末日算法输出该日期所在的星期几。

我正在尝试访问在函数 dayofthemonth 中声明和定义的变量 z,并在名为 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;


}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com