前言
实际开发应用场景中单一的int、char、double等类型数据并不能满足使用要求,而结构体可以很好的表征某些事件与信息,这里记录一下复习结构体知识的过程,包括:结构体定义、使用、与数组、指针的综合应用案例。
1、结构体的定义
- struct为结构体关键字 定义结构为:struct+名字 定义结束后需要在“}”后添加“;”
- 结构体定义结束后,struct Student 整体相当于int、char、float
- 实际函数中使用时,定义同int Data一样,struct Student Data
#include <stdio.h>
#include <string.h>
struct Student
{
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、结构体的使用
结构体定义完成后,函数中进行相关赋值、运算操作时,需要对结构体中的某个元素进行调用,调用方式主要有两种:
- “.”点运算符——结构体名称.元素
- “->”运算符——结构体地址->元素 (即:结构体指针->元素)
#include <stdio.h>
#include <string.h>
struct Student
{
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>
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、结构体、数组、指针简单结合
同int arry[3]表示arry数组中有3个整型数据一样,struct Student arry[3]表示arry数组中有3个结构体数据
#include <stdio.h>
struct Student
{
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、结构体、数组简单结合
以简单投票系统(5个选民3个候选人为例)练习结构体、数组的简单结合应用
#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、结构体、数组、指针、函数综合应用
#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);
}
|