C++结构体数组详细解析

所属分类: 软件编程 / C 语言 阅读数: 57
收藏 0 赞 0 分享

1.定义结构体数组

和定义结构体变量类似,定义结构体数组时只需声明其为数组即可。如:

复制代码 代码如下:

struct Student{
     int num;
     char name[20];
     char sex[5];
     int age;
     float score;
     char addr[30];
};
Student stu[3]; //定义Student类型的数组stu

2.结构体数组的应用举例

题目:对候选人的票的统计程序。

设有3个候选人,最终只能有一个当选为领导。今有10个人参加投票,从键盘先后输入这10个人所投的候选人的名字,要求最后能输出这3个候选人的的票结果。

复制代码 代码如下:

#include<iostream>
using namespace std;
struct Person{
&nbsp;&nbsp; &nbsp;char name[20];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //姓名
&nbsp;&nbsp; &nbsp;int count;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //票数计数器
};
int main(){
&nbsp;&nbsp; &nbsp;Person leader[3]={"Tom",0,"Neo",0,"Marry",0};
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//定义Person类型的数组,内容为3个候选人的姓名和票数
&nbsp;&nbsp; &nbsp;int i,j,k=0;
&nbsp;&nbsp; &nbsp;bool tag;
&nbsp;&nbsp; &nbsp;cout<<"please input the name of the leader : Tom Neo Marry\n\n";
&nbsp;&nbsp; &nbsp;char leadername[20];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //该数组为每次输入的候选人的名字
&nbsp;&nbsp; &nbsp;for(i=0;i<10;i++){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //循环输入这10个人选的候选人的名字
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;cout<<"input name "<<i+1<<" :";
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;cin>>leadername;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;tag=1;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(j=0;j<3;j++){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(strcmp(leadername,leader[j].name)==0){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;leader[j].count++;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;tag=0;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(tag==1)k++;
&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;cout<<endl;
&nbsp;&nbsp; &nbsp;for(i=0;i<3;i++){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout<<leader[i].name<<":"<<leader[i].count<<endl;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;} &nbsp;
&nbsp;&nbsp; &nbsp;cout<<"Abandoned tickets:"<<k<<endl;
&nbsp;&nbsp; &nbsp;return 0;
}

当然,如果不使用结构体也可以解决这个问题:

复制代码 代码如下:

#include<iostream>
#include<string>
using namespace std;
int main(){
 char *name[3]={"Tom","Neo","Marry"};
 int    count[3]={0,0,0};
 int    i,j,k=0;
 bool tag=1;
 cout<<"please input the name of the leader : Tom Neo Marry\n\n";
 char leadername[20];               
 for(i=0;i<10;i++){                
  cout<<"input name "<<i+1<<" :";
  cin>>leadername;
  for(j=0;j<3;j++){
   if(strcmp(leadername,name[j])==0){
    count[j]++;
    tag=0;
   }
  }
  if(tag==1)k++;
  tag=1;
 }
 cout<<endl;
 for(i=0;i<3;i++){
    cout<<name[i]<<":"<<count[i]<<endl; 
 }
 cout<<"Abandoned tickets:"<<k<<endl;
 return 0;
}

或者
复制代码 代码如下:

#include<iostream>
#include<string>
using namespace std;
int main(){
 string name[3]={"Tom","Neo","Marry"};
 int    count[3]={0,0,0};
 int    i,j,k=0;
 bool tag=1;
 cout<<"please input the name of the leader : Tom Neo Marry\n\n";
 string leadername;               
 for(i=0;i<10;i++){                
  cout<<"input name "<<i+1<<" :";
  cin>>leadername;
  for(j=0;j<3;j++){
   if(leadername==name[j]){
    count[j]++;
    tag=0;
   }
  }
  if(tag==1)k++;
  tag=1;
 }
 cout<<endl;
 for(i=0;i<3;i++){
    cout<<name[i]<<":"<<count[i]<<endl; 
 }
 cout<<"Abandoned tickets:"<<k<<endl;
 return 0;
}

但是,相比较使用结构体的方法,我们对于候选人和票数的关系,更加直观,联系更加明显。

更多精彩内容其他人还在看

C++广播通信实例

这篇文章主要介绍了C++实现广播通信的方法,实例讲述了C++ socket广播通信的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C++计算ICMP头的校验和实例

这篇文章主要介绍了C++计算ICMP头的校验和的方法,代码简单实用,对于校验ICMP报文来说有不错的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++设置超时时间的简单实现方法

这篇文章主要介绍了C++设置超时时间的简单实现方法,涉及系统函数setsockopt对套接口的操作,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++实现ping程序实例

这篇文章主要介绍了C++实现ping程序实例,涉及C++对于ICMP数据包的发送与回显处理,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之boost::array的用法

这篇文章主要介绍了C++之boost::array的用法,以实例的形式简单讲述了静态数组的容器boost::array的使用技巧,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之Boost::array用法简介

这篇文章主要介绍了C++之Boost::array用法简介,较为详细的分析了Boost::array中的常见用法,并用实例的形式予以总结归纳,需要的朋友可以参考下
收藏 0 赞 0 分享

VC文件目录常见操作实例汇总

这篇文章主要介绍了VC文件目录常见操作实例汇总,总结了VC针对文件目录的各种常用操作,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

VC打印word,excel文本文件的方法

这篇文章主要介绍了VC打印word,excel文本文件的方法,是VC操作文本文件中非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC++获得当前进程运行目录的方法

这篇文章主要介绍了VC++获得当前进程运行目录的方法,可通过系统函数实现该功能,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC中SendMessage和PostMessage的区别

这篇文章主要介绍了VC中SendMessage和PostMessage的区别,较为全面的分析了SendMessage和PostMessage运行原理及用法上的不同之处,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多