⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pex2_11.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>
#include <string.h>

struct Month
{
	char name[10];	// name of a month
	int monthnum;	// number of days in the month
};
		
// sort a list by name of month
void SortByName(Month months[], int n)
{
	int i, j;
	Month temp;
	
	for (i = 0; i < n-1; i++)
		for (j = i+1; j < n; j++)
			// use strcmp to determine if months[i].name > months[j].name
			if (strcmp(months[i].name,months[j].name) > 0)
			{
				// swap months[i] and months[j]
				temp = months[i];
				months[i] = months[j];
				months[j] = temp;
			}
}
 
// sort a list by number of days in month
void SortByDays(Month months[], int n)
{
	int i, j;
	Month temp;
	
	for (i = 0; i < n-1; i++)
		for (j = i+1; j < n; j++)
			if (months[i].monthnum > months[j].monthnum)
			{
				temp = months[i];
				months[i] = months[j];
				months[j] = temp;
			}
}
 
// sort by number of days in month, using month name as a
// secondary key
void Sort2ByDays(Month months[], int n)
{
	int i, j;
	Month temp;
	
	// do an standard exchange sort using number of days in
	// the month as the primary key
	for (i = 0; i < n-1; i++)
		for (j = i+1; j < n; j++)
			if (months[i].monthnum > months[j].monthnum)
			{
				temp = months[i];
				months[i] = months[j];
				months[j] = temp;

			}
	// pass through the list again and sort each run of months
	// having the same number of days using SortByName, which
	// sorts a run by month name
	i = 0; 
	while (i < n)
	{
		j = i+1;
		// determine if months[i+1] ... months[k] is a run
		while (j < n && months[j].monthnum == months[i].monthnum)
			j++;
		// if j-i >= 2, there is a run of at least j-i months having the
		// same number of days
		if (j - i > 1)
			// sort the j-i elements in the run
			SortByName(&months[i],j-i);
		i = j;
	}
}

void PrintMonths(Month months[], int n)
{
	for (int i = 0; i < n; i++)
		cout << months[i].name << "  (" << months[i].monthnum << ")" << endl;
}

void CopyStructArray(Month dest[], Month source[], int n)
{
	for (int i=0;i < n;i++)
		dest[i] = source[i];
}

void main(void)
{
	Month  months[12] = {{"January",31}, {"February",28}, {"March",31}, 
					    {"April",30}, {"May",31}, {"June",30}, 
					    {"July",31}, {"August",31}, {"September",30}, 
					    {"October",31}, {"November",30}, {"December",31}}; 
	Month test[12];

	CopyStructArray(test,months, 12);
	cout << "SORT BY NAME" << endl;
	SortByName(test,12);
	PrintMonths(test,12);
	cout << endl << endl;
	
	CopyStructArray(test,months, 12);
	cout << "SORT BY DAYS" << endl;
	SortByDays(test,12);
	PrintMonths(test,12);
	cout << endl << endl;
	
	CopyStructArray(test,months, 12);
	cout << "SORT BY DAYS, BREAKING TIES" << endl;
	Sort2ByDays(test,12);
	PrintMonths(test,12);
	cout << endl;
}

/*
<Run>

SORT BY NAME
April  (30)
August  (31)
December  (31)
February  (28)
January  (31)
July  (31)
June  (30)
March  (31)
May  (31)
November  (30)
October  (31)
September  (30)


SORT BY DAYS
February  (28)
April  (30)
June  (30)
September  (30)
November  (30)
March  (31)
July  (31)
August  (31)
January  (31)
October  (31)
May  (31)
December  (31)


SORT BY DAYS, BREAKING TIES
February  (28)
April  (30)
June  (30)
November  (30)
September  (30)
August  (31)
December  (31)
January  (31)
July  (31)
March  (31)
May  (31)
October  (31)
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -