从字符串中取出素数
来源:互联网 发布:淘宝发货地址不一样 编辑:程序博客网 时间:2024/05/16 04:33
/*试建立一个类 Str,把一个字符串中连续的数字字符看成一个整数,若该数是一个素数,则对其求和。如 char s[]=“1ad34be5c67dw11egg77fuy5”中,这样的整数有 1、34、5、67、11、77、5,其中素数有 5、67、11、5,其和为 5+67+11+5=88。请编写程序,实现这样的运算。具体要求如下:(1)私有数据成员 char s[100]; 存放需要处理的字符串。 int b[20]; 存放分离出的整数。 int ss;存放素数之和。 int count; 分离出整数的个数(2)公有成员函数 Str(char *p=0);构造函数,为 s 数组、私有数据成员 ss、count置0. int pre(int n);测试 n 为素数。 void SUM(); 分离字符串中连续的数字为整数,并求出素数和。 void show(); 输出字符串、分离出整数和素数之和。(3)在主函数中完成对该类的测试。输入一个字符串到字符数组 str 中,定义一个 Str 类的对象 test,用 str 初始化对象 test,调用成员函数完成运算。*/#include<iostream>using namespace std;class Str{private: char s[100]; int b[20]; int ss; int count;public: Str(char *); int pre(int); void SUM(); void show();};Str::Str(char *p = 0){ int xunhuan_bianliang = 0; while (*p) { s[xunhuan_bianliang] = *p; p++; xunhuan_bianliang++; } s[xunhuan_bianliang] = '\0'; ss = 0; count = 0;}int Str::pre(int n){ if (n == 1 || n == 0) return 0; for (int i = 2; i < n; i++) { if (n%i == 0) return 0; } return 1;}void Str::SUM(){ char *temp1 = s; while (*temp1) { ss = 0; //尤其重要,要不然出错 char *temp2 = temp1; while (*temp2 <= '9'&&*temp2 >= '0') { temp2++; } //把数字字符串赋值给数组 char *ptr = temp1; //这里要尤其注意,给后面留路子 while (ptr != temp2) { ss *= 10; ss += (*ptr - '0'); ptr++; } if (this->pre(ss)) { b[count] = ss; count++; ss = 0; } //防止多位数 if (temp1 == temp2) temp1++; else temp1 = temp2; }}void Str::show(){ cout << "字符串为:" << s << endl; cout << "分离出的整数为:"; for (int i = 0; i < count; i++) { cout << b[i] << '\t'; }}int main(){ char aa[] = "1ad34be5c67dw11egg77fuy5"; Str str(aa); str.SUM(); str.show(); system("pause"); return 0;}
阅读全文
0 0
- 从字符串中取出素数
- 从字符串中取出数字
- 从一组字符串中取出数字
- 如何从CString类型的字符串中取出数字
- 实例二、从字符串中取出所有整数
- 从字符串中取出指定位置的字符
- 实现从字符串中取出数字(atoi)
- Qt正则表达式从字符串中取出手机号码
- c 语言 从字符串中取出一串单词并倒序
- 从有“,”分隔的字符串中取出被分隔的字符串并存储
- 从一个文件中取出一段字符串,与原有的字符串进行连接
- 利用正则从一个乱七八糟的字符串中取出我们要的字符串
- 从PEB中取出MODULENAME
- 从PEB中取出MODULENAME
- 从JDBC中取出数据
- 从URL 中取出域名
- 从数据库中取出数据
- msSql_函数_从字符串取出数字
- 423C
- mysql
- 活用帮助命令决定你的高度
- python : caesar cipher
- 史诗级崩盘。
- 从字符串中取出素数
- wps演示中在任务栏中显示所有窗口怎么设置
- 吃透Android计算器代码(2)
- 数据结构——二
- Meteor常用的命令
- QT中在QLabel显示图片并且利用鼠标点击画线
- Animation动画
- 解释性语言和编译性语言
- 通配符映射