C language learning - structure supplement

foreword

In actual development and application scenarios, a single type of data such as int, char, and double cannot meet the requirements of use, and structures can represent certain events and information very well. Here is a record of the process of reviewing structure knowledge, including: structure definition , use, and comprehensive application cases of arrays and pointers.

1. Definition of structure

  • Struct is the keyword of the structure . Define the structure as: struct+name. After the definition, you need to add " ; " after " } "
  • After the structure definition is finished, struct Student is equivalent to int , char , float as a whole
  • When used in the actual function, the definition is the same as int Data , and struct Student Data
#include <stdio.h>
#include <string.h>

struct Student//struct 结构体关键字
{
    
    
	int num;
	char name[32];
	char sex[32];
	int age;
	double score;
	char addr[32];
};

struct Day//描述事件/整体
{
    
    
	int year;
	int month;
	int day;
};


int main()
{
    
    
	struct Student RenYuJie;
	struct Student sd1={
    
    2,"张三","b",24,99,"北京"};//定义时使用
	return 0;
}

2. The use of structure

After the definition of the structure is completed, when performing related assignment and calculation operations in the function, it is necessary to call an element in the structure. There are two main calling methods:

  • " . " Dot operator - structure name. element
  • " -> " operator - structure address -> element (ie: structure pointer -> element )
#include <stdio.h>
#include <string.h>

struct Student//struct 结构体关键字
{
    
    
	int num;
	char name[32];
	char sex[32];
	int age;
	double score;
	char addr[32];
};

struct Day//描述事件/整体
{
    
    
	int year;
	int month;
	int day;
};

int main()
{
    
    
	struct Student RenYuJie;
	struct Student sd1={
    
    2,"张三","b",24,99,"北京"};//定义时使用
	
	RenYuJie.num=1;//点运算符访问成员变量
	
	strcpy(RenYuJie.name,"Renyujie");
	strcpy(RenYuJie.sex,"man");
	RenYuJie.age=24;
	RenYuJie.score=100;
	printf("学号:%d;性别:%s\n",RenYuJie.num,RenYuJie.sex);
	
	(&RenYuJie)->num=9;//->访问成员变量
	printf("学号:%d;性别:%s",RenYuJie.num,RenYuJie.sex);
	return 0;
}
#include <stdio.h>
#include <string.h>
//结构体指针定义、使用
//1、变量名访问用点运算符;
//2、地址访问用地址->运算符

struct RenYuan
{
    
    
	int num;
	char name[38];
};

int main()
{
    
    
	struct RenYuan one[2]={
    
    {
    
    1,"ryj"},{
    
    2,"yj"}};
	struct RenYuan *p;
	p=one;
	
	int len=sizeof(one)/sizeof(one[0]);
	
	for(int i=0;i<len;i++)
	{
    
    
		printf("%d号选手的名字是%s\n",(p+i)->num,(p+i)->name);//变量名访问用点运算符;地址访问用地址->运算符
	}
	
}

3. Simple combination of structure, array and pointer

Just like int arry[3 ] means that there are 3 integer data in the arry array, struct Student arry[3] means that there are 3 structure data in the arry array

#include <stdio.h>
//结构体和数组的结合
struct Student//struct 结构体关键字
{
    
    
	int num;
	char name[32];
	char sex[32];
	int age;
	double score;
	char addr[32];
};

int main()
{
    
    
	int arr[3]={
    
    1,2,3};
	struct Student arr2[3]={
    
    {
    
    2,"张三","b",24,99,"北京"},{
    
    3,"李四","g",24,89,"上海"},{
    
    4,"王五","b",21,49,"天津"}};
	
	struct Student *p;
	p=arr2;
	int len=sizeof(arr2)/sizeof(arr2[0]);
	for (int i=0;i<len;i++)
	{
    
    
		printf("%d号选手%s的分数是%f\n",(p+i)->num,(p+i)->name,(p+i)->score);
	}
	p=arr2;//指针归位
}

4. Simple combination of structures and arrays

Take a simple voting system (5 voters and 3 candidates as an example) to practice the simple combined application of structures and arrays

#include <stdio.h>
#include <string.h>
//结构体、数组、函数、简单应用
struct XuanMing
{
    
    
	char name[32];
	int tickets;
};


