最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++結(jié)構(gòu)體數(shù)組詳細解析

 更新時間:2013年10月16日 10:09:19   作者:  
定義結(jié)構(gòu)體數(shù)組和定義結(jié)構(gòu)體變量類似,定義結(jié)構(gòu)體數(shù)組時只需聲明其為數(shù)組即可

1.定義結(jié)構(gòu)體數(shù)組

和定義結(jié)構(gòu)體變量類似,定義結(jié)構(gòu)體數(shù)組時只需聲明其為數(shù)組即可。如:

復(fù)制代碼 代碼如下:

struct Student{
     int num;
     char name[20];
     char sex[5];
     int age;
     float score;
     char addr[30];
};
Student stu[3]; //定義Student類型的數(shù)組stu

2.結(jié)構(gòu)體數(shù)組的應(yīng)用舉例

題目:對候選人的票的統(tǒng)計程序。

設(shè)有3個候選人,最終只能有一個當(dāng)選為領(lǐng)導(dǎo)。今有10個人參加投票,從鍵盤先后輸入這10個人所投的候選人的名字,要求最后能輸出這3個候選人的的票結(jié)果。

復(fù)制代碼 代碼如下:

#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; //票數(shù)計數(shù)器
};
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類型的數(shù)組,內(nèi)容為3個候選人的姓名和票數(shù)
&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; //該數(shù)組為每次輸入的候選人的名字
&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; //循環(huán)輸入這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;
}

當(dāng)然,如果不使用結(jié)構(gòu)體也可以解決這個問題:

復(fù)制代碼 代碼如下:

#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;
}

或者
復(fù)制代碼 代碼如下:

#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;
}

但是,相比較使用結(jié)構(gòu)體的方法,我們對于候選人和票數(shù)的關(guān)系,更加直觀,聯(lián)系更加明顯。

相關(guān)文章

  • C語言 map函數(shù)的基礎(chǔ)用法詳解

    C語言 map函數(shù)的基礎(chǔ)用法詳解

    這篇文章主要為大家介紹了C語言 map函數(shù)的基礎(chǔ)用法,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • C語言從代碼中加載動態(tài)鏈接庫過程解析

    C語言從代碼中加載動態(tài)鏈接庫過程解析

    這篇文章主要介紹了C語言從代碼中加載動態(tài)鏈接庫過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • C語言鏈表實現(xiàn)圖書管理系統(tǒng)

    C語言鏈表實現(xiàn)圖書管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言鏈表實現(xiàn)圖書管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C++聚合關(guān)系類的構(gòu)造函數(shù)的調(diào)用順序詳解

    C++聚合關(guān)系類的構(gòu)造函數(shù)的調(diào)用順序詳解

    下面小編就為大家?guī)硪黄狢++聚合關(guān)系類的構(gòu)造函數(shù)的調(diào)用順序詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考,一起跟隨小編過來看看吧
    2016-05-05
  • VC中SendMessage和PostMessage的區(qū)別

    VC中SendMessage和PostMessage的區(qū)別

    這篇文章主要介紹了VC中SendMessage和PostMessage的區(qū)別,較為全面的分析了SendMessage和PostMessage運行原理及用法上的不同之處,非常具有實用價值,需要的朋友可以參考下
    2014-10-10
  • C++抽象基類講解

    C++抽象基類講解

    這篇文章主要介紹了C++抽象基類講解,象基類abstract base class簡稱ABC,C++實現(xiàn)繼承的時候,需要保證派生類和基類之間是一種is-a的關(guān)系。在大多數(shù)時刻,這樣的關(guān)系是沒有問題的,然而在一些特殊的情況可能會遇到問題,下面來看看文章的具體介紹吧
    2022-01-01
  • 分享常用的3個C++小技巧

    分享常用的3個C++小技巧

    這篇文章主要分享了常用的3個C++小技巧,
    2021-12-12
  • VSCode同時更改所有相同的變量名或類名的圖文教程

    VSCode同時更改所有相同的變量名或類名的圖文教程

    這篇文章主要介紹了VSCode同時更改所有相同的變量名或類名,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • C++利用jsoncpp庫實現(xiàn)寫入和讀取json文件

    C++利用jsoncpp庫實現(xiàn)寫入和讀取json文件

    JsonCpp 是一個C++庫,允許操作 JSON 值,包括序列化和反序列化到字符串和從字符串反序列化。本文主要介紹了如何利用jsoncpp庫實現(xiàn)寫入和讀取json文件,感興趣的可以了解一下
    2023-04-04
  • C++實現(xiàn)將數(shù)組中的值反轉(zhuǎn)

    C++實現(xiàn)將數(shù)組中的值反轉(zhuǎn)

    這里給大家分享的事一則C++實現(xiàn)將數(shù)組中的值反轉(zhuǎn)的代碼,取材自《C++程序設(shè)計》(梁勇著第三版367頁),有需要的小伙伴可以參考下
    2016-05-05

最新評論

霸州市| 富锦市| 泗阳县| 高雄县| 临夏市| 镇原县| 日土县| 天柱县| 麻江县| 普定县| 岐山县| 息烽县| 双峰县| 江西省| 张家港市| 繁峙县| 邯郸县| 嘉义市| 凯里市| 章丘市| 莫力| 南溪县| 新宁县| 静乐县| 江油市| 西丰县| 宁明县| 凉城县| 宣汉县| 张家港市| 凤城市| 纳雍县| 芜湖县| 康定县| 化德县| 中卫市| 仙桃市| 绥化市| 沅陵县| 黄龙县| 涿鹿县|