根据生日算星座 -- 标准答案
代码
1 #include <iostream>
2 using namespace std;
3
4 void tellConstellation()
5 {
6 // the days of each month
7 int dayOfMonth[12] =
8 { 31, 28, 31, 30, 31, 30,
9 31, 31, 30, 31, 30, 31 };
10
11 // names of constellation
12 char * names[ 12 ] =
13 { "白羊座", "金牛座", "双子座",
14 "巨蟹座", "狮子座", "处女座",
15 "天秤座", "天蝎座", "射手座",
16 "摩羯座", "水瓶座", "双鱼座" };
17
18 // index of each constellation.
19 // 0 to 摩羯座, 11 to 射手座
20 // int index;
21
22 int month, day; // birthday to be inputted;
23
24 while ( 1 )
25 {
26 cout << "Please input your birthday (month/day): ";
27 cin >> month >> day;
28
29 if (month < 1 || month > 12)
30 {
31 cout << " Error month, please input again\n";
32 continue;
33 }
34 if (day < 1 || day > dayOfMonth[month - 1])
35 {
36 cout << " Error day, please input again\n";
37 continue;
38 }
39 // 思考题: 下面这段繁琐的判断,可以简化为1个if-else语句。你想到好办法了吗?
40 if ((month == 3 && day >= 21) || (month == 4 && day <= 20))
41 index = 0;
42 else if ((month == 4 && day >= 21) || (month == 5 && day <= 20))
43 index = 1;
44 else if ((month == 5 && day >= 21) || (month == 6 && day <= 21))
45 index = 2;
46 else if ((month == 6 && day >= 22) || (month == 7 && day <= 22))
47 index = 3;
48 else if ((month == 7 && day >= 23) || (month == 8 && day <= 22))
49 index = 4;
50 else if ((month == 8 && day >= 23) || (month == 9 && day <= 22))
51 index = 5;
52 else if ((month == 9 && day >= 23) || (month == 10 && day <= 22))
53 index = 6;
54 else if ((month == 10 && day >= 23) || (month == 11 && day <= 21))
55 index = 7;
56 else if ((month == 11 && day >= 22) || (month == 12 && day <= 21))
57 index = 8;
58 else if ((month == 12 && day >= 22) || (month == 1 && day <= 19))
59 index = 9;
60 else if ((month == 1 && day >= 20) || (month == 2 && day <= 19))
61 index = 10;
62 else if ((month == 2 && day >= 20) || (month == 3 && day <= 20))
63 index = 11;
64
65 cout << "你的星座是:" << names[ index ] << endl;
66 }
67 }
2 using namespace std;
3
4 void tellConstellation()
5 {
6 // the days of each month
7 int dayOfMonth[12] =
8 { 31, 28, 31, 30, 31, 30,
9 31, 31, 30, 31, 30, 31 };
10
11 // names of constellation
12 char * names[ 12 ] =
13 { "白羊座", "金牛座", "双子座",
14 "巨蟹座", "狮子座", "处女座",
15 "天秤座", "天蝎座", "射手座",
16 "摩羯座", "水瓶座", "双鱼座" };
17
18 // index of each constellation.
19 // 0 to 摩羯座, 11 to 射手座
20 // int index;
21
22 int month, day; // birthday to be inputted;
23
24 while ( 1 )
25 {
26 cout << "Please input your birthday (month/day): ";
27 cin >> month >> day;
28
29 if (month < 1 || month > 12)
30 {
31 cout << " Error month, please input again\n";
32 continue;
33 }
34 if (day < 1 || day > dayOfMonth[month - 1])
35 {
36 cout << " Error day, please input again\n";
37 continue;
38 }
39 // 思考题: 下面这段繁琐的判断,可以简化为1个if-else语句。你想到好办法了吗?
40 if ((month == 3 && day >= 21) || (month == 4 && day <= 20))
41 index = 0;
42 else if ((month == 4 && day >= 21) || (month == 5 && day <= 20))
43 index = 1;
44 else if ((month == 5 && day >= 21) || (month == 6 && day <= 21))
45 index = 2;
46 else if ((month == 6 && day >= 22) || (month == 7 && day <= 22))
47 index = 3;
48 else if ((month == 7 && day >= 23) || (month == 8 && day <= 22))
49 index = 4;
50 else if ((month == 8 && day >= 23) || (month == 9 && day <= 22))
51 index = 5;
52 else if ((month == 9 && day >= 23) || (month == 10 && day <= 22))
53 index = 6;
54 else if ((month == 10 && day >= 23) || (month == 11 && day <= 21))
55 index = 7;
56 else if ((month == 11 && day >= 22) || (month == 12 && day <= 21))
57 index = 8;
58 else if ((month == 12 && day >= 22) || (month == 1 && day <= 19))
59 index = 9;
60 else if ((month == 1 && day >= 20) || (month == 2 && day <= 19))
61 index = 10;
62 else if ((month == 2 && day >= 20) || (month == 3 && day <= 20))
63 index = 11;
64
65 cout << "你的星座是:" << names[ index ] << endl;
66 }
67 }
Hint: the rules above can be represented by an array of integers.