数据结构作业—Calendar

本文介绍了一次数据结构作业——创建万年历的实现过程,包括cpp文件的内容,虽然作者认为代码编写不够理想,但仍然提供了一个关于日期类和字符串操作的实际应用案例。
摘要由CSDN通过智能技术生成

感觉写得有点别扭....

万年历...写得不好见谅哈

calendar.cpp:

/**
 * File calendar.cpp, 2011.09.15
 * define class Calendar
 * Copyright by Zhou Junjie, 2011~2012, SE, Digital Media, class2, 10389149
 **/

#include "calendar.h"
#include "util.h"
#include "date.h"
#include "dateFormatException.h"
#include <string>
#include <sstream>
#include <iomanip>
#include <memory>

using namespace std;

Calendar* Calendar::calendar = NULL;

Calendar* Calendar::getInstance()
{
	if (!calendar)
	{
		calendar = new Calendar();
		return calendar;
	}
	else
	{
		return calendar;
	}
}

Calendar::~Calendar()
{
	if (!calendar)
	{
		delete calendar;
		calendar = NULL;
	}
}

string Calendar::getMonthCalendar(int year, int month)
{
	int maxMonthDays = 0;
	int firstDayWeek = 0;
	int dayCount = 0;

	try {
		maxMonthDays = maxMonthDay(year, month);
		firstDayWeek = dayWeek(year, month, 1);
	} catch (runtime_error) {
		throw;
	}
	
	ostringstream oss;

	oss << "            " <<year << "-" << month << endl;

	oss << setw(4) << "日" << setw(4) << "一"
		<< setw(4) << "二" << setw(4) << "三"
		<< setw(4) << "四" << setw(4) << "五"
		<< setw(4) << "六" << "\n";

	// print the prefix blank
	for (int i = 0; i < firstDayWeek; i++)
	{
		oss << setw(4) << "";
		dayCount++;
	}
	for (int i = 1; i <= maxMonthDays; i++)
	{
		oss << setw(4) << i;
		dayCount++;

		if (dayCount % 7 == 0)
			oss << "\n";
	}

	return oss.str();
}

string Calendar::getYearCalendar(int year)
{
	ostringstream oss;

	oss << "          " <<year << "年日历" << endl << endl;

	try 
	{
		for (int month = 1; month <= 12; month++)
		{
			oss << this->getMonthCalendar(year, month) << endl << endl;
		}
	} catch (runtime_error) {
		throw;
	}

	return oss.str();
}

int Calendar::calculateDayInterval(string firstDay, string secondDay)
{
	tr1::shared_ptr<Date> firstDate = NULL;
	tr1::shared_ptr<Date> secondDate = NULL;
	try
	{
		firstDate = tr1::shared_ptr<Date>(Date::parse(firstDay));
		secondDate = tr1::shared_ptr<Date>(Date::parse(secondDay));
	} catch (DateFormatException) {
		throw;
	}

	return (*firstDate - *secondDate);
}

string Calendar::getDayOfWeek(int year, int month, int day)
{
	if (!checkDayIfInvaild(year, month, day))
		throw runtime_error("日期非法");
	else
	{
		string weekDays[7] = {MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
					FRIDAY, SATURDAY, SUNDAY};

		try 
		{
			return weekDays[dayWeek(year, month, day) - 1];
		} catch (runtime_error) {
			throw;
		}
	}
}

date.cpp:

/**
 * File date.cpp, 2011.09.16
 * define class Date
 * Copyright by Zhou Junjie, 2011~2012, SE, Digital Media, class2, 10389149
 **/

#include "date.h"
#include "util.h"
#include "dateFormatException.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;

Date* Date::parse(string dateString)
{
	vector<string> dateContent = split(dateString, "-");

	if (dateContent.size() != 3) 
	{
		throw DateFormatException("日期格式错误");
	}
	else
	{
		int year = atoi(dateContent[0].c_str());
		int month = atoi(dateContent[1].c_str());
		int day = atoi(dateContent[2].c_str());

		try {
			return new Date(year, month, day);
		} catch (runtime_error) {
			throw;
		}

	}
}

Date::Date(int year, int month, int day)
{
	if (!checkDayIfInvaild(year, month, day))
		throw runtime_error("日期非法");

	this->year = year;
	this->month = month;
	this->day = day;
}

