support bazel complie this project and format code.

This commit is contained in:
zhangxing
2023-03-30 00:15:11 +08:00
committed by light-city
parent 1f86192576
commit 7529ae3a55
636 changed files with 10025 additions and 9387 deletions

View File

@@ -0,0 +1,78 @@
# please run `bazel run //practical_exercises/key_exercises:output`
# please run `bazel run //practical_exercises/key_exercises:clock`
# please run `bazel run //practical_exercises/key_exercises:read_file`
# please run `bazel run //practical_exercises/key_exercises:operator_circle`
# please run `bazel run //practical_exercises/key_exercises:io_operator`
# please run `bazel run //practical_exercises/key_exercises:stack`
# please run `bazel run //practical_exercises/key_exercises:try`
# please run `bazel run //practical_exercises/key_exercises:override`
# please run `bazel run //practical_exercises/key_exercises:io_operator_overload`
# please run `bazel run //practical_exercises/key_exercises:array_template`
# please run `bazel run //practical_exercises/key_exercises:array`
# please run `bazel run //practical_exercises/key_exercises:map_insert_look`
# please run `bazel run //practical_exercises/key_exercises:func_temp`
# please run `bazel run //practical_exercises/key_exercises:bracket_overloading`
# please run `bazel run //practical_exercises/key_exercises:operator_cast`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "output",
srcs = ["output.cpp"],
)
cc_binary(
name = "clock",
srcs = ["clock.cpp"],
)
cc_binary(
name = "read_file",
srcs = ["read_file.cpp"],
)
cc_binary(
name = "operator_circle",
srcs = ["operator_circle.cpp"],
)
cc_binary(
name = "io_operator",
srcs = ["io_operator.cpp"],
)
cc_binary(
name = "stack",
srcs = ["stack.cpp"],
)
cc_binary(
name = "try",
srcs = ["try.cpp"],
)
cc_binary(
name = "override",
srcs = ["override.cpp"],
)
cc_binary(
name = "io_operator_overload",
srcs = ["io_operator_overload.cpp"],
)
cc_binary(
name = "array_template",
srcs = ["array_template.cpp"],
)
cc_binary(
name = "array",
srcs = ["array.cpp"],
)
cc_binary(
name = "map_insert_look",
srcs = ["map_insert_look.cpp"],
)
cc_binary(
name = "func_temp",
srcs = ["func_temp.cpp"],
)
cc_binary(
name = "bracket_overloading",
srcs = ["bracket_overloading.cpp"],
)
cc_binary(
name = "operator_cast",
srcs = ["operator_cast.cpp"],
)

View File

@@ -1,26 +1,26 @@
## 重点实战练习
├── [中括号重载.cpp](中括号重载.cpp)
├── [时钟++运算符重载.cpp](时钟++运算符重载.cpp)
├── [运算符重载之强制转换.cpp](运算符重载之强制转换.cpp)
└── [重载圆括号的时钟.cpp](重载圆括号的时钟.cpp)
├── [中括号重载.cpp](bracket_overloading.cpp)
├── [时钟++运算符重载.cpp](clock.cpp)
├── [运算符重载之强制转换.cpp](operator_cast.cpp)
└── [重载圆括号的时钟.cpp](operator_circle.cpp)
├── [函数模板.cpp](函数模板.cpp)
├── [函数模板.cpp](func_temp.cpp)
├── [动态数组.cpp](动态数组.cpp)
├── [动态数组.cpp](array.cpp)
├── [字典插入与查找.cpp](字典插入与查找.cpp)
├── [字典插入与查找.cpp](map_insert_look.cpp)
├── [异常捕获.cpp](异常捕获.cpp)
├── [异常捕获.cpp](try.cpp)
├── [类模板之栈.cpp](类模板之栈.cpp)
├── [类模板之栈.cpp](stack.cpp)
├── [类模板特化之数组.cpp](类模板特化之数组.cpp)
├── [类模板特化之数组.cpp](array_template.cpp)
├── [继承与封装.cpp](继承与封装.cpp)
├── [继承与封装.cpp](override.cpp)
├── [读写文件综合题.cpp](读写文件综合题.cpp)
├── [输入输出运算符重载.cpp](输入输出运算符重载.cpp)
├── [输入输出重载.cpp](输入输出重载.cpp)
├── [输出格式.cpp](输出格式.cpp)
├── [读写文件综合题.cpp](read_file.cpp)
├── [输入输出运算符重载.cpp](io_operator_overload.cpp)
├── [输入输出重载.cpp](io_operator.cpp)
├── [输出格式.cpp](output.cpp)

View File

@@ -0,0 +1,14 @@
/* 数组.cpp */
#include <cstring>
#include <iostream>
int main() {
char *sPtr;
const char *s = "hello";
sPtr = new char[strlen(s) + 1];
strncpy(sPtr, s, strlen(s));
std::cout << sPtr << std::endl;
delete sPtr;
return 0;
}

View File

