c++实验

个人博客:www.huangrd.top,欢迎访问学习更多知识。

西安建筑科技大学c++实验代码

一、数组

编写一个程序,将数字从键盘读入int[ ]类型的数组中。你可以假设数组中最多有50个元素。你的程序最多允许输入50个数字。输出为一个两列的列表。第一列列出了不同的数组元素;第二列是每个元素的出现次数。这个列表应该按照第一列元素从大到小的顺序排序。

对于数组

-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12

输出应该如下所示:

N Count

4 2

3 3

2 2

1 4

-1 1

-12 4

要求写程序注释。

#include <iostream>

using namespace std;

void sortArray(int left,int right,int a[])//快速排序
{
	if (left >= right) //递归边界条件
	{
		return;
	}
	int i, j, base, temp;
	i = left;
	j = right;
	base = a[left];
	while (i < j) 
	{
		while (a[j] <= base && i < j) 
		{
			j--;
		}
		while (a[i] >= base && i < j) 
		{
			i++;
		}
		if (i < j)
		{
			temp = a[i];
			a[i] = a[j];
			a[j] = temp;
		}
	}
	a[left] = a[i];
	a[i] = base;
	sortArray(left, i - 1, a);
	sortArray(i + 1, right, a);
}
void compareArray(int a[], int len)//进行比较
{
	int slowP = 0;//定义快慢指针
	int fastP = 1;
	int j = 1;

	cout << "N  Count" << endl;

	while (fastP <= len)
	{
		if (a[slowP] == a[fastP])//若a[slowP] == a[fastP](快指针等于慢指针),用j来计数,使其++,再利用两个指针++来判断下一个
		{
			j++;

			slowP++;
			fastP++;
		}
		else//若两个指针的值不相同,则输出慢指针,此时慢指针的值真是重复出现的值,并对j的值进行初始化。
		{
			cout << a[slowP] << "  " << j << endl;
			slowP++;
			fastP++;
			j = 1;
		}
	}
}
int main()
{
	int a[50] = { 0 };
	int num = 1;
	int i = 0;
	while(i<16)
	{
		cin >> num;
		a[i] = num;
		i++;
	}
	sortArray(0,i-1,a);//利用冒泡排序排序数组
	compareArray(a, i);//根据题目要求,调用函数输出
	system("pause");
	return 0;
} 

二、面向对象

一、 实验内容

1、创建一个名为Pizza的类,用它来存储单个比萨饼的信息。这个类应包含以下内容:

几个私有变量,分别用来存储比萨的尺寸(小,中,大)、奶酪配料数量、意大利腊香肠配料数量和火腿配料数量。

一个(或多个)构造函数,用于设置所有变量的数值。

几个公有函数,用于获取和设置变量的数值。

一个名为calcCost () 的公有函数,用于返回一个表示比萨饼价格的double值。

比萨饼价格由以下内容决定:

小:10美元+每份配料2美元

中:12美元+每份配料2美元

大:14美元+每份配料2美元

编写测试代码来生成几个比萨饼信息并输出对这些披萨的价格。例如,一个大比萨饼外加一份奶酪配料、一份意大利腊香肠配料及两份火腿配料的总价格应为22美元

要求写程序注释。

2、对程序1进行扩充,需要你创建一个PizzaOrder类,这个类允许一个订单里最多存储三个披萨饼的信息。每个存储进来的披萨饼信息都应该是一个披萨饼对象。除了合适的变量和构造函数外,还要有以下的函数:

public int getNumPizzas()—返回订单中的披萨的个数。

public Pizza* getPizza1()—返回订单中的第一个披萨,如果pizza1未设置就返回null。

public Pizza* getPizza2()— 返回订单中第二个披萨,如果pizza2未设置就返回null。

public Pizza* getPizza3()— 返回订单中第三个披萨,如果pizza3未设置就返回null。

对于带有另一个PizzaOrder对象并为其比萨创建一个独立的复制构造函数。

使用旧订单作为新订单的起始点可能很有用。

