1.封装一个类,实现对一个数求累和阶乘质数
- #include
-
- 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="<
- }
- void product(){
- cout<<mproduct(a)<
- }
- void primeNumber(){
-
- if(a%j==0){
- continue;
- }else{
- cout<
" "; - }
- }
-
- cout<
- }
- };
-
- int main()
- {
- number num;
- num.set(12);
- num.sum();
- num.product();
- num.primeNumber();
- return 0;
- }

2.封装两个类,实现字符串交错输出
- #include
-
- using namespace std;
- class A{
- string str;
- int a;
- public:
- A():str("abcdefghijklmnopqrstuvwxyz"),a(0){}
- void mygetchar(){
- cout <
at(a)<<" "; - a=(a+1)%26;
- }
- };
- class B{
- string str;
- int a;
- public:
- B():str("1234567890"),a(0){}
- void mygetchar(){
- cout<
at(a)<<" "; - a=(a+1)%10;
- }
- };
-
- int main()
- {
- A a;
- B b;
- int i=0;
- int len;
- cin>>len;
- while(i++
- a.mygetchar();
- b.mygetchar();
- }
- return 0;
- }

3. 输入字符串,将字母和数字分别存入两个不同的类的对象,然后输出。
- #include
- #include
- #include
- using namespace std;
- class A{
- string a;
-
- public:
- A(){
-
- }
- void myinsert(char c){
- a+=c;
- }
- void show(){
- }
- };
- class B{
- string b;
-
- public:
- B(){}
- void myinsert(char c){
- b+=c;
- }
- void show(){
- cout<
- }
- public:
-
- };
-
- int main()
- {
- string str;
- A A;
- B B;
- //char a[128];
- cin>>str;
- cout<<"字符串输入成功"<
- for(unsigned int i=0;i
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;
- }
-
相关阅读:
python中的模块与包
lintcode 3605 · 二维网格偏移 【数组相关,模拟即可】
基于火鹰优化算法的函数寻优算法
通过Power Platform自定义D365CE业务需求 - 1. Microsoft Power Apps 简介
Leetcode链表相关题目
hive和hbase的使用问题
GBASE 8A v953报错集锦43--使用 gcdump 指定参数 ignore-table 不导出指定的 表或视图
ESP32基础应用之使用两个ESP32通过阿里云物联网平台实现相互通信
6.S081 附加Lab4 从源代码看进程退出——exit,wait,kill
C++: multiple and virtual inheritance under the hood
-
原文地址:https://blog.csdn.net/m0_60953264/article/details/137395615
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU