Скачиваний:
86
Добавлен:
15.06.2014
Размер:
4.54 Кб
Скачать
#include <iostream>
#include <conio.h>
#include <math.h>
#include <time.h>
#include <iomanip>
#include <string>
using namespace std;

struct date
{
	unsigned y;
	unsigned m;
	unsigned d;
};

int m[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void GetDate(date* d);
void PutDate(date d);
void AddDate(date* d1, date d2);
int DiffDate(date d1, date d2);

void main()
{
	date d1,d2;

	string vvods;
	int vvod;
	GetDate(&d1);
	while(true)
	{
	cout<<"Viberete deistvie:"<<endl;
	cout<<endl;
	cout<<"1) Summirovat s drugoi datoi;"<<endl;
	cout<<"2) Naiti raznost s drygoi datoi;"<<endl;
	cout<<"3) Vvesti datu zanovo;"<<endl;
	cout<<"4) Vivesti datu;"<<endl;
	cout<<"5) Ohistit;"<<endl;
	cout<<"6) Exit;"<<endl;
	cout<<endl;
	cin.exceptions(std::cin.failbit); 
    while (true){
		cin.clear();
		_flushall();
		vvods="";
		cout<<"Vashe deistvie (vvedite 1,2,3,4,5,6): ";
					cin>>vvods;
					cout<<endl;
		if (vvods.find_first_not_of("123456")==string::npos) 
			if ((atoi(vvods.c_str())<=6)&&(atoi(vvods.c_str())>=1)) {vvod=atoi(vvods.c_str()); break;}
		cerr<<"Neverniy vvod!!!"<<endl<<endl;
				}
	switch(vvod)
	{
	case 1:
		GetDate(&d2);
		AddDate(&d1,d2);
		break;
	case 2:
		GetDate(&d2);
		cout<< "raznost dat ravna "<<DiffDate(d1, d2)<<" dney."<<endl;
		break;
	case 3:
		cout<<endl;
		GetDate(&d1);
		break;
	case 4:
		PutDate(d1);
		break;
	case 5:
		system("cls");
		break;
	case 6:
		exit(1);
		break;
	}
	cout<<endl;
	}
}

void GetDate(date* d)
{
	string s1, s, s2=".", s3="";
	int t=1;
	unsigned n;
	int a;
	while (true) {
	 while ( true ){
                
                  int ww=0;  s3="";s="";s1="";
				  cin.clear();
				  _flushall();
						cout<< "vvedite datu v formate 'dd.mm.yyyy': ";
						getline(cin, s1);
						cout<<endl;
						if (s1.find_first_not_of("1234567890.")==string::npos) 
								{
								a=s1.length();
								for (int i=0; i < a ;i++)
									{
										s=s1.at(i);
										if (s==s2) 
											ww++;
									}
								for (int i=0; i < a ;i++)
								{
									s=s1.at(i);
									if (s==s2) 
									switch(t)
										{case 1:
											if (s3=="") ww=3;
											t=2;
											s3="";
										break;
										case 2:
											if (s3=="") ww=3;
											t=3;
											s3="";
										break;
										}
									else 
									{
										s3+=s;
									} 
									s="";
								}
						
								if (s3=="") ww=3;
								s3="";
						}
								if (ww==2) break;
									else {cout<<"Error, vvedite verno"<< endl;	s1="";}		
        }


	 //конец while первого
		t=1;
	for (int i=0; i < a ;i++)
	{
		s=s1.at(i);
		if (s==s2) 
		switch(t)
			{case 1:
				n = atoi(s3.c_str());
				d->d=n;
				t=2;
				s3="";
			break;
			case 2:
				n = atoi(s3.c_str());
				d->m=n;
				t=3;
				s3="";
			break;
			}
		else 
		{
			s3+=s;
		} 
		s="";
	}
	n = atoi(s3.c_str());
	d->y=n;
	if (((d->m)<=12)&&((d->d)<=(m[(d->m)-1]))&&((d->d)!=0)&&((d->m)!=0)) break;
	cout<<"Error, vvedite verno"<< endl;
	}
}

void PutDate(date d)
{
	cout << d.d<<"."<<d.m<<"."<<d.y<<endl;
}

void AddDate(date* d1, date d2)
{
	cout << endl;
	d1->m+=d2.m;
	d1->y+=d2.y;
	if ((d1->m)>12){ d1->y+=1; d1->m-=12;}
	d1->d+=d2.d;
	if ((d1->d)>(m[(d1->m)-1])) 
	{
		d1->d=d1->d-(m[(d1->m)-1]);
		d1->m+=1;
		if ((d1->m)>12){ d1->y+=1; d1->m-=12;}
		if ((d1->d)>(m[(d1->m)-1])) 
	{
		d1->d=d1->d-(m[(d1->m)-1]);
		d1->m+=1;
		if ((d1->m)>12){ d1->y+=1; d1->m-=12;}

	}
	}
	cout<<"Summa dat ravna: ";
	PutDate(*d1);
}

int DiffDate(date d1, date d2)
{
	date dmax,dmin;
	int s=0, t=0;
	if (d1.y>d2.y) {dmax=d1; dmin=d2;}
		else if (d1.y<d2.y) {dmax=d2; dmin=d1;}
			else if (d1.m>d2.m) {dmax=d1; dmin=d2;}
				else if (d1.m<d2.m) {dmax=d2; dmin=d1;}
					else if (d1.d>d2.d) {dmax=d1; dmin=d2;}
						else if (d1.d<d2.d) {dmax=d2; dmin=d1;}
							else if (d1.d==d2.d) {t=1;}
	 if (t==1) s=0;
	 else{
			if 	((dmax.y-dmin.y)>=1) 
	{ while ((dmax.y-dmin.y)>1)
	{
		s+=365;
		dmin.y++;
	}
		//месяц до 12
		 while (dmin.m<12)
		 {
			 s+=m[dmin.m-1];
			 dmin.m++;
		 }
		s+=m[dmin.m-1]-dmin.d+1;
		dmin.d=1;
		dmin.m=1;
		dmin.y+=1;
			}
			//от сюда год одинаковый			
			if  (dmin.m<dmax.m)
							while ((dmin.m)<(dmax.m))
							{
								s+=m[dmin.m-1];
								dmin.m++;
							}
							
							for (dmin.d; dmin.d<dmax.d; dmin.d++)
								s++;}
	return s;
}
Соседние файлы в папке laba4_shakur_1
  • #
    15.06.20144.54 Кб86laba4_shakur_1.cpp
  • #
    15.06.20143.93 Кб84laba4_shakur_1.vcxproj
  • #
    15.06.2014963 б84laba4_shakur_1.vcxproj.filters
  • #
    15.06.2014143 б84laba4_shakur_1.vcxproj.user