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

所属分类: 软件编程 / C 语言 阅读数: 79
收藏 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++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多