The bridge of data structure --- structure (important)

The sharp edge of the sword comes from sharpening, and the fragrance of plum blossoms comes from the bitter cold!

Table of contents:
structure
1 . Basic concept of structure
2. Structure definition and use
3. Structure array
Four. Structure pointer
Five. Structure nested structure
6. Structure as function parameter
7. const usage scenarios in structures
Eight. Exercises
1. Arrange the results of the structure array from small to large
2. The rectangle problem

structure

A structure (struct) refers to a data structure, which is a type of aggregate data type in C language. Structures can be declared as variables, pointers or arrays to implement more complex data structures. . A structure is also a collection of elements, which are called members of the structure, and these members can be of different types, and members are generally accessed by name. .

1. Basic concept of structure

Structures are user-defined data types that allow users to store different data types

2. Structure definition and use

Syntax: struct structure name { structure member list };

There are three ways to create variables through structures:

1)struct structure name variable name

2)struct structure name variable name = { member 1 value, member 2 value...}

3) Create variables by the way when defining the structure

Example:

//Structure definition
struct student
{
//member list
  string name; //Name
  int age; //age
  int score; //Fraction
}stu3; //Structure variable creation method 3

int main() 
{
//Structure variable creation method 1
  struct student stu1; //The struct keyword can be omitted
  stu1.name = "Zhang San";
  stu1.age = 18;
  stu1.score = 100;
  cout << "Name:" << stu1.name << " age:" << stu1.age << " Fraction:" <<
  stu1.score << endl;

//Structure variable creation method 2
struct student stu2 = { "Li Si",19,60 };
cout << "Name:" << stu2.name << " age:" << stu2.age << " Fraction:" <<
stu2.score << endl;

  stu3.name = "Wang Wu";
  stu3.age = 18;
  stu3.score = 80;
  cout << "Name:" << stu3.name << " age:" << stu3.age << " Fraction:" <<
  stu3.score << endl;
  system("pause");
  return 0;
}

Summary 1: The keyword when defining a structure is struct, which cannot be omitted

Summary 2: When creating a structure variable, the keyword struct can be omitted

Summary 3: Structure variables use the operator ''.'' to access members

3. Structure array

Function: put the custom structure into the array for easy maintenance

Syntax: struct structure name array name[number of elements] = { {} , {} , ... {} }

Example:

//Structure definition
struct student
{
  //member list
  string name; //Name
  int age; //age
  int score; //Fraction
}
int main()
   {
//array of structures
struct student arr[3]=
{ 
  {"Zhang San",18,80 },{"Li Si",19,60 },{"Wang Wu",20,70 }
};
for (int i = 0; i < 3; i++)
  {
    cout << "Name:" << arr[i].name << " age:" << arr[i].age << " Fraction:"
    << arr[i].score << endl;
  }
system("pause");
 return 0;
  }

Four. Structure pointer

Role: access members in the structure through pointers

Using the operator ->, you can access the properties of the structure through the structure pointer

Example:

//Structure definition
struct student
{
//member list
string name; //Name
int age; //age
int score; //Fraction
};
int main() {
struct student stu = { "Zhang San",18,100, };
struct student * p = &stu;
p->score = 80; //Pointers can access members through the -> operator
cout << "Name:" << p->name << " age:" << p->age << " Fraction:" << p->score
<< endl;
system("pause");
return 0;
}

Summary: The structure pointer can access the members of the structure through the -> operator

Five. Structure nested structure

Role: A member of a structure can be another structure

For example: each teacher tutors a student, and in the structure of a teacher, record the structure of a student

Example:

//Student structure definition
struct student
{
//member list
string name; //Name
int age; //age
int score; //Fraction
};
//Teacher structure definition
struct teacher
{
//member list
int id; //employee number
string name; //teacher name
int age; //teacher age
struct student stu; //substructure student
};
int main() {
struct teacher t1;
t1.id = 10000;
t1.name = "Pharaoh";
t1.age = 40;
t1.stu.name = "Zhang San";
t1.stu.age = 18;
t1.stu.score = 100;
cout << "Teacher Staff Number: " << t1.id << " Name: " << t1.name << " age: "
<< t1.age << endl;
cout << "Tutor name: " << t1.stu.name << " age:" << t1.stu.age << " Test
 Test score: " << t1.stu.score << endl;
system("pause");
return 0;
}

Summary: In a structure, another structure can be defined as a member to solve practical problems

6. Structure as function parameter

Role: pass the structure as a parameter to the function

There are two delivery methods:

pass by value

address passing

Example:

