欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間: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)文章

  • QT實戰(zhàn)之實現(xiàn)圖片瀏覽系統(tǒng)

    QT實戰(zhàn)之實現(xiàn)圖片瀏覽系統(tǒng)

    這篇文章主要介紹了如何利用QT編寫一個圖片瀏覽系統(tǒng),可以支持自動播放,左右拖動切換,點擊列表切換,點擊按鈕切換等功能,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-04-04
  • c語言單詞本的新增、刪除、查詢按順序顯示功能

    c語言單詞本的新增、刪除、查詢按順序顯示功能

    這篇文章主要介紹了c語言單詞本的新增、刪除、查詢按順序顯示功能,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • OpenCV獲取圖像中直線上的數(shù)據(jù)具體流程

    OpenCV獲取圖像中直線上的數(shù)據(jù)具體流程

    對圖像進(jìn)行處理時,經(jīng)常會有這類需求:客戶想要提取出圖像中某條直線或者ROI區(qū)域內(nèi)的感興趣數(shù)據(jù),進(jìn)行重點關(guān)注,怎么操作呢,下面小編通過實例代碼介紹下OpenCV獲取圖像中直線上的數(shù)據(jù),一起看看吧
    2021-11-11
  • C++ 中循環(huán)鏈表和約瑟夫環(huán)

    C++ 中循環(huán)鏈表和約瑟夫環(huán)

    這篇文章主要介紹了C++ 中循環(huán)鏈表和約瑟夫環(huán)的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 查找算法之二分查找的C++實現(xiàn)

    查找算法之二分查找的C++實現(xiàn)

    今天小編就為大家分享一篇關(guān)于查找算法之二分查找的C++實現(xiàn),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 一文詳解C++11中的lambda函數(shù)

    一文詳解C++11中的lambda函數(shù)

    小編可以明確告訴大家:lambda函數(shù)是C++11中最重要的,使用最廣泛的,最具現(xiàn)代風(fēng)格的內(nèi)容,lambda函數(shù)的出現(xiàn)改變了C++編程的思維方式。所以快和小編學(xué)習(xí)一下C++11中l(wèi)ambda函數(shù)的使用吧
    2023-02-02
  • C++寬字符與普通字符的轉(zhuǎn)換實例詳解

    C++寬字符與普通字符的轉(zhuǎn)換實例詳解

    這篇文章主要介紹了C++寬字符與普通字符的轉(zhuǎn)換實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Mygui中文換行問題解決方案

    Mygui中文換行問題解決方案

    相信大家解決了中文輸入后一定會遇到如何解決中文輸入的問題,中文輸入換行問題是很多gui框架都存在的一個問題,需要的朋友可以了解下
    2012-11-11
  • c語言尾隊列tailq使用示例分享

    c語言尾隊列tailq使用示例分享

    這篇文章主要介紹了c語言尾隊列tailq使用示例,大家參考使用吧
    2014-01-01
  • C++數(shù)據(jù)結(jié)構(gòu)之list詳解

    C++數(shù)據(jù)結(jié)構(gòu)之list詳解

    list是一種序列式容器。list容器完成的功能實際上和數(shù)據(jù)結(jié)構(gòu)中的雙向鏈表是極其相似的,list中的數(shù)據(jù)元素是通過鏈表指針串連成邏輯意義上的線性表,也就是list也具有鏈表的主要優(yōu)點,即:在鏈表的任一位置進(jìn)行元素的插入、刪除操作都是快速的
    2021-11-11

最新評論