4.3作业

文章展示了C++编程中的两个类Num和Let,分别用于数值操作(求和、乘积和质数检测)以及字符处理(循环获取并打印字符)。同时介绍了如何对字符串进行排序。
摘要由CSDN通过智能技术生成

#include <iostream>

using namespace std;

class Num
{
    int a;
public:
    Num():a(0){}
    void set(int a);
    void Sum();
    void Porduct();
    void PrimeNumber();
};

//设置a的值
void Num::set(int a)
{
    this->a = a;
}

//实现1-a的数字之和
void Num::Sum()
{
    int sum = 0;
    for(int i=1;i<=a;i++)
    {
        sum += i;
    }
    cout << "1到a的和 sum = " << sum << endl;
}

//实现1-a的数字乘积
void Num::Porduct()
{
    int mul = 1;
    for(int i=1;i<=a;i++)
    {
        mul *= i;
    }
    cout << "1到a的乘积 mul = " << mul << endl;
}

//输出1-a之间的所有质数
void Num::PrimeNumber()
{
    int i = 0,j = 0;
    int flag = 0;
    cout << "1-a的所有质数:" ;
    for(i=2;i<=a;i++)
    {
        flag = 0;
        for(j=2;j<i;j++)
        {
            if(i%j == 0)
            {
                flag = 1;
            }
        }
        if(flag == 0)
        {
            cout << i << "   " ;
        }
    }
    cout << endl;
}

int main()
{
    Num n1;
    n1.set(5);
    n1.Sum();
    n1.Porduct();
    n1.PrimeNumber();
    return 0;
}

#include <iostream>

using namespace std;

class A
{
    char c;
public:
    A():c('0'){}
    void MyGetChar();
};

class B
{
    char c;
public:
    B():c('a'){}
    void MyGetChar();
};

//A的MyGetChar
void A::MyGetChar()
{
    cout << this->c;
    this->c ++;
    if(this->c > '9')
    {
        this->c = '0';
    }
}

//B的MyGetChar
void B::MyGetChar()
{
    cout << this->c;
    this->c ++;
    if(this->c > 'z')
    {
        this->c = 'a';
    }
}

int main()
{
    A a1;
    B b1;
    for(int i=0;i<50;i++)
    {
        if(i<20)
        {
            a1.MyGetChar();
        }
        if(i<26)
        {
            b1.MyGetChar();
        }
    }
    return 0;
}

#include <iostream>
#include <cstring>
using namespace std;

class Let
{
    char c[20];
public:
    Let(){}
    void dump(string str)
    {
        int j=0;
        for(int i=0;i<str.length();i++)
        {
            if((str.at(i)>='a' && str.at(i)<='z') || (str.at(i)>='A' && str.at(i)<='Z'))
            {
                c[j] = str.at(i);
                j++;
            }
        }
        int len = strlen(c);
        for(int i=0;i<len;i++)
        {
            cout << c[i] ;
        }
        cout << endl;
    }
};

class Num
{
    char a[20];
public:
    Num(){}
    void dump(string str)
    {
        int j=0;
        for(int i=0;i<str.length();i++)
        {
            if((str.at(i)>='0' && str.at(i)<='9'))
            {
                a[j] = str.at(i);
                j++;
            }
        }
        int len = strlen(a);
        for(int i=0;i<len;i++)
        {
            cout << a[i] ;
        }
        cout << endl;
    }
};

//排序函数
string sort(string str)
{
    int i=0,j=0;
    char temp = 0;
    for(i=1;i<str.length();i++)
    {
        for(j=0;j<str.length()-i;j++)
        {
            if(str.at(j)>str.at(j+1))
            {
                temp = str.at(j);
                str.at(j) = str.at(j+1);
                str.at(j+1) = temp;
            }
        }
    }
    return str;
}

int main()
{
    Let l;
    Num n;
    string arr;
    cout << "输入字符串:" ;
    cin >> arr;
    string str = sort(arr);
    l.dump(str);
    n.dump(str);
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值