This commit is contained in:
以诺书 2024-08-05 23:12:46 +08:00
commit 8ebd58a11b
20 changed files with 776 additions and 0 deletions

50
3-33.html Normal file
View File

@ -0,0 +1,50 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS属性border-collapse的应用</title>
<style>
.separate{
border-collapse: separate;
}
.collapse{
border-collapse: collapse;
}
</style>
</head>
<body>
<h3>CSS属性border-collapse的应用</h3>
<hr />
<table border="1"class="separate">
<caption>
双线边框效果
</caption>
<tr>
<td>年份</td>
<td>第一季度</td>
<td>第二季度</td>
<td>第三季度</td>
</tr>
<tr><td>2014</td><td>100</td><td>200</td><td>300</td></tr>
<tr><td>2015</td><td>150</td><td>250</td><td>350</td></tr>
<tr><td>2016</td><td>200</td><td>300</td><td>400</td></tr>
</table>
<br />
<table border="1"class="collapse">
<caption>
折叠边框效果
</caption>
<tr>
<td>年份</td>
<td>第一季度</td>
<td>第二季度</td>
<td>第三季度</td>
</tr>
<tr><td>2014</td><td>100</td><td>200</td><td>300</td></tr>
<tr><td>2015</td><td>150</td><td>250</td><td>350</td></tr>
<tr><td>2016</td><td>200</td><td>300</td><td>400</td></tr>
</tr>
</table>
</body>
</html>

21
6.5.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <iostream>
// 修正模板的定义
template <typename T>
T max(T x, T y)
{
// 删除无效的赋值语句
return (x > y) ? x : y;
}
int main()
{
char c = 0;
int i = 1;
// 调用模板函数时需要指定模板类型
int result = max((int)c,i);
// 输出结果
std::cout << "Max: " << result << std::endl;
return 0;
}

90
all.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <iostream>
#include <string>
// 基类(父类)
class Animal {
public:
// 虚函数,用于实现多态
virtual void makeSound() {
std::cout << "Some generic animal sound" << std::endl;
}
// 公共方法
void eat() {
std::cout << "Eating..." << std::endl;
}
};
// 派生类(子类)
class Dog : public Animal {
private:
std::string name;
int age;
public:
// 构造函数
Dog(std::string n, int a) : name(n), age(a) {}
// 重载虚函数,实现多态
void makeSound() override {
std::cout << "Woof!" << std::endl;
}
// 设置名字(封装)
void setName(std::string n) {
name = n;
}
// 获取名字(封装)
std::string getName() {
return name;
}
// 设置年龄(封装)
void setAge(int a) {
age = a;
}
// 获取年龄(封装)
int getAge() {
return age;
}
};
// 另一个派生类(子类)
class Cat : public Animal {
public:
// 重载虚函数,实现多态
void makeSound() override {
std::cout << "Meow!" << std::endl;
}
};
int main() {
// 创建Dog对象
Dog myDog("Buddy", 3);
myDog.eat(); // 输出: Eating...
myDog.makeSound(); // 输出: Woof!
// 使用封装方法设置和获取属性
myDog.setName("Max");
std::cout << "Dog's name: " << myDog.getName() << std::endl; // 输出: Dog's name: Max
myDog.setAge(4);
std::cout << "Dog's age: " << myDog.getAge() << std::endl; // 输出: Dog's age: 4
// 创建Cat对象
Cat myCat;
myCat.eat(); // 输出: Eating...
myCat.makeSound(); // 输出: Meow!
// 多态演示
Animal* animals[2];
animals[0] = &myDog;
animals[1] = &myCat;
for (int i = 0; i < 2; i++) {
animals[i]->makeSound(); // 输出: Woof! 和 Meow!
}
return 0;
}

BIN
build/Debug/6.5.o Normal file

Binary file not shown.

BIN
build/Debug/all.o Normal file

Binary file not shown.

BIN
build/Debug/c++上机1.o Normal file

Binary file not shown.

BIN
build/Debug/c++上机2.o Normal file

Binary file not shown.