@@ -0,0 +1,80 @@
/* 类模板特化之数组.cpp */
#include <cstring>
#include <iostream>
using namespace std;
#define MAXSIZE 5
template <class T> class Array {
public:
Array() {
for (int i = 0; i < MAXSIZE; i++) {
array[i] = 0;
}
}
T &operator[](int i);
void Sort();
private:
T array[MAXSIZE];
};
template <class T> T &Array<T>::operator[](int i) {
if (i < 0 || i > MAXSIZE - 1) {
cout << "数组下标越界!" << endl;
exit(0);
}
return array[i];
}
template <class T> void Array<T>::Sort() {
int p, j;
for (int i = 0; i < MAXSIZE - 1; i++) {
p = i;
for (j = i + 1; j < MAXSIZE; j++) {
if (array[p] < array[j])
p = j;
}
T t;
t = array[i];
array[i] = array[p];
array[p] = t;
}
}
template <> void Array<char *>::Sort() {
int p, j;
for (int i = 0; i < MAXSIZE - 1; i++) {
p = i;
for (j = i + 1; j < MAXSIZE; j++) {
if (strcmp(array[p], array[j]) < 0)
p = j;
}
char *t = array[i];
array[i] = array[p];
array[p] = t;
}
}
int main(int argc, char const *argv[]) {
Array<int> a1;
Array<char *> b1;
a1[0] = 1;
a1[1] = 23;
a1[2] = 6;
a1[3] = 3;
a1[4] = 9;
a1.Sort();
for (int i = 0; i < 5; i++)
cout << a1[i] << "\t";
cout << endl;
b1[0] = "x1";
b1[1] = "ya";
b1[2] = "ad";
b1[3] = "be";
b1[4] = "bc";
b1.Sort();
for (int i = 0; i < 5; i++)
cout << b1[i] << "\t";
cout << endl;
return 0;
}

View File

@@ -0,0 +1,51 @@
/* 中括号重载.cpp */
#include <cstring>
#include <iostream>
using namespace std;
struct Person { //职工基本信息的结构
double salary;
char *name;
};
class SalaryManaege {
Person *employ; //存放职工信息的数组
int max; //数组下标上界
int n; //数组中的实际职工人数
public:
SalaryManaege(int Max = 0) {
max = Max;
n = 0;
employ = new Person[max];
}
//返回引用特性是可以直接在放在左值,直接使用
double &operator[](char *Name) { //重载[],返回引用
Person *p;
for (p = employ; p < employ + n; p++)
//如果存在处理
if (strcmp(p->name, Name) == 0)
return p->salary;
//不存在情况处理
p = employ + n++;
p->name = new char[strlen(Name) + 1];
strcpy(p->name, Name);
p->salary = 0;
return p->salary;
}
void display() {
for (int i = 0; i < n; i++)
cout << employ[i].name << " " << employ[i].salary << endl;
}
~SalaryManaege() { delete[] employ; }
};
int main() {
SalaryManaege s(3);
s["张三"] = 2188.88;
s["里斯"] = 1230.07;
s["王无"] = 3200.97;
cout << "张三\t" << s["张三"] << endl;
cout << "里斯\t" << s["里斯"] << endl;
cout << "王无\t" << s["王无"] << endl;
cout << "-------下为display的输出--------\n\n";
s.display();
}

View File

@@ -0,0 +1,85 @@
/* 时钟++运算符重载.cpp */
#include <cmath>
#include <iostream>
using namespace std;
/*
* 时钟类
*/
class Clock {
private:
int Hour, Minute, Second;
public:
Clock(int h = 0, int m = 0, int s = 0);
void ShowTime();
Clock &operator++();
Clock operator++(int);
};
/*
* 时钟类构造函数
*/
Clock::Clock(int h, int m, int s) {
if (h >= 0 && h < 24 && m >= 0 && m < 60 && s >= 0 && s < 60) {
Hour = h;
Minute = m;
Second = s;
} else
cout << "输入的时间格式错误!" << endl;
}
/*
* 显示时间
*/
void Clock::ShowTime() {
cout << Hour << ":" << Minute << ":" << Second << endl;
}
/*
* 时间递增一秒(重载前缀++运算符)
*/
Clock &Clock::operator++() {
Second++;
if (Second >= 60) {
Second = Second - 60;
Minute++;
if (Minute >= 60) {
Minute = Minute - 60;
Hour++;
Hour = Hour % 24;
}
}
return *this;
}
/*
* 时间递增一秒(重载后缀++运算符)
*/
Clock Clock::operator++(int) {
Clock old = *this;
++(*this);
return old;
}
/*
* 主函数
*/
int main() {
Clock myClock(23, 59, 59);
cout << "初始化显示时间为:\t\t";
myClock.ShowTime();
cout << "执行myClock++后的时间为:\t";
//先执行ShowTime()输出myClock=23:59:59
//再执行myClock++此时myClock=00:00:00
(myClock++).ShowTime();
cout << "执行++myClock后的时间为:\t";
//先执行++myClock此时myClock=00:00:01
//再执行ShowTime()输出myClock=00:00:01
(++myClock).ShowTime();
}

View File

