结构体基础

本文介绍了C++中的几个编程示例,包括时间的加减、二维向量的加法、查找书籍中的最高价和最低价、通信录按年龄排序以及学生成绩的等级统计,展示了基础数据结构和算法的应用。
摘要由CSDN通过智能技术生成

1.时间换算。用结构类型表示时间内容(时间以时、分、秒表示),输入一个时间数值,再输入一个秒数n(n<68),以h:m:s的格式输出该时间再过n秒后的时间值(超过24点就从0点开始计时)

#include<stdio.h>

typedef struct {
	int hour;
	int minute;
	int second;
}Time;

void InPut_Time(Time* t) {  /*结构指针作为函数参数*/
	scanf("%d:%d:%d", &t->hour, &t->minute, &t->second);
}

void Add_Senconds(Time* t, int n) {
	t->second += n;
	t->minute += n / 60;
	t->hour += t->minute / 60;
	t->second %= 60;
	t->minute %= 60;
	t->hour %= 60;
}

void OutPut_Time(Time* t) {
	printf("%d:%d:%d", t->hour, t->minute, t->second);
}

int main() {

	Time t;/*在定义了结构类型后,还需要定义结构类型的变量,然后才能通过结构变量来操作和访问结构的数据。*/
	int n;
	InPut_Time(&t);
	printf("enter %d seconds:");
	scanf("%d", &n);
	Add_Senconds(&t, n);
	OutPut_Time(&t);

	return 0;
}

2.平面向量加法:输入两个二维平面向量V1=(x1,y1)和V2=(x2,y2)的分量,计算并输出两个向量的和向量。

#include<stdio.h>

typedef struct {
	int x;
	int y;
}Vector2D;

Vector2D Add(Vector2D v1, Vector2D v2) {  /*返回的是两个向量相加后的向量,这个结果本身就是一个Vector2D类型的结构体,包含了x分量和y分量。因此,Add返回类型必须和需要返回的数据类型一致,即Vector2D*/
	Vector2D result;
	result.x = v1.x + v2.x;
	result.y = v1.y + v2.y;
	return result;
}

int main() {

	Vector2D v1, v2, sum;
	printf("enter v1:");
	scanf("%d %d", &v1.x, &v1.y);
	printf("enter v2:");
	scanf("%d %d", &v2.x, &v2.y);
	sum = Add(v1, v2);
	printf("the result is:(%d,%d)", sum.x, sum.y);

	return 0;
}

3.查找书籍:从键盘输入10本书的名称和定价并存入结构数组中,从中查找定价最高和最低的书的名称和定价,并输出。

#include<stdio.h>

typedef struct {
	char name[10];
	float price;
}Book;

void Find_Max_Min(Book book[], int n) {
	Book max = book[0];
	Book min = book[0];
	for (int i = 1; i < n; i++) {
		if (book[i].price > max.price) {
			max = book[i];
		}
		else if (book[i].price < min.price) {
			min = book[i];
		}
	}
	printf("the most expensive book's name is:%s,the price is:%f\n", max.name, max.price);
	printf("the cheapest book's name is:%s,the price is:%f\n", min.name, min.price);
}

int main() {
	int i;
	Book book[10];
	printf("enter books'name and price\n");
	for (i = 0; i < 10; i++) {
		scanf("%s%f", book[i].name, &book[i].price);
	}
	Find_Max_Min(book, 10);

	return 0;
}

易错提示:

正确结果:

4.通信录排序:建立一个通信录,通信录的结构记录包括:姓名、生日、电话号码;其中生日又包括三项:年、月、日。编写程序,定义一个嵌套的结构类型,输入n个(n<10)个联系人的信息,再按他们的年龄从大到小的顺序依次输出其信息。

#include<stdio.h>
#define MAXN 10
typedef struct {
	int year;
	int month;
	int day;
}Date;  /*在定义嵌套结构类型时,必须先定义成员的结构类型,再定义主结构类型*/

typedef struct {
	char name[20];
	Date birthdate;
	char phonenumber[20];
}Contact;

void Sort_Contacts(Contact contacts[], int n) {
	for (int i = 0; i < n-1; i++) {
		for (int j = 0; j < n-1-i; j++) {  /*每一轮都不断地跟后面一个作比较,每一轮都会把最小的排到最终的位置上。每完成排好一个,最后的位置就向前推进*/
			if ((contacts[j].birthdate.year>contacts[j + 1].birthdate.year )||
				(contacts[j].birthdate.year == contacts[j + 1].birthdate.year && contacts[j].birthdate.month>contacts[j + 1].birthdate.month )|| 
				(contacts[j].birthdate.year == contacts[j + 1].birthdate.year && contacts[j + 1].birthdate.month == contacts[j + 1].birthdate.month &&
					contacts[j].birthdate.day >contacts[j + 1].birthdate.day)) {
				Contact temp = contacts[j];
				contacts[j] = contacts[j + 1];
				contacts[j + 1] = temp;
			}
		}
	}
}

int main() {

	int n;

	printf("enter n:");
	scanf("%d", &n);

	Contact contacts[MAXN];
	for (int i = 0; i < n; i++) {
		printf("enter No.%d contact's name,birthday(year,month,day),phonenumber\n", i + 1);
		scanf("%s %d %d %d %s", contacts[i].name, &contacts[i].birthdate.year, &contacts[i].birthdate.month, &contacts[i].birthdate.day, contacts[i].phonenumber);
	}
	Sort_Contacts(contacts, n);

	printf("after sorted:\n");

	for (int i = 0; i < n; i++) {
		printf("%s %d-%d-%d %s\n", contacts[i].name, contacts[i].birthdate.year, contacts[i].birthdate.month, contacts[i].birthdate.day,contacts[i].phonenumber);
	}

	return 0;
}

5.按等级统计学生成绩:输入10个学生的学号、姓名和成绩,输出学生的成绩等级和不及格人数。每个学生的记录包括学号、姓名、成绩和等级,要求定义和调用函数set_grade(),根据学生成绩设置其等级,并统计不及格人数,等级设置:85~100为A,70~84为B,60~69为C,0~59为D。

#include<stdio.h>

typedef struct {
	int sno;
	char name[20];
	int score;
	char grade;
}Student;

void Set_Grade(Student*s) {
	if (s->score >= 85 && s->score <= 100) {
		s->grade = 'A';
	}
	else if (s->score >= 70 && s->score <= 84) {
		s->grade = 'B';
	}
	else if (s->score >= 60 && s->score <= 69) {
		s->grade = 'C';
	}
	else if (s->score >= 0 && s->score <= 59) {
		s->grade = 'D';
	}
}

int main() {
	int count = 0;
	Student stu[3];
	for (int i = 0; i < 3; i++) {
		printf("enter No.%d student's sno,name,score:\n", i + 1);
		scanf("%d %s %d", &stu[i].sno, stu[i].name, &stu[i].score);
		Set_Grade(&stu[i]);
		if (stu[i].grade == 'D') {
			count++;
		}
	}

	for (int i = 0; i < 3; i++) {
		printf("%d-%s-%d-%c\n", stu[i].sno, stu[i].name, stu[i].score, stu[i].grade);
	}
	printf("%d students failed.", count);
	return 0;
}

 易错提示:输入name时输入的是一个字符串,要在结构体中把它定义为数组。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值