第十章 结构体

10-1 输入三名学生的信息并输出

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

struct student{
	long num;
	char name[20];
	int age;
	char sex;
	int score;
};

int main(){
	struct student stu[3];
	int i;
	for(i=0;i<3;i++){
		printf("enter the data of student[%d]\r\n",i);
		scanf("%ld,%s%d,%c,%d",&stu[i].num,&stu[i].name,&stu[i].age,&stu[i].sex,&stu[i].score);
	}
	printf("\n      num\t name\t age     sex       score\n");
	
	for(i=0;i<3;i++){
		printf("\n%ld\t%s\t%d%4c%5d",stu[i].num,stu[i].name,stu[i].age,stu[i].sex,stu[i].score);
	}
	printf("\n");
	return 0;
}
/*
enter the data of student[0]
2000025,wangli
18,f,86
enter the data of student[1]
2000035,yeqing
20,f,99
enter the data of student[2]
2000048,liubo
19,f,100

      num        name    age     sex       score

2000025 wangli  18   f   86
2000035 yeqing  20   f   99
2000048 liubo   19   f  100
Press any key to continue
*/

10-2   统计候选人得票数

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

struct person{
	char name[20];
	int count;
}leader[3]={"hu",0,"li",0,"ma",0};

int main(){
	int i,j;
	char leader_name[20];
	for(i=0;i<=5;i++){
		scanf("%s",leader_name);
		for(j=0;j<3;j++){
			if(strcmp(leader_name,leader[j].name)==0)
				leader[j].count++;
		}
	}
	for(j=0;j<3;j++){
		printf("\r\n%s : %d\r\n",leader[j].name,leader[j].count);
	}
	return 0;
}
/*
li
li
li
ma
hu
hu

hu : 2

li : 3

ma : 1
*/

10-3  结构体指针变量的定义

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

int main(){
	struct student{
		long num;
		char name[12];
		char sex;
		float score;
	}stu;
		
	struct student *p=&stu;
	stu.num = 200052;
	strcpy(stu.name,"zhaoqian");
	stu.sex='f';
	stu.score = 98.25;

	printf("no %d: name = %s,sex is %c,score = %f\r\n",stu.num,stu.name,stu.sex,stu.score);
	printf("no %d: name = %s,sex is %c,score = %f\r\n",(*p).num,(*p).name,(*p).sex,stu.score);
	printf("no %d: name = %s,sex is %c,score = %f\r\n",p->num,p->name,p->sex,p->score);
	return 0;
}
/*
no 200052: name = zhaoqian,sex is f,score = 98.250000
no 200052: name = zhaoqian,sex is f,score = 98.250000
no 200052: name = zhaoqian,sex is f,score = 98.250000
*/

10-4  输出学生情况i表,情况由sta数组存放

#include <stdio.h>
#include <string.h>
struct student{
	long num;
	char name[12];
	char sex;
	int age;
};

struct student stu[3]={{200156,"liming",'m',18},{200569,"huangjun",'f',17},{200789,"liqian",'f',20}};

int main(){	
	struct student *p;
	for(p=stu;p<stu+3;p++)
		printf("no %d: name = %s,sex is %c,score = %d\r\n",p->num,p->name,p->sex,p->age);
	return 0;
}
/*
no 200156: name = liming,sex is m,score = 18
no 200569: name = huangjun,sex is f,score = 17
no 200789: name = liqian,sex is f,score = 20
*/

10-5  用结构体变量作为实参传递数据

#include <stdio.h>
#include <string.h>
struct gzqk{
	char name[12];
	double jbgz;
	double fdgz;
	double sum;
};
int main(){	
	struct gzqk teacher;
	printf("Input name:\r\n");
	scanf("%s",teacher.name);
	teacher.jbgz=1650.2;
	teacher.fdgz=860.7;
	teacher.sum=teacher.fdgz+teacher.jbgz;
	display(teacher);
	printf("%s  , %5.2f, %5.2f, %5.2f\r\n",teacher.name,teacher.jbgz,teacher.fdgz,teacher.sum);
	return 0;
}

display(p)
struct gzqk p;
{
	printf("%s  , %5.2f, %5.2f, %5.2f\r\n",p.name,p.jbgz,p.fdgz,p.sum);
	p.jbgz=2650.2;
	p.fdgz=560.7;
	p.sum=p.fdgz+p.jbgz;
	printf("%s  , %5.2f, %5.2f, %5.2f\r\n",p.name,p.jbgz,p.fdgz,p.sum);
}

/*
Input name:
xuting
xuting  , 1650.20, 860.70, 2510.90
xuting  , 2650.20, 560.70, 3210.90
xuting  , 1650.20, 860.70, 2510.90
*/

10-6   用指向结构体变量的指针作为函数实参传递数据

#include <stdio.h>
#include <string.h>
struct student{
	long num;
	char name[12];
	float score[3];
}stb1={2021006,"liming",60,70,80},stb2={20210556,"huguang",78,89,85};

void output(struct student *p)
{
	printf("%7ld,%5s,%.2f,%.2f,%.2f\r\n",p->num,p->name,p->score[0],p->score[1],p->score[2]);
}
int main(){	
	output(&stb1);
	output(&stb2);
	return 0;
}

/*
2021006,liming,60.00,70.00,80.00
20210556,huguang,78.00,89.00,85.00
*/

10-7  结构体数组作为函数参数

#include <stdio.h>
#include <string.h>
struct student{
	long num;
	char name[12];
	float score[3];
}stb[2]={{2021006,"liming",60,70,80},{20210556,"huguang",78,89,85}};

void output(struct student *p)
{
	for(p=stb;p<stb+2;p++)
		printf("%7ld,%5s,%.2f,%.2f,%.2f\r\n",p->num,p->name,p->score[0],p->score[1],p->score[2]);
}
int main(){	
	output(&stb);
	return 0;
}

/*
2021006,liming,60.00,70.00,80.00
20210556,huguang,78.00,89.00,85.00
*/

10-8   管理通讯录程序

        建立信息、查询信息、修改信息、显示所有信息

        联系人信息包括

        姓名、学号、电话

        最多可以包括100人

        主函数可调用不同的功能模块

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值