A 评分程序
答案:
#include<iostream>
#include<cstring>using namespace std;class Score{
private:string name;//记录学生姓名double s[4];//存储4次成绩,s[0]和s[1]存储2次随堂考试,s[2]存储期中考试,s[3]存储期末考试double total;//记录总评成绩char grade='B'; //记录对应的等级
public:void Input();void Evalauate();void Output();
};void Score::Input()
{string n;double t1 ,t2,t3,t4;cin>>n>>t1>>t2>>t3>>t4;name=n;s[0]=t1;s[1]=t2;s[2]=t3;s[3]=t4;total=t1*0.25+t2*0.25+t3*0.25+t4*0.5;
}void Score::Evalauate()
{if(total>=90)grade='A';else if(total>=80)grade='B';else if(total>=70)grade='C';else if(total>=60)grade='D';elsegrade='E';
}void Score::Output()
{if(s[0]>=0&&s[0]<=50&&s[1]>=0&&s[1]<=50&&s[2]>=0&&s[2]<=100&&s[3]>=0&&s[3]<=100)cout<<"name: "<<name<<", total: "<<total<<", grade: "<<grade;else{cout<<"error";}
}int main()
{Score *s1=new Score;s1->Input();s1->Evalauate();s1->Output();return 0;
}
重要知识点:
类的指针类型对象:
使用指针类型调用其成员。
B 日期类(选做)
答案:
#include<iostream>
#include<cstring>
using namespace std;int getDays(int m, int y)
{if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)){switch (m){case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;case 2:return 29;case 4:case 6:case 9:case 11:return 30;}}else{switch (m){case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;case 2:return 28;case 4:case 6:case 9:case 11:return 30;}}return 0;
}class Date
{
public:Date(int d = 0, int m = 0, int y = 0); //构造函数int get_day() const; // 返回dayint get_month() const; //返回monthint get_year() const; // 返回yearstatic void set_default(int, int, int); //设置default_datestatic int get_default_day(); //返回缺省daystatic int get_default_month(); //返回缺省monthstatic int get_default_year(); //返回缺省yearDate & add_year(int n); //加n年Date & add_month(int n); //加n月,考虑超过12月Date & add_day(int n); //加n天,考虑进位月和年,考虑闰年
private:int day, month, year;static Date default_date; //初始化为 1901年1月1日
};Date::Date(int d,int m,int y)
{day=d;month=m;year=y;
}int Date::get_day() const
{return day;
} int Date::get_month() const
{return month;
} int Date::get_year() const
{return year;
} Date Date::default_date(1, 1, 1901);void Date::set_default(int td, int tm, int ty)
{default_date=Date(td,tm,ty);
}int Date::get_default_day()
{return default_date.day;
}int Date::get_default_month()
{return default_date.month;
}int Date::get_default_year()
{return default_date.year;
}Date & Date::add_day(int n)
{day+=n;while(day>getDays(month,year))//注意使用while而非if,下同{ day-=getDays(month,year);add_month(1); }return *this;
}Date & Date::add_month(int n)
{month+=n;while(month>12){month-=12;year++;}add_day(0);//处理加“月”后“2月30日”之类的情况。下同return *this;
}Date & Date::add_year(int n)
{year+=n;add_day(0);return *this;
}int main()
{char type[110];int day,mon,year;int addday,addmon,addyear;while(cin>>type){if(strcmp(type,"Date") == 0){cin>>day>>mon>>year;Date mydate(day,mon,year);cin>>addday>>addmon>>addyear;mydate.add_day(addday).add_month(addmon).add_year(addyear);cout << mydate.get_day() << " " << mydate.get_month() << " " << mydate.get_year() << endl;}else if(strcmp(type,"defaultDate") == 0) {cout << Date::get_default_day() << " " << Date::get_default_month() << " " << Date::get_default_year() << endl;}else if(strcmp(type,"setdefaultDate") == 0){cin>>day>>mon>>year;Date::set_default(day,mon,year);cout << Date::get_default_day() << " " << Date::get_default_month() << " " << Date::get_default_year() << endl;}}return 0;
}
重要知识点:
吐槽一下,这题出的真逆天
基本上算是对之前知识点的大杂烩
·成员函数返回值为类时, return *this返回自身对象的使用。
·部分代码逻辑值得参考:
如add_day(0);
getDays函数的使用与实现;
经典易错闰年判断:(y % 4 = = 0 && y % 100 != 0) || (y % 400 == 0) 1800年不是闰年