实验四 类与对象

A. 对象数组(类和对象)

题目描述

课堂上我们谈到类类型这个概念,比如第一题我们有学生类这个抽象的概念,成千上万个学生都具有同样的属性,但针对某个具体学生来说,他/她具有自己的鲜明个性,比如计算机专业的王海同学,信息工程学院的李慧同学,那么我们是定义一个该类的变量:Studentstudent; 假设该类包含姓名、学号、性别、所属学院、联系电话等;在程序运行过程,把变量student赋予不同值就可以让它表示是王海还是李慧,尝试定义一个学生数组(比如四个元素大小,请思考此时四个对象是如何初始化的呢?调用的是那个构造方法?),然后输入四个不同学生各项属性给数组里学生初始化(最好定义一个输入函数),然后输出该学生对象数组的各对象的姓名。

输入

输入数组元素的大小

依次每行输入每个对象的各项属性值

输出

每行输出一个学生类对象的姓名

输入样例

3
tom 2013333333 男 计算机学院 13766666666
Marry 2012222222 女 信息工程学院 13555555555
John  2014444444 男 管理学院 13888888888

输出样例

tom
Marry
John

参考代码

#include<iostream>
#include<string>
using namespace std;
class Student
{
	public:
	void set()
	{
		cin>>name>>no>>sex>>major>>number;
	}
	void Print()
	{
		cout<<name<<endl;
	}
	
	private:
	string name;
	string no;
	string sex;
	string major;
	string number;
		
};
int main()
{
	
	int n;
	cin>>n;
	Student *p=new Student[n];
	for(int i=0;i<n;i++)
	{
		p[i].set();	
	}
	for(int i=0;i<n;i++) p[i].Print();
	delete []p;
}

B. 学生类定义(类和对象)

题目描述

面向对象程序设计的中心就是把客观事物抽象为程序世界里一段段代码,校园里的主体是学生,泛泛的学生包含很多属性,比如姓名、学号、所在学院、专业、性别、住址、联系电话。。。。。。等等,有这些属性,需要操纵它们的动作,比如读取姓名、设置姓名、读取学号、设置学号。。。。。。等等,这就是我们课堂说的属性和方法,对于属性和方法,我们又有访问控制方式限制,标示为public、private、protected等,根据以上的信息,请给出一个完整的学生类定义:Student,并测试输出n个该类对象的各项属性值。

输入

第一行表示要输入n个对象

后续各行输入不同对象的各属性值,每个对象一行。

输出

输出不同对象的各自属性

每个对象占一行

输入样例

2
WangHai 2014150112 CSSE ComputerScience male South215 13760222222
LiBin 2013151292 CSSE SoftwareEngineering female South318 13677777777

输出样例

WangHai 2014150112 CSSE ComputerScience male South215 13760222222
LiBin 2013151292 CSSE SoftwareEngineering female South318 13677777777

参考代码

#include<iostream>
#include<string>
using namespace std;
class Student
{
	public:
	void set()
	{
		cin>>name>>no>>major1>>major2>>sex>>address>>number;
	}
	void Print()
	{
		cout<<name<<" "<<no<<" "<<major1<<" "<<major2<<" "<<sex<<" "<<address<<" "<<number<<endl;
	}
	
	private:
	string name;
	string no;
	string major1;
	string major2;
	string sex;
	string address;
	string number;
		
};
int main()
{
		
	int n;
	cin>>n;
	Student *p=new Student[n];
	for(int i=0;i<n;i++)
	{
		p[i].set();	
	}
	for(int i=0;i<n;i++) p[i].Print();
	delete []p;
	
}

C. 五子棋简单实现(类和对象)

题目描述

五子棋是一种两人对弈的纯策略型棋类游戏,双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成5子连线者获胜。假设棋盘为十五路(15×15)棋盘,输入o表示白子落子,u表示黑子落子。

用面向对象的思想实现五子棋游戏包含多个类。本题练习类和对象,将规则类和棋盘类合并,只定义棋盘类,实现五子棋的落子功能,不接受悔棋。

棋盘类:

属性:15*15棋盘。
方法:初始棋盘为空。
接受落子,判定是否合法。
根据落子,修改棋盘状态。
根据落子,判定是否五子连线。
输出棋盘状态。

可根据题目,增加其它方法。不能增加属性。
注1:判定是否合法规则为:①当落子位置在棋盘外时视为非法,②当落子位置中已有棋子时视为非法。除以上两种情况外,其他情况视为合法。

注2:五子连线判定在每次落子后进行,如果得出黑子或白子胜,则后面的落子操作不进行(但OJ要求完成读入)。

注3:落子点有可能非法,输入数据保证了黑、白子间隔落子,不用考虑黑子、白子落子顺序问题。即当一方落子,如果落子合法,输入数据会立即移交给另一方落子; 如果落子非法,输入数据会使得这一方继续落子,当出现落子合法的情况后,输入数据会立即移交给另一方落子。

