الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ممكن اضافة Decrment

بدأه درر الكون في 3 مايو 2009 · 6 رد · 603 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

انا ابي اعرف كيف اضيف على هذا البرنامج

انه يطرح تاريخ اليوم من تاريخ الميلاد

#include <iostream>
using namespace std;
#include <ctime>//used to set today's date
class Date{

//---------------- F R I E N D	F U N C T I O N S ---------------------
//------------------------------------------------------------------------

//Overloading insertion operator must be as a friend funciton
friend ostream& operator << (ostream& outs, Date& d); 

//Overloading extraction operator must be as a friend funciton
friend istream& operator >> (istream& ins, Date& d);


// 
//------------------------- A T T R I B U T E S --------------------------
// 

private:
		static const int DAY = 1;
		static const int MONTH = 2;
		static const int YEAR = 3;

		int day;
		int month;
		int year;


// 
//---------------------------- M E T H O D S -----------------------------
// 

public:
		Date(); //Default constructor
		Date (int d, int m, int y); //Constructor

		//Getter methods. 
		int getDay(){return day;}
		int getMonth(){return month;}
		int getYear(){return year;}

		//Setter methods
		bool setDay(int d){
			if (d > 0 && d < 31){ 
			   day = d;			 
			   return true;
			}
			else return false;
		}

		bool setMonth(int m){
			if (m > 0 && m < 13){ 
			   month = m;			 
			   return true;
			}
			else return false;
		}

		bool setYear(int y){
			if (y > 1990){ 
			   year = y;			 
			   return true;
			}
			else return false;
		}

		bool setDay(char * d){return setDay(atoi(d));}
		bool setMonth(char * m){return setMonth(atoi(m));}
		bool setYear(char * y){return setYear(atoi(y));}

		//Other methods
		Date& addDays(int days);
		Date& addMonths(int months);
		Date& addYears(int years);
		Date& nextDay();

		//Operator Overloaders
		bool operator ==(const Date& d);
		Date operator + (const Date& d);
		Date & operator ++ ();
		Date operator ++ (int);
		int operator [] (int index) const;




// 
//------------------ H E L P E R	F U N C T I O N S -------------------
// 


		//helper function: if given string is an int and not larger than the 
		//max, return it. Otherwise, return 0;
		static int validatePartOfDate(char * toBeValidated, int sec);
};

//Default construcor sets the date to be today's date
Date[img=http://www.arabmoheet.net/forum/editor/smiles/biggrin2.gif]ate(){

time_t tt = time(NULL); /* Get the current date in seconds */
tm ttm = *localtime(&tt); /* Convert to structure */

setDay(ttm.tm_mday);
setMonth(ttm.tm_mon + 1);
setYear(ttm.tm_year + 1900);
}