编写一个main函数来测试这个类。如果订单中有两个或者三个披萨饼,则会用到setPizza2和setPizza3函数。下面的代码示例对函数进行了说明。注意代码不完整。作为程序的一部分,你必须补充完整。例如:

Pizza pizza1 = // 这一行代码创建一个大披萨饼,一份奶酪,一份火腿

Pizza pizza2 = // 这一行代码创建一个中披萨饼,两份奶酪,两份意大利腊香肠。

PizzaOrder order1 = // 这一行代码创建一个订单

order1.setNumPizzas(2); // 订单中有两个披萨

order1.setPizza1(pizza1); // 设置第一个披萨

order1.setPizza2(pizza2); // 设置第二个披萨

double total = order1.calcTotal(); // 计算订单的总价格:18+20 = 38

要求写程序注释。

#include <iostream>
#include <string>
using namespace std;
class Pizza
{
public:
	Pizza() { };//默认构造函数
	Pizza(int si, int cheese, int sauage, int han) {//带参的重载构造函数
		this->size = si;
		this->cheeseNum = cheese;
		this->sausageNum = sauage;
		this->hanNum = han;
	}
	void setPizza(int si, int cheese, int sauage, int han) {//对披萨进行赋值
		this->size = si;
		this->cheeseNum = cheese;
		this->sausageNum = sauage;
		this->hanNum = han;
	}
	void getPizza() {//获取披萨的信息
		string name1 = "big";
		string name2 = "middle";
		string name3 = "small";
		if (size == 1) cout << "Pizza的尺寸是" << name1 << endl;
		else if (size == 2) cout << "Pizza的尺寸是" << name2 << endl;
		else if (size == 3) cout << "Pizza的尺寸是" << name3 << endl;
		else;
		cout << "奶酪的数量是" << cheeseNum << endl;
		cout << "香肠的数量是" << sausageNum << endl;
		cout << "火腿的数量是" << hanNum << endl;
	}
	double calcCost() {//计算披萨的费用
		switch (size) {//利用switch语句来通过披萨的尺寸进行分类
		case 0:return 0;//表示无此披萨
		case 1:return 14 + (cheeseNum + sausageNum + hanNum) * 2;
		case 2:return 12 + (cheeseNum + sausageNum + hanNum) * 2;
		case 3:return 10 + (cheeseNum + sausageNum + hanNum) * 2;
		}
	}
private:
	int size;//尺寸,1-大,2-中,3-小,0-无
	int cheeseNum;//奶酪数量
	int sausageNum;//香肠数量
	int hanNum;//火腿数量
};
class PizzaOrder
{
public:
	PizzaOrder() {
		p[1] = p[2] = p[3] = NULL;
		pi[0].setPizza(0, 0, 0, 0);
		pi[1].setPizza(0, 0, 0, 0);
		pi[2].setPizza(0, 0, 0, 0);
	}
	void setPizzaP1(Pizza p1) {
		pi[0] = p1;
		p[0] = &pi[0];
	}
	void setPizzaP2(Pizza p2) {
		pi[1] = p2;
		p[1] = &pi[1];
	}
	void setPizzaP3(Pizza p3) {
		pi[2] = p3;
		p[2] = &pi[2];
	}
	int getNumPizza() {
		int res = 0;
		for (int i = 0; i < 3; i++)
		{
			if (p[i] != NULL) {
				res++;
			}
		}
		return res;
	}
	Pizza* getPizza1() {
		return p[0];
	}
	Pizza* getPizza2() {
		return p[1];
	}
	Pizza* getPizza3() {
		return p[2];
	}
	double caclTotal() {
		total = pi[0].calcCost() + pi[1].calcCost() + pi[2].calcCost();
		return total;
	}
private:
	Pizza* p[3];
	Pizza pi[3];
	double total;
};
void test01() {//第一题的测试
	Pizza p1;
	p1.setPizza(1, 1, 1, 2);
	cout << "订单的总价是 " << p1.calcCost() << " $" << endl;
}
int main() {
	Pizza pizza1, pizza2;
	PizzaOrder o1;
	pizza1.setPizza(1, 1, 0, 1);
	pizza2.setPizza(2, 2, 2, 0);
	o1.setPizzaP1(pizza1);
	o1.setPizzaP2(pizza2);
	o1.caclTotal();
	cout << "订单的总价是 " << o1.caclTotal() << " $" << endl;
	system("pause");
	return 0;
}

