目录
一,
二.
初级
1. 元素逆置
2.冒泡排序
3helloworld!
4.7的游戏
5.99乘法表
6.结构体始
7.3个老师分别管理5个学生
8.对结构中根据年龄排序
9.简易版通讯录
初级+1
10.堆区创建整型数组(new,delete)
11.运用类,求圆的周长
12.实现学生类,方法可以给学生信息赋值,方法可以显示学生信息
13.1.失败的深浅拷贝函数
13.2.修改
14.返回对象本身用*this的实例
15.测试友元
16.重载加法+
17.步入多态.始
17.1)多态实现计算器,仅仅加减法
17.2)多态实现零件组成成品(有点乱)
18.文件操作.始(很大欠缺)
浪费了许多时间练习,真尴尬呀,小小要求都写不出来,这里就用来...新手一个.
1.1
#include
using namespace std;
void text() {
int arr[7] = { 1,2,3,4,5,6 };
int arr1[10] = {};
for (int i = 0;i < sizeof(arr) / sizeof(arr[0])-1;i++) {
//arr1[sizeof(arr) - i - 1] = arr[0];
arr1[5 - i] = arr[i];
}
for (int i = 0;i < 6;i++) {
cout << arr1[i]<<"\n";
}
cout << sizeof(arr)/sizeof(arr[0]);
}
int main() {
text();
system("pause");
return 0;
}
结果:6543217
通过创建新的数组,将原数组开头放入目标数组尾部,依次加减对换
1.2
void text() {
int arr[] = { 1,2,3,4,5,6 };
int start = 0;
int end = sizeof(arr) / sizeof(arr[0])-1;
for (;start < end;start++,end--) {
int tep = 0;
tep = arr[start];
arr[start] = arr[end];
arr[end] = tep;
}
for (int i = 0;i < 6;i++) {
cout << arr[i]<<"\n";
}
}
结果654321
通过下标的方式,从数组开头结尾向中间两两对换
2.1
void text() {
int arr[] = { 5,2,4,1,7,9,3 };
int longs = sizeof(arr) / sizeof(arr[0]);
for (int i = 0;i < longs;i++) {
for (int j = i;j < longs;j++) {
if (arr[i] > arr[j]) {
int tep = 0;
tep = arr[i];arr[i] = arr[j];arr[j] = tep;
}
}
}
for (int i = 0;i < sizeof(arr) / sizeof(arr[0]);i++) {
cout << arr[i]<<"\n";
}
}
结果1234579
通过外循环对数组进行依次取数,内循环进行与该数后面的数进行比较,将较小的放入开头
#include
using namespace std;
#include
int main() {
cout << "hello world!" << endl;
system("pause");
return 0;
}
一切的开始了,加油
1-100含7,7的倍数打印敲桌子
void text() {
for (int i = 1;i <= 100;i++) {
if (i % 10 == 7 || i / 10 == 7 || i % 7 == 0) {
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
}
结果便是1-100中含7和7的倍数的数字都用敲桌子进行替换
%10得到的是个位数为7、/10得到的是十位数为7、%7求的是7的倍数的数字
void text() {
for (int i = 1;i <= 9;i++) {
for (int j = 1;j <= i;j++) {
cout << j << "*" << i << "=" << i * j<<"\t";
}
cout << endl;
}
}
两个循环求乘法表
6.1
void text() {
struct Student {
string name;
int age;
int score;
}stu2;//1
struct Student stu1;//2
stu1.age = 18;
stu1.score = 99;
stu1.name = "张三";
cout << stu1.name<<"\t" << stu1.age << "岁\t" << "语文成绩:"<
用了两种创建方式,通过结构体创建了张三、李四两个实体变量
6.2
struct Teacher {
string name;
struct Student arr[50];
};
Teacher tea1 = { "李老师"};
tea1.arr[0] = { "赵六",19,99 };
cout << tea1.arr->age << endl;
主要表现为结构体成员可以是结构体
#include
using namespace std;
#include
#include
void text() {
srand((unsigned int)time(NULL));//随机数
struct Student {
string name;
int age;
int score;
};
struct Teacher{
string name;
struct Student Sarr[5];
};
struct Teacher Tarr[3];
string minzi = "ABCDE";
for (int i = 0;i < 3;i++) {
string ls = "teacher_";
Tarr[i].name = ls + minzi[i];
for (int j = 0;j < 5;j++) {
string tx = "student_";
Tarr[i].Sarr[j].name = tx + minzi[j];
int random = rand() % 61 + 40;//随机数编成绩
Tarr[i].Sarr[j].age = 19;
Tarr[i].Sarr[j].score = random;
}
}
for (int i = 0;i < 3;i++) {
cout << Tarr[i].name << "\t" << "\t" << "\t" << "年龄\t" << "\t" << "成绩\t " << endl;
for (int j = 0;j < 5;j++) {
cout << "\t" << Tarr[i].Sarr[j].name <<"\t"
<< "\t" << Tarr[i].Sarr[j].age << "\t"
<< "\t" << Tarr[i].Sarr[j].score << endl;
}
}
int main() {
text();
system("pause");
return 0;
}
定义学生、老师两个结构体,一个老师结构体数组。
运用随机数对学生成绩进行赋值
运用字符串对老师、学生进行快速命名
#include
using namespace std;
#include
struct Yinxiong {
string name;
int age;
string sex;
};
void paixu(struct Yinxiong Ya[], int len) {
for (int i = 0;i < len;i++) {
for (int j = 0;j < len - i-1;j++) {
if (Ya[j].age > Ya[j + 1].age) {
struct Yinxiong tem;
tem = Ya[j];
Ya[j] = Ya[j + 1];
Ya[j + 1] = tem;
}
}
}
}
void text() {
struct Yinxiong Yarr[5] = {
{"张三",19,"男"},
{"bb",22,"男"},
{"cb",18,"女"},
{"db",17,"女"},
{"eb",27,"男"},
};
int len = sizeof(Yarr) / sizeof(Yarr[0]);
for (int i = 0;i < len;i++) {
cout << Yarr[i].name << "\t" << Yarr[i].age << "\t" << Yarr[i].sex << endl;
}
cout << endl;
paixu(Yarr, len);
for (int i = 0;i < len;i++) {
cout << Yarr[i].name << "\t" << Yarr[i].age << "\t" << Yarr[i].sex << endl;
}
}
int main() {
text();
system("pause");
return 0;
}
记录:
整体写的比较繁琐
对于struct Yinxiong 的结构体定义需要弄成全局变量 方便其他函数在定义与传参数时,前后能够相匹配。
22/05/29
#include
using namespace std;
#include
#define MAX 100
void showmeau() {//显示目录函数
cout << "\t欢迎使用通讯录" << endl;
cout << "\t\t请选择0-4" << endl;
cout << "\t1.添加人员" << endl;
cout << "\t2.显示人员" << endl;
cout << "\t3.查找人员" << endl;
cout << "\t4.删除人员" << endl;
cout << "\t0.退出" << endl;
}
void exitTuichu() {//退出通讯录函数
cout << "谢谢您的使用,成功退出" << endl;
return;
}
//定义人的结构
struct Person {
string name;
string eleDianhua;
};
struct Person personarr[MAX];//定义通讯录结构
int sizerenshu = 0;//记录存入的人数
void tianJiarenyuan() {//添加人员函数
if (sizerenshu <= MAX) {
string name1;
string eleDIanhua1;
Person per1;
cout << "请输入人的名字" << endl;
cin >> name1;
cout << "请输入人的电话" << endl;
cin >> eleDIanhua1;
per1.name = name1;
per1.eleDianhua = eleDIanhua1;
//int sizerenshu = sizeof(personarr) / sizeof(personarr[0]);
personarr[sizerenshu] = per1;
sizerenshu++;
cout << "添加成功" << endl;
}
else
{
cout << "人数已满,请做删减操作再添加" << endl;
return;
}
cout << endl;
}
void showPer() {//显示通讯录所有人函数
if (sizerenshu>0)
{
for (int i = 0;i < sizerenshu;i++) {
cout << "姓名\t" << personarr[i].name << "\t电话\t" << personarr[i].eleDianhua << endl;
}
}
else {
cout << "当前通讯录为空" << endl;
}
}
void Cazaoren() {//查找人函数,根据姓名查找
if (sizerenshu > 0) {
string name2;
cout << "请输入您要查找的人的名字" << endl;
cin >> name2;
for (int i = 0;i < sizerenshu;i++) {
if (personarr[i].name == name2) {
cout << "查找成功" << endl;
cout << "姓名\t" << personarr[i].name << "\t电话\t" << personarr[i].eleDianhua << endl;
}
else {
cout << "查找失败" << endl;
}
}
}
else {
cout << "当前通讯录为空" << endl;
}
}
void Sancu() {//人员删除函数
if (sizerenshu > 0) {
for (int i = 0;i < sizerenshu;i++) {
personarr[i] = {};
cout << "删除成功" << endl;
sizerenshu--;
}
}
else {
cout << "当前通讯录为空" << endl;
}
}
//测试函数
void text() {
while (true) {
showmeau();
int xuanZhe = 0;
int xuanze = 0;
cin >> xuanze;
xuanZhe = (int)xuanze;
cout << xuanze << endl;
cout << xuanZhe << endl;
switch (xuanZhe) {
case 1://1.添加人员
tianJiarenyuan();
system("pause");
system("cls");
break;
case 2://2.显示人员
showPer();
system("pause");
system("cls");
break;
case 3://3.查找人员
Cazaoren();
system("pause");
system("cls");
break;
case 4://4.删除人员
Sancu();
system("pause");
system("cls");
break;
case 0://0.退出
exitTuichu();
system("pause");
system("cls");
return;
}
}
}
//主函数
int main() {
text();
//int sizerenshu = sizeof(personarr) / sizeof(personarr[0]);
//cout << sizerenshu << endl;
system("pause");
}
//.通讯录
//1.添加人员
//2.显示人员
//3.查找人员
//4.删除人员
//0.退出
目的:
过程:
分析
目前已知问题:
0530
#include
using namespace std;
void text() {
int* arr = new int[10];//创建10整型数据的数组
for (int i = 0;i < 10;i++) {
arr[i] = i;
}
for (int j = 0;j < 10;j++) {
cout << arr[j] ;
}
cout << endl;
delete[] arr;//释放堆区数组
}
int main() {
text();
system("pause");
}
熟悉 new和delete关键字,一创建一释放delete关键字对数据的释放注意区分是数组还是简单变量
0530
//int c = 10;
class yuan1 {//定义圆类
public:
int banJin;//类内属性
double qiuZoucCan() {//类内方法
return 2 * PI * banJin;
}
//int abc() {
// return c;
//}
};
void text001() {
yuan1 y1;
y1.banJin = 10;
cout<<"半径为"<
熟悉类的定义,类的方法调用
实例类的类内方法可以调用实例本身类的成员属性,亦可以调用全局变量等
0530
class student {
public:
string m_name;
int m_age;
void setstu(string name,int age) {
m_name = name;
m_age = age;
}
void showstu() {
cout << "姓名:" << m_name << "\t年龄:" << m_age << endl;
}
};
void text() {
student st1;
st1.setstu("张三",18);
st1.showstu();
}
0530
class person {
public:
person(){
cout << "无参构造" << endl;
mage = 0;
mhhhe = 0;
}
person(int age, int hhhe) {
cout << "有参构造" << endl;
mage = age;
mhhhe = hhhe;
//int *mhhhe = new int(hhhe);
}
person(const person& p) {
cout << "拷贝构造" << endl;
mage = p.mage;
mhhhe = /*new*/ int(p.mhhhe);
}
~person() {
cout << "析构函数" << endl;
}
public:
int mage;
int mhhhe;
};
void text() {
person p1(10,20);
person p2(p1);
cout << p1.mage << endl;
cout << p1.mhhhe << endl;
//cout << p1.mhhhe << endl;
cout << p2.mage << endl;
cout << p2.mhhhe << endl;
//cout << p2.mhhhe << endl;
}
需要用到new,但是使用的话就会出问题
待更深理解再回复
0530
class person {
public:
person(){
cout << "无参构造" << endl;
mage = 0;
mhhhe = 0;
}
person(int age, int hhhe) {
cout << "有参构造" << endl;
mage = age;
mhhhe = new int(hhhe);
}
person(const person& p) {
cout << "拷贝构造" << endl;
mage = p.mage;
//mhhhe = p.mhhhe;//浅拷贝,默认拷贝
mhhhe = new int(*p.mhhhe);//深拷贝,重构拷贝
}
~person() {
if (mhhhe != NULL) {
delete mhhhe;
mhhhe = NULL;
}
cout << "析构函数" << endl;
}
public:
int mage;
int *mhhhe;
};
void text() {
person p1(10,20);
cout << p1.mage << endl;
cout << *p1.mhhhe << endl;
person p2(p1);
cout << p2.mage << endl;
cout << *p2.mhhhe << endl;
}
发生13.1及之后的问题的原因有:
0530
#include
using namespace std;
class person {
public:
person() {
}
person(int a) {
jiashu = a;
}
int jiashu;
//person addshu(person &p) {//结果为10和20//这两个&&我理解为可以调用本身的这个函数
// this->jiashu = this->jiashu + p.jiashu;
// //return *this;
// return jiashu;
//}
person& addshu(person& p) {//结果为10和30
this->jiashu = this->jiashu + p.jiashu;
//return *this;
return *this;
}
};
void text() {
person p1;
person p2(10);
p1.jiashu = 10;
p2.addshu(p1).addshu(p1);
cout << p1.jiashu << endl;
cout << p2.jiashu << endl;
}
int main() {
text();
system("pause");
return 0;
}
当不是 p2.addshu(p1).addshu(p1);而仅仅只是调用一次addshu()函数时,该函数返回类型可以为int,同时返回return *this;改为return this->jiashu;
0531
//定义一个具有公共、私有的建筑类
class buid {
friend void goodfri(buid& bu);
//friend void buid2::fri1(buid& bu);//定义没有意义
friend class buid2;
public:
buid() {//构造函数
roomsit1 = "keting ";
sitroom2 = "woshi";
}
public:
string roomsit1;
private:
string sitroom2;//私有变量
};
//为朋友的类外函数
void goodfri(buid& bu) {
cout << bu.roomsit1 << endl;
cout << bu.sitroom2 << endl;
}
//不是朋友的类外函数
void badfri(buid& bu) {
cout << bu.roomsit1 << endl;
//cout << bu.sitroom2 << endl;//报错,不可访问
}
//为朋友的类
class buid2 {
public:
void fri1(buid& bu) {
cout << bu.roomsit1 << endl;
cout << bu.sitroom2 << endl;
}
};
//不是朋友的类
class buid3 {
public:
void fri1(buid& bu) {
cout << bu.roomsit1 << endl;
//cout << bu.sitroom2 << endl;//报错,不可访问
}
};
//测试
void text() {
buid bu1;
goodfri(bu1);
badfri(bu1);
buid2 buu1;
buu1.fri1(bu1);
}
对于friend关键字修饰的类外函数,别的类,可以访问本身类的私有成员变量
class A {
friend class B;
public:
A() {
cout << "A 的构造函数" << endl;
age = 10;
bge = 20;
cge = 30;
}
~A() {
cout << "A 的析构函数" << endl;
}
public:
int age;
protected:
int bge;
private:
int cge;
};
class B {
public:
B() {
cout << "B 的构造函数" << endl;
a1 = new A;
}
~B() {
cout << "B 的析构函数" << endl;
if (a1 != NULL) {
delete a1;
a1 = NULL;
}
}
void visit() {
cout << a1->age << endl;
cout << a1->bge << endl;
}
private:
A* a1;
};
void text() {
B b1;
b1.visit();
}
//例 加法
class jia1 {
public:
int a ;
//通过成员函数重载
jia1 operator+ (jia1 jiaa) {//对应j5
jia1 temp;
temp.a = jiaa.a + this->a;
return temp;
}
};
jia1 jiafacongzai(jia1 jiaa, jia1 jiaaa) {//以函数方式,对应j4
jia1 temp;
temp.a = jiaa.a + jiaaa.a;
return temp;
}
//通过全局函数重载
//jia1 operator+(jia1 jiaa, jia1 jiaaa) {//关键字operator可简化函数名不写,对应j5
// jia1 temp;
// temp.a = jiaa.a + jiaaa.a;
// return temp;
//}
void jia() {
jia1 j1;
j1.a = 10;
jia1 j2;
j2.a = 20;
jia1 j3;
j3.a = j1.a + j2.a;//普通加法
cout << j3.a << endl;
jia1 j4;
j4 = jiafacongzai(j1, j2);
cout << j4.a << endl;
jia1 j5 = j1 + j2;
}
主要面对的是自定义类型的时候使用
class Animal {
public:
virtual void move() {
cout << "动物在走" << endl;
}
};
class cat :public Animal {
public:
void move() {
cout << "猫在走猫步" << endl;
}
};
class big :public Animal {
public:
void move() {
cout << "猪在跳舞" << endl;
}
};
void moveaaa(Animal& animal) {
animal.move();
}
void text() {
cout << "zhixin " << endl;
cat cat1;
moveaaa(cat1);
big big1;
moveaaa(big1);
}
当我们传入为猫的时候,结果就是猫的类的函数在执行
缺少virtual的时候,结果都为“动物在走”
class YuanSuanYuanLei {
public:
YuanSuanYuanLei() {
a = 0;
b = 0;
}
virtual int gett() {
return 12;
}
int a;
int b;
};
class jiaafa :public YuanSuanYuanLei {
public:
int gett() {
return a + b;
}
};
class jiannfa :public YuanSuanYuanLei {
public:
int gett() {
return a - b;
}
};
void text() {
//父类指针指向子类对象
YuanSuanYuanLei* abs1 = new jiaafa;
abs1->a = 100;
abs1->b = 23;
cout << abs1->gett() << endl;
delete abs1;
YuanSuanYuanLei* abs2 = new jiannfa;
abs2->a = 100;
abs2->b = 23;
cout << abs2->gett() << endl;
delete abs2;
}
//c公司,d公司
//A零件,B零件==可以组成e成品
//抽象AB零件,成品
class A {
public:
virtual void Asencen() = 0;
string minzi;
};
class B {
public:
virtual void Bsencen() = 0;
string minzi;
};
//class CenPinShiYong {
// 该部分多余,为成品生成的抽象,
// 可是下边并不需要,因为只有一种生成方式
//public:
// virtual ~CenPinShiYong() = 0;
// virtual void CenPin() = 0;
//};
//CenPinShiYong::~CenPinShiYong() {
//
//}
class cgsiA :public A {//c公司生产a
public:
cgsiA() {
minzi = "c公司";
}
void Asencen() {
cout << "c公司生产a" << endl;
}
string minzi;
};
class cgsiB :public B {//c公司生产B
public:
cgsiB() {
minzi = "c公司";
}
void Bsencen() {
cout << "c公司生产B" << endl;
}
string minzi;
};
class dgsiA :public A {//d公司生产a
public:
dgsiA() {
minzi = "d公司";
}
void Bsencen() {
cout << "d公司生产a" << endl;
}
string minzi;
};
class dgsiB :public B {//d公司生产B
public:
dgsiB() {
minzi = "d公司";
}
void Bsencen() {
cout << "d公司生产B" << endl;
}
string minzi;
};
//class CaiSiCenPin :public CenPinShiYong {//cai为kai
// 该部分多余
//public:
// CaiSiCenPin(A* a1, B* b1) {
// ma = a1;
// mb = b1;
// }
// void CenPin() {
// cout << "生成中" << endl;
// ma->Asencen();
// mb->Bsencen();
// cout << ma->minzi<<"和"<minzi << "的生成e成品生成成功" << endl;
// }
// ~CaiSiCenPin()
// {
// if (ma != NULL) {
// delete ma;
// ma = NULL;
// }
// if (mb != NULL) {
// delete mb;
// mb = NULL;
// }
// }
// A* ma;
// B* mb;
//};
class CaiSiCenPin {//cai为kai
public:
CaiSiCenPin(A* a1, B* b1) {
ma = a1;
mb = b1;
}
void CenPin() {
cout << "生成中" << endl;
ma->Asencen();
mb->Bsencen();
cout<< "e成品生成成功" << endl;
}
~CaiSiCenPin()
{
if (ma != NULL) {
delete ma;
ma = NULL;
}
if (mb != NULL) {
delete mb;
mb = NULL;
}
}
private:
A* ma;
B* mb;
};
void text() {
cout << "第一件成品" << endl;
CaiSiCenPin* abs1 = new CaiSiCenPin(new cgsiA, new cgsiB);
abs1->CenPin();
delete abs1;
cout << endl;
cout << "第二件成品" << endl;
CaiSiCenPin* abs2 = new CaiSiCenPin(new cgsiA, new dgsiB);
abs2->CenPin();
delete abs2;
}
void text() {
//ofstream ofs;
//ofs.open("新建文本文档.txt", ios::out);
system("pause");
//ofs << "我很帅,rui";
//ofs.close();
ifstream ifs;
ifs.open("新建文本文档.txt", ios::in);
if (!ifs.is_open())
{
cout << "文本打开失败" << endl;
return;
}
//①
char buf[1024] = { 0 };
while (ifs >> buf) {
cout << buf << endl;
}
//②
//char buf[1024] = { 0 };
//while (ifs .getline(buf,sizeof(buf))) {
// cout << buf << endl;
//}
//③
//string buf;
//while (getline(ifs,buf)) {
// cout << buf << endl;
//}
//④好像读不到中文
//char c;
//while ((c=ifs.get())!=EOF) {
// cout << c << endl;
//}
ifs.close();
}