@@ -1,20 +1,19 @@
/* 函数模板.cpp */
#include <cstring>
#include <iostream>
using namespace std;
template <class T>
T compareMax(T t1, T t2) {
return t1 > t2 ? t1 : t2;
}
template <class T> T compareMax(T t1, T t2) { return t1 > t2 ? t1 : t2; }
template <>
const char *compareMax<const char *>(const char *s1, const char *s2) {
cout << "[for debug]" << " call compareMax template" << endl;
cout << "[for debug]"
<< " call compareMax template" << endl;
return strcmp(s1, s2) >= 0 ? s1 : s2;
}
int main(int argc, char const *argv[]) {
cout << compareMax(1, 2) << endl;
cout << compareMax("asda", "qweq") << endl;
system("pause");
return 0;
}

View File

@@ -0,0 +1,40 @@
/* 输入输出重载.cpp */
#include <iostream>
using namespace std;
#include <cstring>
class Sales {
private:
char name[10];
char id[18];
int age;
public:
Sales(char *Name, char *ID, int Age);
friend Sales &operator<<(ostream &os, Sales &s);
friend Sales &operator>>(istream &is, Sales &s);
};
Sales::Sales(char *Name, char *ID, int Age) {
strcpy(name, Name);
strcpy(id, ID);
age = Age;
}
Sales &operator<<(ostream &os, Sales &s) {
os << s.name << "\t" << s.id << "\t" << s.age << endl;
return s;
}
Sales &operator>>(istream &is, Sales &s) {
cout << "输入雇员的姓名,身份证,年龄:\n";
is >> s.name >> s.id >> s.age;
}
int main(int argc, char const *argv[]) {
Sales s("张三", "15611", 26);
cout << s;
cin >> s;
cout << s;
return 0;
}

View File

@@ -0,0 +1,43 @@
/* 输入输出运算符重载.cpp */
/*
有一销售人员类Sales其数据成员有姓名name身份证号id年龄age。
重载输入/输出运算符实现对Sales类数据成员的输入和输出。
*/
#include <cstring>
#include <iostream>
using namespace std;
class Sales {
private:
char name[10];
char id[18];
int age;
public:
Sales(char *Name, char *ID, int Age);
friend Sales &operator<<(ostream &os, Sales &s); //重载输出运算符
friend Sales &operator>>(istream &is, Sales &s); //重载输入运算符
};
Sales::Sales(char *Name, char *ID, int Age) {
strcpy(name, Name);
strcpy(id, ID);
age = Age;
}
Sales &operator<<(ostream &os, Sales &s) {
os << s.name << "\t"; //输出姓名
os << s.id << "\t"; //输出身份证号
os << s.age << endl; //输出年龄
return s;
}
Sales &operator>>(istream &is, Sales &s) {
cout << "输入雇员的姓名,身份证号,年龄" << endl;
is >> s.name >> s.id >> s.age;
return s;
}
int main() {
Sales s1("杜康", "214198012111711", 40); // L1
cout << s1; // L2
cout << endl; // L3
cin >> s1; // L4
cout << s1; // L5
}

View File

@@ -0,0 +1,80 @@
/* 字典插入与查找.cpp */
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
int main(int argc, char const *argv[]) {
map<const char *, const char *> mp;
map<const char *, const char *>::iterator iter;
const char key[3][20] = {"img", "system", "ip"};
const char value[3][20] = {"d:/a.img", "win7", "193.68.6.3"};
// make_pair插入
for (int i = 0; i < 2; i++) {
mp.insert(make_pair(key[i], value[i]));
}
// pair<const char*,const char*>插入
mp.insert(pair<const char *, const char *>(key[2], value[2]));
//数组插入方式
mp["addr"] = "中国";
//迭代器取出元素
//循环取出元素
for (iter = mp.begin(); iter != mp.end(); iter++) {
cout << iter->first << "\t" << iter->second << endl;
}
char key1[20];
cout << "请输入按key查找";
cin.getline(key1, 20);
//查找元素
for (iter = mp.begin(); iter != mp.end(); iter++) {
if (strcmp(iter->first, key1) == 0) {
cout << iter->first << "查找出来了!"
<< "对应的值为:" << iter->second << endl;
}
}
//第一种删除方式
// find只能用于查找数组建立的形式并且不支持输入用cin等
iter = mp.find("addr");
if (iter != mp.end()) {
cout << iter->first << "按照key查找出来了"
<< "对应的value为" << iter->second << endl;
cout << "开始删除元素!" << endl;
mp.erase(iter);
// break;
}
//第二种方式删除
//按照key删除元素
char drop_key[20];
//按照value删除元素
char drop_value[20];
cout << "请输入按key删除";
cin.getline(drop_key, 20);
cout << "请输入按value删除";
cin.getline(drop_value, 20);
for (iter = mp.begin(); iter != mp.end(); iter++) {
if (strcmp(iter->first, drop_key) == 0) {
cout << iter->first << "按照key查找出来了"
<< "对应的value为" << iter->second << endl;
cout << "开始删除元素!" << endl;
mp.erase(iter);
break;
}
if (strcmp(iter->second, drop_value) == 0) {
cout << iter->second << "value查找出来了"
<< "对应的key为" << iter->first << endl;
cout << "开始删除元素!" << endl;
mp.erase(iter);
break;
}
}
cout << "------删除元素以后--------\n";
//循环取出元素
for (iter = mp.begin(); iter != mp.end(); iter++) {
cout << iter->first << "\t" << iter->second << endl;
}
return 0;
}

View File

