期中考试

1

 1 #include "date.h"
 2 #include "utils.h" 
 3 #include <iostream>
 4 using std::cout;
 5 using std::endl;
 6 
 7 // 补足程序,实现Date类中定义的成员函数 
 8 Date::Date():year(1970),month(1),day(1){
 9 }
10 
11 Date::Date(int y, int m, int d):year(y),month(m),day(d){
12 }
13 
14 void Date::display(){
15     cout<<year<<"-"<<month<<"-"<<day<<endl;
16 }
17 
18 int Date::getYear() const{
19     return year;
20 }
21 
22 int Date::getMonth() const{
23     return month;
24 }
25 
26 int Date::getDay() const{
27     return day;
28 }
29 
30 int Date::dayOfYear(){
31     int i=0;
32     if(isLeap(year)==true)
33         i=1;
34     switch (month)
35     {
36     case 1:
37         return day;
38     case 2:
39         return 31+day;
40     case 3:
41         return 59+i+day;
42     case 4:
43         return 90+i+day;
44     case 5:
45         return 120+i+day;
46     case 6:
47         return 151+i+day;
48     case 7:
49         return 181+i+day;
50     case 8:
51         return 212+i+day;
52     case 9:
53         return 243+i+day;
54     case 10:
55         return 273+i+day;
56     case 11:
57         return 304+i+day;
58     case 12:
59         return 334+i+day;
60     }
61 }

——————————————以下为考试后补充内容————————————

2、

 1 #ifndef ARTICLE_H
 2 #define ARTICLE_H
 3 
 4 #include<string>
 5 using std::string;
 6 
 7 class Article{
 8 public:
 9     Article();
10     void changetit();
11     void changecon();
12     void print();
13 private:
14     string title;
15     string content;
16     string publicTime;
17     static string lastUpdateTime;
18 }
19 
20 #endif
article.h
1 //这个头文件里包含了可用工具函数的声明 
2 
3 #include <string>
4 using std::string;
5 
6 // 函数声明
7 // 返回当前系统时间,格式诸如2019-4-30 13:30
8 string getCurrentTime();  
utils.h
 1 #include<iostream>
 2 #include"article.h"
 3 #include "utils.h"
 4 using namespace std;
 5 
 6 string Article::lastUpdateTime = getCurrentTime();
 7 
 8 Article::Article(){
 9     cout << "输入标题:";
10     cin >> title;
11     cout << "输入内容:";
12     cin >> content;
13     publicTime = lastUpdateTime;
14 }
15 
16 
17 void Article::changetit() {
18     cout << endl;
19     cout << "更改标题为:";
20     cin >> title;
21     }
22 
23 
24 void Article::changecon(){
25     cout << endl;
26     cout << "更改内容为:";
27     cin >> content;
28     }
29 
30 void Article::print(){
31     cout << "==================文章信息==================" << endl;
32     cout<<"标题:		"<<title<<endl;
33     cout<<"内容:		"<<content<<endl;
34     cout<<"发布时间:		"<<publicTime<<endl;
35     cout<<"最后一次更新时间:	"<<lastUpdateTime<<endl;
36 }
article.cpp
 1 #include "utils.h"
 2 #include <ctime>
 3 using std::string;
 4 
 5 const int SIZE = 20;
 6 
 7 // 函数功能描述:返回当前系统时间
 8 // 参数描述:无参数
 9 // 返回值描述:以string类型返回系统当前时间,格式诸如2019-4-30 13:30 
10 string getCurrentTime() {
11     
12     time_t now = time(0);  // 获取当前系统日历时间
13     
14     struct tm *local_time = localtime(&now);  // 把系统日历时间转换为当地时间
15     
16     char date[SIZE];
17 
18     strftime(date, SIZE, "%Y-%m-%d %H:%M", local_time);
19     
20     return (string(date));
21 } 
utils.cpp
 1 #include<iostream>
 2 #include"utils.h"
 3 #include"article.h"
 4 
 5 using namespace std;
 6 
 7 int main() {
 8     Article a;
 9     a.print();
10 
11     a.changetit();
12     a.changecon();
13 
14     a.print();
15 
16     system("pause");
17     return 0;
18 }
main.cpp

并未得出最后效果,这个错误也不知如何解决。

3、

 1 #include "info.h"
 2 #include <iostream>
 3 #include<string>
 4 #include<vector>
 5 // 补足必要的头文件 
 6 // 。。。 
 7 using namespace std;
 8 
 9 int main() {
10     
11     // 补足程序,实现题目要求 
12     // 。。。
13     vector<Info> audienceInfoList;
14     string nicknameN, contactN, cityN;
15     int nN,sum=0;
16 
17     while ((cin >> nicknameN >> contactN >> cityN >> nN )&& (sum<10000) ) {
18             Info a(nicknameN, contactN, cityN, nN);
19             audienceInfoList.push_back(a);
20             sum += nN;
21 
22     }
23     cout << "目前已有" << sum << "人预定参加。" << endl;
24     for (unsigned i = 0;i < audienceInfoList.size();i++)
25         audienceInfoList[i].print();
26     
27     system("pause"); 
28     return 0;
29 }
main.cpp

原文地址:https://www.cnblogs.com/zuiyankh/p/10795846.html