day4: homework

This commit is contained in:
flykhan 2023-07-27 21:04:06 +08:00
parent be4a556f0b
commit 7b786b9b92
7 changed files with 477 additions and 0 deletions

24
day4/homework/h1.cpp Normal file
View File

@ -0,0 +1,24 @@
// 如下程序运行的结果是什么 ? 是否存在错误,如果有错请指出并分析原因。
#include <iostream>
using namespace std;
class Abc
{
private:
int rN;
public:
Abc(int x) : rN(x) {}
static void print()
{
Abc abc(20);
cout << abc.rN << endl;
}
};
int main()
{
Abc::print();
return 0;
}

67
day4/homework/h2.cpp Normal file
View File

@ -0,0 +1,67 @@
// 设已经有A,B,C,D 4个类的定义程序中A,B,C,D析构函数调用顺序为
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class A
{
public:
A() { cout << "A()" << endl; }
~A()
{
cout << "~A()" << endl;
}
};
class B
{
public:
B() { cout << "B()" << endl; }
~B()
{
cout << "~B()" << endl;
}
};
class C
{
public:
C() { cout << "C()" << endl; }
~C()
{
cout << "~C()" << endl;
}
};
class D
{
public:
D() { cout << "D()" << endl; }
~D()
{
cout << "~D()" << endl;
}
};
C c;
int main()
{
A *pa = new A();
B b;
static D d;
delete pa;
}
/*
C()
A()
B()
D()
~A()
~B()
~D()
~C()
*/

35
day4/homework/h3.cpp Normal file
View File

@ -0,0 +1,35 @@
// 运行以下程序,写出其输出的结果?
#include <iostream>
using namespace std;
class Test
{
public:
Test(int i = 0)
{
cout << i;
}
Test(const Test &x)
{
cout << 2;
}
Test &operator=(const Test &x)
{
cout << 3;
return *this;
}
~Test()
{
cout << 4;
}
};
int main()
{
Test obj1(1), obj2(2);
Test obj3 = obj1;
return 0;
}
/* 122444 */

45
day4/homework/h4.cpp Normal file
View File

@ -0,0 +1,45 @@
// 设计Car类包含color颜色、weight(重量) 两个属性和一个带color和weight两个参数的构造函数、以及driver() 驾车、start() 启动、stop() 停车的函数。
// 【提示】设计的类可以在main() 自行测试
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Car
{
public:
Car(const string &color, int weight) : color(color), weight(weight) {}
public:
void driver()
{
cout << "驾车 " << this->weight << " 车重" << endl;
}
void start()
{
cout << "启动 " << this->color << " 颜色的车" << endl;
}
void stop()
{
cout << "停车" << endl;
}
private:
string color; // 颜色
int weight; // 重量
};
void test()
{
Car car("blue", 1200);
car.start();
car.driver();
car.stop();
}
int main()
{
test();
return 0;
}

79
day4/homework/h5.cpp Normal file
View File

@ -0,0 +1,79 @@
// 继上面的Car类 设计Person类并设置Car类为Person类的友元类使得在Person类的成员函数直接访问 Car对象的所有成员。
// 【提示】Person类的构造函数传入 Car类的对象 增加Person的成员函数用于操作Car类的成员函数或访问Car对象的属性。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Car
{
friend class Person;
public:
Car(const string &color, int weight) : color(color), weight(weight) {}
public:
void driver()
{
cout << "驾车 " << this->weight << " 车重" << endl;
}
void start()
{
cout << "启动 " << this->color << " 颜色的车" << endl;
}
void stop()
{
cout << "停车" << endl;
}
private:
string color; // 颜色
int weight; // 重量
};
class Person
{
// friend class Car;
public:
Person(Car &car) : tcar(car) {}
public:
void setCarColor(const string &color)
{
// strcpy(tcar.color, color);
tcar.color = color;
}
void setCarWeight(int weight)
{
tcar.weight = weight;
}
void show()
{
cout << "驾驶着一辆" << tcar.color << ", 重" << tcar.weight << "千克的车辆" << endl;
}
private:
Car &tcar;
};
void test()
{
Car car("红色", 1000);
Person person(car);
person.setCarColor("蓝色");
person.setCarWeight(1230);
car.start();
car.driver();
car.stop();
person.show();
}
int main()
{
test();
return 0;
}

91
day4/homework/h6.cpp Normal file
View File