@@ -0,0 +1,41 @@
/* 运算符重载之强制转换.cpp */
/*
有一个类Circle设计该类的类型转换函数当将Circle对象转换成int型时返回圆的半径当将它转换成double型时就返回圆的周长当将它转换成float型时就返回圆的面积。
*/
/*
类型转换函数没有参数。
类型转换函数没有返回类型。
类型转换函数必须返回将要转换成的type类型数据。
*/
#include <iostream>
using namespace std;
class Circle {
private:
double x, y, r;
public:
Circle(double x1, double y1, double r1) {
x = x1;
y = y1;
r = r1;
}
operator int() { return int(r); }
operator double() { return 2 * 3.14 * r; }
operator float() { return (float)3.14 * r * r; }
};
int main() {
Circle c(2.3, 3.4, 2.5);
int r = c; //调用operator int()将Circle类型转换成int
double length = c; //调用operator double()转换成double
float area = c; //调用operator float()将Circle类型转换成float
double len = (double)c; //将Cirlce类型对象强制转换成double
cout << r << endl;
cout << length << endl;
cout << len << endl;
cout << area << endl;
}

View File

@@ -0,0 +1,25 @@
/* 重载圆括号的时钟.cpp */
#include <iostream>
using namespace std;
class Time {
private:
int hh, mm, ss;
public:
Time(int h = 0, int m = 0, int s = 0) : hh(h), mm(m), ss(s) {}
void operator()(int h, int m, int s) {
hh = h;
mm = m;
ss = s;
}
void ShowTime() { cout << hh << ":" << mm << ":" << ss << endl; }
};
int main() {
Time t1(12, 10, 11);
t1.ShowTime();
t1.operator()(23, 20, 34);
t1.ShowTime();
t1(10, 10, 10);
t1.ShowTime();
}

View File

@@ -0,0 +1,19 @@
/* 输出格式.cpp */
#include <iomanip>
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
char s[20] = "this is a string";
double digit = -36.96656;
cout << setw(30) << left << setfill('*') << s << endl;
cout << dec << setprecision(4) << digit << endl;
cout << dec << 15 << endl;
// setbase(int x)设置进制后,后面所有操作都是按照这个进制来计算!
cout << setbase(10) << 15 << endl;
//四舍五入,并保留2位有效数组
float x = 6.6937;
cout << float(int(x * 1000 + 0.5) / 1000.0) << endl;
return 0;
}

View File

@@ -0,0 +1,87 @@
/* 继承与封装.cpp */
#include <cstring>
#include <iostream>
using namespace std;
class Employee {
public:
Employee(const char *name, const char *id) {
strcpy(Name, name);
strcpy(Id, id);
}
char *getName() { return Name; }
char *getId() { return Id; }
void display() { cout << Name << "\t" << Id << endl; }
private:
char Name[20];
char Id[20];
};
class Manager : public Employee {
public:
//直接调用构造方法传递,基类构造方法有参数,派生类必须通过构造方法,在初始化列表中传递参数
Manager(const char *name, const char *id, int week) : Employee(name, id) {
WeeklySalary = week * 1000;
}
void display() {
cout << "经理:" << getName() << "\t" << getId() << "\t" << WeeklySalary
<< endl;
}
private:
int WeeklySalary;
};
class SaleWorker : public Employee {
public:
SaleWorker(const char *name, const char *id, int profit, int x)
: Employee(name, id) {
workerMoney = baseMoney + x * 0.05 * profit;
}
void display() {
cout << "销售员:" << getName() << "\t" << getId() << "\t" << workerMoney
<< endl;
}
private:
float baseMoney = 800.0;
float workerMoney;
};
class HourWorker : public Employee {
public:
HourWorker(const char *name, const char *id, int h) : Employee(name, id) {
TotalMoney = h * hourMoney;
}
void display() {
cout << "小时工:" << getName() << "\t" << getId() << "\t" << TotalMoney
<< endl;
}
private:
float hourMoney = 100.0;
float TotalMoney;
};
int main(int argc, char const *argv[]) {
cout << "请输入工作周:";
int week;
cin >> week;
Manager m("小王", "11111111", week);
m.display();
cout << "请输入销售利润:";
int profit;
cin >> profit;
cout << "请输入销售件数:";
int x;
cin >> x;
SaleWorker s("小李", "222222", profit, x);
s.display();
cout << "请输入工作小时:";
int hour;
cin >> hour;
HourWorker h("小何", "333333", hour);
h.display();
return 0;
}

View File

