全面复习回顾——C++语法篇3

30、快慢指针

void func(int* p, int n) 
{

    // write your code here......
    int tmp=0;
    int q=0;
    for(int i=tmp;i<n;i++)
    {
        if(p[i]!=0)
        {
            p[q++]=p[i];
            tmp++;
        }
    }
    for(int i=tmp;i<n;i++)
    {
        p[i]=0;
    }
}

31、string的函数就可以实现比大小

int main()
{
    string s1;
    string s2;
    cin>>s1>>s2;
    if(s1>s2)
    {
        cout<<"1";
    }
    else if(s1<s2)
    {
        cout<<"-1";
    }
    else {
        cout<<"0";
    }
}

32、交换两个数

    int *p=&m;
    int *q=&n;
    int tmp;
    tmp=*p;
    *p=*q;
    *q=tmp;

33、

    int count = 0;

    if (!s2.empty()) { // 检查 s2 是否为空
        size_t pos = s1.find(s2);
        while (pos != string::npos) {
            count++;
            pos = s1.find(s2, pos + 1);
        }
    }

34、

    for(char ch:str)
    {
        if(isalpha(ch)) chars++;
        else if(isdigit(ch))    digits++;
        else if(isspace(ch))    whitespace++;
        else others++;
    }

35、递归实现阶乘

long long factorial(int n) {

    // write your code here......
    if(n<0) return -1;
    if(n==0||n==1)  return 1;
    return n*factorial(n-1);
}

36、斐波那契

int getSum(int n) {
    // write your code here......
    if(n<3) return 1;
    return getSum(n-1)+getSum(n-2);
}

37、类

#include <iostream>
using namespace std;

class Cube {

    // write your code here......
    public:
    int length;
    int width;
    int height;
    public:
    void setLength(int le)
    {
        length=le;
    }
    void setHeight(int he)
    {
        height=he;
    }
    void setWidth(int wi)
    {
        width=wi;
    }
    int getLength()
    {
        return length;
    }
    int getHeight()
    {
        return height;
    }
    int getWidth()
    {
        return width;
    }
    int getArea(int length,int width,int height);
    int getVolume(int length,int width,int height);
};

int Cube::getArea(int length,int width,int height)
{
    return height*length*2+length*width*2+width*height*2;
}

int Cube::getVolume(int height,int length,int width)
{
    return length*height*width;
}

int main() {

    int length, width, height;
    cin >> length;
    cin >> width;
    cin >> height;

    Cube c;
    c.setLength(length);
    c.setWidth(width);
    c.setHeight(height);

    cout << c.getLength() << " "
        << c.getWidth() << " "
        << c.getHeight() << " "
        << c.getArea(length,width,height) << " "
        << c.getVolume(length,width,height) << endl;

    return 0;
}

38、点圆类关系

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

// 点类
class Pointer {

    private:
        int x;  // x 坐标
        int y;  // y 坐标

    public:
        void setX(int x) {
            this->x = x;
        }

        int getX() {
            return x;
        }

        void setY(int y) {
            this->y = y;
        }

        int getY() {
            return y;
        }

};

// 圆类
class Circle {

    private:
        Pointer center; // 圆心
        int radius; // 半径

    public:
        void setCenter(int x, int y) {
            center.setX(x);
            center.setY(y);
        }

        void setRadius(int radius) {
            this->radius = radius;
        }

        // write your code here......
        void isPointerInCircle(Pointer p)
        {
            int x1=p.getX();
            int y1=p.getY();
            int x2=center.getX();
            int y2=center.getY();
            int len=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
            if(len<radius)    cout<<"in";
            else if(len>radius)   cout<<"out";
            else    cout<<"on";
        }

};

int main() {

    // 键盘输入点的坐标
    int x, y;
    cin >> x;
    cin >> y;

    // 创建一个点
    Pointer p;
    p.setX(x);
    p.setY(y);

    // 创建一个圆
    Circle c;
    c.setCenter(5, 0);
    c.setRadius(5);

    // 判断点和圆的关系
    c.isPointerInCircle(p);

    return 0;
}

39、析构函数

#include <iostream>
#include <string>

using namespace std;

// Person类
class Person {
    public:
        string name;    // 姓名
        int age;    // 年龄

        // write your code here......
        Person(string n,int a)
        {
            name=n;
            age=a;
        }

        void showPerson() {
            cout << name << " " << age << endl;
        }
};

int main() {

    string name;
    int age;

    cin >> name;
    cin >> age;

    Person p(name, age);
    p.showPerson();

    return 0;
}

40、深拷贝与浅拷贝的问题

#include <iostream>
#include <cstring>
#pragma warning(disable : 4996)
using namespace std;

class Person {

    public:
        char* name; // 姓名
        int age;    // 年龄

        Person(const char* name, int age) {
            this->name = new char[strlen(name) + 1];
            strcpy(this->name, name);
            this->age = age;
        }

        // write your code here......
        Person (const Person &p)
        {
            name=new char(strlen(p.name)+1);
            strcpy(name,p.name);
            age=p.age;
        }

        void showPerson() {
            cout << name << " " << age << endl;
        }

        ~Person() {
            if (name != nullptr) {
                delete[] name;
                name = nullptr;
            }
        }

};

int main() {

    char name[100] = { 0 };
    int age;

    cin >> name;
    cin >> age;

    Person p1(name, age);
    Person p2 = p1;

    p2.showPerson();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值