行程编码(atoi函数)
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void jiema(string s)
{
cout << "解码之后为:" << endl;
for(int i = ; i < s.size(); i++)
{
if(s[i + ] == '*')
{
int j = i + ;
int len = ;
while(s[j + ] >= && s[j + ] <= )
{
len++;
j = j + ;
}
string temp = s.substr(i + , len);
int num = atoi(temp.c_str());
for(int k = ; k < num; k++)
{
cout << s[i];
}
i = j;
}
else
{
cout << s[i]; }
}
}
void bianma(string s)
{
vector<char>A;
int p = ;
int j = ;
for(; j < s.size();)
{
while(s[j] == s[j + ] && j < s.size())
{
p++;
j = j + ;
}
if(p >= )
{
A.push_back(s[j]);
A.push_back('*');
A.push_back(p);
p = ;
j = j + ;
}
else if(p < )
{
for(int k = ; k < p; k++)
{
A.push_back(s[j]);
}
p = ;
j = j + ;
}
}
cout << "编码之后为:" << endl;
for(int b = ; b < A.size(); b++)
{
if(A[b] >= && A[b] <= )
cout << int(A[b]);
else
cout << A[b];
}
cout << endl;
cout << "压缩比为:" << endl;
double y = double(A.size()) / s.size();
cout << y << endl;
}
void main()
{
cout << "请输入代码:①代码中含有数字表示需要解码解压缩②代码中不含数字表示需要编码压缩" << endl;
string s;
for(; cin >> s;)
{
int i = ;
for(; i < s.size(); i++)
{
if(s[i] >= && s[i] <= ) //解码
{
jiema(s);
break;
}
}
if(i == s.size())
{
bianma(s);
}
}
}
行程编码(atoi函数)的更多相关文章
- atoi()函数
原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...
- C语言itoa()函数和atoi()函数详解(整数转字符C实现)
1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...
- 题目1003:A+B ---c_str(),atoi()函数的使用;remove , erase函数的使用
#include<stdio.h> #include<stdlib.h> int sw(char *a){ ,c=; while(a[i]){ ') c=c*+a[i]-'; ...
- atoi()函数的实现
atoi()函数的功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结果返回( ...
- C语言itoa()函数和atoi()函数详解(整数转字符)
http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...
- C语言itoa函数和atoi 函数
C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h> ...
- 【C语言】模拟实现atoi函数
atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- atoi()函数(转载)
atoi()函数 原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数np ...
- 源码实现 --> atoi函数实现
atoi函数实现 atoi()函数的功能是将一个字符串转换为一个整型数值. 例如“12345”,转换之后的数值为12345,“-0123”转换之后为-123. #include <stdio.h ...
随机推荐
- jQuery代码节选(css)
CSS 1.css<p class="p1">1</p> $("p").css("color");获取css属性值$ ...
- JS中的柯里化与反柯里化
先占个位 看了一天折资料,感觉清楚多了
- js 的一些知识 摘自http://img0.pconline.com.cn/Pc_intranet/1105/13/313647_7.pdf
Js 问题分析--js 影响页面性能现状分析:问题陈述分析问题:抽象问题根源,通过实例或推理证明问题的严重性问题引申:以现有问题为点开始扩散,这将导致其它什么问题,或同一类型的问题问题总结:从分散开始 ...
- java接口中定义成员变量
//抽象类中可以定义如下成员变量:public abstract class People { public String name; public int age; public abstract ...
- SQL基础--同义词
同义词的概念: 同义词是Oracle对象的别名,使用同义词访问相同的对象 可以为表.视图.存储过程.函数或另一同义词等对象创建同义词 方便访问其它用户的对象,隐藏了对象的身份 缩短对象名字的长度 同义 ...
- 【数据结构】平衡二叉树—AVL树
(百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...
- Mac安装Bower
1.安装bower,得首先安装node: brew install npm //npm是nodejs的程序包管理器,如果安装过nodejs,可忽略此步. 2.安装Git(因为需要从Git仓库获取一些代 ...
- Atitit 边缘检测原理attilax总结
Atitit 边缘检测原理attilax总结 1. 边缘检测的概念1 1.1. 边缘检测的用途1 2. 边缘检测方法分类1 3. 边缘检测的基本方法2 3.1. Roberts边缘检测算子2 3.2. ...
- Atitit wsdl的原理attilax总结
Atitit wsdl的原理attilax总结 1.1. 在 W3C 的 WSDL 发展史1 1.2. 获取wsdl,可能需要url后面加wsdl,也可能直接url1 1.3. Wsdl的作用2 1. ...
- iOS----自定义UIView,绘制一个UIView
绘制一个UIVIew最灵活的方式就是由它自己完成绘制.实际上你不是绘制一个UIView,你只是子类化了UIView并赋予子类绘制自己的能力.当一个UIVIew需要执行绘图操作的时,drawRect:方 ...