#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>//头文件
using namespace std;//命名空间
//system("color f3");
//system("pause");暂停窗口,防止一闪而过
// #include<iomanip.h> setw()
// return c1 > d1 ? c1 : d1; c1d1中最大值
//函数模版不是函数,它是以具体的类型为实参来生成函数体的模版
//一个函数不能即做为重载函数,又作为有默认参数的函数
//面向对象程序设计4个特点:抽象、封装、继承和多态性
//析构函数只有一个不返回任何值
//构造函数不能是虚函数
/*如果在一个函数中定义了一个对象(它是自动局部对象),
当这个函数被调用结束时,对象应该释放,在对象释放前自动执行析构函数。*/
//----------------------------------------2022.9.1---------------------------------------------
//---------------------------------------九九乘法表------------------------
/*
int main()
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
cout << i << "*" << j << "=" << i * j << ' ';
cout << endl;
}
return 0;
}
*/
//-----------------------------利用函数模板实现比较三个数中最大的------------------------
/*
template <typename T> // 模板声明T为类型参数
T max(T a, T b, T c)
{
if (b > a)a = b;
if (c > a)a = c;
return a;
}
int main()
{
int x, y, z,m;
cout << "请输入三个数" << endl;
cin >> x >> y >> z ;
m = max(x, y, z);
cout << "三个数中最大的数是" << m << endl;
}
*/
/*
#include <iostream>
using namespace std;
int main()
{
string string1, string2, string3;
string temp;
cout << "please input three stirings:";
cin >> string1 >> string2 >> string3;
if (string2 > string3)
{
temp = string2; string2 = string3; string3 = temp;
}
else if (string1 <= string3)
cout << string2 << "" << string1 << "" << string3 << endl;
else
cout << string2 << "" << string1 << "" << string3 << endl;
return 0;
}
*/
/*[实例1.16a]一个班有n个学生,需要把每个学生的简单材料(姓名和学号)输入计算机保存。
然后可以通过输入某-学生的姓名查找其有关资料。当输入-一个姓名后,程序就查找该班中有无此学生,
如果有,则输出他的姓名和学号,如果查不到,则输出“本班无此人”。
为解此问题,可以分别编写两个函数,函数input data 用来输入n个学生的姓名和学号,
函数search用来查找要找的学生是否在本班。
*/
/*
#include<iostream>
using namespace std;
#include<string>
const int N = 50;
string name[N], num[N];
string find_name;
int n;
void search(string find_name) {
int i;
bool flag = false;
for (i = 0; i < n; i++) {
if (name[i] == find_name) {
cout << name[i] << "找到了,学号是" << num[i] << endl;
flag = true;
break;
}
}
if (flag == false) {
cout << "找不到!!!" << endl;
}
}
int main() {
cout << "请输入本班学生的人数:";
cin >> n;
cout << endl;
for (int i = 0; i < n; i++) {
cout << "请输入第" << i + 1 << "名学生姓名和学号:";
cin >> name[i] >> num[i];
cout << endl;
}
cout << "请输入你要找的学生姓名:";
cin >> find_name;
cout << endl;
search(find_name);
return 0;
}
*/
//类
/*
#include<iostream>
using namespace std;
class student//定义结构体类型 不分配内存
{
private:
int number;
int age;
public:
void setdata()
{
cin >> number;
cin >> age;
}
void display()
{
cout << number << age << endl;
}
};
int main()
{
student s1, s2;//相当于int a,b;定义对象
s1.setdata();
s1.display();
s2.setdata();
s2.display();
return 0;
}
*/
//---------------------实现计时器---------------------
/*#include<iostream>
using namespace std;
//例子:实现计时器 小时,分,秒
class Time//定义结构体类型 不分配内存
{
private:
int hour;
int min;
int sec;
public:
void set_time();
void show_time();
};
void Time::set_time()
{
cin >> hour >> min >> sec;
}
void Time::show_time()
{
cout << hour << ":" << min << ":" << sec << endl;
}
int main()
{
Time t1;
t1.set_time();
t1.show_time();
Time t2;
t2.set_time();
t2.show_time();
return 0;
}
*/
//---------------------出生年月日----------------------
/*
#include<iostream>
using namespace std;
class student {
int number;
int age;
class birthday {
private:
int year, month, day;
public:
void setdata() {
cin >> year >> month >> day;
}
void disdata() {
cout << "生日是" << year << "年" << month << "月" << day;
}
};
public:
birthday b1;
};
int main()
{
student s1;
s1.b1.setdata();
s1.b1.disdata();
}
*/
//------------------求3个长方柱的体积-------------------
/*
#include<iostream>
using namespace std;
class cube {
private:
int H, W, L;
public:
void sethwl(int h, int w, int l) {
H = h; W = w; L = l;
}
int volume() {
return H * W * L;
}
};
int main()
{
cube c;
int h, w, l;
cout << "请输入长宽高:" << endl ;
cin >> h >> w>> l;
c.sethwl(h, w, 1);
cout << "体积是" << c.volume() << endl;
}
*/
//利用指针访问私有数据成员
/*
class Test {
int x, y;
public:
void Setxy(int a, int b) { x = a; y = b; }
void Getxy(int* px, int* py) { *px = x; *py = y; } //提取x,y值
void Printxy(void) { cout << "x=" << x << '\t' << "y=" << y << endl; }
};
void main(void)
{
Test p1, p2;
p1.Setxy(3, 5);
int a, b;
p1.Getxy(&a, &b);//将 a=x, b=y
cout << a << '\t' << b << endl;
}
*/
//类引用举例(三角形类:三角形的三边及与三边相关的运算)
/*
#include <iostream>
#include <math.h>
using namespace std;
class Triangle {
private:
float a, b, c;
public:
void Setabc(float x, float y, float z);
void Getabc(float& x, float& y, float& z);
float perimeter(void);
float Area(void);
void print(void);
};
void Triangle::Setabc(float x, float y, float z)
{
a = x; b = y; c = z;
}
void Triangle::Getabc(float& x, float& y, float& z)
{
x = a; y = b; z = c;
}
float Triangle::perimeter(void)
{
return (a + b + c) / 2;
}
float Triangle::Area(void)
{
float area, p;
p = perimeter();
area = sqrt((p - a) * (p - b) * (p - c) * p);
return area;
}
void Triangle::print(void)
{
cout << "周长=" << perimeter() << "\t" << "面积=" << Area() << endl;
}
int main(void)
{
Triangle Tril;
Tril.Setabc(4, 5, 6);
float x, y, z;
Tril.Getabc(x, y, z);
cout << "边长x=" << x << "\t" << "边长y=" << y << "\t" << "边长z=" << z << endl;
cout << "s=" << Tril.perimeter() << endl;
cout << "面积=" << Tril.Area() << endl;
cout << "Tril:" << endl;
Tril.print();
}
*/
//类引用举例(学生类:学生的姓名成绩及相关运算)
/*
#include<iostream>
#include<String.h>
using namespace std;
class Stu {
char Name[20];
float Chinese;
float Math;
public:
float Average() {
return (Chinese+Math) / 2;
}
float Sum() {
return Chinese + Math;
}
void Show() {
cout << Name<<" " << Chinese<<" " << Math << endl;
}
void SetStudent(char n[], float a, float b) {
strcpy(Name,n);
Chinese = a;
Math = b;
}
void SetName(char n[]) {
strcpy(Name,n);
}
char* GetName() {
return Name;
}
};
int main() {
Stu s;
char a[20] = "张三";
s.SetName(a);
cout <<"名字是"<< s.GetName() << endl;
s.SetStudent(a, 100, 90.5);
s.Show();
cout << "平均分是" << s.Average() << endl;
cout << "总分是" << s.Sum() << endl;
}
*/
//返回引用类型的成员函数(可以返回私有数据成员的引用)
/*
#include<iostream>
using namespace std;
class A {
float x, y;
public:
float& Getx(void)
{
return x;
}
void Set(float a, float b)
{
x = a; y = b;
}
void Print(void) {
cout << x << '\t' << y << endl;
}
};
void main(void)