1. 程式人生 > 其它 >201983290531 唐乾凱 面向物件程式設計實驗2

201983290531 唐乾凱 面向物件程式設計實驗2

info.h

#ifndef INFO_HPP
#define INFO_HPP
#include<iostream>
#include<algorithm>
using namespace std;
class Info{
	private:
		string nickname;
		string contact;
		string city;
		int n;
	public:
		~Info();
		Info(string name,string con,string c,int num);
		void print();
};
Info::~Info()
{
}
Info::Info(string name,string con,string c,int num)
{
	nickname=name;
	contact=con;
	city=c;
	n=num;

}
void Info::print()
{
	cout<<nickname<<"  "<<contact<<"  "<<city<<"  "<<n<<endl; 
}
#endif 

task5.cpp

#include "info.h"
#include<iostream>
#include<vector>
#include<string>
#define maxn 100
static int currentnum=0,i=0;
using namespace std;
int main(void)
{
	vector<Info>a;
	const int capacity=100;
	string name,con,ci;
	int n;
	cout<<"錄入資訊:"<<endl;
	while(cin>>name)
	{
		cin>>con>>ci>>n;
		if(currentnum+n>capacity)
		{
			cout<<"對不起還剩:"<<capacity-currentnum<<"個位置."<<endl<<"1.輸出u,更新預定資訊"<<endl
			<<"2.輸入q,退出預定"<<endl;
			cout<<"你的選擇"<<endl;
			char c;
			cin>>c;
			if(c=='u')
			cout<<"請重新輸入"<<endl;
			else
			{
				cout<<endl;
				break;
			}
			 
		}
		else
		{
			Info audi(name,con,ci,n);
			a.push_back(audi);
			i++;
			currentnum+=n;
			if(currentnum==capacity)
			break;
		}
	}
	for(auto t=a.begin();t!=a.end();t++)
	{
		t->print();
	}
}

執行結果為:

textcoder.h

#ifndef TEXTCODER
#define TEXTCODER
#include<iostream>
#include<string>
using namespace std;
char plus_(char n,char c,char m)
{
	for(int i=0;i<5;i++)
	{
		++c;
		if(c>m)
		c=n;
	}
	return c;
}
char subtract_(char n,char c,char m)
{
	for(int i=0;i<5;i++)
	{
		--c;
		if(c<n)
		c=m;
	}
	return c;
}
class TextCoder
{
	private:
		string text;
	public:
		~TextCoder();
		TextCoder(string text_n)
		{
			text=text_n;
		}
		string encoder();
		string decoder();
};
TextCoder::~TextCoder()
{
}
string TextCoder::encoder()
{
	int i=0;
	for(i=0;text[i]!='\0';i++)
	{
		if(text[i]>='a'&&text[i]<='z')
		text[i]=plus_('a',text[i],'z');
		else if(text[i]>='A'&&text[i]<='Z')
		text[i]=plus_('A',text[i],'Z');
	}
	return text;
}
string TextCoder::decoder()
{
	int i=0;
	for(i=0;text[i]!='\0';i++)
	{
		if(text[i]>='a'&&text[i]<='z')
		text[i]=subtract_('a',text[i],'z');
		else if(text[i]>='A'&&text[i]<='Z')
		text[i]=subtract_('A',text[i],'Z');
	}
	return text;
}
#endif 

task6.cpp

#include "textcoder.h"
#include<iostream>
#include<string>
int main(void)
{
	using namespace std;
	string text,encoded_text,decoded_text;
	cout<<"輸入英文文字:";
	while(getline(cin,text))
	{
		encoded_text=TextCoder(text).encoder();
		cout<<"加密後英文文字:\t"<<encoded_text<<endl;
		
		decoded_text=TextCoder(encoded_text).decoder();
		
		cout<<"解密後英文文字:\t"<<decoded_text<<endl;
		cout<<"\n輸入英文文字:";
	}
 } 

執行結果為: