c++期末课程设计(单号题)

期末课程设计

第1题:设计一个学生学籍管理系统
学生信息包括:姓名、学号、性别和英语、数学、程序设计、体育成绩。
从键盘输入数据,建立数据文件student.dat。
实现对学生或学号查询,显示信息。
对所有学生,按照班级计算平均成绩。
分别按照英语、数学、程序设计和体育成绩排序生成结果文件。
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<fstream>
using namespace std;
class Student{
	string Name;
	int No;
	char Sex;
	int English;
	int Math;
	int Program;
	int PE;
public:
	Student(const string na = " ", int no = 0, char sex = 'm', int en = 0, int math = 0, int pro = 0, int pe = 0) :
		Name(na), No(no), Sex(sex), English(en), Math(math), Program(pro), PE(pe){
	}
	void setStudent(const string na, int no, char sex, int en, int math, int pro, int pe)
	{
		this->Name = na;
		this->No = no;
		this->Sex = sex;
		this->English = en;
		this->Math = math;
		this->Program = pro;
		this->PE = pe;
	}
	string getName()
	{
		return Name;
	}
	int getNo()
	{
		return No;
	}
	char getSex()
	{
		return Sex;
	}
	int getEnglish()
	{
		return English;
	}
	int getMath()
	{
		return Math;
	}
	int getProgram()
	{
		return Program;
	}
	int getPE()
	{
		return PE;
	}
	int getSum()
	{
		return English + Math + Program + PE;
	}
	int getClass()
	{
		return No / 100 % 10;
	}
	void show()
	{
		cout << Name << " " << No << " " << Sex << " " << English << " " << Math << " " << Program << " " << PE << " " << endl;
	}

};
bool cmpEnglish(Student a, Student b)
{
	return a.getEnglish()>b.getEnglish();
}
bool cmpMath(Student a, Student b)
{
	return a.getMath()>b.getMath();
}
bool cmpProgram(Student a, Student b)
{
	return a.getProgram()>b.getProgram();
}
bool cmpPE(Student a, Student b)
{
	return a.getPE()>b.getPE();
}
struct compare_name : binary_function<Student, string, bool>
{
	bool operator()(Student &value, string str) const
	{
		if (value.getName() == str)
			return true;
		else
			return false;
	}
};
struct compare_no : binary_function<Student, int, bool>
{
	bool operator()(Student &value, int a) const
	{
		if (value.getNo() == a)
			return true;
		else
			return false;
	}
};
void buildtable(vector<Student> &stu)
{
	ofstream outfile("student.txt", ios::out);
	if (!outfile){
		cout << "open error!" << endl;
		exit(1);
	}
	printf("输入学生个数\n");
	int n;
	cin >> n;
	string na;
	int no;
	char sex;
	int en;
	int math;
	int pro;
	int pe;
	printf("输入学生信息\n");
	for (int i = 0; i<n; i++)
	{
		cin >> na >> no >> sex >> en >> math >> pro >> pe;
		outfile << na << " " << no << " " << sex << " " << en << " " << math << " " << pro << " " << pe << endl;
		Student s(na, no, sex, en, math, pro, pe);
		stu.push_back(s);
	}
	outfile.close();
	printf("创建完毕!\n");
}
void findName(vector<Student> &stu)
{
	printf("输入学生姓名\n");
	string goal_name;
	cin >> goal_name;
	vector<Student>::iterator result_name = find_if(stu.begin(), stu.end(), bind2nd(compare_name(), goal_name)); //查找3
	if (result_name == stu.end())
		cout << "Can't find" << endl;
	else
	{
		result_name->show();
	}
}
void findNo(vector<Student> &stu)
{
	printf("输入学生学号\n");
	int goal_no;
	cin >> goal_no;
	vector<Student>::iterator result_no = find_if(stu.begin(), stu.end(), bind2nd(compare_no(), goal_no)); //查找3
	if (result_no == stu.end())
		cout << "Can't find" << endl;
	else
	{
		result_no->show();
	}
	printf("查询完毕!\n");
}
void Average_score(vector<Student> &stu)
{
	int sum[10] = { 0 };
	int c[10] = { 0 };
	vector<Student>::iterator iter;
	for (iter = stu.begin(); iter != stu.end(); iter++)
	{
		sum[iter->getClass()] += iter->getSum();
		c[iter->getClass()]++;
	}
	for (int i = 0; i<10; i++)
	{
		if (sum[i] != 0)
			sum[i] /= c[i];
	}
	for (int i = 0; i<10; i++)
	{
		if (sum[i] != 0)
			printf("Class %d:%d\n", i, sum[i]);
	}
	printf("统计完毕!\n");
}
void sort_Score(vector<Student> &stu)
{
	ofstream outfile_en("English.txt", ios::out);
	if (!outfile_en){
		cout << "open error!" << endl;
		exit(1);
	}
	sort(stu.begin(), stu.end(), cmpEnglish);
	vector<Student>::iterator iter_en;
	for (iter_en = stu.begin(); iter_en != stu.end(); iter_en++)
	{
		outfile_en << iter_en->getName() << " " << iter_en->getNo() << " " << iter_en->getSex() << " " << iter_en->getEnglish() << " " << iter_en->getMath() << " " << iter_en->getProgram() << " " << iter_en->getPE() << endl;
	}
	outfile_en.close();
	ofstream outfile_ma("Math.txt", ios::out);
	if (!outfile_ma){
		cout << "open error!" << endl;
		exit(1);
	}
	sort(stu.begin(), stu.end(), cmpMath);
	vector<Student>::iterator iter_ma;
	for (iter_ma = stu.begin(); iter_ma != stu.end(); iter_ma++)
	{
		outfile_ma << iter_ma->getName() << " " << iter_ma->getNo() << " " << iter_ma->getSex() << " " << iter_ma->getEnglish() << " " << iter_ma->getMath() << " " << iter_ma->getProgram() << " " << iter_ma->getPE() << endl;
	}
	outfile_ma.close();
	ofstream outfile_pro("Program.txt", ios::out);
	if (!outfile_pro){
		cout << "open error!" << endl;
		exit(1);
	}
	sort(stu.begin(), stu.end(), cmpProgram);
	vector<Student>::iterator iter_pro;
	for (iter_pro = stu.begin(); iter_pro != stu.end(); iter_pro++)
	{
		outfile_pro << iter_pro->getName() << " " << iter_pro->getNo() << " " << iter_pro->getSex() << " " << iter_pro->getEnglish() << " " << iter_pro->getMath() << " " << iter_pro->getProgram() << " " << iter_pro->getPE() << endl;
	}
	outfile_pro.close();
	ofstream outfile_pe("PE.txt", ios::out);
	if (!outfile_pe){
		cerr << "open error!" << endl;
		exit(1);
	}
	sort(stu.begin(), stu.end(), cmpPE);
	vector<Student>::iterator iter_pe;
	for (iter_pe = stu.begin(); iter_pe != stu.end(); iter_pe++)
	{
		outfile_pe << iter_pe->getName() << " " << iter_pe->getNo() << " " << iter_pe->getSex() << " " << iter_pe->getEnglish() << " " << iter_pe->getMath() << " " << iter_pe->getProgram() << " " << iter_pe->getPE() << endl;
	}
	outfile_pe.close();
	printf("文件生成完毕!\n");
}
int main()
{
	printf("1.输入学生信息\n");
	printf("2.按学号查询\n");
	printf("3.按姓名查询\n");
	printf("4.按照班级计算平均成绩\n");
	printf("5.分别按照英语、数学、程序设计和体育成绩排序生成结果文件\n");
	printf("0.退出\n");
	vector<Student>stu;
	while (1)
	{
		printf("请选择:\n");
		int c;
		scanf("%d", &c);
		switch (c)
		{
		case 1:
			buildtable(stu);
			continue;
		case 2:
			findNo(stu);
			continue;
		case 3:
			findName(stu);
			continue;
		case 4:
			Average_score(stu);
			continue;
		case 5:
			sort_Score(stu);
			continue;
		case 0:
			exit(1);
		}
	}
	return 0;
}
第3题:设计一个模拟电信计费程序
假设电信计费标准:固定电话长途话费0.02元 / 秒,固定电话本地话费0.06元 / 分,无线电话长途话费1.00元 / 分,无线电话本地话费0.60元 / 分,无线电话接听话费0.50元 / 分。
源数据文件中存放:电话号码,电信服务类别,通话时间(秒)。
生成固定长途电话文件:长途电话号码和通话时间。
生成固定本地电话文件:本地电话号码和通话时间。
生成无线长途电话文件:长途电话号码和通话时间。
生成无线本地电话文件:本地电话号码和通话时间。
生成无线接听电话文件:接听电话号码和通话时间。
生成统计电信费用文件:电话号码、累计电信费用。
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<sstream>
#include<cstdio>
using namespace std;
class Price{
	string Tel;
	float Expense;
public:
	Price(string tel = "00000000000", float expense = 0) :
		Tel(tel), Expense(expense){}
	string getTel()
	{
		return Tel;
	}
	float getExpense()
	{
		return Expense;
	}
	void addExpense(string type, string time)
	{
		char str[20];
		int h, m, s;
		time.copy(str, 8, 0);
		sscanf(str, "%d:%d:%d", &h, &m, &s);
		if (type == "固定长途")
		{
			Expense += (h * 3600 + m * 60 + s)*0.02;
		}
		if (type == "固定本地")
		{
			Expense += (h * 60 + m + 1)*0.06;
		}
		if (type == "无线长途")
		{
			Expense += h * 60 + m + 1;
		}
		if (type == "无线本地")
		{
			Expense += (h * 60 + m + 1)*0.6;
		}
		if (type == "无线接听")
		{
			Expense += (h * 60 + m + 1)*0.5;
		}
	}
};
struct compare : binary_function<Price, string, bool>
{
	bool operator()(Price &value, string str) const
	{
		if (value.getTel() == str)
			return true;
		else
			return false;
	}
};
void statistics()
{
	vector<Price>phone_bill;
	ifstream f("电话单.txt");
	if (!f)
	{
		cout << "open error!" << endl;
	}
	string str;
	while (getline(f, str))
	{
		string tel, type, time;
		stringstream ss(str);
		ss >> tel;
		ss >> type;
		ss >> time;
		if (type == "固定长途")
		{
			ofstream outfile("固定长途.txt", ios::app);
			if (!outfile)
			{
				cout << "open error" << endl;
			}
			outfile << tel << " " << time << endl;
			outfile.close();
		}
		if (type == "固定本地")
		{
			ofstream outfile("固定本地.txt", ios::app);
			if (!outfile)
			{
				cout << "open error" << endl;
			}
			outfile << tel << " " << time << endl;
			outfile.close();
		}
		if (type == "无线长途")
		{
			ofstream outfile("无线长途.txt", ios::app);
			if (!outfile)
			{
				cout << "open error" << endl;
			}
			outfile << tel << " " << time << endl;
			outfile.close();
		}
		if (type == "无线本地")
		{
			ofstream outfile("无线本地.txt", ios::app);
			if (!outfile)
			{
				cout << "open error" << endl;
			}
			outfile << tel << " " << time << endl;
			outfile.close();
		}
		if (type == "无线接听")
		{
			ofstream outfile("无线接听.txt", ios::app);
			if (!outfile)
			{
				cout << "open error" << endl;
			}
			outfile << tel << " " << time << endl;
			outfile.close();
		}
		vector<Price>::iterator result;
		result = find_if(phone_bill.begin(), phone_bill.end(), bind2nd(compare(), tel));
		if (result == phone_bill.end())
		{
			Price temp(tel, 0);
			temp.addExpense(type, time);
			phone_bill.push_back(temp);
		}
		else
		{
			result->addExpense(type, time);
		}
	}
	ofstream outfile("统计电信费用.txt", ios::out);
	vector<Price>::iterator iter;
	for (iter = phone_bill.begin(); iter != phone_bill.end(); iter++)
	{
		outfile << iter->getTel() << " " << iter->getExpense() << endl;
	}
}
int main()
{
	statistics();
	cout << "统计完毕!" << endl;
	return 0;
}
第5题:设计一个文本行编辑程序
对文本文件按行进行编辑:先从输入文件中读取数据,然后根据行编辑命令处理,将结果写到输出文件中。
行编辑命令包括:
序号	行编辑命令格式	功能
1	*L  m,n	显示从第m至n行的文本
2	*I  m
……
^Z	插入文本(……)在第m行后
3	*D  m,n	删除从第m至n行的文本
4	*R  m,n
……
^Z	用文本(……)替换第m至n行的文本
5	*X	保存并退出编辑程序
6	*Q	放弃并退出程序
#include<iostream>
#include<cstdio>
#include<string>
#include<sstream>
#include<fstream> 
#include<cstdlib>
#include<vector>
using namespace std;
void initial(vector<string> &text)
{
	ifstream infile("输入文件.txt");
	if (!infile)
	{
		cout << "open error" << endl;
		exit(0);
	}
	string temp;
	while (getline(infile, temp))
	{
		text.push_back(temp);
	}
	infile.close();
}
void showtext(vector<string> &text)
{
	int m, n;
	scanf("%d,%d", &m, &n);
	printf("第%d行到第%d行的文本为:\n", m, n);
	for (int i = m - 1; i<n; i++)
		cout << text[i] << endl;

}
void insert(vector<string> &text)
{
	int m;
	scanf("%d\n", &m);
	int t = m;
	string temp;
	while (getline(cin, temp))
	{
		if (temp == "^Z")
			break;
		text.insert(text.begin() + t, temp);
		t++;
	}
	for (int i = 0; i<text.size(); i++)
		cout << text[i] << endl;
}
void Delete(vector<string> &text)
{
	int m, n;
	scanf("%d,%d", &m, &n);
	text.erase(text.begin() + m - 1, text.begin() + n);
	for (int i = 0; i<text.size(); i++)
		cout << text[i] << endl;
}
void Replace(vector<string> &text)
{
	int m, n;
	scanf("%d,%d\n", &m, &n);
	int t = m - 1;
	text.erase(text.begin() + t, text.begin() + n);
	string temp;
	while (getline(cin, temp))
	{
		if (temp == "^Z")
			break;
		text.insert(text.begin() + t, temp);
		t++;
	}
	for (int i = 0; i<text.size(); i++)
		cout << text[i] << endl;
}
void SaveQuit(vector<string> &text)
{
	ofstream outfile("输出文件.txt", ios::out);
	if (!outfile)
	{
		cout << "open error" << endl;
		exit(0);
	}
	for (int i = 0; i<text.size(); i++)
		outfile << text[i] << endl;
	outfile.close();
}
void Quit(vector<string> &text)
{
	initial(text);
}
int main()
{
	vector<string>text;
	initial(text);
	for (int i = 0; i<text.size(); i++)
		cout << text[i] << endl;
	string command;
	while (cin >> command)
	{
		if (command == "*L")
			showtext(text);
		if (command == "*l")
			insert(text);
		if (command == "*D")
			Delete(text);
		if (command == "*R")
			Replace(text);
		if (command == "*X")
			SaveQuit(text);
		if (command == "*Q")
			Quit(text);
	}
	return 0;
}
第7题:设计一个超长整数类
定义并实现超长整数类doublelong,要求如下:
64位数据长度,有符号
支持+、-、*、/运算
支持+=、-=、/=运算
支持cin >> 和cout << 操作
#include<iostream>
#include<cstring>
#define MAX 65*65 
using namespace std;
class doublelong{
	char number[MAX];
public:
	doublelong(const char *str = " ")
	{
		strcpy(number, str);
	}
	void show()
	{
		cout << number << endl;
	}
	void operator>>(doublelong a)
	{
		cin >> number;
	}
	friend istream & operator>>(istream &is, doublelong &ob);
	friend ostream & operator<<(ostream &os, const doublelong &ob);
	friend doublelong operator+(doublelong &ob1, doublelong &ob2);
	friend doublelong operator-(doublelong &ob1, doublelong &ob2);
	friend doublelong operator*(doublelong &ob1, doublelong &ob2);
	friend doublelong operator/(doublelong &ob1, doublelong &ob2);
	void operator+=(doublelong &ob);
	void operator-=(doublelong &ob);
	void operator/=(doublelong &ob);
};
void sub(char str1[], char str2[], char result[])
{
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	if (len1<len2)
	{
		char temp[MAX];
		strcpy(temp, str1);
		strcpy(str1, str2);
		strcpy(str2, temp);
		int t = len1;
		len1 = len2;
		len2 = t;
	}
	int num1[MAX] = { 0 }, num2[MAX] = { 0 };
	int i;
	for (i = 0; i<len1; i++)
	{
		num1[i] = str1[i] - '0';
	}
	for (i = 0; i<len2; i++)
	{
		num2[i] = str2[i] - '0';
	}
	for (i = 0; i<len1 / 2; i++)
	{
		int temp = num1[i];
		num1[i] = num1[len1 - 1 - i];
		num1[len1 - 1 - i] = temp;
	}
	for (i = 0; i<len2 / 2; i++)
	{
		int temp = num2[i];
		num2[i] = num2[len2 - 1 - i];
		num2[len2 - 1 - i] = temp;
	}
	int len = len1>len2 ? len1 : len2;
	int ans[10] = { 0 };
	for (i = 0; i<len; i++)
	{
		if (num1[i] - num2[i]<0)
		{
			ans[i] = num1[i] + 10 - num2[i];
			num1[i + 1] -= 1;
		}
		else
		{
			ans[i] = num1[i] - num2[i];
		}
	}
	for (i = len - 1; i >= 0; i--)
	{
		if (ans[i] != 0)
			break;
	}
	if (i == -1)
		result[0] = '0';
	else
	{
		int j;
		if (result[0] != '-')
			j = 0;
		else
			j = 1;
		while (i >= 0)
		{
			result[j] = ans[i] + '0';
			j++;
			i--;
		}
	}
}
void add(char str1[], char str2[], char result[])
{
	int num1[MAX] = { 0 }, num2[MAX] = { 0 };
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	int i;
	for (i = 0; i<len1; i++)
	{
		num1[i] = str1[i] - '0';
	}
	for (i = 0; i<len2; i++)
	{
		num2[i] = str2[i] - '0';
	}
	for (i = 0; i<len1 / 2; i++)
	{
		int temp = num1[i];
		num1[i] = num1[len1 - 1 - i];
		num1[len1 - 1 - i] = temp;
	}
	for (i = 0; i<len2 / 2; i++)
	{
		int temp = num2[i];
		num2[i] = num2[len2 - 1 - i];
		num2[len2 - 1 - i] = temp;
	}
	int len = len1>len2 ? len1 : len2;
	int ans[MAX] = { 0 };
	for (i = 0; i<len; i++)
	{
		ans[i] += num1[i] + num2[i];
		if (ans[i] >= 10)
		{
			ans[i + 1] += ans[i] / 10;
			ans[i] = ans[i] % 10;
		}
	}
	for (i = len; i >= 0; i--)
	{
		if (ans[i] != 0)
			break;
	}
	int j;
	if (result[0] != '-')
		j = 0;
	else
		j = 1;
	while (i >= 0)
	{
		result[j] = ans[i] + '0';
		j++;
		i--;
	}
}
void multiply(char str1[], char str2[], char result[])
{
	int num1[MAX] = { 0 };
	int num2[MAX] = { 0 };
	int ans[MAX] = { 0 };
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	int len = len1>len2 ? len1 : len2;
	int i, j;
	for (j = 0, i = len1 - 1; i >= 0; i--)
		num1[j++] = str1[i] - '0';
	for (j = 0, i = len2 - 1; i >= 0; i--)
		num2[j++] = str2[i] - '0';
	for (i = 0; i<len1; i++)
	{
		for (j = 0; j<len2; j++)
			ans[i + j] = ans[i + j] + num1[i] * num2[j];
	}
	for (i = 0; i<len * 2; i++)
	{
		if (ans[i] >= 10)
		{
			ans[i + 1] = ans[i + 1] + ans[i] / 10;
			ans[i] = ans[i] % 10;
		}
	}
	for (i = len * 2; i>0; i--)
	{
		if (ans[i] != 0)
			break;
	}
	if (result[0] != '-')
		j = 0;
	else
		j = 1;
	while (i >= 0)
	{
		result[j] = ans[i] + '0';
		j++;
		i--;
	}
}
int SubStract(int *p1, int *p2, int len1, int len2)
{
	int i;
	if (len1<len2)
		return -1;
	if (len1 == len2)
	{
		for (i = len1 - 1; i >= 0; i--)
		{
			if (p1[i]>p2[i])
				break;
			else if (p1[i]<p2[i])
				return -1;
		}
	}
	for (i = 0; i <= len1 - 1; i++)
	{
		p1[i] -= p2[i];
		if (p1[i]<0)
		{
			p1[i] += 10;
			p1[i + 1]--;
		}
	}
	for (i = len1 - 1; i >= 0; i--)
		if (p1[i])
			return i + 1;
	return 0;
}
void division(char str1[], char str2[], char result[])
{
	int i, j;
	int nTimes;
	int nTemp;
	int num1[MAX] = { 0 };
	int num2[MAX] = { 0 };
	int ans[MAX] = { 0 };
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	for (j = 0, i = len1 - 1; i >= 0; j++, i--)
		num1[j] = str1[i] - '0';
	for (j = 0, i = len2 - 1; i >= 0; j++, i--)
		num2[j] = str2[i] - '0';
	if (len1<len2)
	{
		result[0] = '0';
		return;
	}
	nTimes = len1 - len2;
	for (i = len1 - 1; i >= 0; i--)
	{
		if (i >= nTimes)
			num2[i] = num2[i - nTimes];
		else
			num2[i] = 0;
	}
	len2 = len1;
	for (j = 0; j <= nTimes; j++)
	{
		while ((nTemp = SubStract(num1, num2 + j, len1, len2 - j)) >= 0)
		{
			len1 = nTemp;
			ans[nTimes - j]++;
		}
	}
	for (i = 9; i >= 0; i--)
	{
		if (ans[i] != 0)
			break;
	}
	if (result[0] != '-')
		j = 0;
	else
		j = 1;
	while (i >= 0)
	{
		result[j] = ans[i] + '0';
		j++;
		i--;
	}
}
istream & operator>>(istream &is, doublelong &ob)
{
	is >> ob.number;
	return is;
}
ostream & operator<<(ostream &os, const doublelong &ob)
{
	os << ob.number;
	return os;
}
doublelong operator+(doublelong &ob1, doublelong &ob2)
{
	char result[MAX];
	char str1[MAX], str2[MAX];
	strcpy(str1, ob1.number);
	strcpy(str2, ob2.number);
	if (str1[0] == '-'&&str2[0] == '-')
	{
		result[0] = '-';
		add(str1 + 1, str2 + 1, result);
	}
	else if (str1[0] == '-'&&str2[0] != '-')
	{
		int len1 = strlen(str1 + 1);
		int len2 = strlen(str2);
		if (len1 == len2)
		{
			if (strcmp(str1 + 1, str2)>0)
			{
				result[0] = '-';
				sub(str1 + 1, str2, result);
			}
			else
			{
				sub(str2, str1 + 1, result);
			}
		}
		else if (len1>len2)
		{
			result[0] = '-';
			sub(str1 + 1, str2, result);
		}
		else
		{
			sub(str1 + 1, str2, result);
		}
	}
	else if (str1[0] != '-'&&str2[0] == '-')
	{
		int len1 = strlen(str1);
		int len2 = strlen(str2 + 1);
		if (len1 == len2)
		{
			if (strcmp(str1, str2 + 1)>0)
			{
				sub(str1, str2 + 1, result);
			}
			else
			{
				result[0] = '-';
				sub(str2 + 1, str1, result);
			}
		}
		else if (len1<len2)
		{
			result[0]='-';
			sub(str1, str2 + 1, result);
		}
		else
		{
			sub(str1, str2 + 1, result);
		}
	}
	else if (str1[0] != '-'&&str2[0] != '-')
	{
		add(str1, str2, result);
	}
	return doublelong(result);
}
doublelong operator-(doublelong &ob1, doublelong &ob2)
{
	char result[MAX];
	char str1[MAX], str2[MAX];
	strcpy(str1, ob1.number);
	strcpy(str2, ob2.number);
	if (str1[0] != '-'&&str2[0] == '-')
	{
		add(str1, str2 + 1, result);
	}
	else if (str1[0] == '-'&&str2[0] != '-')
	{
		result[0] = '-';
		add(str1 + 1, str2, result);
	}
	else if (str1[0] != '-'&&str2[0] != '-')
	{
		int len1 = strlen(str1);
		int len2 = strlen(str2);
		if (len1 == len2)
		{
			if (strcmp(str1, str2)>0)
			{
				sub(str1, str2, result);
			}
			else
			{
				result[0] = '-';
				sub(str2, str1, result);
			}
		}
		else if (len1>len2)
		{
			sub(str1, str2, result);
		}
		else
		{
			result[0] = '-';
			sub(str1, str2, result);
		}
	}
	else
	{
		int len1 = strlen(str1 + 1);
		int len2 = strlen(str2 + 1);
		if (len1 == len2)
		{
			if (strcmp(str2 + 1, str1 + 1) >= 0)
			{
				sub(str2 + 1, str1 + 1, result);
			}
			else
			{
				result[0] = '-';
				sub(str1 + 1, str2 + 1, result);
			}
		}
		else if (len1>len2)
		{
			result[0] = '-';
			sub(str1 + 1, str2 + 1, result);
		}
		else
		{
			sub(str1 + 1, str2 + 1, result);
		}
	}
	return doublelong(result);
}
doublelong operator*(doublelong &ob1, doublelong &ob2)
{
	char result[MAX];
	char str1[MAX], str2[MAX];
	strcpy(str1, ob1.number);
	strcpy(str2, ob2.number);
	if (strcmp(str1, "0") == 0 || strcmp(str2, "0") == 0)
	{
		return doublelong("0");
	}
	if (str1[0] != '-'&&str2[0] == '-')
	{
		result[0] = '-';
		multiply(str1, str2 + 1, result);
	}
	else if (str1[0] == '-'&&str2[0] != '-')
	{
		result[0] = '-';
		multiply(str1 + 1, str2, result);
	}
	else if (str1[0] == '-'&&str2[0] == '-')
	{
		multiply(str1 + 1, str2 + 1, result);
	}
	else
	{
		multiply(str1, str2, result);
	}
	return doublelong(result);
}
doublelong operator/(doublelong &ob1, doublelong &ob2)
{
	char result[MAX];
	char str1[MAX], str2[MAX];
	strcpy(str1, ob1.number);
	strcpy(str2, ob2.number);
	if (str1[0] != '-'&&str2[0] == '-')
	{
		result[0] = '-';
		division(str1, str2 + 1, result);
	}
	else if (str1[0] == '-'&&str2[0] != '-')
	{
		result[0] = '-';
		division(str1 + 1, str2, result);
	}
	else if (str1[0] == '-'&&str2[0] == '-')
	{
		division(str1 + 1, str2 + 1, result);
	}
	else
	{
		division(str1, str2, result);
	}
	return doublelong(result);
}
void doublelong::operator+=(doublelong &ob)
{
	char result[MAX];
	char str1[MAX], str2[MAX];
	strcpy(str1, this->number);
	strcpy(str2, ob.number);
	if (str1[0] == '-'&&str2[0] == '-')
	{
		result[0] = '-';
		add(str1 + 1, str2 + 1, result);
	}
	else if (str1[0] == '-'&&str2[0] != '-')
	{
		int len1 = strlen(str1 + 1);
		int len2 = strlen(str2);
		if (len1 == len2)
		{
			if (strcmp(str1 + 1, str2)>0)
			{
				result[0] = '-';
				sub(str1 + 1, str2, result);
			}
			else
			{
				sub(str2, str1 + 1, result);
			}
		}
		else if (len1>len2)
		{
			result[0] = '-';
			sub(str1 + 1, str2, result);
		}
		else
		{
			sub(str1 + 1, str2, result);
		}
	}
	else if (str1[0] != '-'&&str2[0] == '-')
	{
		int len1 = strlen(str1);
		int len2 = strlen(str2 + 1);
		if (len1 == len2)
		{
			if (strcmp(str1, str2 + 1)>0)
			{
				sub(str1, str2 + 1, result);
			}
			else
			{
				result[0] = '-';
				sub(str2 + 1, str1, result);
			}
		}
		else if (len1<len2)
		{
			result[0]='-';
			sub(str1, str2 + 1, result);
		}
		else
		{
			sub(str1, str2 + 1, result);
		}
	}
	else if (str1[0] != '-'&&str2[0] != '-')
	{
		add(str1, str2, result);
	}
	strcpy(this->number, result);
}
void doublelong::operator-=(doublelong &ob)
{
	char result[MAX];
	char str1[MAX], str2[MAX];
	strcpy(str1, this->number);
	strcpy(str2, ob.number);
	if (str1[0] != '-'&&str2[0] == '-')
	{
		add(str1, str2 + 1, result);
	}
	else if (str1[0] == '-'&&str2[0] != '-')
	{
		result[0] = '-';
		add(str1 + 1, str2, result);
	}
	else if (str1[0] != '-'&&str2[0] != '-')
	{
		int len1 = strlen(str1);
		int len2 = strlen(str2);
		if (len1 == len2)
		{
			if (strcmp(str1, str2)>0)
			{
				sub(str1, str2, result);
			}
			else
			{
				result[0] = '-';
				sub(str2, str1, result);
			}
		}
		else if (len1>len2)
		{
			sub(str1, str2, result);
		}
		else
		{
			result[0] = '-';
			sub(str1, str2, result);
		}
	}
	else
	{
		int len1 = strlen(str1 + 1);
		int len2 = strlen(str2 + 1);
		if (len1 == len2)
		{
			if (strcmp(str2 + 1, str1 + 1) >= 0)
			{
				sub(str2 + 1, str1 + 1, result);
			}
			else
			{
				result[0] = '-';
				sub(str1 + 1, str2 + 1, result);
			}
		}
		else if (len1>len2)
		{
			result[0] = '-';
			sub(str1 + 1, str2 + 1, result);
		}
		else
		{
			sub(str1 + 1, str2 + 1, result);
		}
	}
	strcpy(this->number, result);
}
void doublelong::operator/=(doublelong &ob)
{
	char result[MAX];
	char str1[MAX], str2[MAX];
	strcpy(str1, this->number);
	strcpy(str2, ob.number);
	if (str1[0] != '-'&&str2[0] == '-')
	{
		result[0] = '-';
		division(str1, str2 + 1, result);
	}
	else if (str1[0] == '-'&&str2[0] != '-')
	{
		result[0] = '-';
		division(str1 + 1, str2, result);
	}
	else if (str1[0] == '-'&&str2[0] == '-')
	{
		division(str1 + 1, str2 + 1, result);
	}
	else
	{
		division(str1, str2, result);
	}
	strcpy(this->number, result);
}
int main()
{
	doublelong a = "100", b = "-7", c;
	c = a*b;
	cout << c << endl;
	c = "0";
	cout << c << endl;
	return 0;
}
第9题:设计一个图形类体系(vc++、vs运行)
设计并实现图形类,包括点、直线、矩形、圆、扇形等;
在此基础上构造二维统计图类,包括折线图,条形图、扇形图,利用它们为数组数据提供直观的统计结果。
#include <windows.h>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define j2h(x) (3.1415926*(x)/180.0)
class Point{
	int x, y;
public:
	Point(int x = 0, int y = 0) :x(x), y(y){
	}
	int getx()
	{
		return x;
	}
	int gety()
	{
		return y;
	}
	void setPoint(int xx = 0, int yy = 0)
	{
		x = xx;
		y = yy;
	}
	void show()
	{
		printf("Point:(%d,%d)\n", x, y);
	}
};
class Line{
	Point a;
	Point b;
public:
	Line(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0) :a(x1, y1), b(x2, y2){
	}
	void show()
	{
		printf("Line:(%d,%d)--(%d,%d)\n", a.getx(), a.gety(), b.getx(), b.gety());
	}
};
class rectangle{
	Point a, b, c, d;
public:
	rectangle(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0, int x3 = 0, int y3 = 0, int x4 = 0, int y4 = 0) :
		a(x1, y1), b(x2, y2), c(x3, y3), d(x4, y4){
	}
	Point getlefttopPoint()
	{
		return a;
	}
	Point getrightbottomPoint()
	{
		return d;
	}
	void setRectangle(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0, int x3 = 0, int y3 = 0, int x4 = 0, int y4 = 0)
	{
		a.setPoint(x1, y1);
		b.setPoint(x2, y2);
		c.setPoint(x3, y3);
		d.setPoint(x4, y4);
	}
	void show()
	{
		printf("Rectangle:(%d,%d),(%d,%d),(%d,%d),(%d,%d)\n", a.getx(), a.gety(), b.getx(), b.gety(), c.getx(), c.gety(), d.getx(), d.gety());
	}
};
class Circle{
protected:
	Point a;
	int r;
public:
	Circle(int x1 = 0, int y1 = 0, int rr = 0) :a(x1, y1), r(rr){
	}
	void show()
	{
		printf("Circle:\n");
		printf("圆心:(%d,%d),半径为%d\n", a.getx(), a.gety(), r);
	}
};
class Sector :public Circle{
	int ca;
public:
	Sector(int x = 0, int y = 0, int r = 0, int ca = 0) :Circle(x, y, r), ca(ca){
	}
	void show()
	{
		printf("圆心:(%d,%d)\n半径:%d\n圆心角:%d\n", a.getx(), a.gety(), r, ca);
	}
	float getcx()
	{
		if (ca <= 90 && ca >= 0)
			return 725 + r*cos(j2h(ca));
		if (ca <= 360 && ca>270)
			return 725 + r*cos(j2h(360 - ca));
		if (ca>90 && ca <= 180)
			return 725 - r*cos(j2h(180 - ca));
		if (ca > 180 && ca <= 270)
			return 725 - r*cos(j2h(ca - 180));
	}
	float getcy()
	{
		if (ca <= 180 && ca >= 0)
			return 75 - r*sin(j2h(ca));
		if (ca>180 && ca <= 360)
			return 75 + r*sin(j2h(ca - 180));
	}
};
class Linechart{
	float a[5];
	HDC hdc;
	PAINTSTRUCT	ps;
public:
	Linechart(float a[], HDC	hdc, PAINTSTRUCT	ps) :hdc(hdc), ps(ps){
		int n = sizeof(a) / sizeof(float);
		for (int i = 0; i < 5; i++)
		{
			this->a[i] = a[i];
		}
	}
	void draw(){
		Point s[5];
		int x = 50;
		for (int i = 0; i<5; i++)
		{
			s[i].setPoint(x, (int)(150 - a[i] * 50));
			x += 50;
		}
		MoveToEx(hdc, 50, 150, NULL);
		LineTo(hdc, 250, 150);
		MoveToEx(hdc, 50, 150, NULL);
		LineTo(hdc, 50, 0);
		MoveToEx(hdc, 50, 0, NULL);
		LineTo(hdc, 45, 5);
		MoveToEx(hdc, 50, 0, NULL);
		LineTo(hdc, 55, 5);
		MoveToEx(hdc, 250, 150, NULL);
		LineTo(hdc, 245, 155);
		MoveToEx(hdc, 250, 150, NULL);
		LineTo(hdc, 245, 145);
		for (int i = 1; i<5; i++)
		{
			MoveToEx(hdc, s[i - 1].getx(), s[i - 1].gety(), NULL);
			LineTo(hdc, s[i].getx(), s[i].gety());
		}
	}
};
class Barchart{
	float a[3];
	HDC hdc;
	PAINTSTRUCT	ps;
public:
	Barchart(float a[], HDC	hdc, PAINTSTRUCT	ps) :hdc(hdc), ps(ps){
		for (int i = 0; i < 3; i++)
		{
			this->a[i] = a[i];
		}
	}
	void draw()
	{
		rectangle s[3];
		int x = 350;
		for (int i = 0; i<3; i++)
		{
			s[i].setRectangle(x, (int)(150 - a[i] * 50), x + 50, (int)(150 - a[i] * 50), x, 150, x + 50, 150);
			x += 50;
		}
		MoveToEx(hdc, 350, 150, NULL);
		LineTo(hdc, 550, 150);
		MoveToEx(hdc, 350, 150, NULL);
		LineTo(hdc, 350, 0);
		MoveToEx(hdc, 350, 0, NULL);
		LineTo(hdc, 345, 5);
		MoveToEx(hdc, 350, 0, NULL);
		LineTo(hdc, 355, 5);
		MoveToEx(hdc, 550, 150, NULL);
		LineTo(hdc, 545, 155);
		MoveToEx(hdc, 550, 150, NULL);
		LineTo(hdc, 545, 145);
		for (int i = 0; i < 3; i++)
		{
			Rectangle(hdc, s[i].getlefttopPoint().getx(), s[i].getlefttopPoint().gety(), s[i].getrightbottomPoint().getx(), s[i].getrightbottomPoint().gety());
		}
	}
};
class Fanchart{
	int ca;
	HDC hdc;
	PAINTSTRUCT	ps;
public:
	Fanchart(int ca, HDC hdc, PAINTSTRUCT	ps) :ca(ca), hdc(hdc), ps(ps){
	}
	void draw()
	{
		Sector s(1, 1, 2, ca);
		Pie(hdc, 650, 0, 800, 150, s.getcx(), s.getcy(), 800, 75);
	}
};
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure.

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	PSTR szCmdLine, int iCmdShow)
{
	static		TCHAR szAppName[] = TEXT("Mywindow");
	HWND		hwnd;
	MSG			msg;
	WNDCLASS	wndClass;		//The window Class

	wndClass.style = CS_HREDRAW | CS_VREDRAW;
	wndClass.lpfnWndProc = WndProc;// assign the window procedure to windows class.
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 0;
	wndClass.hInstance = hInstance;
	wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndClass.lpszMenuName = NULL;
	wndClass.lpszClassName = szAppName;

	//Register the Window Class to the Windows System. 
	if (!RegisterClass(&wndClass))
	{
		MessageBox(NULL, TEXT("This program require Windows NT!"),
			szAppName, MB_ICONERROR);
		return 0;
	}

	//This function will generate an WM_CREATE message.
	hwnd = CreateWindow(szAppName,		//Window class name
		TEXT("Mywindow"),		//Window caption
		WS_OVERLAPPEDWINDOW,			//Window Style
		CW_USEDEFAULT,					//initial x position
		CW_USEDEFAULT,					//initial y position
		CW_USEDEFAULT,					//initial x size
		CW_USEDEFAULT,					//initial y size
		NULL,							//parent window handle
		NULL,							//window menu handle
		hInstance,						//program instance handle
		NULL);							//creation parameters

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);	//This function will generate a WM_PAINT message.
	/* The message loop for this program.
	if received the WM_QUIT message, the function will return 0.*/
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

//define the Window Procedure WndProc
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC			hdc;
	PAINTSTRUCT	ps;
	float a[] = { 1, 2, 0.5, 1, 0.5 };
	float b[] = { 1, 2.5, 2 };
	int angle = 90;
	switch (message) //get the message
	{
	case WM_CREATE:
		return 0;
	case WM_PAINT:
	{
		hdc = BeginPaint(hwnd, &ps);
		Linechart lc(a, hdc, ps);
		lc.draw();
		Barchart bc(b, hdc, ps);
		bc.draw();
		Fanchart fc(360 - angle, hdc, ps);
		fc.draw();
		EndPaint(hwnd, &ps);
		return 0;
	}
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return  DefWindowProc(hwnd, message, wParam, lParam);
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值