Date[img=http://www.arabmoheet.net/forum/editor/smiles/biggrin2.gif]ate (int d, int m, int y){ setDay(d); setMonth(m); setYear(y);}

Date& Date::addDays(int days){
	if (!setDay(this->day + days)){
		setDay( ((this->day + days) % 30));
		addMonths( (this->day + days) / 30);
	}
	return *this;
}

Date& Date::addMonths(int months){
	if (!setMonth(this->month + months)){
		setMonth((this->month + months) % 12);
		addYears( (this->month + months) / 12);
	}
	return *this;
}

Date& Date::addYears(int years){
	setYear(year + years);
	return *this;
}

Date& Date::nextDay(){
	addDays(1);
	return *this;
}


bool Date:[img=http://www.arabmoheet.net/forum/editor/smiles/redface2.gif]perator ==(const Date& d){	 
	return (day == d.day && month == d.month && year == d.year); 
}

Date Date:[img=http://www.arabmoheet.net/forum/editor/smiles/redface2.gif]perator + (const Date& d)
	 {	 Date date(*this);
		  date.addDays(d.day);
		  date.addMonths(d.month);
		  date.addYears(d.year);
		  return date;
	 }

Date & Date:[img=http://www.arabmoheet.net/forum/editor/smiles/redface2.gif]perator ++ (){
		  nextDay(); 
		return *this;
	 }

Date Date:[img=http://www.arabmoheet.net/forum/editor/smiles/redface2.gif]perator ++ (int){
		  Date d(*this);
		  nextDay(); 
		  return d;
	 }

int Date:[img=http://www.arabmoheet.net/forum/editor/smiles/redface2.gif]perator [] (int index) const
	 {	 switch (index){
		case (Date[img=http://www.arabmoheet.net/forum/editor/smiles/biggrin2.gif]AY): return day;
		  case (Date::MONTH): return month;
		  case (Date::YEAR): return year;
		  default: return -1;
		  }
	 }



int Date::validatePartOfDate(char * toBeValidated, int partOfDate){

	int i = atoi(toBeValidated);

	if (i > 0)
	{
	 switch (partOfDate){
		case (Date[img=http://www.arabmoheet.net/forum/editor/smiles/biggrin2.gif]AY): 
			if (i < 31) return i;
			else return 0;

		case (Date::MONTH):
			if (i < 13) return i;
			else return 0;

		case (Date::YEAR): 
			if (i > 1900) return i;
			else return 0;

		default: return 0;
		  }
	}
	return 0;
}

ostream& operator << (ostream& outs, Date& d){
		  outs << d.day <<"-" << d.month << "-" << d.year;
		  return outs;
	 }

istream& operator >> (istream& ins, Date& d){
		  ins >> d.day;
		  ins >> d.month;
		  ins >> d.year;
	  return ins;
	 }


int main(){

	Date today;
	cout<<today<<endl;
	Date d1(1,2,1950);
	Date d2(3,5, 2004);


	cout << "dates equal? " << (d1 == d2)<< endl;

	Date difference(10,2,0);

	cout << " d1 after 10 days and two months " << (d1 + difference) << endl;

	cout << d2;
	cout << " after prefix increment " << (++d2) << endl;

	cout << d2;
	cout << " after adding 29 days " << d2.addDays(29) << endl;

	cout << "please enter the difference you wish:" << endl;
	cin >> difference;
	cout << "difference is now " << difference<< endl;

	cout << "days: " << d2[1] << endl;
/////////////////////////////////////////////////
ابي ادخل هذي في المين 
بس الاشكالية السطر الاخير
Date dateOfBirth(8, 5, 1982);
Date today;
Date myAge;
myAge = today – dateOfBirth

تكفوون ردو بسرعه لان عندي اختبار وهذي النقطه ماشرحتها الاستاذة

تم تعديل هذه المشاركة بواسطة time1 في 3 مايو 2009 في 23:20

#2

معقوله مافي احد يساعدني

#3

السلام عليكم

الكود جميل جدا , ولكن هناك أخطاء وإن شاء الله يتم اصلاحها

ولكن لى استفسار

لماذا اعتبرتى ان كل الشهور 30 يوما ؟

أليس هذا عيب فى البرنامج ؟

منتظر ردك ... لأنه يجب اصلاح هذه المشكله قبل تكملة البرنامج

فى أمان الله

اللهم صل على سيدنا محمد وعلى آله وصحبه وسلم

#4

وعليكــم السـلام ورحمة الله وبركاتـه..

اعتبرتها 30يوم لانه الافضل

وهذا الكود اللي اضفته هل هو صحيح

Date Date::operator -( Date op2)

{ Date temp;

temp.day = day - op2.day;

temp.month = month - op2.month;

temp.year = year - op2.year; .

return temp; }

#5

السلام عليكم ....

اقتباس
اعتبرتها 30يوم لانه الافضل

لا ليس أفضل ... إلا إن كان من طرح السؤال يفترض ذلك من أجل تبسيطه....

العمليات على التواريخ ليست صعبة و لكنها ليست سهلة في نفس الوقت, تحتاج إلى كتابة خوارزمية بسيطة لكل عملية على "الورق" قبل كتابة الكود...

إذا قمت بكتابة الكود مباشرة دون حل المسألة على الورق, صدقني ستنتج كود مثل المكرونة.....

عموماً هذا سؤال حللته منذ زمن بعيد في project euler ..... أتمنى أن تستفيد منه ......

post-89451-1241463397_thumb.png

و هذا هو حلي....

int monthDays[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int februaryDays( int year ){

	if( year % 100 == 0 && year % 400 == 0 )
		return 29;
	if( year % 100 == 0 )
		return 28;
	if( year % 4 == 0 )
		return 29;

	return 28;
}

int numberOfDays( int month, int year ){

	if( month == 2 )
		return februaryDays( year );

	return monthDays[month-1];
}

void addSevenDays( int& day, int& month, int& year ){

	int incrementMonth = 0, incrementYear = 0;

	day += 7;
	incrementMonth = day / (numberOfDays(month, year) + 1);
	day = day % (numberOfDays( month, year )+1) + day / (numberOfDays( month, year )+1);

	month += incrementMonth;
	incrementYear = month / 13;
	month = month % 13 + month / 13;

	year += incrementYear;

	return;
}

int main(){

	int day = 6, month = 1, year = 1901;
	int sundays = 0;

	while( year < 2001 ){

		if( day == 1 )
			sundays++;

		addSevenDays( day, month, year );
	}

	std::cout << "The number Of Sundays on the First Of the month = " << sundays;

	return 0;
}

تحياتي ......

تم تعديل هذه المشاركة بواسطة Khaled.Alshaya في 4 مايو 2009 في 22:00

#6

السلام عليكــم ورحمـة الله وبركاتــه ،،

على الرغم انى غير مقتنع بموضوع ان كل شهر 30 يوما

هذا كودك مع بعض الإصلاحات

مع دالة حساب العمر


using namespace std;
#include <ctime>//used to set today's date
const int DAY = 1;
const int MONTH = 2;
const int YEAR = 3;
class Date
{

friend ostream& operator<<(ostream& , Date&);
friend istream& operator>>(istream& ,Date&);
//------------------------- A T T R I B U T E S --------------------------
//

private:


int day;
int month;
int year;


//
//---------------------------- M E T H O D S -----------------------------
//

public:
Date(); //Default constructor
Date (int d, int m, int y); //Constructor

//Getter methods.
int getDay(){return day;}
int getMonth(){return month;}
int getYear(){return year;}

//Setter methods
bool setDay(int d){
if (d > 0 && d < 31){
day = d;
return true;
}
else return false;
}

bool setMonth(int m){
if (m > 0 && m < 13){
month = m;
return true;
}
else return false;
}

bool setYear(int y){
if (y > 1900){
year = y;
return true;
}
else return false;
}

bool setDay(char * d){return setDay(atoi(d));}
bool setMonth(char * m){return setMonth(atoi(m));}
bool setYear(char * y){return setYear(atoi(y));}

//Other methods
Date& addDays(int days);
Date& addMonths(int months);
Date& addYears(int years);
Date& nextDay();

//Operator Overloaders
bool operator ==(const Date& d);
Date operator + (const Date& d);
Date & operator ++ ();
Date operator ++ (int);
int operator [] (int index) const;




//
//------------------ H E L P E R F U N C T I O N S -------------------
//


//helper function: if given string is an int and not larger than the
//max, return it. Otherwise, return 0;
static int validatePartOfDate(char * toBeValidated, int sec);
};

//Default construcor sets the date to be today's date
Date::Date(){

time_t tt = time(NULL); /* Get the current date in seconds */
tm ttm = *localtime(&tt); /* Convert to structure */

setDay(ttm.tm_mday);
setMonth(ttm.tm_mon + 1);
setYear(ttm.tm_year + 1900);
}

Date::Date(int d, int m, int y){ setDay(d); setMonth(m); setYear(y);}

Date& Date::addDays(int days){
if (!setDay(this->day + days)){
setDay( ((this->day + days) % 30));
addMonths( (this->day + days) / 30);
}
return *this;
}

Date& Date::addMonths(int months){
if (!setMonth(this->month + months)){
setMonth((this->month + months) % 12);
addYears( (this->month + months) / 12);
}
return *this;
}

Date& Date::addYears(int years){
setYear(year + years);
return *this;
}

Date& Date::nextDay(){
addDays(1);
return *this;
}


bool Date::operator ==(const Date& d){
return (day == d.day && month == d.month && year == d.year);
}

Date Date::operator + (const Date& d)
{ Date date(*this);
date.addDays(d.day);
date.addMonths(d.month);
date.addYears(d.year);
return date;
}

Date & Date::operator ++ (){
nextDay();
return *this;
}

Date Date::operator ++ (int){
Date d(*this);
nextDay();
return d;
}

int Date::operator [] (int index) const
{ switch (index){
case (DAY): return day;
case (MONTH): return month;
case (YEAR): return year;
default: return -1;
}
}



int Date::validatePartOfDate(char * toBeValidated, int partOfDate){

int i = atoi(toBeValidated);

if (i > 0)
{
switch (partOfDate){
case (DAY):
if (i < 31) return i;
else return 0;

case (MONTH):
if (i < 13) return i;
else return 0;

case (YEAR):
if (i > 1900) return i;
else return 0;

default: return 0;
}
}
return 0;
}

ostream& operator<<(ostream& outs, Date& d){
outs << d.getDay()<<"-" << d.getMonth() << "-" << d.getYear()<<endl;
return outs;
}

istream& operator >> (istream& ins, Date& d){
int dd,mm,yy;
ins >> dd;
ins >> mm;
ins >> yy;
d.setDay(dd);d.setMonth(mm);d.setYear(yy);
return ins;
} // 4-5-2009
void myage(Date now,Date porn)// 9-10-1978
{
int d1=now.getDay(),m1=now.getMonth(),y1=now.getYear();
int d2=porn.getDay(),m2=porn.getMonth(),y2=porn.getYear();
int d,m,y;
if(d1>=d2)
d=d1-d2;
else
{
d1=d1+30;
m1--;
d=d1-d2;
}
if(m1>=m2)
m=m1-m2;
else
{
m1=m1+12;
y1--;
m=m1-m2;
}
y=y1-y2;
cout<<"Your age is : "<<y<<" years "<<m<<" months "<<d<<" days \n";
}
int main(){

Date today;
cout<<today<<endl;//4 5 2009
Date d1(19,2,1950);
Date d2(3,5, 1990);
myage(today,Date(9,2,1978));
cout << "dates equal? " << (d1 == d2)<< endl;

Date difference(10,2,0);

//cout << " d1 after 10 days and two months " << (d1 + difference) << endl;

cout << d2;
cout << " after prefix increment " << (++d2) << endl;

cout << d2;
cout << " after adding 29 days " << d2.addDays(29) << endl;

cout << "please enter the difference you wish:" << endl;
cin >> difference;
cout << "difference is now " << difference<< endl;
cout << "days: " << d2[1] << endl;
return 0;
}
#include <iostream>

سلام

اللهم صل على سيدنا محمد وعلى آله وصحبه وسلم

#7

مشكورين وجزاكم الله كل خير

مواضيع مشابهة