C++ 实验七

编程题1

题目描述
对本章示范题的用于管理商店商品的实现程序进行完善:完成Wardrobe立柜类的具体定义与使用,并添加“帽子仓库类”以及“立柜仓库类”的定义及使用,以使程序能够对商店的这三种商品(衬衣、帽子、立柜)进行简单的管理与应用。
要对商品实现的操作有:商品的进库(增加某类商品及其库存量),商品的出库(减少某类商品及其库存量),以及某类商品总价格的计算。

输入
60 Tianjin Cotton
80 Beijing Wool
40 Suzhou Cotton M
30 Wuxi Wool S
160 Guangzhou Pine Yellow
200 Suzhou Oak Brown

输出
5 * shirt data in: price/place/material =>
3 * shirt data in: price/place/material =>
60 Tianjin Cotton
60 Tianjin Cotton
60 Tianjin Cotton
60 Tianjin Cotton
60 Tianjin Cotton
80 Beijing Wool
80 Beijing Wool
80 Beijing Wool
shiSto.TotalPrice()=540
60 Tianjin Cotton
60 Tianjin Cotton
60 Tianjin Cotton
60 Tianjin Cotton
shiSto.TotalPrice()=240
5 * Cap data in: price/place/material/style =>
3 * Cap data in: price/place/material/style =>
40 Suzhou Cotton M
40 Suzhou Cotton M
40 Suzhou Cotton M
40 Suzhou Cotton M
40 Suzhou Cotton M
30 Wuxi Wool S
30 Wuxi Wool S
30 Wuxi Wool S
capSto.TotalPrice()=290
40 Suzhou Cotton M
40 Suzhou Cotton M
40 Suzhou Cotton M
40 Suzhou Cotton M
capSto.TotalPrice()=160
5 * Wardrobe data in: price/place/material/color =>
3 * Wardrobe data in: price/place/material/color =>
160 Guangzhou Pine Yellow
160 Guangzhou Pine Yellow
160 Guangzhou Pine Yellow
160 Guangzhou Pine Yellow
160 Guangzhou Pine Yellow
200 Suzhou Oak Brown
200 Suzhou Oak Brown
200 Suzhou Oak Brown
WarSto.TotalPrice()=1400
160 Guangzhou Pine Yellow
160 Guangzhou Pine Yellow
160 Guangzhou Pine Yellow
160 Guangzhou Pine Yellow
WarSto.TotalPrice()=640

