单元综合测试一(1-5章)
已有复数Complex类 ,编写一个类Root实现求解一元二次方程组的程序。
#include
#include
#include
using namespace std;
class Complex {
public:
Complex() {}
void setC(double i, double j) { real = i; image = j; }
void show();
protected:
double real, image;
};
void Complex::show() {
cout << "(" << real << "," << image << "i)" << endl;
}
class Root {
public:
Root(double i, double j, double k) {
a = i; b = j; c = k;
}
void show();
void solve();
void solve2();
protected:
double a, b, c;
};
void Root::show() {
cout << "方程 " << a << setiosflags(ios::showpos) << "*x*x" << b << "*x" << c << "=0 " << resetiosflags(ios::showpos) << "的解是:" << endl;
}
/*
求根公式:x = (-b±√b²-4ac)/2a
对于实数系一元二次方程 ax² + bx + c = 0(a≠0),△=b²-4ac称作一元二次方程组根的判别式.根据判别式,根有三种可能的情况:
1.△=0 有两个相等的实根.这两个根为:
x1 = x2 = -b/2a
2.△>0 有两个不相同的实根。如果系数都为有理数,且△是一个完全平方根,则两根都是有理数,否则为无理数.
3.△<0 有两个不同的复数根,且为共轭复根。这时根为:
x1 = -b/2a + i((√b²-4ac)/2a)
x2 = -b/2a - i((√b²-4ac)/2a)
其中 i² = -1.
*/
void Root::solve() {
show();
double x = -b / 2 / a;
double dt = b * b - 4 * a * c;
Complex c1, c2;
if (dt == 0) {
c1.setC(x, 0);
c2.setC(x, 0);
cout << "\nx1=x2=";
c1.show();
}
else if (dt > 0) {
c1.setC(x + sqrt(dt) / (2 * a), 0);
c2.setC(x - sqrt(dt) / (2 * a), 0);
c1.show();
c2.show();
}
else {
c1.setC(x, sqrt(-dt) / (2 * a));
c2.setC(x, -sqrt(-dt) / (2 * a));
c1.show();
c2.show();
}
}
void Root::solve2() {
show();
double dt = b * b - 4 * a * c;
double x = -b / 2 / a;
if (dt == 0) {
cout << "x1=x2=" << x;
}
else if (dt > 0) {
cout << "x1=" << (-b + sqrt(dt)) / 2 / a << endl;
cout << "x2=" << (-b - sqrt(dt)) / 2 / a << endl;
}
else {
Complex c1, c2;
c1.setC(x, sqrt(-dt) / 2 / a);
c2.setC(x, -sqrt(-dt) / 2 / a);
cout << "x1=";
c1.show();
cout << "x2=";
c2.show();
}
}
int main()
{
double a, b, c;
cout << "\n输入一元二次方程组的系数a,b,c:";
cin >> a >> b >> c;
Root root(a, b, c);
root.solve();
return 0;
}
单元综合测试二(6-9章)
定义堆栈类模板Stack(先进后出),栈的大小由使用者决定。要求该类提供如下两种操作:1.push入栈。2.pop出栈,用数组来实现。
#include
using namespace std;
template <class T, int size>
class Stack {
T x[size];
int current;
public:
Stack() {
current = 0;
}
void push(T);
void pop(T);
};
template<class T, int size>
void Stack<T, size>::push(T t) {
if (current == size) {
cout << "The Stack is full!" << endl;
exit(0);
}
else {
x[current] = 1;
current++;
}
}
template<class T, int size>
void Stack<T, size>::pop(T t) {
if (current == 0) {
cout << "The Stack no object in the Stack!" << endl;
exit(0);
}
else {
T a = x[current - 1];
current--;
return a;
}
}
int main() {
return 0;
}
全真模拟演练一
复数类Complex,私有成员变量实部real,虚部image。添加构造函数,并使用友元函数add实现复数加法。
#include
using namespace std;
class Complex {
private:
double real, image;
public:
Complex() {}
Complex(double a, double b) {
real = a;
image = b;
}
void setRI(double a, double b) {
real = a;
image = b;
}
double getReal() {
return real;
}
double getImage() {
return image;
}
void print() {
if (image > 0) {
cout << "复数:" << real << "+" << image << "i" << endl;
}
if (image < 0) {
cout << "复数:" << real << "-" << image << "i" << endl;
}
}
friend Complex add(Complex, Complex);
};
Complex add(Complex c1, Complex c2) {
return Complex(c1.real + c2.real, c1.image + c2.image);
}
int main() {
Complex c1(19, 0.864), c2,c3;
c2.setRI(90, 125.012);
c3 = add(c1, c2);
cout << "复数1:"; c1.print();
cout << "复数2:"; c2.print();
cout << "复数3:"; c3.print();
return 0;
}
全真模拟演练二
编写一个函数to_lower,实现将字符串大写转小写
#include
#include
using namespace std;
int main() {
void to_lower(char a[]);
char str[10];
cin >> str;
to_lower(str);
cout << str << endl;
/*
void to_uper(char a[]);
char str2[10] = { 'a','b','c','d' };
to_uper(str2);
cout << str2 << endl;
*/
}
void to_lower(char a[]) {
for (int i = 0; i < 10; i++) {
if (a[i] >= 'A' && a[i] <= 'Z') {
a[i] += 32;
}
}
}
/*
void to_uper(char a[]) {
for (int i = 0; i < 10 && a[i] != '\0'; i++) {
if (a[i] >= 'a' && a[i] <= 'z') {
a[i] -= 32;
}
}
}
*/
全真模拟演练三
输入10个数,写一个求平均值的函数,并输出结果.
#include
#include
using namespace std;
void main() {
float average(float a[]);
float score[10];
for (int i = 0; i < 10; i++)
cin >> score[i];
cout << average(score) << endl;
}
float average(float a[]) {
float sum = 0;
for (int i = 0; i < 10; i++)
sum += a[i];
return (sum / 10);
}
全真模拟演练四
在字符串类str中实现一个判断函数,该函数的功能是统计某一字符串类对象(仅单词和空格组成)单词数量,同时保存所有单词在字符串中的起始地址(设改字符串不超过100个单词)
#include
#include
using namespace std;
class str {
public:
string s;
int n, a[100], j, l;
str(string& a) {
s = a;
n = 0;
j = 0;
l = 0;
}
int test(str ms);
int* geta() { return a; }
};
int test(str ms) {
while (ms.l < ms.s.length()) {
if (ms.s[ms.l] != '\0') {
if (ms.j == 0) {
ms.a[ms.n] = ms.l;
ms.j = 1;
ms.n++;
}
}
else {
if (ms.j == 1) {
ms.j = 0;
}
}
ms.l++;
}
return ms.n;
}
int main() {
return 0;
}
全真模拟演练五
定义一个抽象类Shape,由它派生三个类,Square(正方形),Trapezoid(梯形),Triangle(三角形),用虚函数分别计算几种图形面积,并求它们的和。要求用基类指针数组,使它每一个元素指向一个派生类对象。
#include
using namespace std;
class Shape {
public:
virtual double area()const = 0;
};
class Square :public Shape {
public:
Square(double s) :side(s) {}
double area() const { return side * side; }
private:
double side;
};
class Trapezoid :public Shape {
public:
Trapezoid(double i, double j, double k) :a(i), b(j), h(k) {}
double area() const { return (a + b) * h / 2; }
private:
double a, b, h;
};
class Triangle :public Shape {
public:
Triangle(double i, double j) :w(i), h(j) {}
double area() const { return w * h / 2; }
private:
double w, h;
};
void main() {
Shape* p[3];
Square se(5);
Trapezoid td(2, 5, 4);
Triangle te(5, 8);
p[0] = &se;
p[1] = &td;
p[2] = &te;
double da = 0;
for (int i = 0; i < 3; i++) {
da += p[i]->area();
}
cout << da << endl;
}
全真模拟演练六
编写一个类模板void change(T &i,T &j),实现整型、实数、字符串的交换。并编写测试主函数。
#include
using namespace std;
template <class T>
void change(T& i, T& j) {
T temp;
temp = i;
i = j;
j = temp;
}
int main() {
int a, b;
cout << "输入两个整数a,b:";
cin >> a >> b;
change(a, b);
cout << "交换后为a,b:" << a << " " << b << endl;
double c, d;
cout << "\n输入两个实数c,d:";
cin >> c >> d;
change(c, d);
cout << "交换后为c,d:" << c << " " << d << endl;
cin.get();
char* s1, * s2;
char str1[20], str2[20];
s1 = str1; s2 = str2;
cout << "\n输入第一个字符串s1:";
cin.getline(s1, 20);
cout << "输入第二个字符串s2:";
cin.getline(s2, 20);
change(s1, s2);
cout << "交换后的为s1,s2:" << s1 << " \t" << s2 << endl;
return 0;
}
考前深度密押(一)
定义一个图形类figure,其中有保护成员:高度(height),宽度(width),和一个公有的构造函数。该图形派生类有矩形类和等腰三角形类。每个派生类中包含一个函数 area() 用来计算面积。
#include
using namespace std;
class figure {
protected:
double height, width;
public:
figure(double h = 0, double w = 0) :height(h), width(w) {}
};
class triangle :public figure {
protected:
double height, width;
public:
triangle(double h = 0, double w = 0) :height(h), width(w), figure(w, h) { }
double area() {
return height * width / 2;
}
};
class rectangle :public figure {
protected:
double height, width;
public:
rectangle(double h = 0, double w = 0) :height(h), width(w), figure(w, h) { }
double area() {
return height * width;
}
};
void main() {
triangle tri(2, 3);
rectangle rec(2, 3);
cout << tri.area() << endl;
cout << rec.area() << endl;
}
考前深度密押(二)
定义一个生日类,成员变量有年、月、日。定义一个人员类,成员变量有姓名,性别,生日。
人员类中的生日是生日类的对象,两个类都有构造函数和显示函数。在主函数中声明一个人员类对象,显示数据。
#include
#include
using namespace std;
class birth {
private:
int year, month, day;
public:
birth(int y, int m, int d) :year(y), month(m), day(d) {}
void show() {
cout << year << "/" << month << "/" << day << endl;
}
};
class person {
private:
char name[10];
char sex[10];
birth birdy;
public:
person(const char* p, const char* q, int y, int m, int d) :birdy(y, m, d) {
strcpy_s(name, strlen(p) + 1, p);
strcpy_s(sex, strlen(q) + 1, q);
}
void show() {
cout << name << " " << sex << " ";
birdy.show();
}
};
void main() {
person pe("Tom", "男", 1994, 11, 16);
pe.show();
}
2020
1.编写一个函数模板,实现将n个数据进行由小到大排序.
#include
using namespace std;
template<class T>
void Sort(T a[], int n) {
for (int i = 1; i < n; i++)
for (int j = 0; j < n - i; j++)
if (a[j] > a[j + 1]) {
T temp;
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
};
void main() {
int a[5] = { 10,-1,1,3,0 };
Sort(a, 5);
for (int i = 0; i < 5; i++)
cout << a[i] << " ";
}
2.现有学校School类,保护成员Number(编号),Name(姓名)。它的派生类学生Student,数据成员Class_Num(班号),Total(总成绩),用成员函数Display()实现 学生张三,编号2020150601,班级四班,总成绩678 的输出,要求编写派生类的构造函数。
#include
using namespace std;
class School {
public:
School(int nu, const char* s) :Number(nu) {
strcpy_s(Name, strlen(s) + 1, s);
}
protected:
int Number;
char Name[10];
};
class Student :public School {
public:
Student(int nu, const char* na, const char* cn, double t) :Total(t), School(nu, na) {
strcpy_s(Class_Num, strlen(cn) + 1, cn);
}
void Display() {
cout << Number << " " << Name << " " << Class_Num << " " << Total << endl;
}
protected:
char Class_Num[10];
double Total;
};
int main() {
Student stu(2020150601, "张三", "四班", 678);
stu.Display();
return 0;
}
dev c++
#include
#include
using namespace std;
class School {
public:
School(int nu = 0, const char* s = "") {
Number = nu;
strcpy(Name, s);
}
protected:
int Number;
char Name[10];
};
class Student :public School {
public:
Student(int nu = 0, const char* na = "", const char* cn = "", double t = 0) :School(nu, na) {
strcpy(Class_Num, cn);
Total = t;
}
void Display() {
cout << Number << " " << Name << " " << Class_Num << " " << Total << endl;
}
protected:
char Class_Num[10];
double Total;
};
int main() {
Student stu(2020150601, "张三", "四班", 678);
stu.Display();
return 0;
}
2021
1.从键盘输入学号,姓名,成绩,存入score.txt,学号10字节,姓名20字节,成绩整型.
#include
#include
using namespace std;
int main() {
char number[10], name[20];
int score;
ofstream outf("score.txt", ios::out);
if (!outf) {
cout << "打开文件失败";
return 0;
}
cout << "输入学号,姓名,成绩:\n";
while(cin >> number >> name >> score)
outf << number << " " << name << " " << score<<endl;
outf.close();
return 0;
}
2.创建Employee类,保护数据成员:姓名,街道地址,市,省,邮政编码;函数:构造函数,ChangeName函数改变姓名,Display函数打印完整数据 ,定义在类外.
#include
using namespace std;
class Employee {
protected:
char name[10], address[50], city[10], province[10];
int code;
public:
Employee(const char*, const char*, const char*, const char*, int cd);
void ChangeName(const char*);
void Display();
};
Employee::Employee(const char* n, const char* ad, const char* ct, const char* pve, int cd = 0) :code(cd) {
strcpy_s(name, strlen(n) + 1, n);
strcpy_s(address, strlen(ad) + 1, ad);
strcpy_s(city, strlen(ct) + 1, ct);
strcpy_s(province, strlen(pve) + 1, pve);
}
void Employee::ChangeName(const char* n) {
strcpy_s(name, strlen(n) + 1, n);
}
void Employee::Display() {
cout << name << " " << province << " " << city << " " << address << " " << code << endl;
}
int main() {
Employee emp("张三", "某某区某街道某号", "长沙市", "湖南省", 435600);
emp.Display();
return 0;
}
dev c++
#include
#include
using namespace std;
class Employee {
protected:
char name[10], address[50], city[10], province[10];
int code;
public:
Employee(const char*, const char*, const char*, const char*, int cd);
void ChangeName(const char*);
void Display();
};
Employee::Employee(const char* n = "", const char* ad = "", const char* ct = "", const char* pve = "", int cd = 0) {
strcpy(name, n);
strcpy(address, ad);
strcpy(city, ct);
strcpy(province, pve);
code = cd;
}
void Employee::ChangeName(const char* n) {
strcpy(name, n);
}
void Employee::Display() {
cout << name << " " << province << " " << city << " " << address << " " << code << endl;
}
int main() {
Employee emp("张三", "某某区某街道某号", "长沙市", "湖南省", 435600);
emp.Display();
return 0;
}