@@ -0,0 +1,74 @@
/* 读写文件综合题.cpp */
#include <cstring>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
class Person {
public:
Person() {}
Person(char *name, char *id, int math, int chinese, int english) {
strcpy(Name, name);
strcpy(Id, id);
Math = math;
Chinese = chinese;
English = english;
Sum = Math + Chinese + English;
}
void display() {
cout << Name << "\t" << Id << "\t" << Math << "\t" << Chinese << "\t"
<< English << "\t" << Sum << endl;
}
private:
char Name[20];
char Id[20];
int Math;
int Chinese;
int English;
int Sum;
};
int main(int argc, char const *argv[]) {
char ch;
char Name[20], Id[20];
int Math, Chinese, English;
fstream ioFile;
ioFile.open("d:/per.dat", ios::out | ios::app);
cout << "---------建立学生档案信息----------\n";
do {
cout << "请输入姓名:";
cin >> Name;
cout << "请输入身份证号:";
cin >> Id;
cout << "请输入数学成绩:";
cin >> Math;
cout << "请输入汉语成绩:";
cin >> Chinese;
cout << "请输入英语成绩:";
cin >> English;
Person per(Name, Id, Math, Chinese, English);
ioFile.write((char *)&per, sizeof(per));
cout << "您是否继续建档?(Y/y) ";
cin >> ch;
} while (ch == 'y' || ch == 'Y');
ioFile.close();
ioFile.open("d://per.dat", ios::in);
Person p;
ioFile.read((char *)&p, sizeof(p));
vector<Person> v;
vector<Person>::iterator vt;
while (!ioFile.eof()) {
v.push_back(p);
ioFile.read((char *)&p, sizeof(p));
}
ioFile.close();
cout << "---------输出档案信息-----------" << endl;
for (vt = v.begin(); vt != v.end(); vt++) {
(*vt).display();
}
return 0;
}

View File

@@ -0,0 +1,60 @@
/* 类模板之栈.cpp */
#include <iostream>
using namespace std;
template <typename T, int MAXSIZE>
class Stack {
public:
Stack() {}
void init() { top = -1; }
bool isFull() {
if (top >= MAXSIZE - 1)
return true;
else
return false;
}
bool isEmpty() {
if (top == -1)
return true;
else
return false;
}
void push(T e);
T pop();
private:
T elems[MAXSIZE];
int top;
};
template <typename T, int MAXSIZE> void Stack<T, MAXSIZE>::push(T e) {
if (!isFull()) {
elems[++top] = e;
} else {
cout << "栈已满,请不要再加入元素!";
return;
}
}
template <typename T, int MAXSIZE> T Stack<T, MAXSIZE>::pop() {
if (!isEmpty()) {
return elems[top--];
} else {
cout << "栈已空,请不要再弹出元素!";
return 0;
}
}
int main(int argc, char const *argv[]) {
Stack<int, 10> s1;
s1.init();
int i;
for (i = 1; i < 11; i++)
s1.push(i);
for (i = 1; i < 11; i++)
cout << s1.pop() << "\t";
cout << endl;
return 0;
}

View File

@@ -0,0 +1,25 @@
/* 异常捕获.cpp */
#include <iostream>
using namespace std;
//函数异常可以抛出去由主函数来处理
void fun(int x) {
try {
if (x == 0)
throw "异常";
} catch (...) {
cout << "in fun" << endl;
throw 1;
}
}
int main(int argc, char const *argv[]) {
try {
fun(0);
} catch (int n) {
cout << "in main" << endl;
}
return 0;
}

View File

@@ -1,58 +0,0 @@
#include <iostream>
#include <cstring>
using namespace std;
struct Person
{ //职工基本信息的结构
double salary;
char *name;
};
class SalaryManaege
{
Person *employ; //存放职工信息的数组
int max; //数组下标上界
int n; //数组中的实际职工人数
public:
SalaryManaege(int Max = 0)
{
max = Max;
n = 0;
employ = new Person[max];
}
//返回引用特性是可以直接在放在左值,直接使用
double &operator[](char *Name)
{ //重载[],返回引用
Person *p;
for (p = employ; p < employ + n; p++)
//如果存在处理
if (strcmp(p->name, Name) == 0)
return p->salary;
//不存在情况处理
p = employ + n++;
p->name = new char[strlen(Name) + 1];
strcpy(p->name, Name);
p->salary = 0;
return p->salary;
}
void display()
{
for (int i = 0; i < n; i++)
cout << employ[i].name << " " << employ[i].salary << endl;
}
~SalaryManaege() {
delete[] employ;
}
};
int main()
{
SalaryManaege s(3);
s["张三"] = 2188.88;
s["里斯"] = 1230.07;
s["王无"] = 3200.97;
cout << "张三\t" << s["张三"] << endl;
cout << "里斯\t" << s["里斯"] << endl;
cout << "王无\t" << s["王无"] << endl;
cout << "-------下为display的输出--------\n\n";
s.display();
}

View File

@@ -1,14 +0,0 @@
#include<iostream>
#include<cstring>
int main() {
char *sPtr;
const char* s="hello";
sPtr = new char[strlen(s)+1];
strncpy(sPtr,s,strlen(s));
std::cout << sPtr << std::endl;
delete sPtr;
return 0;
}

View File

