【数据结构】day2

1、老师学生
在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct A
{
	char name[10];
	char sex;
	char zy;
	union
	{
		int score;
		char zw[10];
	};
}xx;

xx *create(int n)
{
	struct A *p = (struct A *) malloc (sizeof(struct A)*n);
	if(!p) return NULL;
	return p;
}

void input(xx *p,int n)
{
	for(int i = 0; i < n; i++)
	{
		printf("name\tsex\tzy\n");
		scanf("%s %c %c",(p+i)->name,&(p+i)->sex,&(p+i)->zy);
		if((p+i)->zy == 't') 
		{
			printf("please input zw\n");
			scanf(" %s",(p+i)->zw);
		}
		else 
		{
			printf("please input score\n");
			scanf("%d",&(p+i)->score);

		}
	}
}
void output(xx *p,int n)
{
	for(int i = 0; i < n; i++)
	{
		printf("name :%s\n sex: %c\n zy : %c\n",(p+i)->name,(p+i)->sex, (p+i)->zy);
		if((p+i)->zy == 't') printf("zw : %s\n",(p+i)->zw);
		else printf("score: %d\n",(p+i)->score);
	}
}

void pj(xx *p, int n)
{
	int sum = 0;
	int count = 0;
	for(int i = 0; i < n; i ++)
	{
		if((p+i)->zy == 's')
		{
			sum += (p+i)->score;
			count++;
		}
	}
	printf("平均值是:%.2f\n",(float)sum/count);

}

void ls(xx *p,int n)
{
	int sum = 0,count = 0;
	for(int i = 0; i < n; i ++)
	{
		if((p+i)->zy == 't')
		{
			count++;
		}
	}
	printf("老师个数:%d\n",count);

}

xx *free_p(xx *p)
{
	if(!p) return NULL;
	free(p);
	p = NULL;
	return p;
}

int main(int argc, const char *argv[])
{
	xx *p = create(3);
	input(p,3);
	output(p,3);
	pj(p,3);
	ls(p,3);
	free_p(p);
	return 0;
}

2、
在这里插入图片描述

//5.h
#ifndef __5_H__
#define __5_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct
{
	char name[10];
	int price;
	int num;
}xx;

xx *create(int n);
void input(xx *p,int n);
void add(xx *p,int n);
void find_m(xx *p,int n);
void find_p(xx *p, int n);
void mp(xx *p, int n);
void output(xx *p, int n);
xx * free_p(xx *p);
#endif

//fun5.c

#include "5.h"

xx *create(int n)
{
	xx *p = (xx *)malloc(sizeof(xx)*n);
	if(!p) return NULL;
	return p;
}

void input(xx *p,int n)
{
	for(int i = 0; i < n; i ++)
	{
		printf("name:\tprice\tnum\n");
		scanf(" %s %d %d",(p+i)->name,&(p+i)->price,&(p+i)->num);
	}
}

void add(xx *p,int n)
{
	int sum = 0;
	for(int i = 0; i < n;i++)
	{
		sum = ((p+i)->price)*((p+i)->num);
		printf("add  = %d\n",sum);
	}
	
}

void find_m(xx *p,int n)
{
	int max = 0;
	xx q;
	for(int i = 0; i < n; i++)
	{
		if(max < (p+i)->price)
		{
			max = (p+i)->price;
			q = *(p+i);
		}
	}
	printf("the max price = %s %d %d\n",q.name,q.price,q.num);
}
/
void find_p(xx *p, int n)
{
	xx *q = create(1);
	printf("please input u want search\n");
	scanf(" %s",q->name);
	for(int i = 0; i < n; i++)
	{
		if(!strcmp((p+i)->name,q->name))
			printf("price = %d\n",(p+i)->price);
	}
	free(q);
	q = NULL;
}
//
void mp(xx *p, int n)
{
	for(int i = 1; i < n; i++)
	{
		int count = 0;
		for(int j = 0; j < n-i; j++)
		{
			if((p+j)->price < (p+j+1)->price)
			{
				xx t;
				t = *(p+j);
				*(p+j) = *(p+j+1);
				*(p+j+1) = t;
				count++;
			}
		}
			if(count == 0) return;
	}

}

void output(xx *p, int n)
{
	for(int i = 0; i < n; i ++)
		{
			printf("name %s\t price %d\t num %d\n",(p+i)->name,(p+i)->price,(p+i)->num);
		}

}

xx * free_p(xx *p)
{
	if(!p) return NULL;
	free(p);
	p = NULL;
	return p;
}

//5.c
#include"5.h"

int main(int argc, const char *argv[])
{
	xx *p = create(3);
	input(p,3);
	add(p,3);
	find_m(p,3);
	find_p(p,3);
	mp(p,3);
	output(p,3);	
	p = free_p(p);
	return 0;
}

3、输出字节在这里插入图片描述

一共占32字节
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值