记录学习,写的不好,能凑活用。
第一题
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Worker {
public:
Worker(string a, string b, double c);
static void printSalary();
private:
string name, position;
double salary;
static double count;
static double totalSalary;
};
double Worker::count = 0;
double Worker::totalSalary=0;
Worker::Worker(string a, string b, double c) :name(a), position(b), salary(c) {
count++; totalSalary += c;
}
void Worker::printSalary() {
cout<<"This company has "<<count<<" workers."<<endl;
cout << "Total salary is " << totalSalary << endl;
cout << "Average salary is " << totalSalary/count << endl;
}
第二题
#include <iostream>
#include <string>
#include <cmath>
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() << endl;
}
void setDate(int a, int b, int c) {
year = a; month = b; day = c;
}
};
class Book {
private:
string name, author, isbn;
double price;
Date date;
public:
Book(string a, string b, string c, Date d, double e);
void printInfo();
};
Book::Book(string a, string b, string c, Date d, double e):name(a),author(b),isbn(c),date(d),price(e){}
void Book::printInfo() {
cout <<name <<" " << author << " " << isbn << " " << price << endl;
date.showDate();
}
第三题
#include <iostream>
#include <string>
#include <cmath>
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() << endl;
}
void setDate(int a, int b, int c) {
year = a; month = b; day = c;
}
};
class Product {
private:
string name, company, type;
double price;
Date date;
public:
Product(string a, string b, string c, Date d, double e);
void printInfo();
};
Product::Product(string a, string b, string c, Date d, double e):name(a),company(b),type(c),date(d),price(e){}
void Product::printInfo() {
cout <<name <<" " << company << " " << type << " " << price << endl;
date.showDate();
}
第四题
#include <iostream>
#include <string>
#include <cmath>
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() << endl;
}
void setDate(int a, int b, int c) {
year = a; month = b; day = c;
}
};
class Employee {
private:
string name, department, id;
double salary;
Date date;
public:
Employee(string a, string b, string c, Date d, double e);
void printInfo();
};
Employee::Employee(string a, string b, string c, Date d, double e):name(a),department(b),id(c),date(d),salary(e){}
void Employee::printInfo() {
cout <<name <<" " << department << " " << id << " " << salary << endl;
date.showDate();
}
第五题
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Element {
private:
string symbol;
public:
Element(string a):symbol(a){}
Element():symbol("O"){}
string getSymbol() { return symbol; }
};
class Oxide {
private:
Element ele[2];
int count[2];
public:
Oxide(Element a,int b,Element c,int d){
ele[0] = a;
ele[1] = c;
count[0] = b;
count[1] = d;
}
void printformula() {
cout << ele[0].getSymbol();
if (count[0] != 1)
cout << count[0];
cout << ele[1].getSymbol();
if (count[1] != 1)
cout << count[1];
cout << endl;
}
};
第六题
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Course {
public:
Course(string a, string b, double c,int d);
static void printCount();
private:
string cid,name;
double credit;
int year;
static double semester1;
static double semester2;
static double semester3;
};
double Course::semester1 = 0;
double Course::semester2 = 0;
double Course::semester3 = 0;
Course::Course(string a, string b, double c,int d) :cid(a),name(b), credit(c),year(d) {
switch (d) {
case 1:
semester1 += c;
break;
case 2:
semester2 += c;
break;
case 3:
semester3 += c;
break;
default:
break;
}
}
void Course::printCount() {
cout << semester1 << " " << semester2 << " " << semester3 <<endl;
}
第七题
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Cube{
public:
double length;
Cube(double a):length(a){}
};
class Cuboid{
public:
double length, width, height;
Cuboid(double a,double b,double c):length(a),width(b),height(c){}
};
class Sphere {
public:
double radius;
Sphere(double a) :radius(a) {}
};
class Cone{
public:
double radius, height;
Cone(double a,double b):radius(a),height(b){}
};
class Volume {
public:
static double calVolume(Cube a){
return a.length * a.length * a.length;
}
static double calVolume(Cuboid a) {
return a.length * a.width * a.height;
}
static double calVolume(Sphere a) {
return (3.14159265*a.radius*a.radius*a.radius*4)/3;
}
static double calVolume(Cone a) {
return (a.height * a.radius* a.radius*3.14159265)/3;
}
};
第八题
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class IPAddr {
public:
int n, m;
IPAddr(int n, int m) :n(n), m(m) {
}
void showIPInfo() {
//ff化为二进制每一位都为1,如n&0xff000000可保留数的头八位,之后均为0
//十六进制每两位作为一段,16的平方等于2的八次幂,故将二进制依次右移8,16,24次。
cout << ((n & 0xff000000) >> 24) << "." << ((n & 0x00ff0000) >> 16) << "." << ((n & 0x0000ff00) >> 8) << "." << ((n & 0x000000ff)) << endl;
int a = 0;//存子网掩码的十进制
int b = 31;//一段ip四段十六进制,一共4*8=32位二进制,第一个数离最后一个数有31个数的距离
for (int i = m; i > 0; i--) {
a += (1 << b);
b--;
}
cout << ((a & 0xff000000) >> 24) << "." << ((a & 0x00ff0000) >> 16) << "." << ((a & 0x0000ff00) >> 8) << "." << ((a & 0x000000ff)) << endl;
int c = a & n;
cout << ((c & 0xff000000) >> 24) << "." << ((c & 0x00ff0000) >> 16) << "." << ((c & 0x0000ff00) >> 8) << "." << ((c & 0x000000ff)) << endl;
}
};
第九题
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class MyString {
public:
MyString(string str):str(str) { count++; }
~MyString() { count--; }
static int GetN() {
return count;
}
static string GetString(MyString* a) {
return a->str;
}
private:
string str;
static int count;
};
int MyString::count = 0;
第十题
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Clock {
private:
int hour, minute, second;
public:
Clock(int a, int b, int c) :hour(a), minute(b), second(c) {}
Clock(int a, int b) :hour(a), minute(b), second(40) {}
Clock(int a) :hour(a), minute(30), second(40) {}
Clock() :hour(0), minute(0), second(0) {}
void Display() {
cout << hour << ":" << minute << ":" << second << endl;
}
void SetValue(int a) {
hour = a; minute = 30; second = 40;
}
};
第十一题
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
#define n 5
class Test {
public:
static void Swap(int a, int b) {
}
static void Swap(int *a, int *b) {
int c = *a;
*a = *b;
*b = c;
}
static void Swap(float &a, float &b) {
float c = a;
a = b;
b = c;
}
static void Swap(int *a) {
for (int i = 0; i < 5; i++)
{
a[i]++;
}
}
static void Print(int *a) {
for (int i = 0; i < 5; i++)
{
cout<<a[i] << endl;
}
}
};
第十二题
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Test {
public:
static int GetMax(int a, int b) {
return (a > b )? a : b;
}
static float GetMax(float a,float b) {
return (a > b) ? a : b;
}
static string GetMax(string a, string b) {
return (a > b) ? a : b;
}
};
第十三题
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Test {
public:
Test(string str) :str(str) {
}
Test() :str("") {
}
string GetStr() {
return str;
}
int GetLength() {
return str.length();
}
void SetStr(string stri) {
str = stri;
}
void Reverse() {
for (int i = 0; i < GetLength() / 2; ++i)
{
char c = str[i];
str[i] = str[GetLength() - i - 1];
str[GetLength() - i - 1] = c;
}
for (int i = 0; i < GetLength(); i++)
{
cout << str[i];
}
}
private:
string str;
};
第十四题
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
//百度单例模式
class A {
public:
static A* GetObject(string nam) {
if (a == NULL) {
a = new A();
a->name = nam;
return a;
}
else {
return a;
}
}
void Print() {
cout << name << endl;
}
private:
A(){}
static A* a;
string name;
};
A* A::a = NULL;