计算机程序设计c++ 8-6:数组指针相关应用

这篇博客涵盖了三个主要主题:数组反序、字符串排序和二进制IP地址转点分十进制。首先,展示了如何使用C++实现数组元素的反向排列。接着,详细介绍了字符串排序的实现,包括自定义比较和交换函数。最后,讨论了如何检查32位二进制IP地址的合法性,并将其转换为点分十进制形式。
摘要由CSDN通过智能技术生成

反序输出数组

在这里插入图片描述

  • 实现
#include<iostream>
#include<stdlib.h>
using namespace std;

// 反序存放各数组元素
void invert(double *x, int n)
{
	double t, *i, *j;
	i = x;
	j = x + n -1;
	while(i<j)
	{
		t = *i;
		*i = *j;
		*j = t;
		i++;
		j--;
	}
}

int main()
{
	double a[5] = {1,2,3,4,5.0};
	// double *p = new double[5];  //动态数组
	cout <<"原始数组:";
	for(int i=0; i<5; i++)
	{
		cout << a[i] << " ";
	}
	cout<< endl;
	invert(a, 15);
	cout <<"反序后数组:";
	for(int i=0; i<5; i++)
	{
		cout << a[i] << " ";
	}
	cout<< endl;
	return 0;
}

字符串排序

在这里插入图片描述

  • 实现
#include <iostream>
#include <cstring>

using namespace std;

void sort(char (*p)[10], int n)
{
	for(int i=0; i<n-2; i++)
	{
		for(int j=i+1; j<n; j++)
		{
			char t[10];
			if(strcmp(p[i], p[j]) > 0)
			{
				strcpy(t, p[i]);
				strcpy(p[i], p[j]);
				strcpy(p[j], t);
			}
		}
	}
}

int main()
{
	char str[][10] = {"zhang", "wang", "wen", "xu", "li", "zhou"};
	// char (*s)[10] = new char[n][10];  // 定义动态数组
	sort(str, 6);
	for(int i=0; i<6; i++)
	{
		cout << str[i] << "\t";
	}
	return 0;
}

自己实现比较函数与复制函数,集成交换函数以及显示字符串

char *mystrcpy(char *s1, char *s2)  // copy s2 into s1
{
	while(*s2 != 0)
	{
		*s1++ = *s2++;
	}
	*s1 = '\0';
}

int mystrcmp(char *s1, char *s2)
{
	while(*s1 == *s2 && ! *s1!= 0 && *s2 != 0)
	{
		s1++;
		s2++;
	}
	return *s1 - *s2;  // 正值,前一个大,负值,后一个大
}

void swap(char *p, char *q)
{
	char t[10];
	mystrcpy(t, p);
	mystrcpy(p, q);
	mystrcpy(q, t);
}

void show(char (*s)[10], int n)
{
	for(int i=0; i<n-1; i++)
	{
		cout << s[i] << " ";
	}
	cout << s[n-1] << endl;
}

二进制ip地址转化为点分十进制形式

在这里插入图片描述

  • 实现:
    • 检测字符串中是否有01之外的字符
    • 二进制数 --> 十进制数
#include <iostream>
#include <cstring>

using namespace std;

// check only contain 0/1 in str
bool check(char *str)
{
	for(int i=0; i<32; i++)
	{
		if(str[i] != '1' && str[i] != '2')
		{
			return false;
		}
	}
	return true;
}


// 32位二进制数转化为十进制数
// 1101B = (((0*2+1)*2+1)*2+0)*2+1 = 13
int trans(char *Str)
{
	int n=0, i;
	for(i=0; i<8; i++)
	{
		if(str[i] =='1')
		{
			n = n*2 + 1;
		}
		else
		{
			n = n*2;
		}
	}
	return n;
}


int main()
{
	char IP[33];
	cout << "32位二进制IP地址?" << endl;
	cin >> IP;
	if(strlen(IP) != 32)
	{
		cout << "IP 长度应该是32位" << endl;
	}
	else
	{
		if(!check(IP))
		{
			cout << "字符串中有0/1之外的字符,非正确IP地址." << endl;
		}
		else
		{
			cout << "IP地址对应的点分十进制数写法:" << endl;
			cout << trans(IP) << "." << trans(IP+8) << "." << trans(IP+16) << "." << trans(IP+24) << endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uncle_ll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值