السؤال الرابع
CPP / C++ / C Code:
كود PHP:
class Date()
{
private:
int day,month,year;//the current date.
public:
Date(int d,int m,int y)
{
day=d;
month=m;
year=y;//the current day..
}
void setDate()
{
int d_birth,m_birth,y_birth;//date of birth..
cout<<"enter ur birth day:";
cin>>d_birth;
if (d_birth<0 || d_birth>30)
cout<<"enter ur birth day agin please:";
cout<<"enter ur birth month:";
cin>>m_birth;
if (m_birth<0 || d_birth>12)
cout<<"enter ur birth month agin please:";
cout<<"enter ur birth year:";
cin>>y_birth;
}
string getDate()
{
// i dont know how to convert from int to string :(
}
};
الحل الاخر :
كود PHP:
#include <iostream>
#include <Cstring>
using namespace std;
class Date
{
private:
int day,month,year;//the current date.
public:
int d_birth,m_birth,y_birth;//date of birth..
Date(int d,int m,int y)
{
day=d;
month=m;
year=y;//the current day..
}
void setDate()
{
cout<<"enter ur birth day:";
cin>>d_birth;
if (d_birth<0 || d_birth>30)
cout<<"enter ur birth day agin please:";
cout<<"enter ur birth month:";
cin>>m_birth;
if (m_birth<0 || d_birth>12)
cout<<"enter ur birth month agin please:";
cout<<"enter ur birth year:";
cin>>y_birth;
}
string getDate()
{
char cString[64] = "";
sprintf(cString,"%02d-%02d-%04d",month,day,year);
return string(cString);
}
void nextDay()
{
if (day>=30)
{
day=1;
month++;
}
else
day++;
if (month>12)
{
month=1;
year++;
}
cout<<day<<"-"<<month<<"-"<<year;
}
void print()
{
if (day<10)
cout<<"0"<<day;
if (month<10)
cout<<"0"<<month;
cout<<day<<"-"<<month<<"-"<<year;
}
void age()
{
int age=0,d_dif,m_dif,y_dif,d_m_dif,d_y_dif;
d_dif=day - d_birth;
m_dif=month - m_birth;
y_dif=year - y_birth;
d_m_dif=m_dif*30;
d_y_dif=y_dif*360;
age=d_dif + d_m_dif + d_y_dif;
cout<<age;
}
};
int main()
{
Date currnt(26,10,2007);
int s;
do
{
cout<<endl<<"Menu:"<<endl;
cout<<"1-Set Date"<<endl;
cout<<"2-Get string format"<<endl;
cout<<"3-Print the next day"<<endl;
cout<<"4-Print the birth day in the format dd-mm-yyyy"<<endl;
cout<<"5-Calculate age"<<endl;
cout<<"choose One of this menu or '-1' to exit:"<<endl;
cin>>s;
switch(s)
{
case 1:
currnt.setDate();
break;
case 2:
currnt.getDate();
break;
case 3:
currnt.nextDay();
break;
case 4:
currnt.print();
break;
case 5:
currnt.age();
break;
case -1:
break;
}
}while (s != -1);
return 0;
}
المفضلات