当前位置:编程学习 > C/C++ >>

高分悬赏C++编程问题,要源程序。

1.(1)参考实验实例定义一个类Date,类结构如下:私有数据成员:Int year;Int month;Int day;公有成员函数:Date(int=1900,int=1,int=1);Void setYear(int);Void setMonth(int);Void setDay(int);Void Date::setDate(int yr,int mo,int dy);Int getDay();Int getMonth();Int getYear();Void print();Bool isLeapYear();Int monthDays()要求完成成员函数的定义,修改私有数据成员的成员函数能对初始值进行错误检查。编写主函数,测试类,源程序文件名为sy3_4.cpp。(2)给Date类添加一个成员函数nextday(),使天数可以递增一天,编写一个测试程序,用一个循环结构来测试nextday()函数,源程序文件名为sy3_5.cpp。2.定义一个具有递增功能的Increment类,其中有一个const数据成员increment,代表递增步长,请完成Increment类的定义,并编写主函数进行测试,注意构造函数的实现。参考程序如下,原程序文件名为sy4_2.cpp:#include<iostream.h>Class Increment{Public:Increment(int c=0,int i=1);Void addIncrement(){count+=increment;}Void print()const;Private:Int count;Const int increment;};Increment::Increment(int c,int i):increment(i){count=c;}Void Increment::print()const{Cout<<”count=”<<count<<”,increment=”<<increment<<endl;}Void main(){Increment value(10,5);Cout<<”Before incrementing:”;Value.print();For(int j=1;j<=3;j++){Value.addIncrement”<<j<<”:”;Value.print();}Return o;输出结果:Before incrementing:count=10,increment=5After increment 1:count=15,increment=5After increment 2:count=20,increment=5After increment 3:count=30,increment=5(2)修改上面的程序,将构造函数中用初始化表在构造函数中初始化常数据成员的方式改成如下形式,请上机调试是否可行,观察提示信息,考虑原因。Increment::Increment(int c,int i){count=c;Increment=i;} 问题补充:此为实验内容,哪位高手帮个忙编出程序吧,万分感谢!
追问:第二题能帮下忙吗?我可以再追加50分
答案:1.

#include <iostream>

using namespace std;

class Date{
public:
 Date(int d1 = 1900, int d2 = 1, int d3 = 1): year(d1), month(d2), day(d3) { }
 void setYear(int y) { year = y; }
 void setMonth(int m) { month = m; }
 void setDay(int d) { day = d; }
 void Date::setDate(int, int, int);
 int getDay() { return day; }
 int getMonth() { return month; }
 int getYear() { return year; }
 void print();
 bool isLeapYear();
 int monthDays();
private:
 int year;
 int month;
 int day;
};

bool Date::isLeapYear()
{
 if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/
 {
  return true;
 }else
 {
  return false;
 }
}
int Date::monthDays()
{
 int sum = 0;
 switch(month)/*先计算某月以前月份的总天数*/
 {
  case 1: sum = 31; break;
  case 2: sum = 28; break;
  case 3: sum = 31; break;
  case 4: sum = 30; break;
  case 5: sum = 31; break;
  case 6: sum = 30; break;
  case 7: sum = 31; break;
  case 8: sum = 31; break;
  case 9: sum = 30; break;
  case 10:sum = 31; break;
  case 11:sum = 30; break;
  case 12:sum = 31; break;
  default:cout << "data error";break;
 }
 if(isLeapYear() && month == 2)/*如果是闰年且月份等于于2,总天数应该加一天*/
  sum++;
 return sum;
}
void Date::setDate(int yr, int mo, int dy)
{
 if(yr < 1900)
  cerr << "year error" << endl;
 if(mo > 12)
  cerr << "month error" << endl;
 if(monthDays() < day)
  cerr << "day error" << endl;
 year = yr;
 month = mo;
 day = dy;
}
void Date::print()
{
 cout << "year: " << year << " month: " << month << " day: " << day << endl;
 cout << " is Leap Year : " << (isLeapYear()?"true":"false") << endl;
 cout << " monthDays : " << monthDays() << endl;
}
int main()
{  
 int y, m, d;
 Date da;
 cin >> y >> m >> d;
 da.setDate(y,m,d);
 da.print();
 system("pause");
 return 0;
}

 

2.(加上nextDay()方法)后

#include <iostream>

using namespace std;

class Date{
public:
 Date(int d1 = 1900, int d2 = 1, int d3 = 1): year(d1), month(d2), day(d3) { }
 void setYear(int y) { year = y; }
 void setMonth(int m) { month = m; }
 void setDay(int d) { day = d; }
 void Date::setDate(int, int, int);
 int getDay() { return day; }
 int getMonth() { return month; }
 int getYear() { return year; }
 void print();
 bool isLeapYear();
 int monthDays();
 void nextDay();
private:
 int year;
 int month;
 int day;
};

bool Date::isLeapYear()
{
 if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/
 {
  return true;
 }else
 {
  return false;
 }
}
int Date::monthDays()
{
 int sum = 0;
 switch(month)/*先计算某月以前月份的总天数*/
 {
  case 1: sum = 31; break;
  case 2: sum = 28; break;
  case 3: sum = 31; break;
  case 4: sum = 30; break;
  case 5: sum = 31; break;
  case 6: sum = 30; break;
  case 7: sum = 31; break;
  case 8: sum = 31; break;
  case 9: sum = 30; break;
  case 10:sum = 31; break;
  case 11:sum = 30; break;
  case 12:sum = 31; break;
  default:cout << "data error";break;
 }
 if(isLeapYear() && month == 2)/*如果是闰年且月份等于于2,总天数应该加一天*/
  sum++;
 return sum;
}
void Date::setDate(int yr, int mo, int dy)
{
 if(yr < 1900)
  cerr << "year error" << endl;
 if(mo > 12)
  cerr << "month error" << endl;
 if(monthDays() < day)
  cerr << "day error" << endl;
 year = yr;
 month = mo;
 day = dy;
}
void Date::print()
{
 cout << "year: " << year << " month: " << month << " day: " << day << endl;
 cout << " is Leap Year : " << (isLeapYear()?"true":"false") << endl;
 cout << " monthDays : " << monthDays() << endl;
}
void Date::nextDay()
{
 if(monthDays() == day) // 月的最后一天
 {
  if(month == 12) // 月是最后一个月(跨年)
  {
   ++year;
   month = 1;
   day = 1;
  }else  
  {
   ++month;
   day = 1;
  }
 }else
 {
  ++day; 
 }
}
int main()
{  
 int y, m, d;
 Date da;
 cin >> y >> m >> d;
 da.setDate(y,m,d);
 da.print();
 da.nextDay();
 da.print();
 system("pause");
 return 0;
}

 

上一个:初学者是学C++或C语言还是Java好?
下一个:visual C++ 开发人员应该具备哪些能力

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,