回 帖 发 新 帖 刷新版面

主题:求助,高手帮忙看看这个程序,由于是新手不是很清楚

// 1.cpp : Defines the entry point for the console 

application.
//

#include <iostream> 
using namespace std; 
/*
创建一个date的类来描绘日期,要包含 
1、一个缺省的构造函数2009.5.1 
2、含三个参数的构造函数year.month.day 
3、一个析构函数 
4、获得年月日的三个公有函数,设定年月日的三个公有函

数 
5、重载++和--,使之满足日期对象的特征 
6、-(减号)运算符重载之后能得到两个日期之间的天数 
7、+(加号)运算符重载之后能得到一个日期加上一个天数

之后的另一个日期 
8、给日期对象重载插入>>(insertion)和获取

(extraction)<< 
9、main函数来测试你的日期类 
*/
class Date{
public:
 Date(){ year=2009; month=5; day=1; }
 Date(int p_year,int p_month,int p_day);
 ~Date(){ }
 int get_year();
 int get_month();
 int get_day();
 void display() const;
 void set_year(int p_year);
 void set_month(int p_month);
 void set_day(int p_day);
 Date& operator++();//对应于++a
 Date operator++(int);//对应于a++
 int  operator-(Date a);//相差天数
 Date operator+(int p_day);//新日期
 friend ostream& operator<<(ostream& out, const 

Date& a);//友元函数cout<<a
 friend void operator>>(istream&in, Date& a);//友元

函数cin>>a
 /////////////////////////////////////////////
 int get_maxday();//辅助函数
 void inc();//辅助函数,加一天操作
 bool is_run_nian();//辅助函数,是否闰年
//private://
 int year;
 int month;
 int day;
};

Date::Date(int p_year,int p_month,int p_day){
 this->year=p_year;
 this->month=p_month;
 this->day=p_day;
}

int Date::get_year(){
 return year;
}
int Date::get_month(){
 return month;
}
int Date::get_day(){
 return day;
}
void Date::set_year(int p_year){
 this->year=p_year;
}
void Date::set_month(int p_month){
 this->month=p_month;
}
void Date::set_day(int p_day){
 this->day=p_day;
}

Date& Date::operator++(){//++a
 inc();
 return *this;
}

Date Date::operator++(int){//a++
 Date tmp(this->year,this->month,this->day);
 inc();
 return tmp; 
}

int  Date::operator-(Date a){//相差天数
 //比较难
 //思路:取1900-1-1年为参考,计算Date a与1900-1-1的

相差天数day1
 //然后计算this与1900-1-1的相差天数day2,返回day2-

day1。计算式考虑闰年
 //和各个月天数。
 return 88888;
}
void Date::display () const
{ static char *monthName[13]=

{"","1","2","3","4","5","6",
        "7","8","9","10","11","12"};
    
    cout<< year<<' '<<monthName[month]

<<","<<day<<endl;
    return;
}

Date Date::operator+(int p_day){//新日期
 int i;
 Date tmp(this->year,this->month,this->day);//备份
 Date result;
 for(i=1;i<=p_day;i++){
  inc();
 }
 result.set_year(year);
 result.set_month(month);
 result.set_day(day);
 year=tmp.get_year();//还原
 month=tmp.get_month();//还原
 day=tmp.get_day();//还原
 return result;
}

ostream& operator<<(ostream& out, const Date& a){//

友元函数cout<<a
 

out<<"("<<a.year<<"-"<<a.month<<"-"<<a.day<<")"<<end

l;
 return out;
}


void operator>>(istream&in, Date& a){//友元函数

cin>>a
 int p_year,p_month,p_day;
 cout<<"输入年份:"; cin>>p_year;
 cout<<"输入月份:"; cin>>p_month;
 cout<<"输入天数:"; cin>>p_day;
 a.set_year(p_year);
 a.set_month(p_month);
 a.set_day(p_day);
}

bool Date::is_run_nian(){//辅助函数,是否闰年
 if(year%100==0){//遇到整百年时(如2000,1900,300)

要被400整除才是闰年
  if(year%400==0){
   return true;
  }else{
   return false;
  }
 }else{
  if(year%4==0){//遇到非整百年时(如2004,2005),只

要被4整除就是闰年
   return true;
  }else{
   return false;
  }
 }
}

int Date::get_maxday(){
 if

(month==1||month==3||month==5||month==7||month==8||m

onth==10||month==12) {
  return 31;
 }else if(month==4||month==6||month==9||month==11){
  return 30;
 }else if(month==2){
  if(is_run_nian()){
   return 29;
  }else{
   return 28;
  }
 }else{
  return -99999;
 } 
}

void Date::inc(){
 int maxday=get_maxday();
 if(day>=maxday){
  day=1;
  if(month>=12){
   month=1;
   year++;
  }else{
   month++;
  }
 }else{
  day++;
 }
}

void main() 
{
 cout<<"第一次运行速度有点慢^_^"<<endl;
 Date test1;
 cout<<"默认构造函数2009,5,1:";
 cout<<test1.display();

 Date test2(2009,6,1);
 cout<<"有参数构造函数2009,6,1:";
 cout<<test2.display ();

 cout<<"++(2009-6-1): ";
 cout<<++test2;

 Date test3;
 test3=test1+5;
 cout<<"2009-5-1 + 5:";
 cout<<test3;

 Date test4;
 test4=test1+31;
 cout<<"2009-5-1 + 31:";
 cout<<test4;
}

由于第一次接触C++,这个程序是干什么的
而且最后出现这个错误,改不了了。。
fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.

回复列表 (共1个回复)

沙发

你还是先把c++的基础知识看下吧

我来回复

您尚未登录,请登录后再回复。点此登录或注册