@ -0,0 +1,91 @@
// 自定义ArrayList类实现线性列表类似数组结构的数据元素操作。操作方法
// 1. add(int) 添加int整型的数据元素
// 2. remove(int) 删除数据元素
// 3. int get(int index) 获取指定位置的元素
// 4. void print() 打印列表中所有元素
// 5. int size() 返回列表的当前大小
// 6. void sort(bool reversed = false) 元素排序reversed为true时表示从大小到小反之表示从小到到。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
class ArrayList
{
private:
vector<int> elements; // 动态数组元素
public:
void add(int value) // 添加 int 整型的数据元素
{
elements.push_back(value);
}
void remove(int index) // 删除数据元素
{
int size = elements.size();
if (index >= 0 && index < size)
elements.erase(elements.begin() + index);
}
int get(int index) // 获取指定位置的元素
{
int size = elements.size();
if (index >= 0 && index < size)
return elements[index];
return -1; // 返回无效索引,表示未找到元素
}
void print() // 打印列表中的所有元素
{
for (int i = 0; i < elements.size(); i++)
cout << elements[i] << " ";
}
int size() // 返回列表的当前大小
{
return elements.size();
}
void sort(bool reversed = false) // 默认从小到大排序当reversed 为 true 时,从大到小排序
{
if (reversed == true)
std::sort(elements.rbegin(), elements.rend());
else
std::sort(elements.begin(), elements.end());
}
};
int main()
{
ArrayList list;
list.add(5);
list.add(2);
list.add(8);
list.add(9);
list.add(13);
cout << "原始列表: ";
list.print();
cout << endl;
cout << "列表大小为: " << list.size() << endl;
cout << "删除第二位的元素后显示为: ";
list.remove(2);
list.print();
cout << endl;
cout << "此时第二位的元素为: " << list.get(2) << endl;
cout << "从大到小排序后列表为: ";
list.sort(true);
list.print();
cout << endl;
cout << "从小到大排序后列表为: ";
list.sort();
list.print();
cout << endl;
return 0;
}

136
day4/homework/h6_2.cpp Normal file
View File

@ -0,0 +1,136 @@
#include <iostream>
class ArrayList
{
private:
int *elements; // 存储元素的数组
int capacity; // 数组的容量
int count; // 当前元素的数量
public:
ArrayList()
{
capacity = 10; // 初始容量为10
elements = new int[capacity];
count = 0;
}
~ArrayList()
{
delete[] elements;
}
void add(int value)
{
if (count == capacity)
{
// 如果数组已满,扩展容量为原来的两倍
int newCapacity = capacity * 2;
int *newElements = new int[newCapacity];
for (int i = 0; i < count; i++)
{
newElements[i] = elements[i];
}
delete[] elements;
elements = newElements;
capacity = newCapacity;
}
elements[count] = value;
count++;
}
void remove(int index)
{
if (index >= 0 && index < count)
{
for (int i = index; i < count - 1; i++)
{
elements[i] = elements[i + 1];
}
count--;
}
}
int get(int index)
{
if (index >= 0 && index < count)
{
return elements[index];
}
return -1; // 返回一个特殊值表示索引无效
}
void print()
{
for (int i = 0; i < count; i++)
{
std::cout << elements[i] << " ";
}
std::cout << std::endl;
}
int size()
{
return count;
}
void sort(bool reversed = false)
{
for (int i = 0; i < count - 1; i++)
{
for (int j = 0; j < count - i - 1; j++)
{
if (reversed)
{
if (elements[j] < elements[j + 1])
{
int temp = elements[j];
elements[j] = elements[j + 1];
elements[j + 1] = temp;
}
}
else
{
if (elements[j] > elements[j + 1])
{
int temp = elements[j];
elements[j] = elements[j + 1];
elements[j + 1] = temp;
}
}
}
}
}
};
int main()
{
ArrayList list;
list.add(5);
list.add(2);
list.add(8);
list.add(1);
std::cout << "Original list: ";
list.print();
std::cout << "Size: " << list.size() << std::endl;
std::cout << "Element at index 2: " << list.get(2) << std::endl;
list.remove(1);
std::cout << "After removing element at index 1: ";
list.print();
std::cout << "Sorted list in ascending order: ";
list.sort();
list.print();
std::cout << "Sorted list in descending order: ";
list.sort(true);
list.print();
return 0;
}