4.5作业

文章详细介绍了C++中的类结构,包括类A和类B,展示了如何使用set(),Sum(),Porduct(),PrimeNumber()等成员函数进行整数操作、字符输入以及字符串处理。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <iomanip>
using namespace std;
class A
{
    int a;
public:
  void set();
  int Sum();
  int Porduct();
  void PrimeNumber();
};
void A::set()
{
    cout<<"请输入:"<<endl;
    cin>>this->a;
}
int A::Sum()
{
    int Sum=0;
    for(int i=1;i<=this->a;i++)
    {
        Sum+=i;
    }
    return Sum;
};
int A::Porduct()
{
    int Por=1;
    for(int i=1;i<=this->a;i++)
    {
        Por*=i;
    }
    return Por;
};
void A::PrimeNumber()
{
    int i;
    int j;
    for(i=1;i<=this->a;i++)
    {
        int temp=0;
        for(j=1;j<=i;j++)
        {
            if(i%j==0)
            {
                temp++;
            }
        }
        if(temp==2)
        {
            cout<<i<<setw(3);
        }
    }
    cout<<endl;
}
int main()
{
    A a1;
    a1.set();
    cout<<a1.Sum()<<endl;
    cout<<a1.Porduct()<<endl;
    a1.PrimeNumber();
    return 0;
}

 

 

#include <iostream>
 
using namespace std;
class A
{
public:
    int a=-1;
    void mygetchar();
};
class B
{
public:
    char b='a'-1;
    void mygetchar();
};
void A::mygetchar()
{
    this->a++;
    if(this->a<=9)
    {
        cout<<this->a;
    }
    if(this->a==9)
    {
        this->a=-1;
    }
}
void B::mygetchar()
{
    this->b++;
    if(this->b<='z')
    {
        cout<<this->b;
    }
}
int main()
{
    A a1;
    B b1;
 
    while('z'-b1.b>=10)
    {
        int i=0;
        while(i<=9)
        {
        a1.mygetchar();
        b1.mygetchar();
        i++;
        }
    }
    while('z'-b1.b>0)
    {
        b1.mygetchar();
    }
    cout<<endl;
    return 0;
}
#include <iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
class A{
    string a;
 
public:
    void test(char c)
    {
        a+=c;
    }
    void show(){
        cout<<a<<endl;
    }
};
class B{
    string b;
 
public:
    void test(char c)
    {
        b+=c;
    }
    void show()
    {
        cout<<b<<endl;
    }
public:
 
};
 
int main()
{
    string str;
    A a1;
    B b1;
    cout<<"请输入字符串"<<endl;
    cin>>str;
    for(int i=0;i<int(str.length());i++)
    {
        if(str.at(i)<='z'&&str.at(i)>='a')
        {
            a1.test(str.at(i));
        }else
        {
            b1.test(str.at(i));
        }
    }
    a1.show();
    b1.show();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值