C + + structure array detailed parsing _c language

Source: Internet
Author: User

1. Define the structure body array

Like the definition of a struct variable, you can define an array of structures by simply declaring them as arrays. Such as:

Copy Code code as follows:

struct student{
int num;
Char name[20];
Char sex[5];
int age;
Float score;
Char addr[30];
};
Student Stu[3]; Defines an array of student types Stu

2. The application example of the structure body array

Title: A statistical procedure for the votes of candidates.

With 3 candidates, only one can eventually be elected as leader. Today, 10 people voted and entered the names of the 10 candidates from the keyboard, requesting the final output of the 3 candidates.

Copy Code code as follows:

#include <iostream>
using namespace Std;
struct person{
Char name[20]; Name
int count; Number of votes counter
};
int main () {
Person leader[3]={"Tom", 0, "Neo", 0, "Marry", 0};
Defines an array of person types with the names and votes of 3 candidates
int i,j,k=0;
BOOL tag;
cout<< "Please input the name of the Leader:tom Neo marry\n\n";
Char leadername[20]; The array is the name of each candidate entered
for (i=0;i<10;i++) {//loop the name of the candidate for the 10 candidates
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;
}

Of course, if you do not use a struct, you can solve this problem:

Copy Code code as follows:

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

Or
Copy Code code as follows:

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

However, compared with the method of using the structure, we are more intuitive and more obvious to the relationship between the candidate and the number of votes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.