#include <iostream>
#include <cstring>
using namespace std;
class Base{
	double price;
	char place[20];
public:
	Base(){
		price = 0;
		strcpy(place,"");
	}
	Base(double pr,char *pl){
		price = pr;
		strcpy(place,pl);
	}
	double GetPrice(){
		return price;
	}
	char *GetPlace(){
		return place;
	}
	void Input(){
		cin>>price>>place;
	}
	void Display(){
		cout<<price<<" "<<place;
	}
}; 
class Shirt:public Base{
	public:
	char material[20];
	Shirt():Base(){
		strcpy(material,"");
	}
	Shirt(double pr,char *pl,char *mat):Base(pr,pl){
		strcpy(material,mat);
	}
	void Dispaly(){
		Base::Display();
		cout<<" "<<material;
	}
	void Input(){
		Base::Input();
		cin>>material;
	}
	char *GetMaterial(){
		return material;
	}
};
class Cap:public Shirt{
	char style;
public:
	Cap():Shirt(){
		style = ' ';
	}
	Cap(double pr,char *pl,char *mat,char style1):Shirt(pr,pl,mat){
		style = style1;
	}
	void Dispaly(){
		Base::Display();
		cout<<" "<<material<<" "<<style;
	}
	void Input(){
		Base::Input();
		cin>>material>>style;
	}
	char Getstyle(){
		return style;
	}
};
class Wardrobe:public Base{
	char material[20];
	char color[20];
public:
	Wardrobe():Base(){
		strcpy(material,"");
		strcpy(material,"");
	}
	Wardrobe(double pr,char *pl,char *mat,char *clo):Base(pr,pl){
		strcpy(material,mat);
		strcpy(color,clo);
	}
	void Dispaly(){
		Base::Display();
		cout<<" "<<material<<" "<<color;
	}
	void Input(){
		Base::Input();
		cin>>material>>color;
	}
	char *GetMaterial(){
		return material;
	}
	char *GetColor(){
		return color;
	}
};
const int MAXSIZE = 100;
class ShirtStorage{
	int count;
	Shirt shelf[MAXSIZE];
public:
	ShirtStorage(){
		count = 0;
	}
	void Display(){	
		for(int i = 0;i<count;i++){
			shelf[i].Dispaly();
			cout<<endl;
		}
	}
	void InSomething(int add_cnt){
		shelf[count++].Input();
		for(int i = 1;i<add_cnt;i++){
			shelf[count] = shelf[count-1];
			count++;
		}
	}
	void OutSomething(int del_cnt){
		count -= del_cnt;
	}
	double TotalPrice(){
		double total = 0;
		for(int i = 0;i<count;i++)
		total += shelf[i].GetPrice();
		return total;
	}
};
class CapStorage{
	int count;
	Cap shelf[MAXSIZE];
public:
	CapStorage(){
		count = 0;
	}
	void Display(){
		for(int i = 0;i<count;i++){
			shelf[i].Dispaly();
			cout<<endl;
		}
	}
	void InSomething(int add_cnt){
		shelf[count++].Input();
		for(int i = 1;i<add_cnt;i++){
			shelf[count] = shelf[count-1];
			count++;
		}
	}
	void OutSomething(int del_cnt){
		count -= del_cnt;
	}
	double TotalPrice(){
		double total = 0;
		for(int i = 0;i<count;i++)
		total += shelf[i].GetPrice();
		return total;
	}
};
class WardrobeStorage{
	int count;
	Wardrobe shelf[MAXSIZE];
public:
	WardrobeStorage(){
		count = 0;
	}
	void Display(){
		for(int i = 0;i<count;i++){
			shelf[i].Dispaly();
			cout<<endl;
		}
	}
	void InSomething(int add_cnt){
		shelf[count++].Input();
		for(int i = 1;i<add_cnt;i++){
			shelf[count] = shelf[count-1];
			count++;
		}
	}
	void OutSomething(int del_cnt){
		count -= del_cnt;
	}
	double TotalPrice(){
		double total = 0;
		for(int i = 0;i<count;i++)
		total += shelf[i].GetPrice();
		return total;
	}
};
int main(){
	ShirtStorage st;
	st.InSomething(5);
	st.InSomething(3);
	cout<<"5 * shirt data in: price/place/material =>"<<endl;
	cout<<"3 * shirt data in: price/place/material =>"<<endl;
	st.Display();
	cout<<"shiSto.TotalPrice()="<<st.TotalPrice()<<endl;
	st.OutSomething(4);
	st.Display();
	cout<<"shiSto.TotalPrice()="<<st.TotalPrice()<<endl;
	CapStorage cap;
	cap.InSomething(5);
	cap.InSomething(3);
	cout<<"5 * Cap data in: price/place/material/style =>"<<endl;
	cout<<"3 * Cap data in: price/place/material/style => "<<endl;
	cap.Display();
	cout<<"capSto.TotalPrice()="<<cap.TotalPrice()<<endl;
	cap.OutSomething(4);
	cap.Display();
	cout<<"capSto.TotalPrice()="<<cap.TotalPrice()<<endl;
	WardrobeStorage war;
	war.InSomething(5);
	war.InSomething(3);
	cout<<"5 * Wardrobe data in: price/place/material/color =>"<<endl;
	cout<<"3 * Wardrobe data in: price/place/material/color =>"<<endl;
	war.Display();
	cout<<"WarSto.TotalPrice()="<<war.TotalPrice()<<endl;
	war.OutSomething(4);
	war.Display();
	cout<<"WarSto.TotalPrice()="<<war.TotalPrice()<<endl;
	return 0;
}

编程题2

题目描述
利用继承性与派生类来管理学生教师档案:由Person(人员)类出发(作为基类),派生出Student(学生)及Teacher(教师)类;而后又由Student(学生)类出发(作为基类),派生出GraduateStudent(研究生)类。可假定这几个类各自具有的数据成员为:
Person(人员)类: 姓名、性别、年龄;
Student(学生)类: 姓名、性别、年龄、学号、系别;
Teacher(教师)类: 姓名、性别、年龄、职称、担任课程;
GraduateStudent(研究生)类: 姓名、性别、年龄、学号、系别、导师。
为简化起见,每个类可只设立构造函数以及显示类对象数据的成员函数Print。而后编制简单的主函数,说明上述有关的类对象,并对其类成员函数进行简单使用(调用)