三 、运算符重载和继承

一、 实验内容

(1) 声明Point类,有坐标_x,_y两个成员变量;对Point类重载“++(自增)”、“——(自减)”运算符,实现对坐标值的改变。

(2)定义基类CRole(角色),包含一个protected类型的数据成员char *name,有一个带参构造传递name,析构函数删除name。由CRole类派生出3个子类: CPlane(飞机类)、CTank(坦克类)、子弹类(CBullet),用函数printInfo分别把三者的信息输出到屏幕上(用cout分别输出其类名+对象名即可,对象名在各自构造函数中设置)。然后创建一个链表,该链表中要包括3架飞机、2部坦克,10颗子弹。在main函数最后用printInfo输出所有的对象信息。其中printInfo在基类为虚函数,可以自行增加其他功能。

  • 运算符重载

    #include <iostream>
    using namespace std;
    class Point {
    public:
    	Point(int a, int b) {
    		this->x = a;
    		this->y = b;
    	}
    	friend ostream& operator<<(ostream& output, Point p);//利用友元函数重载
    	friend Point operator++(Point& p, int);
    	friend Point operator--(Point& p, int);
    	friend Point operator++(Point& p);
    	friend Point operator--(Point& p);
    public:
    	int x;
    	int y;
    };
    ostream& operator<<(ostream& output, Point p) {
    	output << p.x << endl << p.y << endl;
    	return output;
    }
    Point operator++(Point& p) {
    	p.x++;
    	p.y++;
    	return p;
    }
    Point operator--(Point& p) {
    	p.x--;
    	p.y--;
    	return p;
    }
    Point operator++(Point& p, int) {
    	Point temp(p);
    	++p;
    	return temp;
    }
    Point operator--(Point& p, int) {
    	Point temp(p);
    	--p;
    	return temp;
    }
    void text() {
    	Point p1(2, 3);
    	p1++;
    	--p1;
    	cout << p1;
    }
    int main() {
    	text();
    	system("pause");
    	return 0;
    }
    
  • 继承

    #include <iostream>
    using namespace std;
    class CRole {
    public:
    	//CRole(){};
    	CRole(const char* p) { name = p; };
    	virtual void printInfo() {};
    	virtual ~CRole() { name = NULL; };;
    	CRole* next;
    protected:
    	const char* name;
    };
    class CPlane :public CRole {
    public:
    	CPlane(const char* p) :CRole(p) {}
    	void printInfo() {
    		cout << "CPlane:  " << name << endl;
    	}
    };
    class CTank :public CRole {
    public:
    	CTank(const char* p) :CRole(p) {}
    	void printInfo() {
    		cout << "CTank:  " << name << endl;
    	}
    };
    class CBullet :public CRole {
    public:
    	CBullet(const char* p) :CRole(p) {}
    	void printInfo() {
    		cout << "CBullet:  " << name << endl;
    	}
    };
    void AddFort(CRole*& h, CRole*& t) {
    	t->next = h;
    	h = t;
    }
    void text1() {
    	CRole* head = NULL, * ptr;
    	ptr = new CPlane("p1");
    	AddFort(head, ptr);
    	ptr = new CPlane("p2");
    	AddFort(head, ptr);
    	ptr = new CPlane("p3");
    	AddFort(head, ptr);
    	ptr = new CTank("t1");
    	AddFort(head, ptr);
    	ptr = new CTank("t2");
    	AddFort(head, ptr);
    	ptr = new CBullet("b1");
    	AddFort(head, ptr);
    	ptr = new CBullet("b2");
    	AddFort(head, ptr);
    	ptr = new CBullet("b3");
    	AddFort(head, ptr);
    	ptr = new CBullet("b4");
    	AddFort(head, ptr);
    	ptr = new CBullet("b5");
    	AddFort(head, ptr);
    	ptr = new CBullet("b6");
    	AddFort(head, ptr);
    	ptr = new CBullet("b7");
    	AddFort(head, ptr);
    	ptr = new CBullet("b8");
    	AddFort(head, ptr);
    	ptr = new CBullet("b9");
    	AddFort(head, ptr);
    	ptr = new CBullet("b10");
    	AddFort(head, ptr);
    	ptr = head;
    	while (ptr) {
    		ptr->printInfo();
    		ptr = ptr->next;
    	}
    }
    //void text(){
    //	CRole *list[15];
    //	list[0] = new CPlane ("p1");
    //	list[1] = new CPlane ("p2");
    //	list[2] = new CPlane ("p3");
    //	list[3] = new CTank ("t1");
    //	list[4] = new CTank ("t2");
    //	list[5] = new CBullet ("b1");
    //	list[6] = new CBullet ("b2");
    //	list[7] = new CBullet ("b3");
    //	list[8] = new CBullet ("b4");
    //	list[9] = new CBullet ("b5");
    //	list[10] = new CBullet ("b6");
    //	list[11] = new CBullet ("b7");
    //	list[12] = new CBullet ("b8");
    //	list[13] = new CBullet ("b9");
    //	list[14] = new CBullet ("b10");
    //	for (int i = 0; i < 15; i++){
    //		list[i]->printInfo();
    //	}
    //}
    int main() {
    	text1();
    	system("pause");
    	return 0;
    }
    

