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输入英文文本:";
	}
 } 

运行结果为:

 

posted @ 2021-10-28 16:33  不知名摸鱼选手  阅读(39)  评论(1)    收藏  举报