
C++编程考试!!求救 十万火急!!哪位编程高手帮一下。感激不尽!!
我们C++考试。。要求如下:自己单独完成,如果答案雷同,都以0分处理,并不能参加笔试所以大侠们最好能自己编。。不要在网上搜来的答案!!谢谢啦题目如下:1、在程序f0904...
我们C++考试。。要求如下:
自己单独完成,如果答案雷同,都以0分处理,并不能参加笔试
所以大侠们最好能自己编。。不要在网上搜来的答案!!谢谢啦
题目如下:1、在程序f0904.cpp(教材300页)中的日期类的基础上,实现一个可以进行加天数操作获得另一个日期,一个可以进行减天数操作获得另一个日期,以及进行日期减日期操作获得相隔天数的日期类,并进行应用程序设计:
创建2005.8.21和2008.8.8两个日期,并计算中间相隔的天数,前者加上300天会是什么日期?后者减去300天会是什么日期?。(总分40分)
2、定期储蓄是储蓄的一种,假设定期分一年期、三年期和五年期,利率分别为5%、8%和10%。用户在办理定期存款帐户时,必须确定其定期时段,中途不再在同一帐号上办理存款业务。取款是一次性完成的,若提前取款,则全部金额的利息按活期利率1%计算。将其银行业务设计成定期类,并且至少给出4个用户进行测试,测试情况包括:一年期定期取款、三年期定期取款、五年期定期取款和提前取款。说明:程序越完善得分越高。(总分60分)
Date::Date(const string& s){
year = atoi(s.substr(0,4).c_str());
month = atoi(s.subtr(5,2).c_str());
day = atoi(s.subtr(8,2).c_str());
init();
}//--------------------------------
Date::Date(int y, int m, int d){
year=y, month=m, day=d;
init();
}//--------------------------------
bool Date::isLeapYear()const{
return (year % 4==0 && year % 100 )|| year % 400==0;
}//--------------------------------
ostream& operator<<(ostream& o, const Date& d){
o<<setfill('0')<<setw(2)<<d.day<<'\n'<<setfill(' ');
}//--------------------------------
int main(){
Daet c("2005-12-28");
Date d("2003,12,6);
Date e(2002);
Date f(2002,12);
Date g;
cout<<c<<d<<e<<f<<g;
}//================================ 展开
自己单独完成,如果答案雷同,都以0分处理,并不能参加笔试
所以大侠们最好能自己编。。不要在网上搜来的答案!!谢谢啦
题目如下:1、在程序f0904.cpp(教材300页)中的日期类的基础上,实现一个可以进行加天数操作获得另一个日期,一个可以进行减天数操作获得另一个日期,以及进行日期减日期操作获得相隔天数的日期类,并进行应用程序设计:
创建2005.8.21和2008.8.8两个日期,并计算中间相隔的天数,前者加上300天会是什么日期?后者减去300天会是什么日期?。(总分40分)
2、定期储蓄是储蓄的一种,假设定期分一年期、三年期和五年期,利率分别为5%、8%和10%。用户在办理定期存款帐户时,必须确定其定期时段,中途不再在同一帐号上办理存款业务。取款是一次性完成的,若提前取款,则全部金额的利息按活期利率1%计算。将其银行业务设计成定期类,并且至少给出4个用户进行测试,测试情况包括:一年期定期取款、三年期定期取款、五年期定期取款和提前取款。说明:程序越完善得分越高。(总分60分)
Date::Date(const string& s){
year = atoi(s.substr(0,4).c_str());
month = atoi(s.subtr(5,2).c_str());
day = atoi(s.subtr(8,2).c_str());
init();
}//--------------------------------
Date::Date(int y, int m, int d){
year=y, month=m, day=d;
init();
}//--------------------------------
bool Date::isLeapYear()const{
return (year % 4==0 && year % 100 )|| year % 400==0;
}//--------------------------------
ostream& operator<<(ostream& o, const Date& d){
o<<setfill('0')<<setw(2)<<d.day<<'\n'<<setfill(' ');
}//--------------------------------
int main(){
Daet c("2005-12-28");
Date d("2003,12,6);
Date e(2002);
Date f(2002,12);
Date g;
cout<<c<<d<<e<<f<<g;
}//================================ 展开
5个回答
2009-06-26
展开全部
第一题,VC6下调试通过:
结果:
日期之差为: 1083
2006年6月17日 2007年10月3日
#include <iostream>
using namespace std;
class Date{/*日期类*/
public :
int year,month,day;
void set(int m=1,int d=1,int y=1900)
{
year=y;
month=m;
day=d;
}
inline int operator-(const Date &b)/*‘-’操作符重载,用于计算两个日期之差*/
{
int days=0;
if (year==b.year&&month==b.month ) {
days=day-b.day;
days=days<0?-days:days;
}
else if (year==b.year) {
int ml,mh;
mh=month>b.month ?(ml=b.month,month):(ml=month,b.month) ;
for (int i=ml; i<mh; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days-=ml==month?day:b.day;
days+=mh==b.month?b.day :day;
}
else{
int yl,yh,ml,mh;
yh=year>b.year ?(yl=b.year,year):(yl=year,b.year) ;
for (int i=yl+1; i<yh; i++) {
if (i%4==0&&i%100||i%400==0)days+=366;
else days+=365;
}
ml=yl==year?month:b.month;
mh=yh==b.year?b.month:month;
for (; ml<=12; ml++) {
switch (ml) {
default:
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days-=yl==year?day:b.day;
for (--mh; mh>=1; mh--) {
switch (mh) {
default:
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days+=yh==b.year?b.day:day;
}
return days;
}
inline bool operator>(const Date &a)/*'>'操作符重载,用于判断日期的先后*/
{
if (year==a.year &&month==a.month ) {
return day>a.day;
}
else if (year==a.year ) {
return month>a.month ;
}
else return year>a.year ;
}
inline Date operator+(int days)/*'+'操作符重载,用于计算days天之后的日期*/
{
for (; days>=0; --days) {
if (day==29&&month==2&&!(year%4==0&&year%100||year%400==0)) {
++month;
day=1;
}
else if (day==30&&month==2&&(year%4==0&&year%100||year%400==0)) {
++month;
day=1;
}
else if (day==31) {
switch (month) {
case 4:
case 6:
case 9:
case 11:month++;day=1;
}
}
else if (day==32) {
month++;
day=1;
if (month==13) {
month=1;
year++;
}
}
if (days) day++;
}
return *this;
}
inline Date operator-(int days)/*'-'操作符重载,用于计算days天之前的日期*/
{
for (; days>=0; --days) {
if (day==29&&month==2&&!(year%4==0&&year%100||year%400==0)) {
--month;
day=1;
} // end if
else if (day==30&&month==2&&(year%4==0&&year%100||year%400==0)) {
--month;
day=1;
} // end else-if
else if (day==31) {
switch (month) {
case 4:
case 6:
case 9:
case 11:month--;day=1;
}
} // end else-if
else if (day==32) {
month--;
day=1;
if (month==0) {
month=12;
year--;
}
}
if (days) day++;
}
return *this;
}
};
ostream& operator<<(ostream &a,Date &b)/*输出操作符重载,用于输出指定格式的日期*/
{
a<<b.year <<"年"<<b.month <<"月"<<b.day <<"日";
return a;
}
int main(void)/*调用实例*/
{
Date a,b;
a.set(8,21,2005);/*设置为2005-8-21*/
b.set(8,8,2008);/*设置为2008-8-8*/
int t=b-a;/*求两个日期之差*/
cout<<"日期之差为: "<<t<<endl;
a=a+300;/*计算2005-8-21 300天之后的日期*/
b=b-300;/*计算2008-8-8 300天之前的日期*/
cout<<a<<" "<<b<<endl;/*输出a和b日期*/
return 0;
}
-------------------------------------------------------------------
第二题:
#include<iostream>
#include<string>
using namespace std;
//-----------------------------------------------------------------
class Account{
protected:
string acntNumber;//账号
double money;//存款数
public:
Account(string acntNo,double Mon){acntNumber=acntNo;money=Mon;}
virtual void display() = 0;
};//------------------------------------------------------------------
class Savings :public Account{
int year;//存款年限
public:
Savings(string acntNo,double Mon,int Ye) :Account(acntNo,Mon),year(Ye){};
void display(){
switch(year){
case 1: cout<<money<<"元定期1年的本息为:"<<money*0.05*1+money<<endl;break;
case 3: cout<<money<<"元定期3年的本息为:"<<money*(1+0.08)*(1+0.08)*(1+0.08)<<endl;break;
case 5: cout<<money<<"元定期5年的本息为:"<<money*(1+0.1)*(1+0.1)*(1+0.1)*(1+0.1)*(1+0.1)<<endl;break;
default: cout<<"error\n";
}
}
void change(int n=0){//提前取款,利息按活期率1%计算
double m=money;
for(int i=1;i<=n;i++){
m=m*(1+0.01);
}
cout<<money<<"已存"<<n<<"年取款后的本息是:"<<m<<endl;
}
};//------------------------------------------------------------------
int main(){
Savings a("000001",2000,1);
Savings b("000002",4000,3);
Savings c("000003",8000,5);
a.display();
b.display();
c.display();
string c1;
cout<<"输入创建的账号:";
cin>>c1;
cout << "请输入要存款数和存款年限,年限只能是1 3 5:" << endl;
double d;
int f;
while((cin>>d>>f,cin.fail()) || (f != 1 && f != 3 && f != 5))
{
cerr << "金额或年份输入有错误 请重新输入" << endl;
cin.clear();
cin.sync();
}
Savings g(c1,d,f);
g.display();
cout<<"\n要提前取款请输入账号:";
string c2;
while(cin >> c2, c2 != c1)
cerr << "账号错误! 请重新输入" << endl;
cout<<"请输入已存款的年数:";
int h;
while(cin >> h, cin.fail()||( h<0 || h>5))
{
cerr << "错误的年份 请重新输入" << endl;
cin.clear();
cin.sync();
}
g.change(h);
system("PAUSE");
return 0;
}
结果:
日期之差为: 1083
2006年6月17日 2007年10月3日
#include <iostream>
using namespace std;
class Date{/*日期类*/
public :
int year,month,day;
void set(int m=1,int d=1,int y=1900)
{
year=y;
month=m;
day=d;
}
inline int operator-(const Date &b)/*‘-’操作符重载,用于计算两个日期之差*/
{
int days=0;
if (year==b.year&&month==b.month ) {
days=day-b.day;
days=days<0?-days:days;
}
else if (year==b.year) {
int ml,mh;
mh=month>b.month ?(ml=b.month,month):(ml=month,b.month) ;
for (int i=ml; i<mh; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days-=ml==month?day:b.day;
days+=mh==b.month?b.day :day;
}
else{
int yl,yh,ml,mh;
yh=year>b.year ?(yl=b.year,year):(yl=year,b.year) ;
for (int i=yl+1; i<yh; i++) {
if (i%4==0&&i%100||i%400==0)days+=366;
else days+=365;
}
ml=yl==year?month:b.month;
mh=yh==b.year?b.month:month;
for (; ml<=12; ml++) {
switch (ml) {
default:
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days-=yl==year?day:b.day;
for (--mh; mh>=1; mh--) {
switch (mh) {
default:
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:
if (year%4==0&&year%100||year%400==0) days+=29;
else days+=28;
break;
}
}
days+=yh==b.year?b.day:day;
}
return days;
}
inline bool operator>(const Date &a)/*'>'操作符重载,用于判断日期的先后*/
{
if (year==a.year &&month==a.month ) {
return day>a.day;
}
else if (year==a.year ) {
return month>a.month ;
}
else return year>a.year ;
}
inline Date operator+(int days)/*'+'操作符重载,用于计算days天之后的日期*/
{
for (; days>=0; --days) {
if (day==29&&month==2&&!(year%4==0&&year%100||year%400==0)) {
++month;
day=1;
}
else if (day==30&&month==2&&(year%4==0&&year%100||year%400==0)) {
++month;
day=1;
}
else if (day==31) {
switch (month) {
case 4:
case 6:
case 9:
case 11:month++;day=1;
}
}
else if (day==32) {
month++;
day=1;
if (month==13) {
month=1;
year++;
}
}
if (days) day++;
}
return *this;
}
inline Date operator-(int days)/*'-'操作符重载,用于计算days天之前的日期*/
{
for (; days>=0; --days) {
if (day==29&&month==2&&!(year%4==0&&year%100||year%400==0)) {
--month;
day=1;
} // end if
else if (day==30&&month==2&&(year%4==0&&year%100||year%400==0)) {
--month;
day=1;
} // end else-if
else if (day==31) {
switch (month) {
case 4:
case 6:
case 9:
case 11:month--;day=1;
}
} // end else-if
else if (day==32) {
month--;
day=1;
if (month==0) {
month=12;
year--;
}
}
if (days) day++;
}
return *this;
}
};
ostream& operator<<(ostream &a,Date &b)/*输出操作符重载,用于输出指定格式的日期*/
{
a<<b.year <<"年"<<b.month <<"月"<<b.day <<"日";
return a;
}
int main(void)/*调用实例*/
{
Date a,b;
a.set(8,21,2005);/*设置为2005-8-21*/
b.set(8,8,2008);/*设置为2008-8-8*/
int t=b-a;/*求两个日期之差*/
cout<<"日期之差为: "<<t<<endl;
a=a+300;/*计算2005-8-21 300天之后的日期*/
b=b-300;/*计算2008-8-8 300天之前的日期*/
cout<<a<<" "<<b<<endl;/*输出a和b日期*/
return 0;
}
-------------------------------------------------------------------
第二题:
#include<iostream>
#include<string>
using namespace std;
//-----------------------------------------------------------------
class Account{
protected:
string acntNumber;//账号
double money;//存款数
public:
Account(string acntNo,double Mon){acntNumber=acntNo;money=Mon;}
virtual void display() = 0;
};//------------------------------------------------------------------
class Savings :public Account{
int year;//存款年限
public:
Savings(string acntNo,double Mon,int Ye) :Account(acntNo,Mon),year(Ye){};
void display(){
switch(year){
case 1: cout<<money<<"元定期1年的本息为:"<<money*0.05*1+money<<endl;break;
case 3: cout<<money<<"元定期3年的本息为:"<<money*(1+0.08)*(1+0.08)*(1+0.08)<<endl;break;
case 5: cout<<money<<"元定期5年的本息为:"<<money*(1+0.1)*(1+0.1)*(1+0.1)*(1+0.1)*(1+0.1)<<endl;break;
default: cout<<"error\n";
}
}
void change(int n=0){//提前取款,利息按活期率1%计算
double m=money;
for(int i=1;i<=n;i++){
m=m*(1+0.01);
}
cout<<money<<"已存"<<n<<"年取款后的本息是:"<<m<<endl;
}
};//------------------------------------------------------------------
int main(){
Savings a("000001",2000,1);
Savings b("000002",4000,3);
Savings c("000003",8000,5);
a.display();
b.display();
c.display();
string c1;
cout<<"输入创建的账号:";
cin>>c1;
cout << "请输入要存款数和存款年限,年限只能是1 3 5:" << endl;
double d;
int f;
while((cin>>d>>f,cin.fail()) || (f != 1 && f != 3 && f != 5))
{
cerr << "金额或年份输入有错误 请重新输入" << endl;
cin.clear();
cin.sync();
}
Savings g(c1,d,f);
g.display();
cout<<"\n要提前取款请输入账号:";
string c2;
while(cin >> c2, c2 != c1)
cerr << "账号错误! 请重新输入" << endl;
cout<<"请输入已存款的年数:";
int h;
while(cin >> h, cin.fail()||( h<0 || h>5))
{
cerr << "错误的年份 请重新输入" << endl;
cin.clear();
cin.sync();
}
g.change(h);
system("PAUSE");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询