5. 编写程序,在已有的几位员工信息(姓名、年龄、薪水)中,每位员工年龄增加一岁,薪水增加200元,输出更新后的员工信息和平均薪水
#include<stdio.h>
#define N 3
struct staff
{
char name[20];
int age;
int salary;
}stu[N];
struct staff_new{
char name[20];
int age;
int salary;
}sdu[N];
int old(struct staff stu[])
{
int i;
for(i=0;i<N;i++){
printf("please enter the name:\n");
scanf("%s",&stu[i].name);
printf("please enter the old age:\n");
scanf("%d",&stu[i].age);
printf("please enter the old salary:\n");
scanf("%d",&stu[i].salary);
}
}
int NEW(struct staff_new sdu[])
{
int i;
for(i=0;i<N;i++){
printf("please enter the name:\n");
scanf("%s",&sdu[i].name);
printf("please enter the new age:\n");
scanf("%d",&sdu[i].age);
}
}
6. 编写程序,自行初始化结构体数据,在已有的两个学生信息(学号、姓名、性别、成绩)中选择其中一个学生的信息覆盖第三个学生信息,然后输出所有学生的信息。
#include<stdio.h>
struct student
{
int num;
char name[20];
char sex[20];
double grade;
}stu[3]={{1,"琦琦","男",44.00},{2,"kunkun","女",55.00},{3,"xuexue","猜",55.00}};
int main()
{
stu[2]=stu[1];
int i;
for(i=0;i<3;i++)
{
printf("%d %s %s %.2f\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].grade);
}
return 0;
}
7. 已知将5个员工信息(姓名、年龄、薪水)存放在结构体数组中,统计薪水不到1000元且年龄不高于30岁的员工人数
#include<stdio.h>
#define N 5
struct staff
{
char name[20];
int age;
int salary;
} stu[N];
int input(struct staff stu[])
{
int i;
for(i=0;i<N;i++)
{
printf("please enter staffs’name,age and salary:\n");
scanf("%s %d %d",&stu[i].name,&stu[i].age,&stu[i].salary);
}
}
int main()
{
int j,k=0;
input(stu);
for(j=0;j<N;j++)
{
if(stu[j].age<=30&&stu[j].salary<1000)
k++;
}
printf("The number of employees who earn less than 1,000 yuan and are no older than 30: %d",k);
}
8. 已知将5个学生的信息(学号、姓名、性别、成绩)存放在结构体数组中,编写程序,删除第1个不及格的学生。
#include <stdio.h>
#define N 5
struct student
{
int num;
char name[20];
char sex[20];
int score;
}stu[N];
int input(struct student stu[])
{
int i;
for(i=0;i<N;i++)
{printf("please enter the num:\n");
scanf("%d",&stu[i].num);
printf("please enter the name:\n");
scanf("%s",&stu[i].name);
printf("please enter the sex:\n");
scanf("%s",&stu[i].sex);
printf("please enter the score:\n");
scanf("%d",&stu[i].score);
printf("\n");
}
}
int main()
{
input(stu);
int i,j=0;
for(i=0;i<N;i++)
{
if(stu[i].score>=60||j>=1)
printf("num:%d name:%s sex:%s score:%d\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].score);
else
j++;
}
return 0;
}
9. 已知将5个员工信息(姓名、年龄、薪水)存放在结构体数组中,编写程序,将新调来的员工信息插入在第3个位置上。
#include<stdio.h>
#include<string.h> //you need to call strcpy()
struct staff
{
char name[20];
int age;
float salary;
}person[5]={{"zhou",22,1500},{"wu",29,1200},{"li",30,900},{"zheng",32,900},{"wang",29,990}};
struct new_staff
{
char name[20];
int age;
float salary;
} new_person={"xie",18,5000};
int main()
{
int i;
strcpy(person[2].name,new_person.name);//The assignment of a string is done using the function strcpy()
person[2].age=new_person.age;
person[2].salary=new_person.salary;
for(i=0;i<5;i++)
{
printf("name:%s age:%d salary:%.2f\n",person[i].name,person[i].age,person[i].salary);
}
}