C++ 练习题四道 hv-LDM

1.判断两个日期间的天数

cpp 复制代码
class test {
public:
	int y, m, d, x;
	test()
	{
		y = 2023, m = 3, d = 1, x = 3;
	}
	test(int yy, int mm, int dd)
	{
		y = yy;
		m = mm;
		d = dd;
	}
	void ReturnWeekDay()
	{
		int iWeek = 0;
		int Y = 0, C = 0, M = 0, D = 0;

		if (m == 1 || m == 2)
		{
			C = (y - 1) / 100;
			Y = (y - 1) % 100;
			M = m + 12;
			D = d;
		}
		else
		{
			C = y / 100;
			Y = y % 100;
			M = m;
			D = d;
		}
		iWeek = Y + Y / 4 + C / 4 - 2 * C + 26 * (M + 1) / 10 + D - 1;
		iWeek = iWeek >= 0 ? (iWeek % 7) : (iWeek % 7 + 7);
		if (iWeek == 7)
		{
			iWeek = 0;
		}
		cout << "today is weekday:" << iWeek << endl;
	}
};
int main()
{
	test t(2013,3,3);
	t.ReturnWeekDay();
	return 0;
}

2.对学生成绩排序

cpp 复制代码
struct S
{
	int grade;
	int number;
	int score;
	bool operator == (S s)
	{
		return score == s.score;
	}
};
class test
{
private:
	list<S>a;
	list<S>::iterator p;
	int n, i;
	static int m;
public:
	test(int nn)
	{
		S temp;
		n = nn;
		srand(time(0));
		for (i = 0; i < n; ++i)
		{
			temp.grade = rand() % 4 + 1;
			temp.number = rand();
			temp.score = rand()%99;
			a.push_back(temp);
		}
	}
	void browse()
	{
		for (p = a.begin(); p != a.end(); ++p)
		{
			cout << p->grade << "-" << p->number << "-" << p->score << endl;
		}
	}
	static bool sort1(S s1, S s2)
	{
		return s1.score < s2.score;
	}
	void Sort()
	{
		a.sort(sort1);
	}
	static bool find1(S s)
	{
		return s.score >= 80;
	}
	static bool find2(S s)
	{
		return s.grade == m;
	}
	void turn()
	{
		int num = 0;
		p = a.begin();
		while (1)
		{
			p = find_if(p, a.end(), find1);
			if (p != a.end())
			{
				++num;
				++p;
			}
			else break;
		}
		cout << num << endl;
		cout << "input delete:" << endl;
		cin >> m;
		p = a.begin();
		while (1)
		{
			p = find_if(p, a.end(), find2);
			if (p != a.end())
			{
				p = a.erase(p);
				++p;
			}
			else break;
		}
		for(;p != a.end();++p)
		{
			if (p->grade == m)
			{
				a.erase(p);
			}
			else break;
		}
	}
};
int test::m;
int main()
{
	test t(10);
	t.Sort();
	t.browse();
	cout << "==========" << endl;
	t.turn();
	t.browse();
	return 0;
}

3.简单排序

cpp 复制代码
class test
{
protected:
	int n;
public:
	test()
	{
		n = 10;

	}
	test(int nn )
	{
		n = nn;

	}
	static bool sort1(int n1, int n2)						//必须静态
	{
		return n1 > n2;						//实现从大到小排序
	}
	void output() {
		int x = 0;
		int i = 0;
		int* p = new int[n];
		for (i = 0; i < n; i++)
		{
			x = rand() % 89 + 11;
			p[i] = x;
		}
		sort(p,p + n,sort1);
		for (i = 0; i < 3; i++)
		{
			cout << p[i] << endl;
		}
	}
};
class test1:public test
{
protected:
	int m;
public:
	test1()
	{
		m = 2;
	}
	test1(int nn,int mm):test(nn)
	{
		m = mm;
	}
	void turn()
	{
		srand(time(0)+rand());
		int i = 0;
		for (i = 0; i < m; i++)
		{
			test1* p = new test1(*this);
			p->output();
			delete p;
		}
	}
};
int main()
{
	test1 t(10,2);
	t.turn();
	return 0;
}

4.文件操作 查找指定字符

cpp 复制代码
class test
{
private:
	string s, s1;
	string::size_type n, n1, n2, pos;
	ifstream f;
public:
	test()
	{
	}
	void turn()
	{
		f.open("dictionary.txt", ios::in);
		if (!f)
		{
			cout << "file error!" << endl;
		}
		string temp;
		cout << "input word:";
		cin >> temp;
		while (!f.eof())
		{
			getline(f, s);
			if (temp == Word(s))
			{
				cout << s << endl;
				break;
			}
		}
		f.close();
	}
	string Word(string s)
	{
		n = s.find(" ", 0);
		s1 = s.substr(0, n);
		return s1;
	}
};
int main()
{
	test t;
	t.turn();
	return 0;
}

class test {
private:
	fstream f; 
	string::size_type n; 
	map<string, string>a;
	map<string, string>::iterator p;
public:
	test()
	{
		string s, s1;
		f.open("dictionary.txt", ios::in); 
		while (true)
		{
			getline(f, s); 
			n = s.find(" ", 0); 
			s1 = s.substr(0, n); 
			a.insert(pair<string, string>(s1, s)); 
			if (f.eof())
				break;
		}
	} 
	~test() 
	{ f.close(); }
	void FIND() 
	{
		string word;
		cout << "input word:";
		cin >> word;
		p = a.find(word);
		if (p != a.end())
		{
			cout << p->second << endl;
		}
		else cout << "no word!" << endl;

	}
};
int main()
{
	test t;
	t.FIND(); 
	return 0;
}
相关推荐
love530love2 分钟前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm
-qOVOp-3 分钟前
408第一季 - 数据结构 - 图II
数据结构
凌辰揽月3 分钟前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
-qOVOp-4 分钟前
408第一季 - 数据结构 - 树与二叉树III
数据结构
海奥华27 分钟前
go中的接口返回设计思想
开发语言·后端·golang
lifallen9 分钟前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法
运维开发王义杰9 分钟前
Python: 告别 ModuleNotFoundError, 解决 pipx 环境下 sshuttle 缺少 pydivert 依赖的终极指南
开发语言·python
jingfeng51410 分钟前
数据结构排序
数据结构·算法·排序算法
k要开心11 分钟前
从C到C++语法过度1
开发语言·c++
小吕学编程14 分钟前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式