// 案例5 阿猫阿狗的爱情游戏2
#include<iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
using namespace std;
class Boy
{
public:
bool House; //有房;
int Age; //年龄;
char Name[20];
public:
Boy(char *name);
bool Gift(char gift);
int AnnualSalary(int salary);
};
Boy::Boy(char *name)
{
srand((unsigned)time(NULL)); // 为rand()函数提供一个随机数种子
if(rand() == 100) House=true;
strcpy(Name,name);
}
bool Boy::Gift(char gift)
{
if(gift=='Y'||gift=='y')
return true;
else
return false;
}
int Boy::AnnualSalary(int salary)
{
cout<<Name<<"日夜操劳,两眼发黑,为的是早日赚到老婆本。"<<endl;
salary=salary*365;
return salary;
}
class Girl
{
public:
int Feeling; // 情感指数
bool Willing; // 是否愿意等
int Birthday; // 生日
char Name[20];
public:
Girl(char *name);
void Love(char *name);
void Marry(char *name);
};
Girl::Girl(char *name)
{
strcpy(Name,name);
}
void Girl::Love(char *name)
{
cout<<"\n 新的故事开始了,"<<Name<<"与"<<name<<"进入相恋阶段......"<<endl;
}
void Girl::Marry(char *name)
{
cout<<"\n 大团圆结局,"<<Name<<"同意嫁给"<<name<<",从此两人过上了幸福生活......"<<endl;
}
/*
日期转换
参数
int day:距元月一日的天数
返回值:
表示日期中的月日
*/
int convertDate(int day)
{
int days[]=
{
31,28,31,30,31,30,31,30,30,31,30,31
};
int i=0;
for (; i<12; i++)
{
if (day>days[i])
{
day=day-days[i];
}
else
{
break;
}
}
return ((i+1)*100+day);
}
int main( )
{
Boy theBoy("dog");
Girl theGirl("cat");
theBoy.Age=24;
theGirl.Feeling=1;
theGirl.Willing=true;
theGirl.Birthday=1001;
int year=1;
char gift='N';
int money=0;
int salary=0;
theGirl.Love(theBoy.Name ); //故事开始
if(theBoy.House == true)
{
theGirl.Marry(theBoy.Name);
return 0;
}
while(theGirl.Willing==true)
{
cout<<"\n*****第"<<year<<"年*****\n";
for( int day=1; day<=365; day++)
{
if( convertDate(day) == 214) // if ( 节日转换(day) == 情人节)
{
cout<<"情人节到了,是否给女朋友送礼物呢?(Y/N)";
cin>>gift;
if( theBoy.Gift(gift))
theGirl.Feeling++;
else
theGirl.Feeling--;
}
if( convertDate(day) == theGirl.Birthday) //if ( 日期转换(day) == theGirl.Birthday)
{
cout<<"生日到了,是否给女朋友送礼物呢?(Y/N)";
cin>>gift;
if( theBoy.Gift(gift))
theGirl.Feeling++;
else
theGirl.Feeling--;
}
}
cout<<"请输入今年的日工资:";
cin>>salary;
money=money+theBoy.AnnualSalary(salary);
cout<<"年终小结:\t"<<"男孩年龄:"<<theBoy.Age<<", 男孩存款:"<<money<<", 女孩情感指数:"<<theGirl.Feeling;
if(money>1000000)
{
theBoy.House=true;
break;
}
year++;
theBoy.Age++;
theGirl.Feeling--;
if(theBoy.Age>30 || theGirl.Feeling<0) theGirl.Willing=false;
}
if(theBoy.House==true)
theGirl.Marry(theBoy.Name);
else
{
Boy anotherBoy("Mouse");
theGirl.Love(anotherBoy.Name);
}
return 0;
}
阿猫阿狗的爱情游戏2
最新推荐文章于 2018-11-10 23:23:30 发布