BIN
build/Debug/c++上机3.o Normal file

Binary file not shown.

BIN
build/Debug/c++上机4.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
build/Debug/outDebug Executable file

Binary file not shown.

140
c++上机1.cpp Normal file
View File

@ -0,0 +1,140 @@
#include <iostream>
#include <cstring>
class CTString {
private:
char *m_pData; // 用于保存字符数据
int m_nLen; // 记录字符长度
public:
// 构造函数和析构函数
CTString();
CTString(const char *);
CTString(const CTString&);
~CTString();
// 其它成员函数
CTString* Copy(CTString*);
CTString* Connect(CTString*);
int Find(CTString*);
int Find(char) const;
void Print() const;
// 获取字符数据的公有函数
const char* GetData() const {
return m_pData;
}
// 查找子串
int FindSubstring(CTString*);
};
// 构造函数
CTString::CTString() {
m_nLen = 0;
m_pData = new char[1];
*m_pData = '\0';
}
CTString::CTString(const char *pData) {
m_nLen = strlen(pData);
m_pData = new char[m_nLen + 1];
strcpy(m_pData, pData);
}
// 拷贝构造函数
CTString::CTString(const CTString& str) {
m_nLen = str.m_nLen;
m_pData = new char[m_nLen + 1];
strcpy(m_pData, str.m_pData);
}
// 析构函数
CTString::~CTString() {
delete [] m_pData;
}
// 拷贝函数
CTString* CTString::Copy(CTString *str) {
*str = *this;
return str;
}
// 连接函数
CTString* CTString::Connect(CTString *str) {
CTString *temp = new CTString;
temp->m_nLen = m_nLen + str->m_nLen;
temp->m_pData = new char[temp->m_nLen + 2]; // 加上一个空格的长度
strcpy(temp->m_pData, m_pData);
strcat(temp->m_pData, " "); // 添加一个空格
strcat(temp->m_pData, str->m_pData);
return temp;
}
// 查找子串
int CTString::FindSubstring(CTString *str) {
char *ptr = strstr(m_pData, str->m_pData);
if (ptr) {
// 计算子串在主串中的位置,考虑到额外的空格
int pos = ptr - m_pData;
return pos - 1; // 减去额外的空格
} else {
return -1; // 未找到
}
}
// 查找字符
int CTString::Find(char ch) const {
char *ptr = strchr(m_pData, ch);
if (ptr) {
// 计算字符在字符串中的位置,考虑到额外的空格
int pos = ptr - m_pData;
return pos - 1; // 减去额外的空格
} else {
return -1; // 未找到
}
}
// 打印输出字符串内容
void CTString::Print() const {
std::cout << "字符串内容:" << m_pData << std::endl;
}
int main() {
char strInput1[100], strInput2[100], charToFind;
std::cout << "请输入第一个字符串: ";
std::cin.getline(strInput1, 100);
std::cout << "请输入第二个字符串: ";
std::cin.getline(strInput2, 100);
std::cout << "请输入要查找的字符: ";
std::cin >> charToFind;
CTString str1(strInput1);
CTString str2(strInput2);
CTString str3(str1);
CTString *str4 = new CTString;
str3.Copy(str4);
CTString *str5 = str4->Connect(&str2);
str1.Print();
str3.Print();
str5->Print();
const char *data = str5->GetData();
int subStrPos = str5->FindSubstring(&str2);
if (subStrPos >= 0)
std::cout << "子串位置: " << subStrPos << std::endl;
int charPos = str5->Find(charToFind);
if (charPos >= 0)
std::cout << "字符位置: " << charPos << std::endl;
delete str4;
delete str5;
return 0;
}

128
c++上机2.cpp Normal file
View File

