UVa 450 - Little Black Book

題目:有一些不同部門的人事信息,需要將他們合併后按照姓氏排列。

分析:字符串。直接按照題意模擬即可,讀取數據排序輸出。

說明:注意數組開的大一點防止RE(RE了2次╮(╯▽╰)╭)。

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

typedef struct _information
{
	char Title[101];
	char FirstName[101];
	char LastName[101];
	char HomeAddress[101];
	char Department[101];
	char HomePhone[101];
	char WorkPhone[101];
	char CampusBox[101];
}information;
information I[5005];

int cmp(const void *a, const void *b)
{
	information *p = (information *)a;
	information *q = (information *)b;
	return strcmp(p->LastName, q->LastName);
}

int get_information_block(char target[], char source[], int index)
{
	for (int i = 0; source[index] != ',' && source[index]; ++ i) {
		target[i] = source[index ++];
	}
	return index+1;
}

int main()
{
	int  n;
	char buf[1001], Department[1001];
	while (~scanf("%d",&n)) {
		getchar();
		int count = 0;
		while (n --) {
			gets(Department);
			while (gets(buf) && buf[0]) {
				int now = 0;
				now = get_information_block(I[count].Title, buf, now);
				now = get_information_block(I[count].FirstName, buf, now);
				now = get_information_block(I[count].LastName, buf, now);
				now = get_information_block(I[count].HomeAddress, buf, now);
				strcpy(I[count].Department, Department);
				now = get_information_block(I[count].HomePhone, buf, now);
				now = get_information_block(I[count].WorkPhone, buf, now);
				now = get_information_block(I[count].CampusBox, buf, now);
				count ++;
			}
		}
		
		qsort(I, count, sizeof(information), cmp);
		for (int i = 0; i < count; ++ i) {
			puts("----------------------------------------");
			printf("%s %s %s\n",I[i].Title,I[i].FirstName,I[i].LastName);
			printf("%s\n",I[i].HomeAddress);
			printf("Department: %s\n",I[i].Department);
			printf("Home Phone: %s\n",I[i].HomePhone);
			printf("Work Phone: %s\n",I[i].WorkPhone);
			printf("Campus Box: %s\n",I[i].CampusBox);
		}
	}
	
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值