UVa 450 - Little Black Book

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

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

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

#include 
#include 
#include 

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;
}


你可能感兴趣的:(解题报告,字符串)