每天学习一点编程(10)(字符串的合并排序处理)
2014-05-24 16:02
656 查看
将输入的两个字符串合并。
对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。这里的下标意思是字符在字符串中的位置。
对排训后的字符串进行操作,如果字符为‘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)
{
}
程序虽然运行没有错误,但是感觉写得比较乱。
别人可能只要二三十分钟就能完成的工作,我却花了6个多小时才做出来,感觉自己的基本功还需要大大的加强。
对合并后的字符串进行排序,要求为:下标为奇数的字符和下标为偶数的字符分别从小到大排序。这里的下标意思是字符在字符串中的位置。
对排训后的字符串进行操作,如果字符为‘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 , str2 ; 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 < 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 < 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个多小时才做出来,感觉自己的基本功还需要大大的加强。
相关文章推荐
- 每天学习一点编程(15)(HWOJ字符串排序)
- perl学习(10) 字符串处理函数和排序
- 每天学习一点编程(4)(在字符串中找出连续最长的数字串,并把这个串的长度返回)
- 每天学习一点编程(13)(字符串压缩)
- 每天学习一点编程(6)(输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字)
- 每天学习一点编程(14)(数组/字符串循环移位)
- 每天学习一点编程(5)(在一个字符串中找到第一个只出现一次的字符)
- 每天学习一点编程(1)(输入一个表示整数的字符串,把该字符串转换成整数并输出)
- 编程之美a题小数据 学习用sstream 处理字符串
- 工作8年对技术学习过程的一些 总结 与 感悟 为什么有时迷茫、无奈 学习编程语言的最高境界最重要的是编程思想 T 字发展 学技术忌讳”什么都会“ 每天进步一点等式图 时间管理矩阵
- perl学习(10) 字符串处理函数和排序
- 学习笔记之10-字符和字符串常用处理函数
- 王亟亟的Python学习之路(九)-sorted()排序以及简单字符串处理
- 每天学习一点编程(12)(求满足要求的9位数)
- 每天学习一点编程(2)(输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变)
- 每天学习一点编程(11)(输出一个序列的所有子集)
- WINDOWS核心编程学习笔记--字符串处理
- 王亟亟的Python学习之路(九)-sorted()排序以及简单字符串处理
- Shell 编程10(字符串处理)
- 每天学习一点编程(3)(输出给定字串的全部连续子串)