@@ -1,78 +0,0 @@
#include<iostream>
#include<map>
#include<cstring>
using namespace std;
int main(int argc, char const *argv[])
{
map<const char*,const char*> mp;
map<const char*,const char*>::iterator iter;
const char key[3][20]={"img","system","ip"};
const char value[3][20]={"d:/a.img","win7","193.68.6.3"};
//make_pair插入
for(int i=0;i<2;i++){
mp.insert(make_pair(key[i],value[i]));
}
//pair<const char*,const char*>插入
mp.insert(pair<const char*,const char*>(key[2],value[2]));
//数组插入方式
mp["addr"]="中国";
//迭代器取出元素
//循环取出元素
for(iter=mp.begin();iter!=mp.end();iter++){
cout<<iter->first<<"\t"<<iter->second<<endl;
}
char key1[20];
cout<<"请输入按key查找";
cin.getline(key1,20);
//查找元素
for(iter=mp.begin();iter!=mp.end();iter++){
if(strcmp(iter->first,key1)==0){
cout<<iter->first<<"查找出来了!"<<"对应的值为:"<<iter->second<<endl;
}
}
//第一种删除方式
//find只能用于查找数组建立的形式并且不支持输入用cin等
iter=mp.find("addr");
if(iter!=mp.end()){
cout<<iter->first<<"按照key查找出来了"<<"对应的value为"<<iter->second<<endl;
cout<<"开始删除元素!"<<endl;
mp.erase(iter);
// break;
}
//第二种方式删除
//按照key删除元素
char drop_key[20];
//按照value删除元素
char drop_value[20];
cout<<"请输入按key删除";
cin.getline(drop_key,20);
cout<<"请输入按value删除";
cin.getline(drop_value,20);
for(iter=mp.begin();iter!=mp.end();iter++){
if(strcmp(iter->first,drop_key)==0){
cout<<iter->first<<"按照key查找出来了"<<"对应的value为"<<iter->second<<endl;
cout<<"开始删除元素!"<<endl;
mp.erase(iter);
break;
}
if(strcmp(iter->second,drop_value)==0){
cout<<iter->second<<"value查找出来了"<<"对应的key为"<<iter->first<<endl;
cout<<"开始删除元素!"<<endl;
mp.erase(iter);
break;
}
}
cout<<"------删除元素以后--------\n";
//循环取出元素
for(iter=mp.begin();iter!=mp.end();iter++){
cout<<iter->first<<"\t"<<iter->second<<endl;
}
system("pause");
return 0;
}

View File

@@ -1,25 +0,0 @@
#include<iostream>
using namespace std;
//函数异常可以抛出去由主函数来处理
void fun(int x){
try{
if (x==0)
throw "异常";
}catch(...){
cout<<"in fun"<<endl;
throw 1;
}
}
int main(int argc, char const *argv[])
{
try{
fun(0);
}catch(int n){
cout<<"in main"<<endl;
}
system("pause");
return 0;
}

View File

