• 期中编程题


    /*问题描述*/

    补足日期类实现,使得能够根据日期获取这是一年中第多少天。(12分) 

    /*代码如下*/

    // 函数声明 
    bool isLeap(int);
    untils.h
    1 bool isLeap(int year) {
    2     if( (year % 4 == 0  &&  year % 100 !=0) || (year % 400 == 0) )
    3         return true;
    4     else
    5         return false;
    6 }
    untils.cpp
     1 #ifndef DATE_H
     2 #define DATE_H
     3 
     4 class Date {
     5     public:
     6         Date(); // 默认构造函数,将日期初始化为1970年1月1日 
     7         Date(int y, int m, int d); // 带有形参的构造函数,用形参y,m,d初始化年、月、日 
     8         void display(); // 显示日期 
     9         int getYear() const;  // 返回日期中的年份 
    10         int getMonth() const; // 返回日期中的月份 
    11         int getDay() const; // 返回日期中的日字 
    12         int dayOfYear(); // 返回这是一年中的第多少天
    13 
    14     private:
    15         int year;
    16         int month;
    17         int day;  
    18 };
    19 
    20 #endif
    date.h
    #include "date.h"
    #include "utils.h" 
    #include <iostream>
    using std::cout;
    using std::endl;
    
    // 补足程序,实现Date类中定义的成员函数 
    Date::Date() :year(1970), month(1), day(1) {} // 默认构造函数,将日期初始化为1970年1月1日 
    Date::Date(int y, int m, int d) {
        year = y; month = m; day = d;
    }// 带有形参的构造函数,用形参y,m,d初始化年、月、日 
    void Date::display() {
        cout << year << "-" << month << "-" << day << endl;
    }// 显示日期 
    int Date::getYear() const { return year; }  // 返回日期中的年份 
    int Date::getMonth() const { return month; } // 返回日期中的月份 
    int Date::getDay() const { return day; }// 返回日期中的日字 
    int Date::dayOfYear() {
        int month1[2][12] = { 31,29,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31 };
        if (isLeap(year)) {
            int days = day;
            for (int i = 0; i < month - 1; i++)
                days += month1[1][i];
            return days;
        }
        else {
            int days = day;
            for (int i = 0; i < month - 1; i++)
                days += month1[1][i];
            return days;
        }
    }
    date.cpp
     1 #include "utils.h"
     2 #include "date.h"
     3 
     4 #include <iostream>
     5 using namespace std;
     6 int main() {
     7     
     8     Date epochDate;
     9     epochDate.display();
    10     cout << "" <<epochDate.getYear()<<"年第"<< epochDate.dayOfYear() << "天.
    
    " ;
    11     
    12     Date today(2019,4,30);
    13     
    14     today.display();
    15     cout << "" <<today.getYear()<<"年第"<< today.dayOfYear() << "天.
    
    " ;
    16     
    17     Date tomorrow(2019,5,1);
    18     tomorrow.display();
    19     cout << "" <<tomorrow.getYear()<<"年第"<< tomorrow.dayOfYear() << "天.
    
    ";
    20     
    21     system("pause");
    22     return 0;
    23 }
    date.main.cpp

    /*运行截图*/

    /*问题描述*/

     博客文章管理(博客文章发布、更新)(18分)

    具体要求如下: 基于以下描述设计并实现文章类Article 每篇博客文章都有文章标题(title),  文章内容(content),发布时间(publicTime), 最后一次更新时间 lastUpdateTime四个属性。四个属性都是string类型。 使用默认构造函数构造文章对象时,要求输入文章标题,文章内容,标题和内容都允许包含空格。构 造对象时,自动获取当前系统时间作为发布时间,首次构造时,最后一次更新时间与发布时间相同。 支持更新文章标题。每次更新标题后,获取当前系统时间作为最后一次更新时间。 支持更新文章内容。每次更新内容后,获取当前系统时间作为最后一次更新时间。 支持打印文章信息,打印信息包括文章标题、文章内容、发布时间、最后一次更新时间。 

    /*代码如下*/

    1 //这个头文件里包含了可用工具函数的声明 
    2 
    3 #include <string>
    4 using std::string;
    5 
    6 // 函数声明
    7 // 返回当前系统时间,格式诸如2019-4-30 13:30
    8 string getCurrentTime();  
    untils.h
     1 #define _CRT_SECURE_NO_WARNINGS//可能是不同编译环境原因,这里定义一个宏,避免了函数弃用
     2 #include "utils.h"
     3 #include <ctime>
     4 using std::string;
     5 
     6 
     7 const int SIZE = 20;
     8 
     9 // 函数功能描述:返回当前系统时间
    10 // 参数描述:无参数
    11 // 返回值描述:以string类型返回系统当前时间,格式诸如2019-4-30 13:30 
    12 string getCurrentTime() {
    13     
    14     time_t now = time(0);  // 获取当前系统日历时间
    15     
    16     struct tm *local_time = localtime(&now);  // 把系统日历时间转换为当地时间
    17     
    18     char date[SIZE];
    19 
    20     strftime(date, SIZE, "%Y-%m-%d %H:%M", local_time);
    21     
    22     return (string(date));
    23 } 
    untils.cpp
     1 #pragma once
     2 #ifndef ARTICLE_H
     3 #define ARTICLE_H
     4 #include<string>
     5 using namespace std;
     6 
     7 class article {
     8 public:
     9     article(string title0, string content0, string publictime0);
    10     article();
    11     void print_info();
    12     void update_title(string s1);
    13     void update_content(string s2);
    14     friend string getCurrentTime();
    15 private:
    16     string title;
    17     string content;
    18     string publictime;
    19     string lastupdate;
    20 };
    21 
    22 #endif // !ARTICLE_H
    article.h
     1 #include"article.h"
     2 #include<iostream>
     3 #include<string>
     4 #include<iomanip>
     5 using namespace std;
     6 
     7 string getCurrentTime();
     8 
     9 article::article(string title0, string content0, string publictime0) {
    10     publictime = getCurrentTime();
    11     title = title0;
    12     content = content0;
    13 }
    14 article::article():title("陈涛&沈桥"), content("陈涛最帅,沈桥最美"), publictime(getCurrentTime()),lastupdate(getCurrentTime()){}
    15 void article::print_info() {
    16     lastupdate = getCurrentTime();
    17     cout << "============当前文章信息============" << endl;
    18     cout << "文章标题:" << title << endl;
    19     cout << "文章内容:" << content << endl;
    20     cout << "发布时间:" << publictime << endl;
    21     cout << "最新更新时间:" << lastupdate << endl;
    22     cout << "*************************************" << endl;
    23 }
    24 void article::update_title(string s1) {
    25     title = s1;
    26 }
    27 void article::update_content(string s2) {
    28     content = s2;
    29 }
    article.cpp
     1 #include"article.h"
     2 #include<iostream>
     3 #include<string>
     4 using namespace std;
     5 
     6 int main() {
     7     article love;
     8     love.print_info();
     9     article A("博客", "博客内容","当前时间");
    10     A.print_info();
    11     string s1, s2;
    12     cout << "重新设置标题:" << endl;
    13     cin >> s1;
    14     A.update_title(s1);
    15     cout << "更新博客内容" << endl;
    16     cin >> s2;
    17     A.update_content(s2);
    18     A.print_info();
    19     system("pause");
    20     return 0;
    21 }
    main.cpp

    /*运行截图*/

     

    /*问题描述*/

     预约信息登记显示 (10分)

    问题场景描述如下: 某独立音乐人要举办一场免费小型liveshow。livehouse场地容量有限,最多容纳100位乐迷听众。现通过某平 台开通线上预约登记。线上预约登记信息如下: 昵称/称呼(nickname) 联系方式(contact) 所在城市(city) 预定参加人数(n) 设已经基于上述问题场景设计并实现了一个信息类Info用于记录报名登记听众信息。要求: 创建项目文件。添加文件info.h,info.cpp, main.cpp到项目中。 在main()中,定义一个vector<Info>类对象audienceInfoList,用于存放线上预约登记的听众信息。 从键盘录入每一个预约登记信息,直到停止录入(按下Ctrl+Z, 也可以通过程序自行设计其它停止录入方 式)或者预定参加人数达到上限,输出audienceInfoList对象中所有预约听众信息。 

    /*代码如下*/

     1 #ifndef INFO_H
     2 #define INFO_H
     3 
     4 #include <string>
     5 using std::string;
     6 
     7 class Info {
     8     public:
     9         Info(string nickname0, string contact0, string city0, int n);
    10         void print();        
    11     private:
    12         string nickname;    // 称呼/昵称 
    13         string contact;        // 联系方式,可以是email,也可以是手机号 
    14         string city;        // 所在城市 
    15         int n;                // 预定到场人数 
    16 };
    17 
    18 #endif 
    info.h
    #include "info.h"
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    Info::Info(string nickname0, string contact0, string city0, int n0): nickname(nickname0), contact(contact0), city(city0), n(n0){
        
    }
    
    
    void Info::print() {
        cout << "称呼:		" << nickname << endl;
        cout << "联系方式:	" << contact << endl;
        cout << "所在城市:	" << city << endl;
        cout << "预定人数:	" << n << endl << endl;
     }
    info.cpp
     1 #include "info.h"
     2 #include <iostream>
     3 #include<vector> 
     4 using namespace std;
     5 
     6     
     7 int main() {
     8 
     9     vector<Info>audienceInfoList;//创建vector类
    10     string nickname, contact, city;
    11     int n;
    12     int i = 0,judge;
    13     cout << "请依次录入称呼,联系方式,城市,预定票数,停止录入请按0" << endl;
    14     while (cin >> nickname >> contact >> city >> n >> judge) {
    15         Info info(nickname, contact, city, n);
    16         i++;
    17         audienceInfoList.push_back(info);//追加
    18         if(judge==0)
    19         for (int k = 0; k < i; k++)
    20             audienceInfoList[k].print();
    21     }
    22     
    23     return 0;
    24 }
    main.cpp

    /*运行截图*/

  • 相关阅读:
    20155322 2017-2018-1《信息安全系统设计》第七周学习总结
    20155322 2017-2018-1《信息安全系统设计》实验二:固件程序设计
    20155322 2017-2018-1《信息安全系统设计》第六周学习总结
    20155322 2017-2018-1《信息安全系统设计》第六周 课下作业
    01--DNS服务器1
    华为lab-rs-v1-2.5_流量优化
    华为lab-rs-v1-2.4_OSPF提升
    华为lab-rs-v1-2.3_OSPF基础
    华为lab-rs-v1-2.2_RIP基础
    华为lab-rs-v1-2.1_IP基础
  • 原文地址:https://www.cnblogs.com/shenqidetao/p/10802029.html
Copyright © 2020-2023  润新知