欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

C++結構體數(shù)組詳細解析

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

1.定義結構體數(shù)組

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

復制代碼 代碼如下:

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

2.結構體數(shù)組的應用舉例

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

設有3個候選人,最終只能有一個當選為領導。今有10個人參加投票,從鍵盤先后輸入這10個人所投的候選人的名字,要求最后能輸出這3個候選人的的票結果。

復制代碼 代碼如下:

#include<iostream>
using namespace std;
struct Person{
    char name[20];                       //姓名
    int count;                           //票數(shù)計數(shù)器
};
int main(){
    Person leader[3]={"Tom",0,"Neo",0,"Marry",0};
                                        //定義Person類型的數(shù)組,內容為3個候選人的姓名和票數(shù)
    int i,j,k=0;
    bool tag;
    cout<<"please input the name of the leader : Tom Neo Marry\n\n";
    char leadername[20];                //該數(shù)組為每次輸入的候選人的名字
    for(i=0;i<10;i++){                   //循環(huán)輸入這10個人選的候選人的名字
        cout<<"input name "<<i+1<<" :";
        cin>>leadername;
        tag=1;
        for(j=0;j<3;j++){
            if(strcmp(leadername,leader[j].name)==0){
                leader[j].count++;
                tag=0;
            }
        }
        if(tag==1)k++;
    }
    cout<<endl;
    for(i=0;i<3;i++){
       cout<<leader[i].name<<":"<<leader[i].count<<endl;    
    }  
    cout<<"Abandoned tickets:"<<k<<endl;
    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;
}

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

上一篇:C++求斐波那契數(shù)的實例代碼

欄    目:C語言

下一篇:篩選法的C++實現(xiàn)

本文標題:C++結構體數(shù)組詳細解析

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3940.html

網頁制作CMS教程網絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網 版權所有