long long Date::operator-(Date& rightDate)
{
	return abs(getDays() - rightDate.getDays());
}

long long Date::getDays()
{
	int leapYear = 0;
	int notLeapYear = 0;
	int totalMonthDays = 0;

	for (int i = 1900; i < year; i++)
	{
		if (isLeapYear(i))
			leapYear++;
		else
			notLeapYear++;
	}

	for (int i = 1; i < month; i++)
	{
		totalMonthDays += maxMonthDay(year, month);
	}

	return (leapYear * 366 + notLeapYear * 365 + totalMonthDays + day);
}

util.cpp:

/**
 * File util.cpp, 2011.09.15
 * Define the function in file util.h
 * Copyright by Zhou Junjie, 2011~2012, SE, Digital Media, class2, 10389149
 **/

#include "util.h"
#include <vector>
#include <string>

using namespace std;

vector<string> split(string source, string sep)
{
	vector<string> result;
    
	int sepLen = sep.length();
	int lastPosition = 0;
	int index = -1;

	while (-1 != (index = source.find(sep, lastPosition)))
	{
		result.push_back(source.substr(lastPosition, index - lastPosition));
		lastPosition = index + sepLen;
	}

	string lastString = source.substr(lastPosition);

	if (!lastString.empty())
	{
		result.push_back(lastString);
	}
	
	return result;
}

bool checkDayIfInvaild(int year, int month, int day)
{
	if (day < 1) 
		return false;

	if ((year % 4 == 0 && year %100 !=0) || year % 400 == 0)
	{
		if (month == 2)
		{
			if (day > 29)
			{
				return false;
			}
		}
	}
	else
	{
		switch (month)
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if (!(day >=1 && day <= 31))
				return false;
			break;

		case 4:
		case 6:
		case 9:
		case 11:
			if (!(day >= 1 && day <= 30))
				return false;
			break;
		case 2:
			if (!(day >= 1 && day <= 28))
				return false;
			break;
		}	
	}

	return true;
}

bool isLeapYear(int year)
{
	if ((year % 4 == 0 && year %100 !=0) || year % 400 == 0)
		return true;
	else 
		return false;
}

int maxMonthDay(int year, int month)
{
	if (month <=0 || month > 12)
	{
		throw runtime_error("月份错误");
	}

	int monthDays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
	if (isLeapYear(year) && month == 2)
		return 29;
	else
		return monthDays[month - 1];
}

int dayWeek(int year, int month, int day)
{
	if (month <= 0 || month > 12)
	{
		throw runtime_error("月份错误");
	}
	else if (day <= 0 || day > maxMonthDay(year, month))
	{
		throw runtime_error("日期错误");
	}
	else
	{
		int date = 0;
		float result = 0;
		
		for (int i = 1; i < month; i++) 
			date += maxMonthDay(year, i);

		date += day;

		result = year - 1 + (float)(year - 1) / 4 + 
				 (float)(year - 1) / 100 + (float)(year - 1) / 400 - 40 + date;

		return ((int)result % 7);
	}
}

主程序 Cal.cpp

/**
 * File Cal.cpp, 2011.09.17
 * the main interface of this program
 * Copyright by Zhou Junjie, 2011~2012, SE, Digital Media, class2, 10389149
 **/

#include "calendar.h"
#include "dateFormatException.h"
#include "util.h"
#include <iostream>
#include <sstream>
#include <string>
#include <memory>
#include <ctime>

using namespace std;

string instrument();
string info();
void operation(int choice, tr1::shared_ptr<Calendar> calendar);

int main()
{
	cout << info() << endl;
	cout << instrument() << endl;

	tr1::shared_ptr<Calendar> calendar(Calendar::getInstance());
	int choice;

	cin >> choice;

	while (true)
	{
		if (choice == 6)
			break;
		else {
			string yesOrNot = "";
			operation(choice, calendar);

			cout << "是否继续?(y/n)" << endl;
			cin >> yesOrNot;

			if (yesOrNot == "y" || yesOrNot == "Y" || yesOrNot == "yes")
			{
				cout << instrument() << endl;
				cin >> choice;
			}
			else
				break;
		}
		

	}

	return 0;
}

