每天学习一点编程(10)(字符串的合并排序处理)

将输入的两个字符串合并。

对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。这里的下标意思是字符在字符串中的位置。

对排训后的字符串进行操作,如果字符为‘0’——‘9’或者‘A’——‘F’或者‘a’——‘f’,则对他们所代表的16进制的数进行BIT倒序的操作,并转换为相应的大写字符。如字符为‘4’,为0100b,则翻转后为0010b,也就是2。转换后的字符为‘2’; 如字符为‘7’,为0111b,则翻转后为1110b,也就是e。转换后的字符为大写‘E’。

举例:输入str1为"dec",str2为"fab",合并为“decfab”,分别对“dca”和“efb”进行排序,排序后为“abcedf”,转换后为“5D37BF”

/*
功能: 字符串处理输入:两个字符串,需要异常处理输出:合并处理后的字符串,具体要求参考文档
返回: 无
*/

void ProcessString(char* str1,char *str2,char * strOutput)
{

}

#include <iostream>
#include <string>
#define N 10

using namespace std;
void ProcessString(char* str1,char *str2,char * strOutput);
char byte_reverse(int n, char *t1, int *t3, int *t4, char *t5);
int main()
{
	char str1[N], str2[N];
	char strOutput[2*N+1];
	cin >> str1 >> str2;
	int len1 = strlen(str1);
	int len2 = strlen(str2);
	int t ;
	for(int i = 0; i < len1; i ++)
		if(str1[i] >= '0' && str1[i] <= '9' || str1[i] >= 'a' && str1[i] <= 'f' || str1[i] >= 'A' && str1[i] <= 'F')
			t = 1;
	for(int i = 0; i < len2; i ++)
		if(str2[i] >= '0' && str2[i] <= '9' || str2[i] >= 'a' && str2[i] <= 'f' || str2[i] >= 'A' && str2[i] <= 'F')
			t = 2;

	if(t == 2)
		ProcessString(str1, str2, strOutput);
	system("pause");
	return 0;
}
void ProcessString(char* str1,char *str2,char * strOutput)
{
	int len1 = strlen(str1);
	int len2 = strlen(str2);

	char temp_str[2*N + 1];
	int i, j, k, m, n;
	for(i = 0; i < len1; i ++)
		temp_str[i] = str1[i];
	for(j = 0; j < len2; j++)
		temp_str[i++] = str2[j];
	temp_str[i] = '\0';
	int len = strlen(temp_str);

	for(m = 0; m < len; m = m + 2)
	{
		k = m;
		for(n = m + 2; n < len; n = n +2)
			if(temp_str[n] < temp_str[k])
				k = n;
		if(k != m)
		{
			char temp;
			temp = temp_str[k];
			temp_str[k] = temp_str[m];
			temp_str[m] = temp;
		}
	}
	for(m = 1; m < len; m = m + 2)
	{
		k = m;
		for(n = m + 2; n < len; n = n +2)
			if(temp_str[n] < temp_str[k])
				k = n;
		if(k != m)
		{
			char temp;
			temp = temp_str[k];
			temp_str[k] = temp_str[m];
			temp_str[m] = temp;
		}
	}

	int ii;
	for(ii = 0; ii < len; ii ++)
		strOutput[ii] = temp_str[ii];
	strOutput[ii] = '\0';

	char t1[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
	char t2[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
	int t3[6] = {10, 11, 12, 13, 14, 15};
	int t4[10]= {0,1,2,3,4,5,6,7,8,9}; 
	char t5[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    char temp_out[2 * N + 1];
	for(ii = 0; ii < len; ii ++)
	{
		if(strOutput[ii] >= 'a' && strOutput[ii] <= 'f' || strOutput[ii] >= 'A' && strOutput[ii] <= 'F')
		{
			for(j = 0; j < 6; j ++)
				if(strOutput[ii] == t1[j] || strOutput[ii] == t2[j])
					temp_out[ii] = byte_reverse(t3[j], t2, t3, t4, t5);
		}
		else if(strOutput[ii] >= '0' && strOutput[ii] <= '9')
		{
			for(j = 0; j < 10; j ++)
				if(strOutput[ii] == t5[j])
					temp_out[ii] = byte_reverse(t4[j], t2, t3, t4, t5);
		}
	}
	temp_out[ii] = '\0';
	for(ii = 0; ii < len; ii ++)
		strOutput[ii] = temp_out[ii];
	strOutput[ii] = '\0';
	cout << strOutput << endl;

}

void reverse(int n, int *temp1)
{
	int i = 0;
	while(n != 0)
	{
		temp1[i++] = n % 2;
		n = n / 2;
	}
}

char byte_reverse(int n, char *t2, int *t3, int *t4, char *t5)
{
	char temp_out1;
	int temp1[4] = {0,0,0,0};
	int i;
	reverse(n, temp1);

	int j;
	int sum = 0;
	for(j = 0; j < 4; j ++)
		sum += temp1[j] * (1 << (4 -1 - j)) ;
	int k;
	if(sum > 9)
	{
		for(j = 0; j < 6; j ++)
			if(sum == t3[j])
				k = j;
		temp_out1 = t2[k];
	}
	else
	{
		for(j = 0; j < 10; j ++)
			if(sum == t4[j])
				k = j;
		temp_out1 = t5[k];
	}
	return temp_out1;
}

程序虽然运行没有错误,但是感觉写得比较乱。

别人可能只要二三十分钟就能完成的工作,我却花了6个多小时才做出来,感觉自己的基本功还需要大大的加强。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值