期中
1.
#include "date.h" #include "utils.h" #include <iostream> using std::cout; using std::endl; // 补足程序,实现Date类中定义的成员函数 /*#ifndef DATE_H #define DATE_H class Date { public: Date(); // 默认构造函数,将日期初始化为1970年1月1日 Date(int y, int m, int d); // 带有形参的构造函数,用形参y,m,d初始化年、月、日 void display(); // 显示日期 int getYear() const; // 返回日期中的年份 int getMonth() const; // 返回日期中的月份 int getDay() const; // 返回日期中的日字 int dayOfYear(); // 返回这是一年中的第多少天 private: int year; int month; int day; }; #endif */ Date::Date(){ year=1970; month=1; day=1; } Date::Date(int y, int m, int d):year(y),month(m),day(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 m1[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if(isLeap(year)==1) m1[2]=29; else m1[2]=28; int day2=0; if(month==1){ day2=day; } else{ for(int i=1;i<month;i++){ day2=day2+m1[i]; } day2=day2+day; } return day2; }

2.
#ifndef ARTICLE_H #define ARTICLE_H #include<string> using std::string; class Article{ public: Article(); void changeTitle(); //修改标题 void changeContent(); //修改内容 void printArticle(); //打印 private: string title; string content; string publicTime; //发布时间 string lastUpdateTime; //最后一次发布时间 }; #endif
#include"article.h" #include"utils.h" #include<string> #include<iostream> using namespace std; Article::Article(){ cout << "标题: " << endl; getline(cin,title); cout << "内容: " << endl; getline(cin,content); publicTime=getCurrentTime(); lastUpdateTime=publicTime; } void Article::changeTitle(){ cout << "更改标题..." << endl; cout << "输入新标题:" << endl; getline(cin,title); lastUpdateTime=getCurrentTime(); } void Article::changeContent(){ cout << "修改内容..." << endl; cout << "输入新内容:" << endl; getline(cin,content); lastUpdateTime=getCurrentTime(); } void Article::printArticle(){ cout << "==================文章信息====================" << endl; cout << "标题: " << title << endl; cout << "内容:" << endl << content << endl; cout << "发布时间: " << publicTime << endl; cout << "最后一次更新时间: " << lastUpdateTime << endl; }
#include"article.h" #include"utils.h" #include<iostream> #include<string> using namespace std; int main(){ Article a; a.printArticle(); a.changeTitle(); a.changeContent(); a.printArticle(); return 0; }
运行结果如下:

3.
#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
#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; }
#include "info.h" #include <iostream> #include<string> #include<vector> using namespace std; int main() { cout << "录入信息: " << endl; cout << endl; cout << "称呼/昵称, 联系方式(邮箱/手机号), 所在城市, 预定参加人数" << endl; string myname,mycontract,mycity; int number; int num=0; vector<Info>audienceInfoList; while(cin >> myname >> mycontract >> mycity >> number){ audienceInfoList.push_back(Info(myname,mycontract,mycity,number)); num=num+number; if(num>=100) break; } cout << "截至目前,共有" << num << "位听众预定参加。预定听众信息如下: " << endl; for(int i=0; i<audienceInfoList.size(); i++){ audienceInfoList[i].print(); } system("pause"); return 0; }
运行结果如下:

浙公网安备 33010602011771号