@ -0,0 +1,128 @@
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
class CEmployee {
private:
char *m_pName; // 姓名
int m_nAge; // 年龄
float m_fSalary; // 薪水
public:
// 构造函数和析构函数
CEmployee(char *pName = NULL, int age = 0, float salary = 0.0) : m_nAge(age), m_fSalary(salary) {
if (pName != NULL) {
m_pName = new char[strlen(pName) + 1];
strcpy(m_pName, pName);
} else {
m_pName = NULL;
}
}
CEmployee(const CEmployee &emp) {
m_pName = new char[strlen(emp.m_pName) + 1];
strcpy(m_pName, emp.m_pName);
m_nAge = emp.m_nAge;
m_fSalary = emp.m_fSalary;
}
~CEmployee() {
if (m_pName != NULL) {
delete[] m_pName;
}
}
// 其它成员函数
void SetName(char *name) {
if (m_pName != NULL) {
delete[] m_pName;
}
m_pName = new char[strlen(name) + 1];
strcpy(m_pName, name);
}
char *GetName() {
return m_pName;
}
void SetAge(int age) {
m_nAge = age;
}
int GetAge() {
return m_nAge;
}
void SetSalary(float salary) {
m_fSalary = salary;
}
float GetSalary() {
return m_fSalary;
}
void Print() {
cout << "姓名:" << m_pName << ",年龄:" << m_nAge << ",薪水:" << m_fSalary << endl;
}
};
class CManager : public CEmployee {
private:
int m_nLevel; // 级别
public:
// 构造函数和析构函数
CManager(char *pName = NULL, int age = 0, float salary = 0.0, int nLevel = 0) : CEmployee(pName, age, salary), m_nLevel(nLevel) {}
CManager(const CEmployee &emp, int level) : CEmployee(emp), m_nLevel(level) {}
~CManager() {}
// 其它成员函数
void SetLevel(int level) {
m_nLevel = level;
}
int GetLevel() {
return m_nLevel;
}
void Print() {
CEmployee::Print();
cout << "级别:" << m_nLevel << endl;
}
};
int main() {
vector
<CEmployee> employees;
vector<CManager> managers;
char name[50];
int age;
float salary;
int level;
int numEmployees;
// 输入雇员数量
cout << "请输入雇员数量:";
cin >> numEmployees;
cin.ignore(); // 忽略输入缓冲区中的换行符
// 输入雇员信息
for (int i = 0; i < numEmployees; ++i) {
cout << "请输入雇员姓名:";
cin.getline(name, 50);
cout << "请输入雇员年龄:";
cin >> age;
cout << "请输入雇员薪水:";
cin >> salary;
cin.ignore(); // 忽略输入缓冲区中的换行符
// 创建雇员对象
CEmployee emp(name, age, salary);
employees.push_back(emp);
}
// 输入管理者信息
cout << "请输入经理姓名:";
cin.getline(name, 50);
cout << "请输入经理年龄:";
cin >> age;
cout << "请输入经理薪水:";
cin >> salary;
cout << "请输入经理级别:";
cin >> level;
cin.ignore(); // 忽略输入缓冲区中的换行符
// 创建经理对象
CManager manager(name, age, salary, level);
managers.push_back(manager);
// 输出所有雇员信息
cout << "雇员信息:" << endl;
for (int i = 0; i < employees.size(); ++i) {
employees[i].Print();
}
// 输出所有经理信息
cout << "经理信息:" << endl;
for (int i = 0; i < managers.size(); ++i) {
managers[i].Print();
}
return 0;
}

104
c++上机3.cpp Normal file
View File

