五一 作业

cpp 复制代码
#include <iostream>
 
using namespace std;
class Num
{
private:
    int a;
public:
    Num() {}
    Num(int a):a(a){}
    //设置a的值
    void set(int a){
        this->a=a;
    }
    //1-a的和
    void Sum(){
        if(a<1){
            cout<<"a<1"<<endl;
            return;
        }
        int sum=0;
        for(int i=1;i<=a;i++){
            sum+=i;
        }
        cout<<"Sum="<<sum<<endl;
    }
    //1-a的乘积
    void Porduct(){
        if(a<1){
            cout<<"a<1"<<endl;
            return;
        }
        int product=1;
        for(int i=1;i<=a;i++){
            product*=i;
        }
        cout<<"Product="<<product<<endl;
    }
    //1-a的所有质数
    void PrimeNumber(){
        if(a<1){
            cout<<"a<1"<<endl;
            return;
        }
        cout<<"PrimeNumber:";
        for(int i=2;i<=a;i++){
            int flag=0;
            for(int j=2;j<i;j++){
                if(i%j==0){
                    flag++;
                }
            }
            if(0==flag){
                cout<<i<<" ";
            }
        }
    cout<<endl;
    }
};
int main()
{
    Num num1;
    num1.set(10);
    num1.Sum();
    num1.Porduct();
    num1.PrimeNumber();
    return 0;

}
  1. 已知C风格的字符串,完成对字符串通过下标访问时的异常处理机制(越界访问)
cpp 复制代码
#include <iostream>
#include <cstring>
using namespace std;
 
class Str
{
private:
    char *str;
    int size;
public:
    Str() {}
    Str(const char *str):str(nullptr),size(0){
        size=strlen(str);
        this->str=new char[size+1];
        strcpy(this->str,str);
    }
    char &at(int pos){
        if(pos>=size){
            throw char(1);
        }
        return *(str+pos);
    }
};
int main()
{
    Str s1("hello");
    cout<<s1.at(3)<<endl;
    cout<<s1.at(5)<<endl;
    return 0;
}
cpp 复制代码
#include <iostream>
 
using namespace std;
class A
{
private:
    static char a;
public:
    A() {}
    static void MyGetChar(){
        if(a>'9'){
            a='0';
        }
        cout<<a;
        a++;
    }
};
char A::a='0';
class B
{
private:
    static char b;
public:
    B() {}
    static void MyGetChar(){
        if(b>'z'){
//            cout<<endl;
            b='a';
        }
        cout<<b;
        b++;
    }
};
char B::b='a';
int main()
{
    for(int i=0;i<26;i++){
        if(i<20){
            A().MyGetChar();
        }
        B().MyGetChar();
    }
    cout<<endl;
    for(int i=0;i<26;i++){
        if(i<20){
            A().MyGetChar();
        }
        B().MyGetChar();
    }
    return 0;
}
cpp 复制代码
#include <iostream>
 
using namespace std;
class A
{
private:
    string str;
public:
    A() {}
    A(string str):str(str){
        int j=0;
        for(unsigned int i=0;i<this->str.size();i++){
            if(this->str.at(i)>='a'&&this->str.at(i)<='z'){
                this->str.at(j)=this->str.at(i);
                j++;
            }
        }
        this->str.resize(j);
    }
    void dump(){
        char temp;
        for(unsigned int i=0;i<str.size()-1;i++){
            for(unsigned int j=0;j<str.size()-1-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;
                }
            }
        }
        cout<<str<<endl;
    }
 
};
class B
{
private:
    string str;
public:
    B() {}
    B(string str):str(str){
        int j=0;
        for(unsigned int i=0;i<this->str.size();i++){
            if(this->str.at(i)>='0'&&this->str.at(i)<='9'){
                this->str.at(j)=this->str.at(i);
                j++;
            }
        }
        this->str.resize(j);
    }
    void dump(){
        char temp;
        for(unsigned int i=0;i<str.size()-1;i++){
            for(unsigned int j=0;j<str.size()-1-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;
                }
            }
        }
        cout<<str<<endl;
    }
};
int main()
{
    string str;
    cin>>str;
    A a(str);
    a.dump();
    B b(str);
    b.dump();
    return 0;
}
相关推荐
C++ 老炮儿的技术栈21 分钟前
UDP 与 TCP 的区别是什么?
开发语言·c++·windows·算法·visual studio
殇者知忧24 分钟前
【论文笔记】若干矿井粉尘检测算法概述
深度学习·神经网络·算法·随机森林·机器学习·支持向量机·计算机视觉
wgslucky27 分钟前
Dubbo报错:module java.base does not “opens java.lang“ to unnamed module
java·开发语言·dubbo
whyeekkk1 小时前
python打卡第48天
开发语言·python
DougLiang2 小时前
关于easyexcel动态下拉选问题处理
java·开发语言
mochensage2 小时前
CSP信奥赛C++常用系统函数汇总
c++·信奥
mochensage2 小时前
C++信息学竞赛中常用函数的一般用法
java·c++·算法
fpcc2 小时前
跟我学c++中级篇——多线程中的文件处理
c++
chengooooooo2 小时前
leetcode Top100 238. 除自身以外数组的乘积|数组系列
算法·leetcode
GUIQU.2 小时前
【每日一题 | 2025年6.2 ~ 6.8】第16届蓝桥杯部分偏简单题
算法·蓝桥杯·每日一题