C++結構體數(shù)組詳細解析
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)系更加明顯。
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結構課程設計- 解析最少換車次數(shù)的問題詳解
- 01-10數(shù)據(jù)結構課程設計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置