1.冒泡排序:
#include <iostream>
using namespace std;
void ipanel_bubble_sort(int *arr, int sz)
{
for(int j = (sz -1) ;j>0;j--)
{
for(int i = 0;i < j; i++)
{
if(arr[i] > arr[i+1])
{
int tmp;
tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
}
}
}
}
int main()
{
int arr[] = {7, 1, 3, 4, 8, 5};
int index = 0;
int size = sizeof(arr)/ sizeof(int);
ipanel_bubble_sort(arr, size);
for(;index< size;index++)
cout << arr[index] << " " ;
}
2.实现函数:unsigned int ipanel_parse_ip_addr(const char *ip);
功能:解析IPv4地址
测试用例:unsigned int ip_addr = ipanel_parse_ip_addr("192.168.10.1");
printf("%x\n", ip_addr);
要求输出:c0a80a01
解法一:
#include <iostream>
using namespace std;
int my_atoi(char *str)
{
int n = 0;//区分正负数的flag变量
int sum = 0;
if (*str == '-') {
str++;
n = 1;
}
while ((*str) <= '9' && (*str) >= '0') {
sum = sum * 10 + ((int) (*str) - 48);
str++;
}
if (n == 1) {
sum = -sum;//如果是负数的话,先按正数处理,再取反
}
return sum;
}
unsigned int ipanel_parse_ip_addr(const char *ip)
{
char arr[20];
int tmp[4];
int i = 0;
int j = 0;
char *p = const_cast<char *>(ip);
//用'.'来划分字符串,并将其转化成整数存入数组tmp[]
while (*p != '\0') {
if (*p != '.') {
arr[i] = *p;
++i;
}
else {
arr[i] = '\0';
tmp[j] = my_atoi(arr);
++j;
i = 0;
}
++p;
}
arr[i] = '\0';
tmp[j] = my_atoi(arr);
//printf("%x%x%02x%02x\n", tmp[0],tmp[1],tmp[2],tmp[3] );
//算出点分十进制表示的整数
unsigned int sum = 0;
for(int k = 0; k < 4; ++k )
{
sum = sum * 256 + tmp[k];//有点类似于秦九韶算法
}
//unsigned int sum = tmp[3] + (tmp[2] * 256) + (tmp[1] * 256 * 256) + (tmp[0] * 256 * 256 * 256);
return sum;
//cout << tmp[0] << endl;
//cout << tmp[1] << endl;
//cout << tmp[2] << endl;
//cout << tmp[3] << endl;
}
int main()
{
unsigned int ip_addr = ipanel_parse_ip_addr("192.168.10.1");
//将返回的无符号整型数,按照十六进制输出
printf("%x\n", ip_addr);
}
解法二:
#include <iostream>
#include <stdio.h>
#include <assert.h>
using namespace std;
unsigned int ipanel_parse_ip_addr(const char *ip)
{
assert(ip != NULL);
unsigned int ip_addr = 0;
unsigned int tmp = 0;
while (*ip != '\0')
{
if(*ip != '.' )
{
tmp = tmp * 10 + (*ip - '0');
++ip;
}
else
{
ip_addr |= tmp;
ip_addr <<= 8;
++ip;
tmp = 0;
}
}
ip_addr |= tmp;
return ip_addr;
}
int main()
{
unsigned int ip_addr = ipanel_parse_ip_addr("192.168.0.200");
printf("%x\n", ip_addr);
return 0;
}
3.统计一个十进制转二进制书里面,有多少个“1”
#include<iostream>
#include<stdlib.h>
using namespace std;
func(int x)
{
int countx = 0;
while(x)
{
countx++;
x = x&(x-1);
}
return countx;
}
int main()
{
int result = func(65530);
cout<< result << endl;
}
4.数字转中文:
#include <iostream>
#include <string>
using namespace std;
string unite[5]={"","十","百","千","万"}; //单位
string num[10]={"零","一","二","三","四","五","六","七","八","九"}; //一个汉字占两个字节,另外再加一个'\0'字符.
string func(int a)
{
int flag=0,tmp;
string strtmp;
string result;
int atemp=a; //设定a的临时存储值,防止每次进入循环都进行末尾零的判断
while(a!=0)
{
while(atemp%10==0)
{
flag++;
atemp/=10;
a/=10;
}
tmp=a%10;
if(tmp!=0)
strtmp=num[tmp]+unite[flag];
else if(tmp==0)
strtmp="零";
result=strtmp+result;
a/=10;
flag++;
}
return result;
}
string convert(int a)
{
string result,temp;
if(a<100000)
result=func(a);
else
{
temp=func(a/10000);
result=temp+ "万"+ func(a-a/10000*10000);
}
return result;
}
int main()
{
int num;
string numstring;
cout<<"please input the num: ";
cin>>num;
numstring=convert(num);
cout<<"the convert result is: "<<numstring<<endl;
}
5.如何拿出一个整数的各个位上的数字:
#include <iostream>
using namespace std;
int main()
{
int a = 1234;
while (a)
{
int tmp = a % 10;
a /= 10;
cout<< tmp << " ";
}
}