【C++】c++类实现的几个例子

1、判断输出闰年

#include<iostream>
#include "FileNameClass.h"
using namespace std;

class date
{
public:
	void Set(int, int, int);
	int IsLeapYear();
	void ISPrint();
	void NOPrint();
private:
	int month, day, year;
};
void date::Set(int m, int d, int y)
{
	month = m;
	day = d;
	year = y;
}
int date::IsLeapYear()
{
	return(year % 4 == 0 & year % 100 != 0 | year % 400 == 0);
}
void date::ISPrint()
{
	cout << month << "/" << day<< "/" << year <<"是闰年!"<< endl;
}
void date::NOPrint()
{
	cout << month << "/" << day << "/" << year << "不是闰年!" << endl;
}
void main()
{
	date a;
	int i, j, k,ii;
	cout << "输入日期判断是否为闰年!(请按:月-日-年输入)" << endl;
	while (1)
	{
		cin >> i >> j >> k;
		a.Set(i, j, k);
		ii = a.IsLeapYear();
		if (ii == 0)
			a.NOPrint();
		else
			a.ISPrint();
	}
}

2、计算点的直角坐标和极坐标


#include<iostream>
#include<cmath>
#include "FileNameClass.h"
using namespace std;
class Point
{
public:
	void Set(double, double);
	double xoffset();
	double yoffset();
	double angle();
	double radius(double,double);
protected:
	double x, y;
};
void Point::Set(double ix, double iy)
{
	x = ix; y = iy;
}
double Point::xoffset()
{
	return x;
}
double Point::yoffset()
{
	return y;
}
double Point::angle()
{
	return(180 / 3.1415926) * atan2(y, x);
}
double Point::radius(double x,double y)
{
	return sqrt(pow(x, 2) + pow(y, 2));
}


int main()
{
	Point p;
	double x, y;
	cout << "计算点的直角坐标和极坐标,直到x分量值小于0\n";
	for (;;)
	{
		cout << "请输入x,y坐标:";
		cin >> x >> y;
		if (x < 0)break;
		p.Set(x, y);
		cout << "------角度--------" << p.angle() << "-------------\n";
		cout << "------极径--------" << p.radius(x,y) << "-------------\n";
		cout << "------X分量-------" << p.xoffset() << "-------------------\n";
		cout << "------Y分量-------" << p.yoffset() << "-------------------\n";
	}
	return 0;
}

3、定义一个字符串类string,使其至少具有内容(contents)和长度(length)两个数据成员,并具有显示字符串、求字符串长度、给原字符串后添加一个字符串等功能。

#include <iostream>
#include <cstring>
#include "FileNameClass.h"
using namespace std;
class String {
private:
    char* contents; int length;
public:
    String(const char* str) {
        length = strlen(str);
        contents = new char[length + 1];
        strcpy(contents, str);
    }
    void display() {
        std::cout << "字符串:" << contents << std::endl;
    }
    int getLength() {
        return length;
    }
    void addstr(const char* str) {
        int newLength = length + strlen(str);
        char* newContents = new char[newLength + 1];
        strcpy(newContents, contents);
        strcat(newContents, str);
        contents = newContents;
        length = newLength;
    }
};
int main() {
    String _str("aaaaaaaaa");/*输入源字符串*/
    _str.display();
    int len = _str.getLength();
    cout << "长度: " << len << endl;
    _str.addstr("xxxxxxxxx");/*输入拼接字符串*/
    _str.display();
    return 0;
}

4、编写一个属性类Attribute,使其能够计算最大值、最小值、平均值、均方根等属性。

#include <iostream>
#include "FileNameClass.h";
using namespace std;
#include<cmath>
class Attribute {
private:
    double* numbers;int size;
public:
    Attribute() {
        numbers = nullptr;size = 0;
    }
    void addNumber(double num) {
        double* temp = new double[size + 1];
        for (int i = 0; i < size; i++) {
            temp[i] = numbers[i];
        }
        temp[size] = num;
        delete[] numbers;
        numbers = temp;
        size++;
    }
    double getMax() {
        if (size == 0) {
            return 0.0;
        }
        double maxNum = numbers[0];
        for (int i = 1; i < size; i++) {
            if (numbers[i] > maxNum) {
                maxNum = numbers[i];
            }
        }
        return maxNum;
    }
    double getMin() {
        if (size == 0) {
            return 0.0;
        }
        double minNum = numbers[0];
        for (int i = 1; i < size; i++) {
            if (numbers[i] < minNum) {
                minNum = numbers[i];
            }
        }
        return minNum;
    }
    double getAverage() {
        if (size == 0) {
            return 0.0;
        }
        double sum = 0.0;
        for (int i = 0; i < size; i++) {
            sum += numbers[i];
        }
        return sum / size;
    }
    double getRootMeanSquare() {
        if (size == 0) {
            return 0.0;
        }
        double sum = 0.0;
        for (int i = 0; i < size; i++) {
            sum += numbers[i] * numbers[i];
        }
        return std::sqrt(sum / size);
    }
};

int main() {
    for (;;) {
        Attribute attribute;
        int count;
        cout << "请输入要输入的数的个数:";
        cin >> count;
        if (count <= 0) {
            cout << "输入的个数必须大于0 !" << endl;
            return 0;
        }
        double num;
        for (int i = 0; i < count; i++) {
            cout << "请输入第" << i + 1 << "个数:";
            cin >> num;
            attribute.addNumber(num);
        }
        cout << "---------------------------------\n";
        cout << "最大值   |   " << attribute.getMax() << "\t" << endl;
        cout << "---------------------------------\n";
        cout << "最小值   |   " << attribute.getMin() << "\t" << endl;
        cout << "---------------------------------\n";
        cout << "平均值   |   " << attribute.getAverage() << "\t" << endl;
        cout << "---------------------------------\n";
        cout << "均方根   |   " << attribute.getRootMeanSquare() << "\t" << endl;
        cout << "---------------------------------\n";
}
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值