期末课程设计
第1题:设计一个学生学籍管理系统
学生信息包括:姓名、学号、性别和英语、数学、程序设计、体育成绩。
从键盘输入数据,建立数据文件student.dat。
实现对学生或学号查询,显示信息。
对所有学生,按照班级计算平均成绩。
分别按照英语、数学、程序设计和体育成绩排序生成结果文件。
#include
#include
#include
#include
#include
#include
#include
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
{
bool operator()(Student &value, string str) const
{
if (value.getName() == str)
return true;
else
return false;
}
};
struct compare_no : binary_function
{
bool operator()(Student &value, int a) const
{
if (value.getNo() == a)
return true;
else
return false;
}
};
void buildtable(vector &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> 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 &stu)
{
printf("输入学生姓名\n");
string goal_name;
cin >> goal_name;
vector::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 &stu)
{
printf("输入学生学号\n");
int goal_no;
cin >> goal_no;
vector::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 &stu)
{
int sum[10] = { 0 };
int c[10] = { 0 };
vector::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 &stu)
{
ofstream outfile_en("English.txt", ios::out);
if (!outfile_en){
cout << "open error!" << endl;
exit(1);
}
sort(stu.begin(), stu.end(), cmpEnglish);
vector::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::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::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::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");
vectorstu;
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
#include
#include
#include
#include
#include
#include
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
{
bool operator()(Price &value, string str) const
{
if (value.getTel() == str)
return true;
else
return false;
}
};
void statistics()
{
vectorphone_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::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::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
#include
#include
#include
#include
#include
#include
using namespace std;
void initial(vector &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 &text)
{
int m, n;
scanf("%d,%d", &m, &n);
printf("第%d行到第%d行的文本为:\n", m, n);
for (int i = m - 1; i &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)
{
int m, n;
scanf("%d,%d", &m, &n);
text.erase(text.begin() + m - 1, text.begin() + n);
for (int i = 0; i &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)
{
ofstream outfile("输出文件.txt", ios::out);
if (!outfile)
{
cout << "open error" << endl;
exit(0);
}
for (int i = 0; i &text)
{
initial(text);
}
int main()
{
vectortext;
initial(text);
for (int i = 0; i> 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
#include
#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 (len1len2 ? len1 : len2;
int ans[10] = { 0 };
for (i = 0; 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; ilen2 ? len1 : len2;
int ans[MAX] = { 0 };
for (i = 0; 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= 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= 0; i--)
{
if (p1[i]>p2[i])
break;
else if (p1[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= 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 (len10)
{
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 (len1number, 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
#include
#include
#include
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);
}