清明作业 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;
}
相关推荐
BUG收容所所长3 分钟前
二分查找的「左右为难」:如何优雅地找到数组中元素的首尾位置
前端·javascript·算法
weixin_4196583126 分钟前
数据结构之栈
数据结构
图先26 分钟前
数据结构第一章
数据结构
itsuifengerxing40 分钟前
python 自定义无符号右移
算法
猎板PCB厚铜专家大族1 小时前
高频 PCB 技术发展趋势与应用解析
人工智能·算法·设计规范
dying_man1 小时前
LeetCode--24.两两交换链表中的结点
算法·leetcode
yours_Gabriel1 小时前
【力扣】2434.使用机器人打印字典序最小的字符串
算法·leetcode·贪心算法
Dovis(誓平步青云)2 小时前
探索C++标准模板库(STL):String接口的底层实现(下篇)
开发语言·c++·stl·string
草莓熊Lotso2 小时前
【数据结构初阶】--算法复杂度的深度解析
c语言·开发语言·数据结构·经验分享·笔记·其他·算法
KyollBM2 小时前
【CF】Day75——CF (Div. 2) B (数学 + 贪心) + CF 882 (Div. 2) C (01Trie | 区间最大异或和)
c语言·c++·算法