样例输出
== per1.Display() => name,age,sex
sun 42 M
== stu1.Display() => name,age,sex,Reg_Number,department
guo 22 F 1001
== teach1.Display() => name,age,sex,course,post
fang 38 M english professor
== gStu.Display() => name,age,sex,Reg_Number,department,advisor
wu 25 M 1021 comp wei
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
class Person{
public:
	string name;
	char sex;
	int age;
	Person(string name1,int age1,char sex1){
		name = name1;
		age = age1;
		sex = sex1;
	}
	void Print(){
		cout<<name<<" "<<age<<" "<<sex<<endl;
	}
};
class Student:public Person{
public:
	long num;
	string ma;
	Student(string name1,int age1,char sex1,long num1,string ma1)
	:Person(name1,age1,sex1){
		num = num1;
		ma = ma1;
	}
	void Print1(){
		cout<<name<<" "<<age<<" "<<sex<<" "<<num<<" "<<ma<<endl;
	}	
};
class Teacher:public Person{
public:
	string pro;
	string cls;
	Teacher(string name1,int age1,char sex1,string cls1,string pro1)
	:Person(name1,age1,sex1){
		cls = cls1;
		pro = pro1;
	}
	void Print2(){
		cout<<name<<" "<<age<<" "<<sex<<" "<<cls<<" "<<pro<<endl;
	}
};
class GraduateStudent:public Student{
public:
	string tea;
	GraduateStudent(string name1,int age1,char sex1,long num1,string ma1,string tea1)
	:Student(name1,age1,sex1,num1,ma1){
		tea = tea1;
	}
	void Print3(){
		cout<<name<<" "<<age<<" "<<sex<<" "<<num<<" "<<ma<<" "<<tea<<endl;
	}
};
int main(){
	Person p("sun",42,'M');
	Student s("guo",22,'F',1001,"comp");
	Teacher t("fang",38,'M',"english","professor");
	GraduateStudent g("wu",25,'M',1021,"comp","wei");
	cout<<"== per1.Display() => name,age,sex"<<endl;
	p.Print();
	cout<<"== stu1.Display() => name,age,sex,Reg_Number,department"<<endl;
	s.Print1();
	cout<<"== teach1.Display() => name,age,sex,course,post"<<endl;
	t.Print2();
	cout<<"== gStu.Display() => name,age,sex,Reg_Number,department,advisor"<<endl;
	g.Print3();
	return 0;
}

编程题3

题目描述
自定义一个日期时间类DateTimeType,它含有类DateType与类TimeType的类对象作为其数据成员,并具有所列的其他几个成员函数。而后编制主函数,说明DateTimeType的类对象,并对其成员函数以及二对象成员所属类的公有成员函数进行使用。
class DateTimeType { //自定义的日期时间类 DateTimeType
DateType date; //类 DateType 的类对象 date 作为其数据成员
TimeType time; //类 TimeType 的类对象 time 作为其另一个数据成员
public:
DateTimeType(int y0=1, int m0=1, int d0=1, int hr0=0, int mi0=0, int se0=0);
//构造函数,设定 DateTimeType 类对象的日期时间,并为各参数设置了默认值
DateType& GetDate(){ return date; } //返回本类的私有数据对象 data
TimeType& GetTime(){ return time; } //返回本类的私有数据对象 time
void IncrementSecond(int s); //增加若干秒,注意“进位”问题
void PrintDateTime(); //屏幕输出日期时间对象的有关数据
};
注意,每一个DateTimeType类对象中总包含有一个DateType类对象(对象成员)以及一个TimeType类对象(对象成员),编制与实现本程序时,也必须包含DateType与TimeType自定义类(类型)。
之所以设置了公有的类成员函数GetDate与GetTime,是为类外如主函数处使用该类的私有数据成员date与time提供方便(否则的话,类外无法直接访问该类的私有数据成员)。另外,两成员函数返回的都为引用,为的是可将返回对象当作一个独立变量来使用(如可以继续作左值等)。例如,假设编制了如下形式的主函数:
void main() {
DateTimeType dttm1(1999,12,31,23,59,59), dttm2;
(dttm1.GetDate()).PrintDate(); //调用对象成员所属类的公有成员函数
cout<<endl;
dttm1.PrintDateTime(); //调用本派生类的成员函数 PrintDateTime
dttm2.PrintDateTime();
dttm1.IncrementSecond(30) ; //调用本派生类成员函数
dttm1.PrintDateTime();
}

