c++ 实验

#include <iostream>
#include <assert.h>
using namespace std;

template<typename T>T my_max(T* array, int count){
    assert(count > 0);
    
    int max_num = array[0];
    if (count == 1)
        return max_num;
        
    for(int i=1; i<count; i++){
        if (max_num < array[i]){
            max_num = array[i];
        }
    }
    
    return max_num;
}

//
//int my_max(int* array, int count){
//    if (count == 0) return -1;
//    
//    int max_num = array[0];
//    if (count == 1)
//        return max_num;
//        
//    for(int i=0; i<count - 1; i++){
//        if (max_num < array[i+1]){
//            max_num = array[i+1];
//        }
//    }
//    
//    return max_num;
//}

int main(){
    double array[32] = {3, 5, 1, 2, 9919, 0, 9, 999999};
//    int array[0];
    cout<< my_max(array, sizeof(array)/sizeof(array[0])) << endl;
    
}

==============================================================

#include <iostream>
using namespace std;

template<typename T>string debug_rep(const T &p){
    string prex("1:");
    return prex+p;
}

template<typename T>string debug_rep(T *p){
    string prex("2:");
    return prex+p;
}

string debug_rep(const string &s){
    string prex("3:");
    return prex+s;
}
int main(){
    string s("hi");
    cout<<debug_rep(&s)<<endl;
    const string *sp = &s;
    cout<<debug_rep(sp)<<endl;
    
}

#include <iostream>
using namespace std;

template<typename T>
class MyStack{
    private:
        T* array;
        int count;
    public:
        MyStack(int size){
            this->array = new T[size];
        }
        
        ~MyStack(){
            delete this->array;
        }
        
        bool isEmpty(){
            if (this->count == 0){
                return true;
            }else{
                return false;
            }
        }
        
        T pop(){
            T tmpElement = this->array[this->count-1];
             this.array[this->count-1] = '\0';
             return tmpElement;
        }
        
        void push(T element){
            
            this->cout++;
        }
};
int main(){
    
//    double array[32] = {3, 5, 1, 2, 9919, 0, 9, 999999};
    MyStack<int> myStack;
//    cout << stack.PopElement() << endl;

=====================================12/13======================

#include <iostream>
#include <string.h>

using namespace std;

const double PI=3.14;

class Shape{
public:
    virtual double area() = 0;
};

class Cricle:public Shape{
public:
    Cricle(double r=0):radius(r){
    }
    double area(){
        return PI*radius*radius;
    }
private:
    double radius;
};

class Square:public Shape{
public:
    Square(double len=0):length(len){
    }
    double area(){
        return length*length;
    }
private:
    double length;
};

int main(){
    const int MAX = 30;
    Shape* shape[MAX];
    int cnt = 0;
    char cmd;
    cin>>cmd;
    double num;
    while(cmd != 'q'){
        switch(cmd){
            case 's':
                cin>>num;
                shape[cnt++] = new Square(num);
                break;
            case 'c':
                cin>>num;
                shape[cnt++] = new Cricle(num);
                break;
            case 'q':
                break;
            cin>>cmd;
        }
    }
    
    for(int i=0; i<cnt; i++){
        cout<<"area:"<<shape[i]->area()<<endl;
    }
    
    for(int i=0; i<cnt; i++){
        delete shape[i];
    }

===============================

#include <iostream>

using namespace std;

class Complex{
    public:
        Complex(double r, double i=0):real(r),image(i){
        }
        Complex operator+(const Complex& c){
            return Complex(this->real + c.real, this->image + c.image);
        }
        Complex operator-(const Complex& c){
            return Complex(this->real - c.real, this->image - c.image);
        }
        Complex operator*(const Complex& c){
            return Complex(this->real * c.real - this->image * c.image, this->real * c.image + this->image * c.real);
        }
        
        void display(){
            cout<<real<<"+"<<image<<"i"<<endl;
        }
        friend bool operator>(const Complex& c1, const Complex& c2);
        friend bool operator<(const Complex& c1, const Complex& c2);
        friend bool operator==(const Complex& c1, const Complex& c2);
        
    private:
        double real;
        double image;
};

bool operator>(const Complex& c1, const Complex& c2){
    return c1.real * c1.real + c1.image * c1.image > c2.real * c2.real + c2.image * c2.image;
}
bool operator<(const Complex& c1, const Complex& c2){
    return c1.real * c1.real + c1.image * c1.image < c2.real * c2.real + c2.image * c2.image;
}
bool operator==(const Complex& c1, const Complex& c2){
    return c1.real * c1.real + c1.image * c1.image == c2.real * c2.real + c2.image * c2.image;
}

bool operator>(const Complex& c1, double r){
    return c1.real * c1.real + c1.image * c1.image > r;
}
        
int main(){
    Complex c1(1,2),c2(3);
    c1.display();
    c2.display();
    (c1+c2).display();
    
}

===================================

#include <iostream>
#include <fstream>
#include <exception>

using namespace std;

class MyPrimeFile{
public:
    bool open(const char* filePath){
        return fs.open(filePath);
    }
    void close(){
        fs.open();
    }
    const MyPrimeFile& operator>>(int& l){
        fs>>l;
        return *this;
    }
    const MyPrimeFile& operator<<(int r){
        if(!isPrime(r)){
            throw exception();
        }
        fs<<r;
        return *this;
    }
    MyPrimeFile operator+(const MyPrimeFile& r){
        MyPrimeFile mpf("tmp");
        mpf.open();
        int line;
        
//        fs>>line;
//        mpf<<line;
        while(!fs.eof()){
            fs>>line;
            if (!fs.eof()){
                mpf << line;
            }
            mpf<<line;
        }
        
        while(!r.fs.eof()){
            r.fs>>line;
            if(!r.fs.eof()){
                mpf<<line;
            }
        }
        mpf.close();
        return mpf;
    }
private:
    fstream fs;
    bool isPrime(int n){
        for(int i=2; i<n-1; i++){
            if (n%i == 0){
                return false;
            }
            return true;
        }
    }
};

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值