清明作业 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;
}
相关推荐
一眼万里*e3 分钟前
MavLink消息协议
c++
m0_603888717 分钟前
More Images, More Problems A Controlled Analysis of VLM Failure Modes
人工智能·算法·机器学习·ai·论文速览
恶魔泡泡糖14 分钟前
51单片机矩阵按键
c语言·算法·矩阵·51单片机
叶子20242215 分钟前
电力系统分析---对称分量法
算法
千金裘换酒1 小时前
LeetCode 二叉树的最大深度 递归+层序遍历
算法·leetcode·职场和发展
爱敲代码的TOM1 小时前
详解一致性哈希算法
算法·哈希算法
lzllzz231 小时前
递归的理解
算法·深度优先·图论
星火开发设计1 小时前
C++ stack 全面解析与实战指南
java·数据结构·c++·学习·rpc··知识
AI视觉网奇1 小时前
ue 设置骨骼网格体
c++·ue5
小O的算法实验室1 小时前
2024年IEEE TITS SCI2区TOP,考虑无人机能耗与时间窗的卡车–无人机协同路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进