字符串IP地址判断
判断字符串是不是有效的IP地址 。注:类似于 01.1.1.1, 1.02.3.4 子段以 0开头不合法;子段为单个0 认为是合法IP,如0.0.0.0 算合法IP ;
#include <iostream>
#include <ctype.h>
using namespace std;
bool isIPAddressValid(const char* pszIPAddr) {
if (pszIPAddr == NULL)
return false;
const char* a = pszIPAddr;
int begin, end, len;
len = strlen(pszIPAddr);
for (begin = 0; begin < len; ++begin) {
if (a[begin] != ' ')
break;
}
for (end = len - 1; end >= 0; --end) {
if (a[end] != ' ') {
break;
}
}
if (begin >= end || !isdigit(a[begin]) || !isdigit(a[end]))
return false;
struct state {
char currrent;
char previous;
int charSeqNum;
int pointNum;
} st = { 0, 0, 0, 0 };
int i, j, num;
for (i = begin; i <= end; ++i) {
st.previous = st.currrent;
st.currrent = a[i];
if (st.currrent == '.') {
if (st.previous == '.')
return false;
st.pointNum++;
if (st.pointNum > 3)
return false;
num = a[i - st.charSeqNum] - '0';
for (j = 1; j < st.charSeqNum; ++j) {
num = num * 10 + a[i - st.charSeqNum + j] - '0';
}
if (num > 255) {
return false;
}
st.charSeqNum = 0;
} else if (isdigit(st.currrent)) {
st.charSeqNum++;
if (st.previous == '0' && st.charSeqNum == 2) {
return false;
}
if (st.charSeqNum > 3) {
return false;
}
if (i == end) {
num = a[i + 1 - st.charSeqNum] - '0';
for (j = 1; j < st.charSeqNum; ++j) {
num = num * 10 + a[i + 1 - st.charSeqNum + j] - '0';
}
if (num > 255) {
return false;
}
}
} else {
return false;
}
}
if (st.pointNum != 3)
return false;
return true;
}
int main() {
const char* a = " 110.1.210.1 ";
bool b = isIPAddressValid(a);
cout << b;
}
查找兄弟单词
编程实现一个可存储若干个单词的字典,可以增加,查找,删除等。
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
set<string> dict;
int AddOneWord(char* Word) {
string a = Word;
if (dict.insert(a).second)
return 0;
else
return -1;
}
bool isBro(string a, string b) {
sort(a.begin(), a.end());
sort(b.begin(), b.end());
return a == b;
}
int FindSimilarWordNum(char* Word) {
string a = Word;
set<string>::iterator it;
int count = 0;
for (it = dict.begin(); it != dict.end(); ++it) {
if (a != *it && isBro(a, *it))
++count;
}
return count;
}
int FindOneSimilarWord(char* Word, int Seq, char* SimilarWord) {
string a = Word;
vector<string> ve;
set<string>::iterator it;
for (it = dict.begin(); it != dict.end(); ++it) {
if (a != *it && isBro(a, *it)) {
ve.push_back(*it);
}
}
if (ve.size() == 0 || Seq > ve.size()) {
*SimilarWord = '\0';
return -1;
} else {
ve[Seq - 1].copy(SimilarWord, ve[Seq - 1].length(), 0);
return 0;
}
}
void ClearAllWords(void) {
dict.clear();
}
int main() {
char *Test_Word[7] = { "mock", "aabc", "abc", "ckom", "bcaa", "abca", };
AddOneWord(Test_Word[0]);
AddOneWord(Test_Word[1]);
AddOneWord(Test_Word[2]);
AddOneWord(Test_Word[3]);
AddOneWord(Test_Word[4]);
AddOneWord(Test_Word[5]);
int a = FindSimilarWordNum(Test_Word[0]);
cout << a << endl;
char *ExpectWord = {"bcaa"};
char SimilarWord[51] = {'\0'};
int Seq = 2;
int b = FindOneSimilarWord (Test_Word[1], Seq, SimilarWord);
cout << b << endl;
cout << SimilarWord;
}
整形字符串排序
一个字符串内有很多正整数,按照每个正整数的后三位数字组成的整数进行从小到大排序,不足三位,则按照实际位数组成的整数进行比较。
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
bool comp(const int &a, const int &b) {
return a % 1000 < b % 1000;
}
int find_string(const char* input_string, int serial_number,
int output_string_max_length, char* output_string) {
if (input_string == 0 || !*input_string) {
*output_string = '\0';
return -1;
}
vector<int> nums;
int n = 0;
const char *p = input_string;
while (*p) {
if (*p == ' ') {
nums.push_back(n);
n = 0;
++p;
continue;
}
n = n * 10 + *p - '0';
++p;
}
nums.push_back(n);
sort(nums.begin(), nums.end(), comp);
if(serial_number > nums.size()){
*output_string = '\0';
return -1;
}
int a = nums[serial_number - 1];
int k = 0, tt = a;
while(tt != 0){
++k;
tt /= 10;
}
if(output_string_max_length <= k){
*output_string = '\0';
return -1;
}
sprintf(output_string, "%d", a);
return 0;
}
int main() {
const char *in_str = "1223 22 3232 2016";
char out_str[5];
find_string(in_str, 3, sizeof(out_str), out_str);
cout << out_str; // 1223
}
在字符串中找出最长连续的数字串
请一个在字符串中找出连续最长数字串,不要求数字顺序,并把这个串长度返回,若存在长度相同的连续数字串,返回最后一个连续数字串。
#include <iostream>
unsigned int Continumax(char** pOutputstr, char* intputstr){
if( intputstr == NULL ){
*pOutputstr = (char*)malloc(2);
**pOutputstr = '\0';
return 0;
}
*pOutputstr = (char*)malloc(strlen(intputstr)+1);
char *p = intputstr;
unsigned int count = 0;
unsigned int max = 0;
char *pcur,*pre;
pcur = p;
pre = p;
while(*p){
if( isdigit(*p) ){
pcur = p;
while( isdigit(*p) ){
++count;
++p;
}
}
if(count >= max){
max = count;
pre = pcur;
}
count = 0;
++p;
}
if(max == 0) {
**pOutputstr = '\0';
} else {
char *pt = *pOutputstr;
unsigned int i = 0;
for(; i < max; ++i){
*pt++ = *pre++;
}
*pt = '\0';
}
return max;
}
int main(){
char* intputstr = "abc12cab12345";
char* output;
Continumax(&output, intputstr);
std::cout << output; // 12345
}
Fibonacci 数列计算和转换
求解扩展Fibanacci 的 第n项和 前n项和,扩展Fibanacci 的前两项由输入参数指定 。
#include <iostream>
using namespace std;
int GetExtFibonacci(int first, int second, int num) {
int re, i;
if (num == 1)
return first;
if (num == 2)
return second;
for (i = 3; i <= num; ++i) {
re = first + second;
first = second;
second = re;
}
return re;
}
int CalcTotalValueOfExtFibonacci(int first, int second, int num) {
int re, i;
if (num == 1)
return first;
if (num == 2)
return first + second;
int count = first + second;
for (i = 3; i <= num; ++i) {
re = first + second;
first = second;
second = re;
count += re;
}
return count;
}
int main() {
cout<< GetExtFibonacci(1, 1, 5);
cout<<"\n";
cout<< CalcTotalValueOfExtFibonacci(1,1,5);
}
二维数组列排序
对二维数组以行为单位进行排序。排序根据给定列的元素的大小进行,如本列相同,则按照后面的一列。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
using namespace std;
void swapRow(int * pArray, unsigned int col, unsigned int rowA,
unsigned int rowB) {
for (unsigned int i = 0; i < col; ++i) {
int temp = pArray[rowA * col + i];
pArray[rowA * col + i] = pArray[rowB * col + i];
pArray[rowB * col + i] = temp;
}
}
int cal(int * pArray, unsigned int i, unsigned int col, unsigned int row) {
int re = 0;
--i;
for (; i < col; ++i) {
re = re * 10 + pArray[col * row + i];
}
return re;
}
void RangeArray(int * pArray, unsigned int m, unsigned int n, unsigned int i) {
int rowA, rowB;
unsigned int k, j;
for (k = 0; k < m - 1; ++k) {
for (j = 0; j < m - 1 - k; ++j) {
rowA = cal(pArray, i, n, j);
rowB = cal(pArray, i, n, j + 1);
if (rowA > rowB) {
swapRow(pArray, n, j, j + 1);
}
}
}
}
int main() {
int pArray[3][2] = { 3, 1, 5, 4, 2, 2 };
RangeArray(&pArray[0][0], 3, 2, 2);
int *p = &pArray[0][0];
for (int i = 0; i < 6; ++i) {
cout << p[i] << " "; // 3 1 2 2 5 4
}
}
迭代器(多维数组切片)
设计多维数迭代器,使其可以迭代指定切片内的元素。
#include <stdlib.h>
//-------------------------------------
#define MAX_COORD_NUM 10
typedef struct {
unsigned long start; //起始下标
unsigned long end; //结束下标
} CoordSlice;
typedef struct {
int* first_element_of_orignal_array; //原始数组的第一个元素指针
unsigned long coord_num; //维度个数,最大取值为MAX_COORD_NUM,值为10
//原始数组从左到右各维度下标属性表示如下(从0到coord_num-1):
unsigned long coord_spec[MAX_COORD_NUM]; //原始数组每个维度下标的最大取值
CoordSlice coord_slice[MAX_COORD_NUM]; //切片在每个维度上的起止下标
} SliceDesc;
typedef struct {
int *first;
unsigned long coord_num; //维度个数
CoordSlice coord_slice[MAX_COORD_NUM]; //切片在每个维度上的起止下标
unsigned long current[MAX_COORD_NUM]; //当前坐标位置
unsigned long size_dim[MAX_COORD_NUM]; //每个维度偏移大小
} SliceIterator;
//-----------------------------------------
int SliceIteratorInit(const SliceDesc* slice_desc,
SliceIterator* slice_iterator) {
if (slice_desc == NULL || slice_iterator == NULL)
return -1;
if (slice_desc->first_element_of_orignal_array == NULL)
return -1;
for (unsigned long i = 0; i < MAX_COORD_NUM; ++i) {
if (slice_desc->coord_slice[i].start > slice_desc->coord_slice[i].end)
return -1;
if (slice_desc->coord_slice[i].end > slice_desc->coord_spec[i])
return -1;
}
slice_iterator->first = slice_desc->first_element_of_orignal_array;
slice_iterator->coord_num = slice_desc->coord_num;
for (unsigned long i = 0; i < MAX_COORD_NUM; ++i) {
slice_iterator->coord_slice[i] = slice_desc->coord_slice[i];
slice_iterator->current[i] = slice_desc->coord_slice[i].start;
}
// 从左到右,从高维到低维
for (unsigned long i = 0; i < slice_desc->coord_num; ++i) {
slice_iterator->size_dim[i] = 1;
for (unsigned long j = i + 1; j < slice_desc->coord_num; ++j) {
slice_iterator->size_dim[i] *= (slice_desc->coord_spec[j] + 1);
}
}
return 0;
}
int* SliceIteratorGetCurr(const SliceIterator* slice_iterator) {
if (SliceIteratorEnd(slice_iterator))
return NULL;
int count = 0;
for (long i = (long) slice_iterator->coord_num - 1; i >= 0; --i) {
count += slice_iterator->size_dim[i] * slice_iterator->current[i];
}
return slice_iterator->first + count;
}
void SliceIteratorNext(SliceIterator* slice_iterator) {
slice_iterator->current[slice_iterator->coord_num - 1]++;
for (long i = (long) slice_iterator->coord_num - 1; i > 0; --i) {
if (slice_iterator->current[i] > slice_iterator->coord_slice[i].end) {
slice_iterator->current[i - 1]++;
slice_iterator->current[i] = slice_iterator->coord_slice[i].start;
}
}
return;
}
int SliceIteratorEnd(const SliceIterator* slice_iterator) {
if (slice_iterator->current[0] > slice_iterator->coord_slice[0].end)
return 1;
return 0;
}
int CopySlice(const SliceDesc* slice_desc, int** first_element_of_slice_array,
unsigned long* slice_element_num) {
if (slice_desc == NULL)
return -1;
if (slice_desc->first_element_of_orignal_array == NULL)
return -1;
for (unsigned long i = 0; i < MAX_COORD_NUM; ++i) {
if (slice_desc->coord_slice[i].start > slice_desc->coord_slice[i].end)
return -1;
if (slice_desc->coord_slice[i].end > slice_desc->coord_spec[i])
return -1;
}
SliceIterator it;
SliceIteratorInit(slice_desc, &it);
long len = 0;
while (SliceIteratorEnd(&it) != 1) {
++len;
SliceIteratorNext(&it);
}
*slice_element_num = len;
*first_element_of_slice_array = (int*) malloc(sizeof(int) * len);
SliceIteratorInit(slice_desc, &it);
int *q = *first_element_of_slice_array;
while (SliceIteratorEnd(&it) != 1) {
*q++ = *SliceIteratorGetCurr(&it);
SliceIteratorNext(&it);
}
return 0;
}