@ -0,0 +1,104 @@
#include <iostream>
class CComplex {
private:
float real;
float imag;
public:
// 构造函数
CComplex(float r = 0.0, float i = 0.0) : real(r), imag(i) {}
// 拷贝构造函数
CComplex(const CComplex& other) : real(other.real), imag(other.imag) {}
// 赋值运算符重载
CComplex& operator=(const CComplex& other) {
real = other.real;
imag = other.imag;
return *this;
}
// 加法运算符重载
CComplex operator+(const CComplex& other) const {
return CComplex(real + other.real, imag + other.imag);
}
// 减法运算符重载
CComplex operator-(const CComplex& other) const {
return CComplex(real - other.real, imag - other.imag);
}
// 乘法运算符重载
CComplex operator*(const CComplex& other) const {
return CComplex(real * other.real - imag * other.imag, real * other.imag + imag * other.real);
}
// 除法运算符重载
CComplex operator/(const CComplex& other) const {
float denominator = other.real * other.real + other.imag * other.imag;
if (denominator == 0.0) {
std::cerr << "错误:除数为零!" << std::endl;
return CComplex();
}
return CComplex((real * other.real + imag * other.imag) / denominator, (imag * other.real - real * other.imag) / denominator);
}
// 相等运算符重载
bool operator==(const CComplex& other) const {
return (real == other.real) && (imag == other.imag);
}
// 不等运算符重载
bool operator!=(const CComplex& other) const {
return !(*this == other);
}
// 下标运算符重载
float operator[](int index) const {
if (index == 0) return real;
else if (index == 1) return imag;
else {
std::cerr << "错误:下标越界!" << std::endl;
return 0.0;
}
}
// 打印复数
void print() const {
std::cout << "(" << real << " + " << imag << "i)";
}
// 虚析构函数
virtual ~CComplex() {}
};
int main() {
float real1, imag1, real2, imag2;
// 输入第一个复数
std::cout << "请输入第一个复数的实部:";
std::cin >> real1;
std::cout << "请输入第一个复数的虚部:";
std::cin >> imag1;
// 输入第二个复数
std::cout << "请输入第二个复数的实部:";
std::cin >> real2;
std::cout << "请输入第二个复数的虚部:";
std::cin >> imag2;
// 创建两个复数对象
CComplex a(real1, imag1);
CComplex b(real2, imag2);
// 测试运算符重载
CComplex c = a + b;
std::cout << "a + b = ";
c.print();
std::cout << std::endl;
c = a - b;
std::cout << "a - b = ";
c.print();
std::cout << std::endl;
c = a * b;
std::cout << "a * b = ";
c.print();
std::cout << std::endl;
c = a / b;
std::cout << "a / b = ";
c.print();
std::cout << std::endl;
// 测试相等和不等运算符重载
if (a == b) {
std::cout << "a 等于 b。" << std::endl;
} else {
std::cout << "a 不等于 b。" << std::endl;
}
// 测试下标运算符重载
std::cout << "a 的实部:" << a[0] << std::endl;
std::cout << "a 的虚部:" << a[1] << std::endl;
return 0;
}

93
c++上机4.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <iostream>
// 节点结构
template <typename T>
struct Node {
T data;
Node* next;
Node(const T& data) : data(data), next(nullptr) {}
};
// 链栈类模板
template <typename T>
class LinkStack {
private:
Node<T>* top; // 栈顶指针
size_t size; // 栈大小
public:
// 构造函数
LinkStack() : top(nullptr), size(0) {}
// 析构函数
~LinkStack() {
while (!isEmpty()) {
pop();
}
}
// 判断栈是否为空
bool isEmpty() const {
return top == nullptr;
}
// 获取栈的大小
size_t getSize() const {
return size;
}
// 入栈操作
void push(const T& data) {
Node<T>* newNode = new Node<T>(data);
newNode->next = top;
top = newNode;
++size;
}
// 出栈操作
void pop() {
if (isEmpty()) {
std::cout << "Stack is empty, cannot pop.\n";
return;
}
Node<T>* temp = top;
top = top->next;
delete temp;
--size;
}
// 获取栈顶元素
T peek() const {
if (isEmpty()) {
throw std::runtime_error("Stack is empty, cannot peek.");
}
return top->data;
}
};
int main() {
LinkStack<int> stack;
// 测试入栈操作
stack.push(10);
stack.push(20);
stack.push(30);
// 打印栈顶元素
std::cout << "Top element: " << stack.peek() << std::endl;
// 测试出栈操作
stack.pop();
std::cout << "Top element after pop: " << stack.peek() << std::endl;
// 测试栈大小
std::cout << "Stack size: " << stack.getSize() << std::endl;
// 清空栈
while (!stack.isEmpty()) {
std::cout << "Popping: " << stack.peek() << std::endl;
stack.pop();
}
return 0;
}

View File