四、文件流

题目之前删了

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string str[87314];//定义一个字符串数组
int wordFind(const std::string& word)
{
	int count = 0;
	for (const char* p = word.c_str(); *p; ++p)
		count += *p == *(p + 1);
	return count;
}
int main() {
    int count_max = -1;
    string word_max;

    ifstream words("words.txt");
    for (string word; words >> word; )
    {
        int count = wordFind(word);
        if (count_max < count)
        {
            count_max = count;
            word_max = word;
        }   
    }
    ofstream fout("newWords.txt");
    fout << word_max;
    cout << word_max << endl;
	system("pause");
	return 0;
}
1 、结构体与类的编写: (A)利用struct关键字定义一个学生结构体(包括学号、姓名、性别):类名:student, num、name、sex,在主函数定义两个对象stud1,stud2,对stud1对象赋值并输出,对第2个对象stud2赋值输出; (B)利用class关键字将1改成类的编写,其它不变 (C)将输出封装成display,输入封装成setdata函数,分别在类里面定义2函数,在主函数中输入输出; (D)将上面两成员函数移至类外定义并输出 (E)将setdata函数利用对象的引用做函数参数,在主函数中输入输出 2、(1)定义一个时间类,属性包括小时、分、秒,定义两成员函数:settime,showtime,分别以两种方式、类内定义成员函数和内外定义成员函数 (2)对1两成员函数分别利用对象的引用做参数、默认参数做参数进行编写与调用并输出。属性 3、编写一个程序,模拟电梯的功能。功能接口包括电梯上行按钮、下行按钮、楼层选择和电梯在行驶过程中的楼层显示。 要求: (1)、由用户选择按上行按钮还是下行按钮,选择操作后再由用户输入要进入的楼层,进而电梯开始运行,显示所到的每一楼层层数。 (2).如果是上行,则选择输入的楼层号不能比当前楼层号小,否则应给出不合法提示。 (3). 如果是下行,则选择输入的楼层号不能比当前楼层号大,否则应给出不合法提示。 (4).电梯一旦开始运作就会始终运行,直到窗口关闭。 (5).电梯在经过不同楼层时,最好每个楼层的显示之间能有延迟,最终停靠的楼层的输出形式能更加醒目。如果可以,在电梯最初开始运行时,能在电梯由内部显示当前日期(提示:实现这些功能时,需要调用系统api,实现时间显示功能可以使用CDate类)。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值