4.6

数求和阶乘和质数 

#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;
}


实现字符串交错输出 
#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;
}
将字母和数字分别存入两个不同的类的对象,然后输出。
#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;
}

相关推荐

  1. 学完Efficient c++ (46-47)

    2024-04-07 12:10:03       20 阅读
  2. 46. 全排列

    2024-04-07 12:10:03       28 阅读
  3. 46. 全排列(回溯)

    2024-04-07 12:10:03       33 阅读
  4. LeetCode 46 全排列

    2024-04-07 12:10:03       40 阅读
  5. LeetCode 46. 全排列

    2024-04-07 12:10:03       38 阅读
  6. 46)设计停车系统

    2024-04-07 12:10:03       33 阅读
  7. LeetCode 46.全排列

    2024-04-07 12:10:03       25 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-07 12:10:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-07 12:10:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-07 12:10:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-07 12:10:03       20 阅读

热门阅读

  1. JVM常量池

    2024-04-07 12:10:03       18 阅读
  2. MySQL中的事务隔离级别与MVCC及两者间的关联

    2024-04-07 12:10:03       16 阅读
  3. Netty和websocket,如何部署Netty

    2024-04-07 12:10:03       13 阅读
  4. 用FPGA搞图像算法需要具备哪些基础

    2024-04-07 12:10:03       12 阅读
  5. 手写一个民用Tomcat (02)

    2024-04-07 12:10:03       15 阅读
  6. 计算机网络的分层结构及模型

    2024-04-07 12:10:03       16 阅读
  7. 设计模式面试题(六)

    2024-04-07 12:10:03       15 阅读