Announcement

Collapse
No announcement yet.

[Help] Nhờ mấy anh chị kiểm tra giùm e cái code này

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Help] Nhờ mấy anh chị kiểm tra giùm e cái code này

    Code:
    // OOP-Giuaky2011.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    class ngay
    {
    private:
    	int dd,mm,yy;
    public:
    	ngay()
    	{
    		dd=mm=yy=0;
    	}
    	~ngay()
    	{
    	}
    	ngay(int a,int b,int c)
    	{
    		dd=a;mm=b;yy=c;
    	}
    	friend istream &operator >>(istream &is,ngay &d);
    	friend ostream &operator <<(ostream &os,ngay &d);
    	int operator -(ngay a)
    	{
    		int kc=0;
    		kc=abs(yy-a.yy)*365;
    		int ngay1=dd-1+(mm-1)*30;
    		int ngay2=a.dd-1+(a.mm-1)*30;
    		kc=kc+abs(ngay1-ngay2);
    		return kc;
    	}
    	ngay operator +(int a)
    	{
    		ngay tamp;
    		switch(mm)
    		{
    		case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    		{
    			if(dd<=31-a) 
    			{
    				tamp.dd=dd+a;
    				tamp.mm=mm;
    				tamp.yy=yy;
    			}
    			else 
    			{
    				if(mm==12)
    				{
    					tamp.dd=dd+a-31;
    					tamp.mm=1;
    					tamp.yy=yy+1;
    				}
    				else
    				{
    					tamp.dd=dd+a-31;
    					tamp.mm=mm+1;
    					tamp.yy=yy;
    				}
    			}
    			break;
    		}
    		case 4: case 6: case 9: case 11:
    		{
    			if(dd<=30-a)
    			{
    				tamp.dd=dd+a;
    				tamp.mm=mm;
    				tamp.yy=yy;
    			}
    			else
    			{
    				tamp.dd=dd+a-30;
    				tamp.mm=mm+1;
    				tamp.yy=yy;
    			}
    			break;
    		}
    		case 2:
    		{
    			if((yy%4==0&&yy%100!=0)||(yy%400)==0)
    			{
    				if(dd<=29-a)
    				{
    					tamp.dd=dd+a;
    					tamp.mm=mm;
    					tamp.yy=yy;
    				}
    				else 
    				{
    					tamp.dd=dd+a-29;
    					tamp.mm=3;
    					tamp.yy=yy;
    				}
    			} 
    			else
    			{
    				if(dd<=28-a)
    				{
    					tamp.dd=dd+a;
    					tamp.mm=mm;
    					tamp.yy=yy;
    				}
    				else
    				{
    					tamp.dd=dd+a-28;
    					tamp.mm=3;
    					tamp.yy=yy;
    				}
    			}
    			break;
    		}
    	}
    		return tamp;
    	}
    	ngay operator -(int a)
    	{
    		ngay tamp;
    		switch(mm)
    		{
    		case 1: case 2: case 4: case 6: case 8: case 9: case 11: 
    		{
    			if(dd>=1+a) 
    			{
    				tamp.dd=dd-a;
    				tamp.mm=mm;
    				tamp.yy=yy;
    			}
    			else 
    			{
    				if(mm==1)
    				{
    					tamp.dd=dd-a+31;
    					tamp.mm=12;
    					tamp.yy=yy-1;
    				}
    				else
    				{
    					tamp.dd=dd-a+31;
    					tamp.mm=mm-1;
    					tamp.yy=yy;
    				}
    			}
    			break;
    		}
    		case 5: case 7: case 10: 
    		{
    			if(dd>=1+a)
    			{
    				tamp.dd=dd-a;
    				tamp.mm=mm;
    				tamp.yy=yy;
    			}
    			else
    			{
    				tamp.dd=dd-a+30;
    				tamp.mm=mm-1;
    				tamp.yy=yy;
    			}
    			break;
    		}
    		case 3:
    		{
    			if((yy%4==0&&yy%100!=0)||(yy%400)==0)
    			{
    				if(dd>=1+a)
    				{
    					tamp.dd=dd-a;
    					tamp.mm=mm;
    					tamp.yy=yy;
    				}
    				else 
    				{
    					tamp.dd=dd-a+29;
    					tamp.mm=2;
    					tamp.yy=yy;
    				}
    			} 
    			else
    			{
    				if(dd>=1+a)
    				{
    					tamp.dd=dd-a;
    					tamp.mm=mm;
    					tamp.yy=yy;
    				}
    				else
    				{
    					tamp.dd=dd-a+28;
    					tamp.mm=2;
    					tamp.yy=yy;
    				}
    			}
    			break;
    		}
    		}
    		return tamp;
    	}
    };
    istream &operator >>(istream &is,ngay &d)
    {
    	cout<<"Nhap ngay thang nam (dd/mm/yyyy)"<<endl;
    	cin>>d.dd; cin>>d.mm; cin>>d.yy;
    	return is;
    }
    ostream &operator <<(ostream &os,ngay &d)
    {
    	cout<<"Ngay thang nam: ";
    	os<<d.dd<<"/"<<d.mm<<"/"<<d.yy<<endl;
    		return os;
    }
    void main()
    {
    	ngay a(31,12,2010),b;
    	cin>>b;
    	cout<<a-b<<endl;
    	b=a+5; 
    	b=a-4;
    	cout<<b;
    	system("pause");
    }
    Không hiểu sao đến chỗ b=a-4 là ngày lại tự động bị trả về 0/0/0 . mong mấy a chị giúp đỡ

  • #2
    có j khó hiểu đâu. ở chỗ định nghĩa toán tử "-": ngay operator -(int a) . Ở trong cái switch(mm), bạn đưa ra mấy cái case thiếu mất tháng 12 của người ta, mà trong hàm main bạn lại khởi tạo a là "ngay a(31,12,2010)", rồi xuống phía dưới dùng toán tử "-" thì nó không ra kết quả 0/0/0 mới lạ đó.
    bây giờ bạn thử test với tháng của a là tháng 11 đi, nó sẽ ra kết quả đúng thôi.
    University of Information Technology
    Cao Văn Nhàn _ CE10520355
    SĐT: 0188 403 4943

    Email: caovannhan2002@gmail.com

    Comment


    • #3
      her e quên e quên, thiếu tháng, thế mà e ko nhìn ra, cảm ơn a

      Comment

      LHQC

      Collapse
      Working...
      X