[Object-Oriented Programming] Midterm Project

the requirement: 

The Personal Diary is a CLI (Command Line Interface) software, consists of three programs:

pdadd 
pdlist [ ]
pdshow 
pdremove 
pdadd is used to add an entity into the diary for the date. If an entity of the same date is in the diary, the existing one will be replaced. pdadd reads lines of the diary from the stdin, line by line, until a line with single '.' character or the EOF character (Ctrl-D in Unix and Ctrl- Z in Windows).
pdlist lists all entities in the diary ordered by date. If start and end date are provided through command line parameters, it lists entities between start and end only. This program lists to the stdout.
pdshow prints the content of the entity specified by the date to the stdout. 
pdremove removes one entity of the date. It returns 0 at success and -1 at failure.
The software stores diary in one data file, reads to the memory at begining of each program and stores back to the file at the end of process.
Common classes and functions should be shared between programs. No interactive is permitted, so these programs are able to work together by the means of redirection or tee, are able to be used in a shell script.


The challenge is that it requires us to write the functions in separate files, We need to read in .txt and generate .txt and use shell script also. Haven't got to know these before. Go through some courses and ask my friend about some questions. Finally finished!

we create a class diary:

#include<iostream>
#include <string>
#include <vector>
#include <fstream>
#include <map>
using namespace std;
typedef vector<string>::size_type VStringSize;
#define fpath "diary.txt"

class Diary
{
private:
	string date;
	vector<string> text;
public:
	void Reset();
	void PdAdd();
	void SetDate(string);
	void AddLine(string);
	string GetText(VStringSize);
	string GetDate();
	VStringSize Size();

};
#include"diary.h"


void Diary::PdAdd()
{
	string str;
	getline(cin,str);
	while (str!=".")
	{
		text.push_back(str);
		getline(cin,str);
	}
}
void Diary::SetDate(string D)
{
	date = D;
}
void Diary::Reset()
{
	text.clear();
}


string Diary::GetDate()
{
	return date;
}


VStringSize Diary::Size()
{
	return text.size();
}
string Diary::GetText(VStringSize i)
{
	return text[i];
}
void Diary::AddLine(string Line)
{
	text.push_back(Line);
}

and the four following function with their own main( ) and include "diary.h":

pdadd:

#include"diary.h"
int compstr(string str1,string str2)
{
	return str1.compare(str2)==1;
}
int main(int garc,char *garv[])
{
	vector<Diary> my_diary;
	string str;
	Diary diary;
	string date;
	ifstream f_in(fpath);
	while (!f_in.eof())
	{
		getline(f_in,date);
		diary.SetDate(date);
		diary.Reset();
		string str;
		getline(f_in,str);
		while (str!=".")
		{
			diary.AddLine(str);
			getline(f_in,str);
		}
		my_diary.push_back(diary);
	}
	f_in.close();

	date = garv[1];
	diary.Reset();
	diary.SetDate(date);
	getline(cin,str);
	while (str!=".")
	{
		diary.AddLine(str);
		getline(cin,str);
	}
	vector<Diary>::size_type i,Size,ii;
	Size=my_diary.size();
	
	for (i=0;i<Size;i++)
		if (my_diary[i].GetDate()==date) break;
	if (i==Size)
	{
		my_diary.push_back(diary);
		for (i=0;i<Size;i++)
			if(compstr(my_diary[i].GetDate(),diary.GetDate()))	break;
		for (ii=Size;ii>i;ii--)
			my_diary[ii]=my_diary[ii-1];
		my_diary[i]=diary;
	}
	else
		my_diary[i] = diary;
	
	ofstream f_out(fpath);
	for (i=0;i<my_diary.size();i++)
	{
		f_out<<my_diary[i].GetDate()<<endl;
		vector<string>::size_type j;
		for (j=0;j<my_diary[i].Size();j++)
			f_out<<my_diary[i].GetText(j)<<endl;
		f_out<<"."<<endl;
	}
	f_out.close();
	
	return 0;
}


pdlist:

