结构体
定义与使用
#include<iostream>
using namespace std;
struct book {
string name;
double price;
int value;
}cpp;
int main()
{
book b;
b.name = "c语言程序设计";
b.price = 49.9;
cout << b.name << endl;
cout << b.price<<endl;
book py = { "python",39.9 };
cout << py.name << endl;
cout << py.price << endl;
cpp.name = "c++基础编程";
cout << cpp.name;
}
结构体数组
#include<iostream>
using namespace std;
struct book {
string name;
double price;
int value;
}cpp;
int main()
{
/*book b[10];
b[0].name = "c程序设计基础";
cout << b[0].name << endl;*/
book books[3] = {
{"c语言程序设计",11,11},
{"python",22,22},
{"c++基础编程",33,33}
};
for (int i = 0; i < 3; i++)
{
cout << books[i].name << ' ' << books[i].price << ' ' << books[i].value << endl;
}
}
结构体指针
把结构体b赋值给c,又想把b的值一起改了,这时将他们全部指向一个地址即可,出现结构体指针指向b的地址,修改pb指向的b,顺便把c也给修改了。
#include<iostream>
using namespace std;
struct book {
string name;
double price;
int value;
};
int main()
{
book b;
book c = b;
book* pb = &b;
pb->name = "c程序设计基础";
cout << pb->name;
}
嵌套结构体
结构体2中存在结构体1的元素,在使用时需要先用b.pt.x;使用,如果再多一个结构体也是同理。
#include<iostream>
using namespace std;
struct Point {
double x, y;
};
struct circle {
Point pt;
double rad;
};
struct circles {
int size;
circle c[100];
};
int main()
{
circle c;
/*book b;
book c = b;
book* pb = &b;
pb->name = "c程序设计基础";
cout << pb->name << endl;
b.pt.x = 11;
cout << b.pt.x;*/
c.pt.x = 9;
c.pt.y = 10;
circles cs = {
2,{
{ {9,8},5},
{ {2,1},1}
}
};
for (int i = 0; i < cs.size; i++)
{
circle temp = cs.c[i];
cout << '(' << temp.pt.x << ',' << temp.pt.y << ')' << temp.rad << endl;
}
}
结构体传参
还是和普通的传参一样会出现的问题,就是看似像个都相同其实他们存放的地址不同导致传参修改后并没有修改,所以我们要加上指针,在使用时如果一定要用(*c).pt.x+=x;如果不给*c加括号就会报错,因为优先级不够高直接执行了后面的.。正常的应该使用指针的->。
#include<iostream>
using namespace std;
struct Point {
double x, y;
};
struct circle {
Point pt;
double rad;
};
struct circles {
int size;
circle c[100];
};
void movecircle(circle *c,int x,int y)
{
c->pt.x += x;//(*c).pt.x += x;
c->pt.y += y;//(*c).pt.y += y;
}
int main()
{
circle c = {
{9,8},5
};
int x, y;
cin >> x >> y;
/*book b;
book c = b;
book* pb = &b;
pb->name = "c程序设计基础";
cout << pb->name << endl;
b.pt.x = 11;
cout << b.pt.x;*/
/*c.pt.x = 9;
c.pt.y = 10;
circles cs = {
2,{
{ {9,8},5},
{ {2,1},1}
}
};
for (int i = 0; i < cs.size; i++)
{
circle temp = cs.c[i];
cout << '(' << temp.pt.x << ',' << temp.pt.y << ')' << temp.rad << endl;
}*/
movecircle(&c, x, y);
cout << '(' << c.pt.x << ',' << c.pt.y << ')' << c.rad << endl;
return 0;
}
那什么情况下要用指针,而什么情况下要用结构体呢?当我发现程序功能只涉及到只读的情况下就用结构体,而需要修改特定值时就需要用指针,当然!只读时使用指针也没问题,就是改一下->,但是如果是一个print函数他只要读,你把他里面的值改了怎么办呢?这时候就可以加上const,类似于const circle c。这样就不会变了,用指针或者结构体都行因为常量不可修改!!
习题:Rectangles Problem - 2056
难点:题目给定的对角线坐标不确定是左上右下还是右上左下,需要进一步判断,测试版本没有maxmin函数需要自行实现,再求两个矩形的相交部分时需要画图画抽象为具体,两条平行线的左端点x相比较较大的赋值到maxx,同理左端点y的较大值复制到maxy。右端点x相比较小的赋值到minx,右端点y较小的复制到miny。然后将minx-maxx 乘上 miny-maxy就是最后的ans 如果minx<maxx呢说明不相交,ans为0,同理miny<maxy ans为0,最后打印出来就行。
#include<iostream>
using namespace std;
struct Point {
double x, y;
};
struct Rect {
Point lt;
Point rd;
};
Rect r1, r2;
double Max(double a, double b)
{
if (a > b)
return a;
else
return b;
}
double Min(double a, double b)
{
if (a > b)
return b;
else
return a;
}
Rect temp1,temp2;
int main()
{
while (cin >> temp1.lt.x >> temp1.lt.y >> temp1.rd.x >> temp1.rd.y)
{
cin >> temp2.lt.x >> temp2.lt.y >> temp2.rd.x >> temp2.rd.y;
r1.lt.x = Min(temp1.lt.x, temp1.rd.x);
r1.lt.y = Min(temp1.lt.y, temp1.rd.y);
r1.rd.x = Max(temp1.lt.x, temp1.rd.x);
r1.rd.y = Max(temp1.lt.y, temp1.rd.y);
r2.lt.x = Min(temp2.lt.x, temp2.rd.x);
r2.lt.y = Min(temp2.lt.y, temp2.rd.y);
r2.rd.x = Max(temp2.lt.x, temp2.rd.x);
r2.rd.y = Max(temp2.lt.y, temp2.rd.y);
double maxx = Max(r1.lt.x, r2.lt.x);
double minx = Min(r1.rd.x, r2.rd.x);
double maxy = Max(r1.lt.y, r2.lt.y);
double miny = Min(r1.rd.y, r2.rd.y);
double ans=(minx - maxx)* (miny - maxy);
if (minx < maxx)ans = 0;
if (miny < maxy)ans = 0;
printf("%.2lf\n", ans);
}
return 0;
}