天天看點

第十章 結構體

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人

        主函數可調用不同的功能子產品