贵大懒人养成器,欢迎伸手。萌新写的问题肯定多多,轻喷。
第一题
#include
#include
using namespace std;
class Test {
public:
int DataLength(const string& a) {
return a.length();
}
int DataLength(int b) {
int d = 1;
while ((b /= 10) > 0) {
d++;
}
return d;
}
};
第二题
#include
#include
using namespace std;
class Rectangle {
public:
int top, right, bottom, left;
Rectangle(int a, int b, int c, int d) :top(a), right(b), bottom(c), left(d) {}
int check() {
if (top > bottom && right > left) { return 1; }
else { return 0; }
}
void showLeftTop() {
if(check())
cout<< left << "," << top << endl;
else
cout << 0 << "," << 0 << endl;
}
void showRightTop() {
if (check())
cout << right << "," << top << endl;
else
cout << 0 << "," << 0 << endl;
}
void showLeftBottom() {
if (check())
cout << left << "," << bottom << endl;
else
cout << 0 << "," << 0 << endl;
}
void showRightBottom() {
if (check())
cout << right << "," << bottom << endl;
else
cout << 0 << "," << 0 << endl;
}
int getHeight(){
if (check())
return (top - bottom);
else
return 0;
}
int getWidth() {
if (check())
return (right - left);
else
return 0;
}
int getArea() {
return (getHeight() * getWidth());
}
int getPerimeter() {
return (2 * (getHeight() + getWidth()));
}
};
#include
#include
using namespace std;
class Circle {
public:
Circle(int c) :c(c) {}
private:
int c;
};
class Rectangle {
public:
Rectangle(int b, int c) :b(b), c(c) {}
private:
int b, c;
};
class Area {
public:
static double calArea(Circle a) { return (a.c*a.c*3.14159265); }
static int calArea(Rectangle a) {
return (a.b * a.c);
}
};
第三题
class Area {
public:
static double calArea(Circle &a) { return (a.getRadius() * a.getRadius() * 3.14159265); }
static double calArea(Rectangle &a) {
return (a.getHeight() * a.getWidth());
}
static double calArea(const Circle& a) { return (a.getRadius() * a.getRadius() * 3.14159265); }
static double calArea(const Rectangle& a) {
return (a.getHeight() * a.getWidth());
}
};
第五题
#include
using namespace std;
class IntegerProcessing {
public:
static int getNumberOfOne(int a) {
int n = 0;//记录1的个数
do{
if (a &1!= 0) {
n++;
}
a =a>>1;
}while(a!=0);
return n;
}
static int getBinMirror(int a) {
int b = 0;
for (int i = 0; i < 32; i++) {
if (i < 16) {
b |= ((a & (1 << i)) << (31 - 2 * i));//a&(1<> (2 * i - 31));
}
}
return b;
}
};
第六题
#include
#include
using namespace std;
class Array2D {
public:
Array2D(int a):rows(a){
array = (double(*)[8])malloc(a * 8 * sizeof(double));
}
~Array2D()
{
cout << "释放了一个" << rows << "行8列的数组" << endl;
}
void setElem(int a, int b, double c) {
array[a][b] = c;
}
int getRows() {
return rows;
}
int getColumns() {
return 8;
}
double getMaxOfRow(int a) {
double b = array[a][0];//存最大的数
for (int i = 0; i < 8; i++) {
if (array[a][i] > b) {
b = array[a][i];
}
}
return b;
}
double getMinOfRow(int a) {
double b =array[a][0];//存最小的数
for (int i = 0; i < 8; i++) {
if (array[a][i] < b) {
b = array[a][i];
}
}
return b;
}
double getAvgOfRow(int a) {
double b = 0;//存数的和
for (int i = 0; i < 8; i++) {
b += array[a][i];
}
return (b/8);
}
double getMinOfArray() {
double b = array[0][0];//存最小的数
for (int a = 0; a < rows; a++) {
for (int i = 0; i < 8; i++) {
if (array[a][i] < b) {
b = array[a][i];
}
}
}
return b;
}
double getMaxOfArray() {
double b = array[0][0];//存最大的数
for (int a = 0; a < rows; a++) {
for (int i = 0; i < 8; i++) {
if (array[a][i] > b) {
b = array[a][i];
}
}
}
return b;
}
double getAvgOfArray() {
double b =0;//存最小的数
for (int a = 0; a < rows; a++) {
for (int i = 0; i < 8; i++) {
b += array[a][i];
}
}
return (b/(rows*8));
}
private:
int rows;
double(*array)[8];
};
第七题
#include
#include
#include
using namespace std;
class Point {
public:
Point(double x, double y) :x(x), y(y) {}
friend double dist(Point& a, Point& b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
private:
double x, y;
};
class Circle {
public:
Circle(double x, double y,double radius) :a(x,y), radius(radius) {}
double getArea() {
return (radius * radius * 3.14159265);
}
double getPerimeter() {
return (2 * radius * 3.14159265);
}
int isInTheCircle(Point &p){
if (dist(p, a) < radius) {
return 1;
}
else {
return 0;
}
}
private:
Point a;
double radius;
};
第八题
#include
#include
#include
class A{
public:
A(){
std::cout << "A's constructor is called!" << std::endl;}
~A(){
std::cout << "A's destructor is called!" << std::endl;}
};
class B {
public:
B(){
std::cout << "B's constructor is called!" << std::endl;}
~B(){
std::cout << "B's destructor is called!" << std::endl;}
};
class C {
public:
C() {
std::cout << "C's constructor is called!" << std::endl;}
~C() {
std::cout << "C's destructor is called!" << std::endl;}
};
class D :public B, public C, public A {//教材p286
public:
D() {
std::cout << "D's constructor is called!" << std::endl;}
~D() {
std::cout << "D's destructor is called!" << std::endl;}
};
第九题
#include
#include
#include
using namespace std;
class Test {
private:
int x;
public:
Test(int x):x(x){}
void printInfo() {
cout << 2 * x << endl;
}
void printInfo() const{
cout <
第十题
#include
#include
#include
using namespace std;
class Student
{
private:
string name;
string subject;
int age;
int id;
static int num;//存储人数
public:
Student(string a,string b, int c) :name(a), subject(b), age(c) {
num++;
id = 20200000 + num;
};
Student() :name("Mike"), subject("Math"), age(18) {
num++;
id = 20200000 + num;
};
Student(Student &a) :name(a.name), subject(a.subject), age(a.age) {
num++;
id = 20200000 + num;
};
void showInfo() {
cout << name << " " << id << " " << subject << " " << age << endl;
}
};
int Student::num = 0;
第十一题
#include
#include
#include
using namespace std;
class Point {
public:
Point(double x, double y) :x(x), y(y) {}
friend double dist(Point& a, Point& b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
private:
double x, y;
};
class Triangle {
public:
Triangle(Point a, Point b, Point c) :a(a), b(b), c(c) {
x = dist(a, b);
y = dist(a, c);
z = dist(b, c);
p = (x + y + z) / 2;
}
double getArea() {
return sqrt((p-x)*(p-y)*(p-z)*p);
}
private:
Point a, b, c;
double x, y, z,p;//海伦公式求面积
};
第十二题
#include
#include
#include
using namespace std;
class Date
{
private:
int year, month, day;
public:
Date(int a, int b, int c) :year(a), month(b), day(c) {};
Date() :year(2021), month(4), day(16) {};
int getYear() const { return year; }
int getMonth() const { return month; }
int getDay() const { return day; }
void showDate() {
cout << getYear() << "/" << getMonth() << "/" << getDay()<