int main()
{
    
    
	struct XuanMing xm[3];
	int i;
	int len;
	char tmpName[32];
	int key=0;
	struct XuanMing max;
	len =sizeof(xm)/sizeof(xm[0]);
//=====================================
//============初始化选民信息===========
//=====================================
	for(i=0;i<len;i++)
	{
    
    
		xm[i].tickets=0;
		printf("请输入第%d个选民的名字:\n",i+1);
		scanf("%s",&xm[i].name);
	}
//=====================================
//===============唱票环节==============
//=====================================	
	for(i=0;i<5;i++)
	{
    
    
		printf("您将投票给谁?\n");
		memset(tmpName,'\0',sizeof(tmpName));//每次清空一下
		scanf("%s",&tmpName);
		
		for(int j=0;j<len;j++)
		{
    
    
			if(strcmp(tmpName,xm[j].name)==0)
			{
    
    
				xm[j].tickets++;
				key=1;
			}
		}
		if(key==0)
		{
    
    
			printf("本次选举没有此人,视为弃票!!!\n");
		}
	}
//======================================	
//============信息输出环节==============
//======================================	
    for(i=0;i<len;i++)
    {
    
    
	  printf("%s的总票数为:%d\n",xm[i].name,xm[i].tickets);
	}
//======================================	
//==============排序环节================
//======================================
	max=xm[0];
	  for(i=0;i<len;i++)
    {
    
    
	  if(max.tickets<xm[i].tickets)
	  {
    
    
		  max=xm[i];
	  }
     }	 
	
	printf("本次投票的结果为%s:\n",max.name);
	
	printf("%s的总票数为:%d\n",max.name,max.tickets);
}

5. Comprehensive application of structures, arrays, pointers, and functions

#include <stdio.h>
#include <string.h>
//结构体、数组、指针、函数综合应用
struct XuanMing
{
    
    
	char name[32];
	int tickets;
};

void initSelectInformation(struct XuanMing *pxm,int len)
{
    
    
	for(int i=0;i<len;i++)
	{
    
    
		(pxm+i)->tickets=0;
		printf("请输入第%d个选民的名字:\n",i+1);
		scanf("%s",&(pxm+i)->name);
	}
}

void computeSelectInformation(int sizeRen,struct XuanMing *pxm,int len)
{
    
    	
	int key=0;
	char tmpName[32];
	
	for(int i=0;i<sizeRen;i++)
	{
    
    
		printf("您将投票给谁?\n");
		memset(tmpName,'\0',sizeof(tmpName));//每次清空一下
		scanf("%s",&tmpName);
		
		for(int j=0;j<len;j++)
		{
    
    
			if(strcmp(tmpName,(pxm+j)->name)==0)
			{
    
    
				(pxm+j)->tickets++;
				key=1;
			}
		}
		if(key==0)
		{
    
    
			printf("本次选举没有此人,视为弃票!!!\n");
		}
	}
}


void claimSelectInformation(struct XuanMing *pxm,int len)
{
    
    
	for(int i=0;i<len;i++)
    {
    
    
	  printf("%s的总票数为:%d\n",(pxm+i)->name,(pxm+i)->tickets);
	}
}

void maxSelectInformation(struct XuanMing *pxm,int len)
{
    
    	
    struct XuanMing max;
	max=*(pxm);
	for(int i=0;i<len;i++)
    {
    
    
	  if(max.tickets<(pxm+i)->tickets)
	  {
    
    
		  max=*(pxm+i);
	  }
     }	 
	
	printf("本次投票的结果为%s:\n",max.name);
	
	printf("%s的总票数为:%d\n",max.name,max.tickets);
}
int main()
{
    
    
	struct XuanMing xm[3];
	struct XuanMing *pxm;
	pxm=xm;
	int len =sizeof(xm)/sizeof(xm[0]);
	int sizeRen=5;

//============初始化选民信息===========
	initSelectInformation(pxm,len);

//===============唱票环节==============
	computeSelectInformation(sizeRen,pxm,len);

//============信息输出环节=============
	claimSelectInformation(pxm,len);

//==============排序环节===============
	maxSelectInformation(pxm,len);
}

Guess you like

Origin blog.csdn.net/ONERYJHHH/article/details/125802809