结构体实验报告—5到9题

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);
	 } 
 }
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验目的: 1.了解结构体和共用体的概念和用法; 2.掌握结构体和共用体的定义和使用方法; 3.掌握结构体和共用体在程序中的应用。 实验原理: 结构体是由不同数据类型的数据成员组成的一种自定义数据类型,数据成员可以是基本数据类型、数组、指针和其他结构体类型等。结构体中的数据成员可以被单独访问和操作。 共用体是一种特殊的结构体,它的所有成员共享同一块内存空间。共用体中所有成员的地址都是相同的,因此修改一个成员的值会影响其他成员的值。共用体常用于数据类型转换和数据压缩等方面。 实验步骤: 1.定义一个结构体类型,包含学生的姓、学号、成绩等信息。 2.定义一个共用体类型,包含一个整型变量和一个浮点型变量。 3.在主函数中定义结构体类型的变量和共用体类型的变量,并对它们进行赋值。 4.输出结构体类型的变量和共用体类型的变量的值。 5.修改共用体类型的变量的值,并输出修改后的结果。 实验代码: ```c #include <stdio.h> #include <string.h> //定义结构体类型 struct Student { char name[20]; int id; float score; }; //定义共用体类型 union Data { int i; float f; }; int main() { //定义结构体类型的变量并赋值 struct Student stu = {"Tom", 10001, 89.5}; //输出结构体类型的变量的值 printf("Name: %s\n", stu.name); printf("ID: %d\n", stu.id); printf("Score: %.1f\n", stu.score); //定义共用体类型的变量并赋值 union Data data; data.i = 100; //输出共用体类型的变量的值 printf("i = %d\n", data.i); printf("f = %.1f\n", data.f); //修改共用体类型的变量的值 data.f = 3.14; //输出修改后的结果 printf("i = %d\n", data.i); printf("f = %.1f\n", data.f); return 0; } ``` 实验结果: ```c Name: Tom ID: 10001 Score: 89.5 i = 100 f = 0.0 i = 1078523331 f = 3.1 ``` 实验分析: 在本次实验中,我们定义了一个结构体类型和一个共用体类型,并对它们进行了赋值和输出。同时,我们还修改了共用体类型的变量的值,并输出了修改后的结果。 通过本次实验,我们深入了解了结构体和共用体的概念和用法,掌握了结构体和共用体的定义和使用方法,进一步掌握了结构体和共用体在程序中的应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值