清明节作业

#include <iostream>

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="<<sun<<endl;

}

void product(){

cout<<mproduct(a)<<endl;

}

void primeNumber(){

for(int j=1;j<a;j++){

if(a%j==0){

continue;

}else{

cout<<j<<" ";

}

}

cout<<endl;

}

};

int main()

{

number num;

num.set(12);

num.sum();

num.product();

num.primeNumber();

return 0;

}

实现字符串交错输出

#include <iostream>

using namespace std;

class A

{

string str;

int a;

public:

A():str("abcdefghijklmnopqrstuvwxyz"),a(0){}

void mygetchar()

{

cout <<str.at(a)<<" ";

a=(a+1)%26;

}

};

class B{

string str;

int a;

public:

B():str("1234567890"),a(0){}

void mygetchar()

{

cout<<str.at(a)<<" ";

a=(a+1)%10;

}

};

int main()

{

A a;

B b;

int i=0;

int len;

cin>>len;

while(i++<len)

{

a.mygetchar();

b.mygetchar();

}

return 0;

}

将字母和数字分别存入两个不同的类的对象,然后输出。

#include <iostream>

#include<cstring>

#include<stdio.h>

using namespace std;

class A

{

string a;

public:

A()

{

}

void myinsert(char c)

{

a+=c;

}

void show(){

cout<<a<<endl;

}

};

class B

{

string b;

public:

B(){}

void myinsert(char c)

{

b+=c;

}

void show(){

cout<<b<<endl;

}

public:

};

int main()

{

string str;

A A;

B B;

//char a[128];

cin>>str;

cout<<"字符串输入成功"<<endl;

for(unsigned int i=0;i<str.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;

}

相关推荐
豆沙沙包?7 分钟前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头26 分钟前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
liuyang-neu41 分钟前
java内存模型JMM
java·开发语言
int型码农1 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
利刃大大1 小时前
【在线五子棋对战】二、websocket && 服务器搭建
服务器·c++·websocket·网络协议·项目
UFIT1 小时前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面1 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked931 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
怀旧,1 小时前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法
积极向上的向日葵1 小时前
有效的括号题解
数据结构·算法·