清明作业 c++

1.封装一个类,实现对一个数求累和阶乘质数

cpp 复制代码
#include <iostream>

using namespace std;
int mproduct(int a){
    if(a>1){
        return a*mproduct((a-1));
    }else{
        return 1;
    }
}
class number{
    int a;
public:
    number():a(5){};
    number(int a):a(a){}
    void set(int a){this->a=a;}
    void sum(){
        int sun=0;
        for(int i=1;i<=a;i++){
            sun+=i;
        }
        cout<<"sun="<<sun<<endl;
    }
    void product(){
        cout<<mproduct(a)<<endl;
    }
    void primeNumber(){

            for(int j=1;j<a;j++){
                if(a%j==0){
                    continue;
                }else{
                    cout<<j<<"  ";
                }
            }

        cout<<endl;
    }
};

int main()
{
    number num;
    num.set(12);
    num.sum();
    num.product();
    num.primeNumber();
    return 0;
}

2.封装两个类,实现字符串交错输出

cpp 复制代码
#include <iostream>

using namespace std;
class A{
    string str;
    int a;
public:
    A():str("abcdefghijklmnopqrstuvwxyz"),a(0){}
    void mygetchar(){
        cout <<str.at(a)<<"  ";
        a=(a+1)%26;
    }
};
class B{
    string str;
    int a;
public:
    B():str("1234567890"),a(0){}
    void mygetchar(){
        cout<<str.at(a)<<"  ";
        a=(a+1)%10;
    }
};

int main()
{
    A a;
    B b;
    int i=0;
    int len;
    cin>>len;
    while(i++<len){
        a.mygetchar();
        b.mygetchar();
    }
    return 0;
}
  1. 输入字符串,将字母和数字分别存入两个不同的类的对象,然后输出。
cpp 复制代码
#include <iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
class A{
    string a;

public:
    A(){

    }
    void myinsert(char c){
        a+=c;
    }
    void show(){
        cout<<a<<endl;
    }
};
class B{
    string b;

public:
    B(){}
    void myinsert(char c){
        b+=c;
    }
    void show(){
        cout<<b<<endl;
    }
public:

};

int main()
{
    string str;
    A A;
    B B;
    //char a[128];
    cin>>str;
    cout<<"字符串输入成功"<<endl;
    for(unsigned int i=0;i<str.length();i++){
        if(str.at(i)<'9'&&str.at(i)>'0'){
            A.myinsert(str.at(i));
        }else{
            B.myinsert(str.at(i));
        }
    }
    A.show();
    B.show();
    return 0;
}
相关推荐
ShineWinsu6 小时前
对于C++:类和对象的解析—下(第二部分)
c++·面试·笔试·对象··工作·stati
2013092416276 小时前
1968年 Hart, Nilsson, Raphael 《最小成本路径启发式确定的形式基础》A* 算法深度研究报告
人工智能·算法
如何原谅奋力过但无声6 小时前
【力扣-Python-滑动窗口经典题】567.字符串的排列 | 424.替换后的最长重复字符 | 76.最小覆盖子串
算法·leetcode
BHXDML7 小时前
第七章:类与对象(c++)
开发语言·c++
玄冥剑尊7 小时前
贪心算法进阶
算法·贪心算法
玄冥剑尊7 小时前
贪心算法深化 I
算法·贪心算法
52Hz1187 小时前
力扣73.矩阵置零、54.螺旋矩阵、48.旋转图像
python·算法·leetcode·矩阵
BHXDML7 小时前
第一章:线性回归& 逻辑回归
算法·逻辑回归·线性回归
yyf198905258 小时前
C++ 跨平台开发的挑战与应对策略
c++
iAkuya8 小时前
(leetcode)力扣100 二叉搜索树种第K小的元素(中序遍历||记录子树的节点数)
算法·leetcode·职场和发展