@@ -1,93 +0,0 @@
#include<iostream>
#include<cmath>
using namespace std;
/*
* 时钟类
*/
class Clock
{
private:
int Hour, Minute, Second;
public:
Clock(int h=0, int m=0, int s=0);
void ShowTime();
Clock& operator ++();
Clock operator ++(int);
};
/*
* 时钟类构造函数
*/
Clock::Clock(int h,int m, int s)
{
if(h>=0 && h<24 && m>=0 && m<60 && s>=0 && s<60)
{
Hour = h;
Minute =m;
Second= s;
}
else
cout<<"输入的时间格式错误!"<<endl;
}
/*
* 显示时间
*/
void Clock::ShowTime()
{
cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
/*
* 时间递增一秒(重载前缀++运算符)
*/
Clock& Clock::operator ++()
{
Second++;
if (Second >= 60)
{
Second = Second - 60;
Minute++;
if (Minute >= 60)
{
Minute = Minute - 60;
Hour++;
Hour = Hour % 24;
}
}
return *this;
}
/*
* 时间递增一秒(重载后缀++运算符)
*/
Clock Clock::operator ++(int)
{
Clock old = *this;
++(*this);
return old;
}
/*
* 主函数
*/
int main()
{
Clock myClock(23,59,59);
cout<<"初始化显示时间为:\t\t";
myClock.ShowTime();
cout<<"执行myClock++后的时间为:\t";
//先执行ShowTime()输出myClock=23:59:59
//再执行myClock++此时myClock=00:00:00
(myClock++).ShowTime();
cout<<"执行++myClock后的时间为:\t";
//先执行++myClock此时myClock=00:00:01
//再执行ShowTime()输出myClock=00:00:01
(++myClock).ShowTime();
system("pause");
}

View File

@@ -1,63 +0,0 @@
#include<iostream>
using namespace std;
template<typename T,int MAXSIZE>
class Stack{
public:
Stack(){}
void init(){
top=-1;
}
bool isFull(){
if(top>=MAXSIZE-1)
return true;
else
return false;
}
bool isEmpty(){
if(top==-1)
return true;
else
return false;
}
void push(T e);
T pop();
private:
T elems[MAXSIZE];
int top;
};
template<typename T,int MAXSIZE> void Stack<T,MAXSIZE>::push(T e){
if(!isFull()){
elems[++top]=e;
}
else{
cout<<"栈已满,请不要再加入元素!";
return;
}
}
template<typename T,int MAXSIZE> T Stack<T,MAXSIZE>::pop(){
if(!isEmpty()){
return elems[top--];
}
else{
cout<<"栈已空,请不要再弹出元素!";
return 0;
}
}
int main(int argc, char const *argv[])
{
Stack<int,10> s1;
s1.init();
int i;
for(i=1;i<11;i++)
s1.push(i);
for(i=1;i<11;i++) cout<<s1.pop()<<"\t";
cout<<endl;
system("pause");
return 0;
}

View File

@@ -1,76 +0,0 @@
#include<iostream>
#include<cstring>
using namespace std;
#define MAXSIZE 5
template<class T>
class Array{
public:
Array(){
for(int i=0;i<MAXSIZE;i++){
array[i]=0;
}
}
T &operator[](int i);
void Sort();
private:
T array[MAXSIZE];
};
template<class T> T& Array<T>::operator[](int i){
if(i<0||i>MAXSIZE-1){
cout<<"数组下标越界!"<<endl;
exit(0);
}
return array[i];
}
template<class T> void Array<T>::Sort(){
int p,j;
for(int i=0;i<MAXSIZE-1;i++){
p=i;
for(j=i+1;j<MAXSIZE;j++){
if(array[p]<array[j])
p=j;
}
T t;
t=array[i];
array[i]=array[p];
array[p]=t;
}
}
template<> void Array<char *>::Sort(){
int p,j;
for(int i=0;i<MAXSIZE-1;i++){
p=i;
for(j=i+1;j<MAXSIZE;j++){
if(strcmp(array[p],array[j])<0)
p=j;
}
char* t=array[i];
array[i]=array[p];
array[p]=t;
}
}
int main(int argc, char const *argv[])
{
Array<int> a1;
Array<char*>b1;
a1[0]=1;a1[1]=23;a1[2]=6;
a1[3]=3; a1[4]=9;
a1.Sort();
for(int i=0;i<5;i++)
cout<<a1[i]<<"\t";
cout<<endl;
b1[0]="x1"; b1[1]="ya"; b1[2]="ad";
b1[3]="be"; b1[4]="bc";
b1.Sort();
for(int i=0;i<5;i++)
cout<<b1[i]<<"\t";
cout<<endl;
system("pause");
return 0;
}

View File

@@ -1,90 +0,0 @@
#include<iostream>
#include<cstring>
using namespace std;
class Employee{
public:
Employee(const char *name,const char *id){
strcpy(Name,name);
strcpy(Id,id);
}
char* getName(){
return Name;
}
char* getId(){
return Id;
}
void display(){
cout<<Name<<"\t"<<Id<<endl;
}
private:
char Name[20];
char Id[20];
};
class Manager:public Employee{
public:
//直接调用构造方法传递,基类构造方法有参数,派生类必须通过构造方法,在初始化列表中传递参数
Manager(const char *name,const char *id,int week):Employee(name,id){
WeeklySalary=week*1000;
}
void display(){
cout<<"经理:"<<getName()<<"\t"<<getId()<<"\t"<<WeeklySalary<<endl;
}
private:
int WeeklySalary;
};
class SaleWorker:public Employee{
public:
SaleWorker(const char*name,const char *id,int profit,int x):Employee(name,id){
workerMoney=baseMoney+x*0.05*profit;
}
void display(){
cout<<"销售员:"<<getName()<<"\t"<<getId()<<"\t"<<workerMoney<<endl;
}
private:
float baseMoney=800.0;
float workerMoney;
};
class HourWorker:public Employee{
public:
HourWorker(const char*name,const char *id,int h):Employee(name,id){
TotalMoney=h*hourMoney;
}
void display(){
cout<<"小时工:"<<getName()<<"\t"<<getId()<<"\t"<<TotalMoney<<endl;
}
private:
float hourMoney=100.0;
float TotalMoney;
};
int main(int argc, char const *argv[])
{
cout<<"请输入工作周:";
int week;
cin>>week;
Manager m("小王","11111111",week);
m.display();
cout<<"请输入销售利润:";
int profit;
cin>>profit;
cout<<"请输入销售件数:";
int x;
cin>>x;
SaleWorker s("小李","222222",profit,x);
s.display();
cout<<"请输入工作小时:";
int hour;
cin>>hour;
HourWorker h("小何","333333",hour);
h.display();
system("pause");
return 0;
}

View File

@@ -1,73 +0,0 @@
#include<iostream>
#include<fstream>
#include<cstring>
#include<vector>
using namespace std;
class Person{
public:
Person(){}
Person(char *name,char *id,int math,int chinese,int english){
strcpy(Name,name);
strcpy(Id,id);
Math=math;
Chinese=chinese;
English=english;
Sum=Math+Chinese+English;
}
void display(){
cout<<Name<<"\t"<<Id<<"\t"<<Math<<"\t"<<Chinese<<"\t"<<English<<"\t"<<Sum<<endl;
}
private:
char Name[20];
char Id[20];
int Math;
int Chinese;
int English;
int Sum;
};
int main(int argc, char const *argv[])
{
char ch;
char Name[20],Id[20];
int Math,Chinese,English;
fstream ioFile;
ioFile.open("d:/per.dat",ios::out|ios::app);
cout<<"---------建立学生档案信息----------\n";
do{
cout<<"请输入姓名:";
cin>>Name;
cout<<"请输入身份证号:";
cin>>Id;
cout<<"请输入数学成绩:";
cin>>Math;
cout<<"请输入汉语成绩:";
cin>>Chinese;
cout<<"请输入英语成绩:";
cin>>English;
Person per(Name,Id,Math,Chinese,English);
ioFile.write((char *)&per,sizeof(per));
cout<<"您是否继续建档?(Y/y) ";
cin>>ch;
}while(ch=='y'||ch=='Y');
ioFile.close();
ioFile.open("d://per.dat",ios::in);
Person p;
ioFile.read((char*)&p,sizeof(p));
vector<Person> v;
vector<Person>::iterator vt;
while(!ioFile.eof()){
v.push_back(p);
ioFile.read((char*)&p,sizeof(p));
}
ioFile.close();
cout<<"---------输出档案信息-----------"<<endl;
for(vt=v.begin();vt!=v.end();vt++){
(*vt).display();
}
system("pause");
return 0;
}

View File

@@ -1,41 +0,0 @@
/*
有一销售人员类Sales其数据成员有姓名name身份证号id年龄age。
重载输入/输出运算符实现对Sales类数据成员的输入和输出。
*/
#include<iostream>
#include<cstring>
using namespace std;
class Sales{
private:
char name[10];
char id[18];
int age;
public:
Sales(char *Name,char *ID,int Age);
friend Sales &operator<<(ostream &os,Sales &s); //重载输出运算符
friend Sales &operator>>(istream &is,Sales &s); //重载输入运算符
};
Sales::Sales(char *Name,char *ID,int Age) {
strcpy(name,Name);
strcpy(id,ID);
age=Age;
}
Sales& operator<<(ostream &os,Sales &s) {
os<<s.name<<"\t"; //输出姓名
os<<s.id<<"\t"; //输出身份证号
os<<s.age<<endl; //输出年龄
return s;
}
Sales &operator>>(istream &is,Sales &s) {
cout<<"输入雇员的姓名,身份证号,年龄"<<endl;
is>>s.name>>s.id>>s.age;
return s;
}
int main(){
Sales s1("杜康","214198012111711",40); //L1
cout<<s1; //L2
cout<<endl; //L3
cin>>s1; //L4
cout<<s1; //L5
}

View File

@@ -1,41 +0,0 @@
#include<iostream>
using namespace std;
#include<cstring>
class Sales{
private:
char name[10];
char id[18];
int age;
public:
Sales(char *Name,char *ID,int Age);
friend Sales &operator<<(ostream &os,Sales &s);
friend Sales &operator>>(istream &is,Sales &s);
};
Sales::Sales(char *Name,char *ID,int Age){
strcpy(name,Name);
strcpy(id,ID);
age=Age;
}
Sales &operator<<(ostream&os,Sales &s){
os<<s.name<<"\t"<<s.id<<"\t"<<s.age<<endl;
return s;
}
Sales &operator>>(istream&is,Sales &s){
cout<<"输入雇员的姓名,身份证,年龄:\n";
is>>s.name>>s.id>>s.age;
}
int main(int argc, char const *argv[])
{
Sales s("张三","15611",26);
cout<<s;
cin>>s;
cout<<s;
system("pause");
return 0;
}

View File

@@ -1,19 +0,0 @@
#include<iostream>
#include<iomanip>
using namespace std;
int main(int argc, char const *argv[])
{
char s[20]="this is a string";
double digit=-36.96656;
cout<<setw(30)<<left<<setfill('*')<<s<<endl;
cout<<dec<<setprecision(4)<<digit<<endl;
cout<<dec<<15<<endl;
//setbase(int x)设置进制后,后面所有操作都是按照这个进制来计算!
cout<<setbase(10)<<15<<endl;
//四舍五入,并保留2位有效数组
float x=6.6937;
cout<<float(int(x*1000+0.5)/1000.0)<<endl;
system("pause");
return 0;
}

View File

@@ -1,35 +0,0 @@
/*
有一个类Circle设计该类的类型转换函数当将Circle对象转换成int型时返回圆的半径当将它转换成double型时就返回圆的周长当将它转换成float型时就返回圆的面积。
*/
/*
类型转换函数没有参数。
类型转换函数没有返回类型。
类型转换函数必须返回将要转换成的type类型数据。
*/
#include <iostream>
using namespace std;
class Circle{
private:
double x,y,r;
public:
Circle(double x1,double y1,double r1){x=x1;y=y1;r=r1; }
operator int(){return int(r);}
operator double(){return 2*3.14*r;}
operator float(){return (float)3.14*r*r;}
};
int main(){
Circle c(2.3,3.4,2.5);
int r=c; //调用operator int()将Circle类型转换成int
double length=c; //调用operator double()转换成double
float area=c; //调用operator float()将Circle类型转换成float
double len=(double) c; //将Cirlce类型对象强制转换成double
cout<<r<<endl;
cout<<length<<endl;
cout<<len<<endl;
cout<<area<<endl;
system("pause");
}

View File

@@ -1,25 +0,0 @@
#include <iostream>
using namespace std;
class Time{
private:
int hh,mm,ss;
public:
Time(int h=0,int m=0,int s=0):hh(h),mm(m),ss(s){}
void operator()(int h,int m,int s) {
hh=h;
mm=m;
ss=s;
}
void ShowTime(){
cout<<hh<<":"<<mm<<":"<<ss<<endl;
}
};
int main(){
Time t1(12,10,11);
t1.ShowTime();
t1.operator()(23,20,34);
t1.ShowTime();
t1(10,10,10);
t1.ShowTime();
system("pause");
}