Compare commits

..

No commits in common. "7b786b9b925c3af77167e17c6643026df8daaa66" and "f956e9c5e2a090d642effcc370b61e9a0a44b468" have entirely different histories.

8 changed files with 0 additions and 660 deletions

View File

@ -1,183 +0,0 @@
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
// 声明遥控器类
class Remote;
// 定义电视类
class Television
{
friend class Remote;
public:
enum
{
On, // 电视开关状态
Off
};
enum
{
minVol,
maxVol = 100 // 音量从 0 到 100
};
enum
{
minChannel = 1,
maxChannel = 255 // 频道范围 1 到 255
};
Television()
{
mStatus = Off; // 状态
mVolume = minVol; // 声音
mChannel = minChannel; // 频道
}
public:
void power() // 开关机
{
this->mStatus = (this->mStatus == On ? Off : On);
}
void VolumeUp() // 调高音量
{
if (this->mVolume >= maxVol)
return;
this->mVolume++;
}
void VolumeDown() // 调低音量
{
if (this->mVolume <= minVol)
return;
this->mVolume--;
}
void ChannelUp() // 向上更换电视频道
{
if (this->mChannel >= maxChannel)
return;
this->mChannel++;
}
void ChannelDown() // 向下更换电视频道
{
if (this->mChannel <= minChannel)
return;
this->mChannel--;
}
void ShowTeleState() // 展示当前电视状态信息
{
cout << "开机状态: " << (mStatus == On ? "已开机" : "已关机") << endl;
if (mStatus == On)
{
cout << "当前音量: " << mVolume << endl;
cout << "当前频道: " << mChannel << endl;
}
cout << "----------------------" << endl;
}
private:
int mStatus; // 电视状态
int mVolume; // 声音
int mChannel; // 频道
};
// 定义遥控器
class Remote
{
public:
Remote(Television *tv) // 构造函数
{
pTV = tv;
}
public:
void power() // 开关机
{
pTV->power();
}
void VolumeUp() // 调高音量
{
pTV->VolumeUp();
}
void VolumeDown() // 调低音量
{
pTV->VolumeDown();
}
void ChannelUp() // 向上更换电视频道
{
pTV->ChannelUp();
}
void ChannelDown() // 向下更换电视频道
{
pTV->ChannelDown();
}
// 设置频道,遥控器新增功能
void SetChannel(int channel)
{
if (channel < Television::minChannel || channel > Television::maxChannel)
return;
pTV->mChannel = channel;
}
void ShowTeleState() // 展示当前电视状态信息
{
pTV->ShowTeleState();
}
private:
Television *pTV;
};
// 直接操作电视
void test01()
{
Television tv;
tv.ShowTeleState();
tv.power(); // 开机
tv.VolumeUp(); // 音量 +1
tv.VolumeUp();
tv.VolumeUp();
tv.VolumeUp();
tv.ChannelUp(); // 频道 +1
tv.ChannelUp();
tv.ShowTeleState();
}
// 通过遥控器操作电视
void test02()
{
// 创建电视
Television tv;
// 创建遥控器
Remote rmt(&tv);
rmt.ShowTeleState();
rmt.power(); // 开机
rmt.VolumeUp(); // 音量 +1
rmt.VolumeUp();
rmt.VolumeUp();
rmt.VolumeUp();
rmt.ChannelUp(); // 频道 +1
rmt.ChannelUp();
rmt.SetChannel(100);
rmt.ShowTeleState();
}
int main()
{
// test01();
test02();
return 0;
}

View File

@ -1,24 +0,0 @@
// 如下程序运行的结果是什么 ? 是否存在错误,如果有错请指出并分析原因。
#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;
}

View File

@ -1,67 +0,0 @@
// 设已经有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()
*/

View File

@ -1,35 +0,0 @@
// 运行以下程序,写出其输出的结果?
#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 */

View File

@ -1,45 +0,0 @@
// 设计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;
}

View File

@ -1,79 +0,0 @@
// 继上面的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;
}

View File

@ -1,91 +0,0 @@
// 自定义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;
}

View File

@ -1,136 +0,0 @@
#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;
}