清明作业 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;
}
相关推荐
汉克老师4 分钟前
GESP2026年9月认证C++一级( 第一部分选择题(8~15题)精讲
c++·gesp·小学生·学c++编程
小小、码农20 分钟前
〖Linux文件系统〗:彻底打通文件 IO 全链路
linux·开发语言·数据结构·c++·系统
大侠归来24 分钟前
cJSON 源码解析:parse_string 函数深入剖析
linux·网络·算法
東隅已逝,桑榆非晚35 分钟前
priority_queue的介绍和使用
c++·经验分享·笔记·学习
茉莉玫瑰花茶1 小时前
OpenGL [ 纹理 ]
开发语言·c++·opengl
余额瞒着我当琳1 小时前
C++ map 底层与AVL 树
开发语言·c++·算法
释厄6232 小时前
物理发展史·量子力学=节点2·牛顿力学=节点3·宇宙的语言算法AI
人工智能·算法·microsoft
CQU_JIAKE2 小时前
9.30【A】
算法
INGNIGHT2 小时前
1897 · 会议室 3(扫描线)
数据结构·算法
All for pursuit.2 小时前
【贪心-2】253.会议室 II
数据结构·c++·算法·leetcode