1.
实现部分:
#include "date.h" #include "utils.h" #include <iostream> using std::cout; using std::endl; Date::Date():year(1970),month(1),day(1){} Date::Date(int y, int m, int d):year(y),month(m),day(d){} int Date::getYear()const { return year; } int Date::getMonth()const { return month; } int Date::getDay()const { return day; } void Date::display() { cout<<year<<"-"<<month<<"-"<<day<<endl; } int Date::dayOfYear() { int a[11]={31,28,31,30,31,30,31,31,30,31,30}; int sum=0; for(int t=0;t<month-1;t++) { sum+=a[t]; } sum+=day; if(isLeap(year)) { sum++; } return sum; } // 补足程序,实现Date类中定义的成员函数
图片:
2.
Article:
Article.h:
#ifndef Article_H #define Article_H #include<string> #include<iostream> using namespace std; class Article{ public: Article(string t0,string c0,string pt,string lt); void updatetitl(); void updatecon(); void show(); friend string getCurrentTime(); private: string title; string content; string publictm; string lasttm; }; #endif
Article.cpp:
#include"Article.h" #include<string> #include<iostream> using namespace std; string getCurrentTime(); Article::Article(string t0,string c0,string pt,string lt):title(t0),content(c0),publictm(pt),lasttm(lt){} void Article::show() { cout<<"-----------------"<<endl; cout<<"文章标题: "<<title<<endl; cout<<"文件内容: " <<content<<endl; cout<<"发布时间: "<<publictm<<endl; cout<<"最后一次更新时间: "<<lasttm<<endl; cout<<"------------------"<<endl; } void Article::updatetitl(){ string getCurrentTime(); cout<<"输入新标题:"<<endl; string a; getline(cin,a); title=a; string t; t=getCurrentTime(); lasttm=t; } void Article::updatecon() { string getCurrentTime(); cout<<"输入新内容:"<<endl; string a; getline(cin,a); content=a; string t; t=getCurrentTime(); lasttm=t; }
utils.h:
bool isLeap(int);
utils.cpp:
#include "utils.h" #include <ctime> using std::string; const int SIZE = 20; // 函数功能描述:返回当前系统时间 // 参数描述:无参数 // 返回值描述:以string类型返回系统当前时间,格式诸如2019-4-30 13:30 string getCurrentTime() { time_t now = time(0); // 获取当前系统日历时间 struct tm *local_time = localtime(&now); // 把系统日历时间转换为当地时间 char date[SIZE]; strftime(date, SIZE, "%Y-%m-%d %H:%M", local_time); return (string(date)); }
main.cpp:
#include"Article.h" #include<string> #include<iostream> using namespace std; int main() { string title1,content1,publictm1,lasttm1; cout<<"输入标题:"<<endl; string getCurrentTime(); getline(cin,title1); cout<<"输入内容:"<<endl; getline(cin,content1); publictm1=getCurrentTime(); lasttm1=getCurrentTime(); Article t1(title1,content1,publictm1,lasttm1); t1.show(); t1.updatetitl(); t1.updatecon(); t1.show(); return 0; }
图片:
3.
Info:
info.h:
#ifndef INFO_H #define INFO_H #include <string> using std::string; class Info { public: Info(string nickname0, string contact0, string city0, int n); void print(); private: string nickname; // 称呼/昵称 string contact; // 联系方式,可以是email,也可以是手机号 string city; // 所在城市 int n; // 预定到场人数 }; #endif
info.cpp:
#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 << "称呼:\t\t" << nickname << endl; cout << "联系方式:\t" << contact << endl; cout << "所在城市:\t" << city << endl; cout << "预定人数:\t" << n << endl; }
main.cpp:
#include "info.h" #include <iostream> #include<vector> using namespace std; int main() { vector<Info>a; string nickname1, contact1, city1; int n; int k; int sum = 0; cin >> k; int j = 0; cout << "录入信息:" << endl; while (k--) { cout << "输入姓名:" << endl; cin >> nickname1; cout << "输入联系方式:" << endl; cin >> contact1 ; cout << "输入地址:" << endl; cin >> city1; cout << "输入人数:" << endl; cin >> n; sum += n; Info info(nickname1, contact1, city1, n); j++; a.push_back(info); } for(int i=0;i<j;i++) { cout << "输出信息:" << endl; a[i].print(); cout << "截至目前总人数为:" << sum << endl; } return 0; }
图片:
以上即为期中考试三道编程题,考试的时候由于不是特别熟悉考试编程软件的使用,以及具体某些操作,因而编程的速度过慢,只完成了一道,所以还需多加练习,增加熟练度,方能更加高速,正确的完成任务课程。