zoukankan      html  css  js  c++  java
  • DS博客作业01--日期抽象数据类型设计与实现

    1.思维导图及学习体会

    1.1第一章绪论知识点思维导图

    1.2 学习体会

    这次博客园和大作业是我在编程学习中的有意义的进步,第一次尝试使用vs,并且通过同学的一些网站的推荐,和热心同学的帮忙,简单学会用c++,并且在大作业中更好地掌握抽象数据类型。算是对数据结构学习良好的开端!

    2.大作业作业内容

    2.1 设计日期的ADT类型

    ADT Date{
    数据对象:
        D = {year,month,day | year,month,day属于int类型}
    数据关系:
        R = {<year,month>,<month,day>}
    数据操作:
        Status InitDate(Date &date, int year, int month, int day);//构造三元组
        string OutDate(Date date);//使时间以string类型输出
        Status leepYear(DATE &date);//闰年判断
        string week(DATE &date);//星期几
        string englishName(DATE &date);//英文
        string AddDay(DATE date,int addDay);//添加日期
        char Compare(DATE &date, DATE &otherDate)//比较日期
    }ADT Date
    

    2.2.数据抽象:头文件

    date.h

    #include "common.h"
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <string>
    #include <stdlib.h>
    #include <fstream>
    using namespace std;
    typedef int * DATE;
    Status InitDATE(DATE &date, int year, int mounth, int day);//操作结果:构造了三元组T,判断日期的合法性
    string outDate(DATE &date);//字符型
    Status leepYear(DATE &date);//闰年判断
    string week(DATE &date);//星期几
    string englishName(DATE &date);//英文
    string AddDay(DATE date,int addDay);//添加日期
    char Compare(DATE &date, DATE &otherDate);//比较
    

    common.h

    #pragma once
    #define Ok 1;
    #define Error 0;
    #define Yes 1;
    #define No -1;
    #define O 0
    typedef int Status;
    

    2.3数据封装说明

    (1)构造三元组:判断日期的合法性,记得要new一个。

    Status InitDATE(DATE &date, int year, int mounth, int day)
    {
    	int result=Ok;
    	date = new int [3];//动态分配
    	if (!date) exit(OVERFLOW);//防止运算上溢
    	date[0] = year; date[1] = mounth; date[2] = day;
    	if (year < 0) {
    		result = Error;
    	}
    	else {
    		if (mounth < 1 || mounth>12)
    		{
    			result = Error;
    		}
    		else {
    			if (leepYear(date) == 0)
    			{
    				if (day > 28 || day < 0) {
    					result = Error;
    				}
    			}
    			else if (leepYear(date) == 1)
    			{
    				if (day < 0 || day>29)
    					result = 0;
    			}
    		}
    	}
    	return result;
    }
    

    (2)以string形式输出时间:使用c++里面的语法,sprintf来连接成字符串然后输出

    string outDate(DATE &date)
    {
    	char str[50];
    	sprintf(str, "%d/%d/%d", date[0], date[1], date[2]);
    	return str;
    }
    

    (3)判断闰年:c语言那时候的题目

    Status leepYear(DATE &date)
    {
    	Status result;
    	if (date[0] % 4 == 0 && date[0] % 100 != 0 || date[0] % 400 == 0)
    	{
    		result = Ok;
    	}
    	else {
    		result = Error;
    	}
    	return result;
    }
    

    (4)判断周几:使用了公式,不然还真的是毫无头绪

    string week(DATE &date)
    {
    	int year = date[0];
    	int mounth = date[1];
    	int day = date[2];
    	if (mounth == 1 || mounth == 2) {
    		mounth += 12;
    		year--;
    	}
    	int iWeek = (day + 2 * mounth + 3 * (mounth + 1) / 5 + year + year/ 4 - year / 100 + year / 400) % 7;
    	switch (iWeek)
    	{
    	case 0: return "这个星期是星期一"; 
    	case 1: return "这个星期是星期二"; 
    	case 2: return "这个星期是星期三"; 
    	case 3: return "这个星期是星期四"; 
    	case 4: return "这个星期是星期五"; 
    	case 5: return "这个星期是星期六"; 
    	case 6: return "这个星期是星期日"; 
    	}
    }
    

    (5)英文字母:简单的switch

    string englishName(DATE &date)
    {
    	switch (date[1])
    	{
    	case 1:return "January";
    	case 2:return "February";
    	case 3:return "March";
    	case 4:return "April";
    	case 5:return "May";
    	case 6:return "June";
    	case 7:return "July";
    	case 8:return "Augest";
    	case 9:return "Septemper";
    	case 10:return "October";
    	case 11:return "November";
    	case 12:return "December";
    	}
    }
    

    (6)增加日期:设用两个数组,首先判断是否为闰年,二月份是两个数组不同的地方,然后通过要加的日子递减,来使日期递增,不要忘记每个月满日子,要归回1,并且月份增加,年也是同样的思想,月份记得只有十二个。

    string AddDay(DATE date, int addDay)
    {
    	int leepYear1[13] = {31,29,31,30,31,30,31,31,30,31,30,31,0};
    	int ordinaryYear1[13]= { 31,28,31,30,31,30,31,31,30,31,30,31,0 };
    	if(leepYear(date)) {
    		do {
    			date[2]++;
    			if (date[2] > leepYear1[date[1] - 1])
    			{
    				date[1]++;
    				date[2] = 1;
    				if (date[1] > 12)//月满十二,年进一
    				{
    					date[0]++;
    					date[1] = 1;//月归1
    				}
    			}
    			addDay--;//递减
    		} while (addDay);
    	}
    	else {
    		do {
    			date[2]++;
    			if (date[2] > ordinaryYear1[date[1] - 1])
    			{
    				date[1]++;
    				date[2] = 1;
    				if (date[1] > 12)
    				{
    					date[0]++;
    					date[1] = 1;
    				}
    			}
    			addDay--;
    		} while (addDay);//平年也是如此
    	}
    	return outDate(date);
    }
    

    (7)比较日期:直接乘再加,让年月日的大小已经到达无法影响的时候,然后对比大小即可

    char Compare(DATE &date, DATE &otherDate)
    {
    	int dayOne = date[0] * 10000 + date[1] * 100 + date[2];
    	int dayTwo = otherDate[0] * 10000 + otherDate[1] * 100 + otherDate[2];
    	if (dayOne == dayTwo)
    	{
    		return '=';
    	}
    	else if (dayOne > dayTwo) {
    		return '>';
    	}else {
    		return '<';
    	}
    }
    

    (8)主函数:用c++读写文件,一定要记得关文件

    int main()
    {
    	DATE date, otherDate;
    	int year, mounth, day;
    	int otherYear, otherMounth, otherDay;
    	int addDay;
    	ifstream readFile;以读的方式
    	ofstream writeFile;以写的方式
    	readFile.open("input.txt", ios::in);
    	writeFile.open("output.txt",ios::out||ios::trunc);
    	do {
    		readFile >> year >> mounth >> day;//读取文件内容
    		if (InitDATE(date, year, mounth, day)) {
    			cout << outDate(date) << endl;
    			writeFile << outDate(date) << endl;
    		}
    		else {
    			cout << "日期不存在!" << endl;
    		}
    		if (leepYear(date) == 1)//判断闰平年
    		{
    			cout << "是闰年" << endl;
    		}
    		else {
    			cout << "不是闰年" << endl;
    		}
    		cout << week(date) << endl;;
    		cout << "你所要比较的日期:";
    		cin >> otherYear >> otherMounth >> otherDay;//输入比较的日期
    		if (InitDATE(otherDate, otherYear, otherMounth, otherDay))
    			cout << outDate(date) << Compare(date, otherDate) << outDate(otherDate) << endl;//输出比较结果
    		else {
    			cout << "比较的日期不合法呢" << endl;
    		}
    		cout << "你所要增加的天数是:";
    		cin >> addDay;
    		cout<<AddDay(date, addDay)<<endl;//输出添加日子后的日期
    		cout << endl;
    	} while (!readFile.eof());
    	readFile.close();//关文件
    	writeFile.close();//关文件
    }
    

    3.结果展示

    4.调试碰到问题

    1.没有动态分配,导致后面出现的时间是乱码
    2.在vs中的文本文件放错地方,导致一直无法出现正确的文本日期
    3.误以为<string.h>和是一样的,其实不然,cin和cout都必须是要有头文件
    4.最重要的是vs还不会很熟练的使用,c++语法还是很陌生。

  • 相关阅读:
    B站排行榜第一的视频,看看5W弹幕都在说些什么?
    手把手教你如何用Python获取爱奇艺电视剧弹幕数据
    Python爬虫中最重要、最常见、一定要熟练掌握的库
    机器学习
    nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
    14、ERROR: for proxy Cannot start service proxy: driver failed **** Error starting userland proxy: listen tcp 0.0.0.0:80: listen: address already in use
    13、file /usr/bin/docker from install of docker-ce-18.03.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.13.1-203.git0be3e21.el7.centos.x86_64
    12、Error response from daemon: Get https://192.168.247.151/v2/: dial tcp 192.168.247.151:443: connect: connection refused
    Selenium无法定位元素的九种解决方案
    Linux安装Oracle数据库SQLPlus客户端
  • 原文地址:https://www.cnblogs.com/wxj991220/p/10507781.html
Copyright © 2011-2022 走看看