#include"diary.h"
#include<stdlib.h>
int between(string str,string upper,string low)
{
	if (str.compare(upper)<=0&&str.compare(low)>=0)	
		return 1;
	else	
		return 0;
}

int main(int argc,char *argv[])
{
	/*int argc=3;
	char *argv[3]={"","2013/01/01","2013/05/11"};*/
	vector<Diary> my_diary;
	string str;
	Diary diary;
	string date;
	ifstream f_in(fpath);
	while (!f_in.eof())
	{
		getline(f_in,date);
		diary.SetDate(date);
		diary.Reset();
		string str;
		getline(f_in,str);
		while (str!=".")
		{
			diary.AddLine(str);
			getline(f_in,str);
		}
		my_diary.push_back(diary);
	}
	f_in.close();

	vector<Diary>::size_type i,Size;
	Size=my_diary.size();
	VStringSize j;
	if (argc==3)
	{
		string dateUpper,dateLow;
		dateUpper=argv[2];
		dateLow=argv[1];

		for (i=0;i<Size;i++)
			if (between(my_diary[i].GetDate(),dateUpper,dateLow))
			{
				cout<<my_diary[i].GetDate()<<endl;
				cout<<my_diary[i].GetText(0)<<endl;
	
			}
	}
	else
		for (i=0;i<my_diary.size();i++)
		{
			cout<<my_diary[i].GetDate()<<endl;
			vector<string>::size_type j;
			cout<<my_diary[i].GetText(0)<<endl;

		}
	
	return 0;
}

pdremove:

#include"diary.h"
int main(int argc,char* argv[])
{
	vector<Diary> my_diary;
	string str;
	Diary diary;
	Diary *p;
	string date;
	ifstream f_in(fpath);
	while (!f_in.eof())
	{
		getline(f_in,date);
		diary.SetDate(date);
		diary.Reset();
		string str;
		getline(f_in,str);
		while (str!=".")
		{
			diary.AddLine(str);
			getline(f_in,str);
		}
		my_diary.push_back(diary);
	}
	f_in.close();

	date=argv[1];
	vector<Diary>::size_type i,ii,Size;
	Size=my_diary.size();
	for (i=0;i<Size;i++)
		if (my_diary[i].GetDate()==date) break;
	if (i==Size)
		cout<<"There is not such a diary which date is "<<date<<"."<<endl;
	else
	{	for (ii=i;ii<Size-1;ii++)
			my_diary[ii] = my_diary[ii+1];
		my_diary.pop_back();
	

		ofstream f_out(fpath);
		for (i=0;i<my_diary.size();i++)
		{
			f_out<<my_diary[i].GetDate()<<endl;
			vector<string>::size_type j;
			for (j=0;j<my_diary[i].Size();j++)
				f_out<<my_diary[i].GetText(j)<<endl;
			f_out<<"."<<endl;
		}

		f_out.close();
		return 1;
	}
	return 0;
}

pdshow:

#include "diary.h"
int main(int argc,char *argv[])
{
	vector<Diary> my_diary;
	string str;
	Diary diary;
	Diary *p;
	string date;
	ifstream f_in(fpath);
	while (!f_in.eof())
	{
		getline(f_in,date);
		diary.SetDate(date);
		diary.Reset();
		string str;
		getline(f_in,str);
		while (str!=".")
		{
			diary.AddLine(str);
			getline(f_in,str);
		}
		my_diary.push_back(diary);
	}
	f_in.close();
	date=argv[1];
	vector<Diary>::size_type i,Size;
	Size=my_diary.size();
	for (i=0;i<Size;i++)
		if (my_diary[i].GetDate()==date) break;
	if (i==Size)
		cout<<"There is not such a diary which date is "<<date<<"."<<endl;
	else
	{
		cout<<my_diary[i].GetDate()<<endl;
		vector<string>::size_type j;
		for (j=0;j<my_diary[i].Size();j++)
			cout<<my_diary[i].GetText(j)<<endl;
		cout<<"."<<endl;
	}

	return 0;
}

It's getting late. For a healthy body I'd better not stay up late. Good night everybody. Code a lot this semester. I will post some of them later.


  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值