样例输出
1999-12-31
1999-12-31 23:59:59
1-1-1 0:0:0
2000-1-1 0:0:29
#include <iostream>
using namespace std;
bool func(int n){
	if(n%400 == 0||n%4 == 0&&n%100!=0)
	return true;
	else
	return false;
}
class TimeType{
public:
	int s;
	int mi;
	int h;
	TimeType(int h1,int m1,int s1){
		h = h1;
		mi = m1;
		s = s1;	
	}
	void PrintTime(){
		cout<<h<<"-"<<mi<<"-"<<s<<endl;
	}
};
class DateType{
public:
	int y;
	int m;
	int d;
	DateType(int y1,int m1,int d1){
		y = y1;
		m = m1;
		d = d1;
	}
	void PrintDate(){
		cout<<y<<"-"<<m<<"-"<<d<<endl;
	}
};
class DateTimeType {  //自定义的日期时间类 DateTimeType 
DateType date; //类 DateType 的类对象 date 作为其数据成员 
TimeType time; //类 TimeType 的类对象 time 作为其另一个数据成员 
public: 
DateTimeType(int y0=1, int m0=1, int d0=1, int hr0=0, int mi0=0, int se0=0); 
//构造函数,设定 DateTimeType 类对象的日期时间,并为各参数设置了默认值 
DateType& GetDate(){ return date; } //返回本类的私有数据对象 data 
TimeType& GetTime(){ return time; } //返回本类的私有数据对象 time 
void IncrementSecond(int ss);  //增加若干秒,注意“进位”问题 
void PrintDateTime(); //屏幕输出日期时间对象的有关数据 
}; 
DateTimeType::DateTimeType(int y0,int m0,int d0,int hr0,int mi0,int se0)
:date(y0,m0,d0),time(hr0,mi0,se0){
};
void DateTimeType::IncrementSecond(int ss){
	int p1 = ss+time.s;
	int p2 = time.mi;
	int p3 = time.h;
	int p4 = date.d;
	int p5 = date.m;
	int p6 = date.y;
	if(p1>=60){
		p1 = p1-60;
		p2++;
	}
	if(p2>=60){
		p2 = 0;
		p3++;
	}
	if(p3>=24){
		p3 = 0;
		p4++;
	}
	if(p4 == 32&&p5 == 12){
		p4 = 1;
		p5 = 1;
		p6++;
	}
	if(func(p6)){
		if(p5 == 30&&p4 == 2){
			p4++;
			p5 = 1;
		}
	}
	else{
		if(p5 == 29&&p4 == 2)
		p4++;
		p5 = 1;
	}
	if((p4 == 1||p4 == 3||p4 == 5||p4 == 7||p4 == 8||p4 == 10)&&p5 == 32){
		p4++;
		p5 = 1;
	}
	if((p4 == 4||p4 == 6||p4 == 9||p4 == 11)&&p5 == 31){
		p4++;
		p5 = 1;
	}
	time.s = p1;
	time.mi = p2;
	time.h = p3;
	date.d = p4;
	date.m = p5;
	date.y = p6;
}
void DateTimeType::PrintDateTime(){
	cout<<date.y<<"-"<<date.m<<"-"<<date.d<<" "<<time.h<<":"<<time.mi<<":"<<time.s<<endl;;
};
int main() { 
DateTimeType dttm1(1999,12,31,23,59,59), dttm2; 
(dttm1.GetDate()).PrintDate(); //调用对象成员所属类的公有成员函数 
dttm1.PrintDateTime(); //调用本派生类的成员函数 PrintDateTime 
dttm2.PrintDateTime(); 
dttm1.IncrementSecond(30) ; //调用本派生类成员函数 
dttm1.PrintDateTime(); 
} 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值