习题9-5 通讯录排序 (20 分)

此篇博客介绍了使用C++编写一个程序,通过冒泡排序算法,根据朋友的生日对一组朋友信息进行升序排列。涉及到数据结构、字符数组和字符串处理,适合初学者理解数组操作与比较排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
#define MAX 17
struct Message
{
  char name[10];
  int birthday;
  char phone[MAX];
  
} friend;
int main(int argc, char *argv[]) {
	int n,i,j;
	int t[8];
	char p1[10];
	char p2[MAX];
	scanf("%d",&n);
	struct Message friend[n];
	for(i=0;i<n;i++){
		scanf("%s",&friend[i].name);
		scanf("%d",&friend[i].birthday);
		scanf("%s",&friend[i].phone);
	}	
	for(i=1;i<n;i++){
		for(j=0;j<n-i;j++){
			if(friend[j].birthday>friend[j+1].birthday){
				t[8]=friend[j].birthday;
				friend[j].birthday=friend[j+1].birthday;
				friend[j+1].birthday=t[8];
				
				strcpy(p1,friend[j].name);
				strcpy(friend[j].name,friend[j+1].name);
				strcpy(friend[j+1].name,p1);
				
				strcpy(p2,friend[j].phone);
				strcpy(friend[j].phone,friend[j+1].phone);
				strcpy(friend[j+1].phone,p2);
				
			}
		}		
	}
	for(i=0;i<n;i++){
		
		printf("%s %d %s",friend[i].name,friend[i].birthday,friend[i].phone);
		printf("\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值