//Student structure definition
struct student
{
//member list
string name; //Name
int age; //age
int score; //Fraction
};
//pass by value
void printStudent(student stu )
{
stu.age = 28;
cout << "Name in subfunction:" << stu.name << " age: " << stu.age << " Fraction:"
<< stu.score << endl;
}
//address passing
void printStudent2(student *stu)
{
stu->age = 28;
cout << "Name in subfunction:" << stu->name << " age: " << stu->age << " point
 number:" << stu->score << endl;
}
int main() {
student stu = { "Zhang San",18,100};
//pass by value
printStudent(stu);
cout << "Name in main function:" << stu.name << " age: " << stu.age << " Fraction:"
<< stu.score << endl;
cout << endl;
//address passing
printStudent2(&stu);
cout << "Name in main function:" << stu.name << " age: " << stu.age << " Fraction:"
<< stu.score << endl;
system("pause");
return 0;
}

Summary: If you don't want to modify the data in the main function, pass it by value, otherwise pass it by address

7. const usage scenarios in structures

Function: use const to prevent misoperation

Example:

//Student structure definition
struct student
{
//member list
string name; //Name
int age; //age
int score; //Fraction
};
//const usage scenarios
void printStudent(const student *stu) //Add const to prevent misoperation in the function body
{
//stu->age = 100; //The operation failed because the const modification was added
cout << "Name:" << stu->name << " age:" << stu->age << " Fraction:" << stu-
>score << endl;
}
int main() {
student stu = { "Zhang San",18,100 };
printStudent(&stu);
system("pause");
return 0;
}

Eight. Exercises

1. Arrange the results of the structure array from small to large

#include<stdio.h>

typedef struct student    
{
char *name;
int sno;
int age;
float score;
}Student;
void sortScore(Student st[],int len)
   {
    int flag = 0;
for(int i=0;i<len-1;i++)
{
        flag = 1;
for(int j=0;j<len-1-i;j++)
{
if(st[j].score>st[j+1].score)
 {
Student temp = st[j];
st[j] = st[j+1];
st[j+1] = temp;
 }
}
     if(flag==0)
       {
         break;
        }
}
}
void printStudent(Student stu[],int len)
{
for(int i=0;i<len;i++)
{
            printf("name:%s,sno:%d,age:%d,score:%.1f\n",stu[i].name,stu[i].sno,
    stu[i].age,stu[i].score);
}
}
int main()
{
Student stu[3] = {{"Tom",1101,18,99.2},{"Boy",1102,20,98.1},{"Smith",1103,22,99.0}};
sortScore(stu,3);
printStudent(stu,3);
return 0;
}

2. The rectangle problem

describe

Now there are many rectangles, each of which has a number, and this number can be repeated; you also know the width and length of the rectangle, and the number, length, and width are all integers; now you need to sort them in the following way (the default sorting rules are from small to big);

1. Sort by number from small to large

2. For rectangles with equal numbers, sort according to the length of the rectangles;

3. If the number and length are the same, sort according to the width of the rectangle;

4. If the number, length, and width are the same, only one rectangle is reserved for sorting, and redundant rectangles are deleted; finally, all rectangles are sorted and displayed in the specified format;

enter

The first line has an integer 0<n<10000, indicating that there are n sets of test data next;

The first line of each group has an integer 0<m<1000, indicating that there are m rectangles;

The next m lines, each line has three numbers, the first number represents the number of the rectangle,

The second and third values ​​are larger for length, and smaller for width, equal

Indicate that this is a square (the length, width and number of the data agreement are both less than 10000);

output

Sequentially output the numbers of all eligible rectangles for each set of data Length Width

sample input
1
8
1 1 1
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
sample output
1 1 1
1 2 1
1 2 2
2 1 1
2 2 1
#include<stdio.h>
#include<algorithm>
using namespace std;
struct ak
{
    int num,lenth,width;
}a[1005];
bool cmp(ak a,ak b)
{
    if(a.num<b.num)
         return 1;
    else if(a.num==b.num&&a.lenth<b.lenth)
        return 1;
    else if(a.num==b.num&&a.lenth==b.lenth&&a.width<b.width)
        return 1;
    else  return 0;
}
int main()
{
    int t,n,i,b,c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d",&a[i].num,&b,&c);
            if(b>=c)
            {
                a[i].lenth=b;
                a[i].width=c
            }
            else
            {
                a[i].lenth=c;
                a[i].width=b;
            }        
        }
        sort(a,a+n,cmp);
        for(i=0;i<n;i++)
        {
            if(a[i].num==a[i+1].num&&a[i].lenth==a[i+1].lenth&&a[i].width==a[i+1].width)
                continue;
                printf("%d %d %d\n",a[i].num,a[i].lenth,a[i].width);
        }
    }
    return 0;
}        

Tags: C C++ data structure

Posted by sandgnat on Sat, 25 Feb 2023 21:07:39 +0530