注4:输入数据保证最终的棋盘至少有一个棋子,不会出现整个棋盘无棋子的情况。

输入

第1行:测试次数t ( 1 <= t <= 15)

每组测试数据表示一次五子棋游戏,格式为:

每组中的第1行:落子次数n (1 <= n <= 300)

每组中的第2行到第n + 1行:落子,o或u, x, y,分别代表落子方和落子坐标(落子坐标与屏幕坐标方向一致, x, y为整数,合法坐标为(1,1)到(15,15))

输出

对每组测试数据,输出棋盘最终状态(#表示黑子,@表示白子),

输出五子棋游戏结果:

白子胜、黑子胜、白子继续、黑子继续。

各组测试数据的输出以空行分隔。

输入样例

2
11
u 8 8
o 7 9
u 9 9
o 10 10
u 7 9
u 8 10
o 11 11
u 8 9
o 8 7
u 8 11
o 9 8
11
u 8 8
o 10 6
u 8 9
o 8 7
u 10 8
o 11 8
u 8 11
o 11 7
u 8 12
o 10 7
u 8 10

输出样例

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 @ 0 0 0 0 0 0 
0 0 0 0 0 0 @ # # # # 0 0 0 0 
0 0 0 0 0 0 0 @ # 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 @ 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 @ 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
黑子继续

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 @ # # # # # 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 @ @ # 0 0 0 0 0 0 0 
0 0 0 0 0 0 @ @ 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
黑子胜

PS:这题真的有点麻烦,要考虑的条件很多。。。。

参考代码

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
class CBoard {
private:
    char board[16][16];
public:
    CBoard() {
        for (int i = 1; i <= 15; i++)
            for (int j = 1; j <= 15; j++)
                board[i][j] = '0';
    }
    int Getche(char type, int i, int j) { //收到落子后输出结果 1→合法,2→不合法,3→黑子胜,4→白子胜
        int flag = 1;
        if (i <= 15 && i >= 1 && j <= 15 && j >= 1 && board[i][j] == '0') {
            flag = Update(type, i, j);
        }
        else {
            flag = 2;
        }
        return flag;
    }
    int Update(char type, int i, int j) { //更新完数据后,输出结果 1→合法,2→不合法,3→黑子胜,4→白子胜
        if (type == 'u') board[i][j] = '#';
        else board[i][j] = '@';
        int flag = Judge(type, i, j);
        return flag;
    }
    int Judge(char type, int i, int j) { //判断输出结果 1→合法,2→不合法,3→黑子胜,4→白子胜
        char chess = (type == 'u' ? '#' : '@');
        int flag = 1;
        int tmpi = i, tmpj = j;
        int count = 0;
        //从上往下
        while (tmpi > 0 && board[tmpi][tmpj] == chess) {
            tmpi--;
            count++;
        }
        tmpi = i + 1;
        while (tmpi < 16 && board[tmpi][tmpj] == chess) {
            tmpi++;
            count++; 
        }
        if (count >= 5) {
            flag = (chess == '#' ? 3 : 4);
            goto end;
        }
        //横的
        tmpi = i;
        count = 0;
        while (tmpj > 0 && board[tmpi][tmpj] == chess) {
            tmpj--;
            count++;
        }
        tmpj = j + 1;
        while (tmpj < 16 && board[tmpi][tmpj] == chess) {
            tmpj++;
            count++;
        }
        if (count >= 5) {
            flag = (chess == '#' ? 3 : 4);
            goto end;
        }

        tmpj = j;
        count = 0;
        while (tmpi > 0 && tmpj > 0 && board[tmpi][tmpj] == chess) {
            tmpi--; tmpj--;
            count++;
        }
        tmpi = i + 1;
        tmpj = j + 1;
        while (tmpi < 16 && tmpj < 16 && board[tmpi][tmpj] == chess) {
            tmpi++; tmpj++;
            count++;
        }
        if (count >= 5) {
            flag = (chess == '#' ? 3 : 4);
            goto end;
        }

        tmpj = j; tmpi = i;
        count = 0;
        while (tmpi < 16 && tmpj > 0 && board[tmpi][tmpj] == chess) {
            tmpi++; tmpj--;
            count++;
        }
        tmpi = i - 1;
        tmpj = j + 1;
        while (tmpi > 0 && tmpj < 16 && board[tmpi][tmpj] == chess) {
            tmpi--;
            tmpj++;
            count++;
        }
        if (count >= 5) {
            flag = (chess == '#' ? 3 : 4);
            goto end;
        }
    end:
        return flag;
    }
    void Print() {
        for (int i = 1; i <= 15; i++) {
            for (int j = 1; j <= 15; j++) {
                cout << board[i][j] << ' ';
            }
            cout << endl;
        }
    }
};

int main() {
    int n;
    cin >> n;
    for (int n_ = 0; n_ < n; n_++){
        CBoard board;
        int t, i, j, flag = 1;
        char type;
        cin >> t;
        for (int z = 0; z < t; z++) {          
			cin >> type >> i >> j;
            if (flag == 1 || flag == 2)
                flag = board.Getche(type, i, j);

        }
        if (n_ != 0) cout << endl;
        board.Print();
        if (flag == 3) cout << "黑子胜" << endl;
        else if (flag == 4) cout << "白子胜" << endl;
		else {
			if (flag == 1) {
				if (type == 'u') cout << "白子继续" << endl;
				else cout << "黑子继续" << endl; 
			}
			else {
				if (type == 'o') cout << "白子继续" << endl;
				else cout << "黑子继续" << endl; 
			}
		} 
    }
    return 0;
}

D. 存折类定义(类与对象)

题目描述

定义一个存折类CAccount,存折类具有帐号(account, long)、姓名(name,char[10])、余额(balance,float)等数据成员,可以实现存款(deposit,操作成功提示“saving ok!”)、取款(withdraw,操作成功提示“withdraw ok!”)和查询余额(check)的操作,取款金额必须在余额范围内,否则提示“sorry! over limit!”。

编写主函数,建立这个类的对象并测试,输入账号、姓名、余额后,按照查询余额、存款、查询余额、取款、查询余额的顺序调用类方法并输出。

输入

第一个存折的账号、姓名、余额

存款金额

取款金额

第二个存折的账号、姓名、余额

存款金额

取款金额

输出

第一个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

第二个存折的账户余额

存款操作结果

账户余额

取款操作结果

账户余额

输入样例

9111 Tom 1000
500
1000
92220 John 500
500
1500

输出样例

Tom's balance is 1000
saving ok!
Tom's balance is 1500
withdraw ok!
Tom's balance is 500
John's balance is 500
saving ok!
John's balance is 1000
sorry! over limit!
John's balance is 1000

参考代码

#include<iostream>
#include<string>
using namespace std;
class CAccount
{
	public:
	void set()
	{
		cin>>account>>name>>balance>>cunkuan>>qukuan;
	}
	void check()//输出查询余额 
	{
		cout<<name<<"'s balance is "<<balance<<endl;
	}
	void deposit()
	{
		balance+=cunkuan;
		cout<<"saving ok!"<<endl;
	}
	void withdraw()
	{
		if(qukuan>balance) cout<<"sorry! over limit!"<<endl;
		else
		{
			balance-=qukuan;
			cout<<"withdraw ok!"<<endl;
		 } 
	} 
	private:
		long account;
		string name;
		float balance;
		float cunkuan;
		float qukuan;
	
		
};
int main()
{
	CAccount p[5];
	for(int i=0;i<2;i++)
	{
		p[i].set();	
	}
	for(int i=0;i<2;i++)
	{
		p[i].check();
		p[i].deposit() ;
		p[i].check() ;
		p[i].withdraw() ;
		p[i].check() ;
	} 
	
}

E. 最胖的加菲(类与对象+数组)

题目描述

有一群猫猫,每只猫都有自己的名称和体重。

用类来描述猫,名称和体重都是私有属性,要求加入属性的get方法。其他函数根据需要自己定义

创建一个动态的猫对象数组,存储各只猫的名称和体重

根据猫的体重对数组做升序排序,并输出排序后每只猫的名称

题目涉及的数值均用整数处理

输入

第一行输入n表示有n只猫

第二行输入一只猫的名称和体重

依次输入n行

输出

输出一行,输出排序后的猫的名称

输入样例

4
chocolate 1500
water 400
cheese 3000
vegetable 200

输出样例

vegetable water chocolate cheese

参考代码

#include<iostream>
#include<string>
using namespace std;
class Cat
{
	public:
	void set()
	{
		cin>>name>>weight;
	}
	int get_wei()//得到体重 ,因为体重是私有属性,必须用get来排序 
	{
		return weight;
	}
    void get_nam()
    {
    	cout<<name;
	}
	private:
		string name;
		int weight;
	
		
};
void Sort(Cat &a,Cat &b)//用引用即可排序
	{
		Cat temp; 
		if(a.get_wei() >b.get_wei()) //比较的就是体重 
		{
			temp=a;
			a=b;
			b=temp;
		}
	} 
int main()
{
	int n;
	cin>>n;
	Cat *p=new Cat[n];
	for(int i=0;i<n;i++)
	{
		p[i].set() ;//存入数据 
	}
	for(int i=1;i<n;i++)//冒泡排序 
	  for(int j=0;j<n-i;j++)
	  {
	  	Sort(p[j],p[j+1]);
	  } 
	for(int i=0;i<n;i++) 
	  {
	  	p[i].get_nam();
	  	if(i<n-1) cout<<" ";
	  } 
	delete []p;
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值