@ -0,0 +1,44 @@
#include <iostream>
#include <vector>
#include <string>
// 基类 CTimer
class CTimer {
protected:
unsigned long timerID;
public:
CTimer(unsigned long id) : timerID(id) {}
virtual ~CTimer() {}
// 纯虚函数,子类必须重载
virtual std::string GetTimeZone() const = 0;
unsigned long GetTimerID() const {
return timerID;
}
};
// 子类 CBeijingTimer
class CBeijingTimer : public CTimer {
public:
CBeijingTimer(unsigned long id) : CTimer(id) {}
virtual ~CBeijingTimer() {}
// 重载基类的虚函数
virtual std::string GetTimeZone() const {
return "Beijing Time";
}
};
int main() {
std::vector<CTimer*> timerList;
// 创建一个北京时间的定时器并添加到列表中
CTimer* beijingTimer = new CBeijingTimer(1);
timerList.push_back(beijingTimer);
// 查询当前所有创建的Timer并打印信息
std::cout << "Timer List:" << std::endl;
for (std::vector<CTimer*>::iterator it = timerList.begin(); it != timerList.end(); ++it) {
std::cout << "Timer ID: " << (*it)->GetTimerID() << ", Time Zone: " << (*it)->GetTimeZone() << std::endl;
}
// 清理内存
for (std::vector<CTimer*>::iterator it = timerList.begin(); it != timerList.end(); ++it) {
delete *it;
}
timerList.clear();
return 0;
}

49
c++上机思考题1.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <iostream>
#include <cstring>
class CTString {
private:
char *str;
static int objectCount; // 静态成员变量,用于记录对象个数
public:
CTString() {
str = NULL; // or str = 0;
objectCount++; // 增加对象计数
}
CTString(const char *s) {
str = new char[strlen(s) + 1];
strcpy(str, s);
objectCount++;
}
~CTString() {
objectCount--; // 先减少对象计数
delete[] str; // 释放字符串内存
}
void Print() {
if (str)
std::cout << str << std::endl;
else
std::cout << "空字符串" << std::endl;
}
void Copy(const char *s) {
delete[] str;
str = new char[strlen(s) + 1];
strcpy(str, s);
}
static int getObjectCount() {
return objectCount;
}
};
int CTString::objectCount = 0; // 静态成员变量初始化
int main() {
CTString *pstr;
pstr = new CTString;
pstr->Print();
pstr->Copy("hello");
pstr->Print();
std::cout << "总共创建的对象数量: " << CTString::getObjectCount() << std::endl;
delete pstr;
// 等待用户按下任意键后再退出
std::cin.get();
return 0;
}

57
c++上机思考题4.cpp Normal file
View File

@ -0,0 +1,57 @@
//定义一个数组类模版,然后以它为基类模板,如何定义一个派生类模板?
#include <iostream>
#include <vector>
// 基类模板:数组类模板
template<typename T>
class Array {
protected:
std::vector<T> data;
public:
Array(int size) : data(size) {}
void set(int index, const T& value) {
data[index] = value;
}
T get(int index) const {
return data[index];
}
int size() const {
return data.size();
}
};
// 派生类模板:增强数组类模板
template<typename T>
class EnhancedArray : public Array<T> {
public:
EnhancedArray(int size) : Array<T>(size) {}
// 新增函数:求和
T sum() const {
T sum = 0;
for (const auto& element : this->data) {
sum += element;
}
return sum;
}
// 新增函数:平均值
double average() const {
if (this->data.empty()) {
return 0.0;
}
T sum = this->sum();
return static_cast<double>(sum) / this->data.size();
}
};
int main() {
EnhancedArray<int> arr(5);
for (int i = 0; i < 5; ++i) {
arr.set(i, i + 1);
}
std::cout << "Array elements: ";
for (int i = 0; i < arr.size(); ++i) {
std::cout << arr.get(i) << " ";
}
std::cout << std::endl;
std::cout << "Sum: " << arr.sum() << std::endl;
std::cout << "Average: " << arr.average() << std::endl;
return 0;
}