BJFUOJ-C++程序设计-实验2-类与对象

A 评分程序



答案:

cpp 复制代码
#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';
	else
		grade='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 日期类(选做)



答案:

cpp 复制代码
#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; // 返回day
   int get_month() const; //返回month
   int get_year() const; // 返回year
   static void set_default(int, int, int); //设置default_date
   static int get_default_day(); //返回缺省day
   static int get_default_month(); //返回缺省month
   static int get_default_year(); //返回缺省year
   Date & 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年不是闰年

相关推荐
炸膛坦客5 小时前
单片机/C/C++八股:(二十六)IIC 专题(I²C)---- 上集
c语言·c++·单片机
旖-旎6 小时前
LeetCode 518:零钱兑换||(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题
灯澜忆梦6 小时前
GO_并发编程---定时器
开发语言·后端·golang
-银雾鸢尾-6 小时前
C#中的StringBuilder相关方法
开发语言·c#
Henry Zhu1236 小时前
C++中的特殊成员函数与智能指针
c++
-银雾鸢尾-6 小时前
C#中结构体与类的区别;抽象类与接口的区别
开发语言·c#
大模型码小白7 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
_wyt0019 小时前
多重背包问题详解
c++·背包dp
段一凡-华北理工大学9 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
Ljwuhe9 小时前
C++——多态
开发语言·c++