סדנא תרגיל 2 שאלה 2

benny++C, class, סדנא ב++C, תרגילים, תרגילים בסדנא ב++CLeave a Comment

מחלקה לייצוג תאריך – הגדרת מחלקה לייצוג תאריך, יום שנה וחודש (השדות במחלקה) התכנית כוללת העמסת אופרטורים, כולל אופרטורים אונריים , אופרטור ++ תחילי וסופי ועוד (עיינו בקובץ התרגיל)
תרגיל 2 שאלה 2 סדנא ++C

תכנית ראשית

/******************************
targil: 2/2
discription: a calender whith a few option

*******************************/
#include <iostream>
#include "Date.h"
using namespace std;

int main()
{
cout<<"Enter a date\n";

int day;
int month;
int year;
char slash;
int choice=0;
int numberDyas;
bool x = false;
Date B;
 

cin>>day>>slash>>month>>slash>>year;

Date A(day,month,year);

A.print();

do
{
	cout<<"What do you want to do\n";
	
	cin>>choice;

	switch (choice)
	{

        case 1: cout<<"Enter a date\n";
			cin>>day>>slash>>month>>slash>>year;
			A.setDate(day, month, year);
			A.print();

            break;


        case 2:
			(++A).print();

			 break;

		case 3:
		
			(A++).print();

		 break;

		case 4: cout<<"Enter#days\n";

			cin>>numberDyas;
			A+=numberDyas;
			A.print();

		 break;

		case 5: cout<<"Enter a date\n";
			
			cin>>day>>slash>>month>>slash>>year;

			B.setDate(day,month,year);

			x=(A>B);   

			if (x)       // if the first date  is later then the new date
			{
			cout<<">:true\n";
			}
			else            // if the first date  is earlyer then the new date
			{
			cout<<">:false\n";
			}
			
		 break;

		 case 6: cout<<"Enter a date\n";

			x=(A<B);       

			if (x)          // if the first date  is earlyer then the new date
			{
			cout<<"<:true\n";
			}
			else           // if the first date  is later then the new date
			{
			cout<<"<:false\n";
			}

		 break;

		 case 7: cout<<"Enter a date\n";

			cin>>day>>slash>>month>>slash>>year;

			B.setDate(day,month,year);

			x=(A==B);

			if (x)        // if the both dates are equals
			{
			cout<<"==:true\n";
			}
			else
			{
			cout<<"==:false\n";     // if the both dates are not equals
			}

		 break;

	default:
		break;

	}
	

} while (choice != -1);

	return 0;
}
/**********OUTPUT**************

Enter a date
-5/1/2010
Error day
1/1/2010
What do you want to do
1
Enter a date
6/1/2000
6/1/2000
What do you want to do
2
7/1/2000
What do you want to do
3
7/1/2000
What do you want to do
4
Enter#days
30
8/2/2000
What do you want to do
5
Enter a date
8/2/2001
>:false
What do you want to do
7
Enter a date
8/2/2000
==:true
What do you want to do

*****************************/

מימוש פונקציות המחלקה

#include "Date.h"
#include <iostream>
using namespace std;

Date::Date(int d,int m,int y): day(d),month(m),year(y)   //c-tor
{
	if (d<1||d>30)          
	{
		cout<<"Error day\n"; day=1;
		
	}
	if (m<1||m>12)
	{
		cout<<"Error month\n"; month=1;
	}
	if (y<1970||m>2099)
	{
		cout<<"Error year\n"; year=1970;
	}
}

Date::Date(const Date&Temp)        //copy c-tor
{
	this->day=Temp.day;
	this->month=Temp.month;
	this->year=Temp.year;
}

int Date::get_day()
{
	return day;
}
int Date::get_month()
{
	return month;
}
int Date::get_year()
{
	return year;
}
	



void Date::setDate(int d, int m, int y)
{
	if (d<1||d>30||m<1||m>12||y<1970||m>2099)
	{

	}
	else
	{
		day=d;  month=m;  year=y;
	}

}

void Date::print()
{
	cout<<day<<"/"<<month<<"/"<<year<<endl;
	
	
}

Date Date::operator++(int x)
{
	Date Temp(day,month,year);
	if (day==30)
	{
		if (month==12)
		{
			day=1; month=1; year++; 
		}
		else
		{
			day=1; month++;
		}
	}
	else
	{
		day++; 
	}
	return Temp;
	
}
Date Date::operator++()
{
	
	if (day==30)
	{
		if (month==12)
		{
			day=1; month=1; year++; 
		}
		else
		{
			day=1; month++;
		}
	}
	else
	{
		day++; 
	}
	return *this;
	
}


void Date::operator+=(int days)
{
	
	
	if (day+days>30)
	{
		
		if (((month-1)*30+day+days)/360>0)
		{
			year+=(month*30+day+days)/360;
		}
		if ((month+(day+days)/30)>12)
		{
			if ((day+days)/30<12)
			{
				month = month + ((day+days)/30)-12; 
			}
			else
			{
				month = ((day+days)/30)/12;
			}
		}
		else
		{
			month++;
		}
		

		day=(day+days)%30;
	}
	else
	{
		
		day+=days;
	}
	
	

}

bool Date::operator==(Date &a)
{

	if (day==a.get_day()&&month==a.get_month()&&year==a.get_year())
	{
		return true;
	}
	return false;

}

bool Date::operator>(Date &a)
{


	

	if (year>a.get_year())
	{
		return true;
	}
	if (year<a.get_year())
	{
	 return false;
	}
	if (year==a.get_year())
	{
		if (month>a.get_month())
		{
			return true;
		}
		if (month<a.get_month())
		{
			 return false;
		}
		if (month==a.get_month())
		{
			if (day>a.get_day())
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}

}


bool Date::operator<(Date &a)
{


	a.print();cout<<endl;

	if (year<a.get_year())
	{
		return true;
	}
	return false;

}







//Date Date::operator>(Date days)
//{
//
//
//}


Date::~Date(void)
{
}

המחלקה

#pragma once
class Date
{
	int day;
	int month;
	int year;
public:
	Date(int d=1,int m=1,int y=1970);  //c-tor
	Date(const Date&temp);  //copy c-tor
	void setDate(int, int, int);

	int get_day();
	int get_month();
	int get_year();

	void print();

	Date operator++();     // ++x;   fref
	Date operator++(int);  // x++;   after
	void operator+=(int);
	bool operator==(Date &a);
	bool operator>(Date &a);
	bool operator<(Date &a);







	~Date(void);
};


כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *