五一 作业

devtools/2024/10/18 14:17:49/

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

 

2. 已知C风格的字符串,完成对字符串通过下标访问时的异常处理机制(越界访问)


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

 

 


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

 

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


http://www.ppmy.cn/devtools/36370.html

相关文章

Raft共识算法图二解释

下面是有关Raft协议中不同术语和概念的翻译及解释&#xff1a; 术语和概念&#xff1a; 任期号&#xff08;term number&#xff09;&#xff1a;用来区分不同的leader。前一个日志槽位的信息&#xff08;prelogIndex&#xff09;&#xff1a;这是前一个日志条目的索引&#…

后端如何处理接口的重复调用

首先是&#xff0c;原理在请求接口之前&#xff0c;使用过滤器拦截数据&#xff0c;来进行判断两次数据是否一致。 1.自定义注解 2.创建一个Handler处理器 3.RepeatSubmitInterceptor的实现类 4.过滤器的配置

翻译《The Old New Thing》- Does Windows have a limit of 2000 threads per process?

Does Windows have a limit of 2000 threads per process? - The Old New Thing (microsoft.com)https://devblogs.microsoft.com/oldnewthing/20050729-14/?p34773 Raymond Chen 2005年07月29日 Windows 是否有一个每个进程2000线程的限制&#xff1f; 简要 文章解释了在 W…

LeetCode 226.翻转二叉树

题目描述 给你一棵二叉树的根节点 root &#xff0c;翻转这棵二叉树&#xff0c;并返回其根节点。 示例 1&#xff1a; 输入&#xff1a;root [4,2,7,1,3,6,9] 输出&#xff1a;[4,7,2,9,6,3,1]示例 2&#xff1a; 输入&#xff1a;root [2,1,3] 输出&#xff1a;[2,3,1]示例…

VBA快速删除Excel工作表

Excel处理数据时,经常需要删除部分工作表,像这样 工作中一个工作薄中十几张甚至几十张工作表是常有的事,手动一张张去删,费时费力,VBA代码可以帮你快速搞定,这就需要非常简单的代码 。 1、如果只留下一张名为“Sheet1”的工作表,其余的全删除,代码如下: For Each She…

fero - yolo - mamba:基于选择性状态空间的面部表情检测与分类

fero - yolo - mamba:基于选择性状态空间的面部表情检测与分类 摘要IntroductionRelated work FER-YOLO-Mamba: Facial Expression Detection and Classification Based on Selective State Space 摘要 面部表情识别&#xff08;FER&#xff09;在理解人类情绪线索方面起着关键…

免备案香港主机会影响网站收录?

免备案香港主机会影响网站收录?前几天遇到一个做电子商务的朋友说到这个使用免备案香港主机的完整会不会影响网站的收录问题&#xff0c;这个问题也是站长关注较多的问题之一。小编查阅了百度官方规则说明&#xff0c;应该属于比较全面的。下面小编给大家介绍一下使用免备案香…

每日OJ题_贪心算法三③_力扣45. 跳跃游戏 II(dp解法+贪心解法)

目录 力扣45. 跳跃游戏 II 解析代码1_动态规划 解析代码2_贪心 力扣45. 跳跃游戏 II 45. 跳跃游戏 II 难度 中等 给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]。 每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说&#xff0c;如果你在 num…