结构体练习题

1.学生信息用结构体tStudent{char name[100]; bool isMale; int chi, math}描述,给定若干个学生的信息,输入一个学生姓名,输出该学生的姓名、语文成绩、数学成绩、总分。

#include<iostream>
#include<cstring>
using namespace std;
struct tstudent
{   char  name[100]; 
    bool isMale; 
    int chi, math, total;
}stu[10]={{"Li1", 1, 100, 100},{"Li2", 0, 100, 99},{"Hznag1", 1, 99, 90},{"zhang2", 0, 90, 100}};
int main()
{
 char  name[100]; 
 cin>>name;
 for(int i=0;i<4;i++)
 {
  if(!strcmp(name,stu[i].name))	//strcmp()将输入姓名与学生姓名作比较,若正确,结果为0,非运算后为,输出相关信息。
 {
   stu[i].total=stu[i].chi+stu[i].math;
   cout<<stu[i].name<<" "<<stu[i].chi<<" "<<stu[i].math<<" "<<stu[i].total;
  }
 }
}

2.学生信息用结构体tStudent{char name[100]; bool isMale; int chi, math}描述,给定若干个学生的信息,输出所有男生的姓名、语文成绩、数学成绩、总分。

#include<iostream>
using namespace std;
struct tstudent
{
 char  name[100]; 
 cin>>name;
 bool isMale; 
 int chi, math,total;
}stu[10]={{"a",1,100,100},{"b",0,99,98},{"c",1,98,100},{"d",0,100,99}};
int main()
{
 for(int i=0;i<4;i++)
 {
  if(stu[i].isMale==1)
  {
   stu[i].total=stu[i].chi+stu[i].math;
   cout<<stu[i].name<<" "<<stu[i].chi<<" "<<stu[i].math<<" "<<stu[i].total;
  }
 }
}
发布了123 篇原创文章 · 获赞 109 · 访问量 6517

猜你喜欢

转载自blog.csdn.net/huangziguang/article/details/104966242