string instrument()
{
	ostringstream oss;

	oss << "请输入你的选择" << endl
		<< "1.输出今年日历" << endl
		<< "2.输出某一年的日历" << endl
		<< "3.输出某一天是星期几" << endl
		<< "4.输出两个日期之间相差多少天" << endl
		<< "5.输出某一个月的月历" << endl
		<< "6.退出"
		<< endl;

	return oss.str();
}

string info()
{
	ostringstream oss;

	oss << "Calendar v1.0" << endl
		<< "Copyright by Zhou Junjie, 2011~2012, Software Engineering, Digital Media" 
		<< endl;

	return oss.str();
}

void operation(int choice, tr1::shared_ptr<Calendar> calendar)
{
	switch (choice)
	{
	case 1:
		{
			time_t rawTime = time(NULL);
			tm* timePtr = localtime(&rawTime);
			int thisYear = timePtr->tm_year + 1900;
			try
			{
				cout << calendar->getYearCalendar(thisYear) << endl;
			} catch (runtime_error e) {
				cout << e.what() << endl;
			}
		}
		break;
	case 2:
		{
			int year;
			cout << "请输入年份" << endl;
			cin >> year;

			try
			{
				cout << calendar->getYearCalendar(year) << endl;
			} catch (runtime_error e) {
				cout << e.what() << endl;
			}
		}
		break;
	case 3:
		{
			string date;
			cout << "请输入日期(yyyy-MM-dd)" << endl;
			cin >> date;
			vector<string> dateContent = split(date, "-");
	
			if (dateContent.size() != 3)
			{
				cout << "日期格式错误" << endl;
				break;
			}
			else
			{
				int year = atoi(dateContent[0].c_str());
				int month = atoi(dateContent[1].c_str());
				int day = atoi(dateContent[2].c_str());

				try 
				{
					cout << date << " 是 " << calendar->getDayOfWeek(year, month, day) << endl;
				} catch (runtime_error e) {
						cout << e.what() << endl;
				}
			}
		}
		break;
	case 4:
		{
			string firstDay = "";
			string secondDay = "";
	
			cout << "请输入第一个日期(yyyy-MM-dd)" << endl;
			cin >> firstDay;
	
			cout << "请输入第二个日期(yyyy-MM-dd)" << endl;
			cin >> secondDay;

			try
			{
				cout << firstDay << " 与 " << secondDay << " 相差 " 
					 << calendar->calculateDayInterval(firstDay, secondDay) << " 天" << endl;
 			} catch (DateFormatException e) {
				cout << e.what() << endl;
			}
		}
		break;
	case 5:
		{
			int year;
			int month;

			cout << "请输入年份,月份" << endl;
			cin >> year >> month;
	
			try
			{
				cout << calendar->getMonthCalendar(year, month) << endl;
			} catch (runtime_error e) {
				cout << e.what() << endl;
			}
		}
		break;
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验一 进程调度 编写并调试一个模拟的进程调度程序,采用“短进程优先”调度算法对五个进程进行调度。以加深对进程的概念及进程调度算法的理解. 下面是采用动态优先数的调度程序,可作参考。  例题: 设计一个有 N个进程共行的进程调度程序。   进程调度算法:采用最高优先数优先的调度算法(即把处理机分配给优先数最高的进程)和先来先服务算法。   每个进程有一个进程控制块( PCB)表示。进程控制块可以包含如下信息:进程名、优先数、到达时间、需要运行时间、已用CPU时间、进程状态等等。   进程的优先数及需要的运行时间可以事先人为地指定(也可以由随机数产生)。进程的到达时间为进程输入的时间。   进程的运行时间以时间片为单位进行计算。   每个进程的状态可以是就绪 W(Wait)、运行R(Run)、或完成F(Finish)三种状态之一。   就绪进程获得 CPU后都只能运行一个时间片。用已占用CPU时间加1来表示。   如果运行一个时间片后,进程的已占用 CPU时间已达到所需要的运行时间,则撤消该进程,如果运行一个时间片后进程的已占用CPU时间还未达所需要的运行时间,也就是进程还需要继续运行,此时应将进程的优先数减1(即降低一级),然后把它插入就绪队列等待CPU。   每进行一次调度程序都打印一次运行进程、就绪队列、以及各个进程的 PCB,以便进行检查。重复以上过程,直到所要进程都完成为止。实验二 作业调度 一、实验目的:用高级语言编写和调试一个或多个作业调度的模拟程序,以加深对作业调度算法的理解。 二、实验内容: 1.写并调试一个单道处理系统的作业等待模拟程序。 2.作业等待算法:分别采用先来先服务(FCFS)、响应比高者优先(HRN)的调度算法。 3.由于在单道批处理系统中,作业一投入运行,它就占有计算机的一切资源直到作业完成为止,因此调度作业时不必考虑它所需要的资源是否得到满足,它所占用的 CPU时限等因素。 4.每个作业由一个作业控制块JCB表示,JCB可以包含如下信息:作业名、提交时间、所需的运行时间、所需的资源、作业状态、链指针等等。作业的状态可以是等待W(Wait)、运行R(Run)和完成F(Finish)三种状态之一。每个作业的最初状态总是等待W。 5.对每种调度算法都要求打印每个作业开始运行时刻、完成时刻、周转时间、带权周转时间,以及这组作业的平均周转时间及带权平均周转时间。 三、思考:比较各种算法的优缺点。 实验三 动态分区分配方式的模拟 1、实验目的:了解动态分区分配方式中的数据结构和分配算法,并进一步加深对动态分区存储管理方式及其实现过程的理解 2、实验内容: (1)用C语言分别实现采用首次适应算法和最佳适应算法的动态分区分配过程和回收过程。其中,空闲分区通过空闲分区链(表)来管理;在进行内存分配时,系统优先使用空闲区低端的空间。 (2)假设初始状态下,可用的内存空间为640KB,并有下列的请求序列: •作业1申请130KB •作业2申请60KB •作业3申请100KB •作业2释放60KB •作业4申请200KB •作业3释放100KB •作业1释放130KB •作业5申请140KB •作业6申请60KB •作业7申请50KB •作业8申请60KB 请分别采用首次适应算法和最佳适应算法进行内存的分配和回收,要求每次分配和回收后显示出空闲内存分区链的情况。 3、思考:讨论各种分配算法的特点。
S3C44B0X英文原版数据手册 SAMSUNG's S3C44B0X 16/32-bit RISC microprocessor is designed to provide a cost-effective and high performance micro-controller solution for hand-held devices and general applications. To reduce total system cost, S3C44B0X also provides the following: 8KB cache, optional internal SRAM, LCD controller, 2-channel UART with handshake, 4- channel DMA, System manager (chip select logic, FP/ EDO/SDRAM controller), 5-channel timers with PWM, I/O ports, RTC, 8-channel 10-bit ADC, IIC-BUS interface, IIS-BUS interface, Sync. SIO interface and PLL for clock. The S3C44B0X was developed using a ARM7TDMI core, 0.25 um CMOS standard cells, and a memory compiler. Its low-power, simple, elegant and fully static design is particularly suitable for cost-sensitive and power sensitive applications. Also S3C44B0X adopts a new bus architecture, SAMBA II (SAMSUNG ARM CPU embedded Microcontroller Bus Architecture). An outstanding feature of the S3C44B0X is its CPU core, a 16/32-bit ARM7TDMI RISC processor (66MHz) designed by Advanced RISC Machines, Ltd. The architectural enhancements of ARM7TDMI include the Thumb decompressor, an on-chip ICE breaker debug support, and a 32-bit hardware multiplier. By providing a complete set of common system peripherals, the S3C44B0X minimizes overall system costs and eliminates the need to configure additional components. The integrated on-chip functions that are described in this document are as follows: · 2.5V Static ARM7TDMI CPU core with 8KB cache . (SAMBA II bus architecture up to 66MHz) · External memory controller. (FP/EDO/SDRAM Control, Chip Select logic) · LCD controller (up to 256 color DSTN) with 1-ch LCD-dedicated DMA. · 2-ch general DMAs / 2-ch peripheral DMAs with external request pins · 2-ch UART with handshake(IrDA1.0, 16-byte FIFO) / 1-ch SIO · 1-ch multi-master IIC-BUS controller · 1-ch IIS-BUS controller · 5-ch PWM timers & 1-ch internal timer · Watch Dog Timer · 71 general purpose I/O ports / 8-ch external interrupt source · Power control: Normal, Slow, Idle, and Stop mode · 8-ch 10-bit ADC. · RTC with calendar function. · On-chip clock generator with PLL.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值