本文共 6412 字,大约阅读时间需要 21 分钟。
1#include#include #include #include #include using namespace std;//第一题void convert(char *input,char* output){ char *ptrin = input; int repeat_count = 0; char temp; while(*ptrin != '\0') { if (ptrin != input) { if (*ptrin == *(ptrin - 1)) { repeat_count++; if (repeat_count == 2) { repeat_count = 0; } } else { repeat_count = 0; } } temp = (char)(*ptrin + repeat_count + 1); if (temp > 'z') { *output++ = (char)(temp - 'z' + 'a' - 1); } else { *output++ = temp; } ptrin++; } *output = '\0';}//第二题void my_word(char input[], char output[]){ vector index; vector words; vector count; char *ptrinput = input; string temp; //获得单词 int i = 0; while(*ptrinput != '\0') { if (isalpha(*ptrinput)) { temp += *ptrinput++; } else { if (temp.size() > 1) { words.push_back(temp); count.push_back(temp.size()); index.push_back(i++); } temp.clear(); ptrinput++; } } if (*ptrinput == '\0') { if (temp.size() > 1) { words.push_back(temp); count.push_back(temp.size()); index.push_back(i++); } temp.clear(); } //单词降序排列 int j = 0; int tmp; int len = index.size(); for (i = 0; i < len; i++) { for (j = 0; j < len - 1; j++) { if (count[j+1] > count[j]) { tmp = index[j+1]; index[j+1] = index[j]; index[j] = tmp; tmp = count[j+1]; count[j+1] = count[j]; count[j] = tmp; } } } //根据index输出单词 string outstr; int k = 0; while (k < index.size()) { while (k > 0 && (words[index[k]] == words[index[k -1]])) { k++; if (k == index.size()) { break; } } if (k < index.size()) { outstr += words[index[k]]; outstr += ' '; k++; } } for (int m = 0; m < outstr.size(); m++) { *output++ = outstr[m]; } *output = '\0';}//第三题void Decrease(char *input1, char*input2, char *output){ int leftlen1 = 0;; int rightlen1 = 0; int leftlen2 = 0; int rightlen2 = 0; char *ptrinput1 = input1; char *ptrinput2 = input2; while (*ptrinput1 != '.' && *ptrinput1 != '\0' ) { rightlen1++; ptrinput1++; } //第一个数据有小数点 if (*ptrinput1 == '.') { ptrinput1++; while (*ptrinput1 != '\0' ) { leftlen1++; ptrinput1++; } } while (*ptrinput2 != '.' && *ptrinput2 != '\0') { rightlen2++; ptrinput2++; } //第二个数据有小数点 if (*ptrinput2 == '.') { ptrinput2++; while (*ptrinput2 != '\0' ) { leftlen2++; ptrinput2++; } } //数据对齐 int calen = max(rightlen1,rightlen2) + max(leftlen1,leftlen2); char *temp1 = new char[calen+1]; char *temp2 = new char[calen+1]; char *outemp = new char[calen+1]; int i = 0; int index1 = 0; int index2 = 0; ptrinput1 = input1; ptrinput2 = input2; //右边数据对齐 if (rightlen1 < rightlen2) { for (i = 0; i < rightlen2 - rightlen1; i++) { temp1[index1++] = '0'; } } else if (rightlen1 > rightlen2) { for (i = 0; i < rightlen1 - rightlen2; i++) { temp2[index2++] = '0'; } } for (i = 0; i < rightlen1; i++) { temp1[index1++] = *ptrinput1++; } for (i = 0; i < rightlen2; i++) { temp2[index2++] = *ptrinput2++; } //补齐小数点 if (leftlen1 == 0 && leftlen2 != 0) { temp1[index1++] = '.'; temp2[index2++] = *ptrinput2++; } else if (leftlen1 != 0 && leftlen2 == 0) { temp1[index1++] = *ptrinput1++; temp2[index2++] = '.'; } else { temp1[index1++] = *ptrinput1++; temp2[index2++] = *ptrinput2++; } //左边数据对齐 for (i = 0; i < leftlen1; i++) { temp1[index1++] = *ptrinput1++; } for (i = 0; i < leftlen2; i++) { temp2[index2++] = *ptrinput2++; } if (leftlen1 < leftlen2) { for (i = 0; i < leftlen2 - leftlen1; i++) { temp1[index1++] = '0'; } } else if (leftlen1 > leftlen2) { for (i = 0; i < leftlen1 - leftlen2; i++) { temp2[index2++] = '0'; } } //开始做减法 int flagbit = 0; for (i = calen; i > -1; i--) { if (temp1[i] == '.') { outemp[i] = '.'; } else if (temp1[i] == '\0') { ; } else { if ((temp1[i] - flagbit) < temp2[i]) { outemp[i] = (char)(temp1[i]- flagbit + 10 - temp2[i] + '0'); flagbit = 1; } else { outemp[i] = (char)(temp1[i]- flagbit - temp2[i] + '0'); flagbit = 0; } } } //清除无效的0 char *ptrpre = outemp; char *ptrend = outemp + calen; while(*ptrpre == '0') { ptrpre++; } while(*ptrend == '0') { ptrend--; } //通过借位标志判断两个数的大小关系 if (flagbit == 0) { if (*ptrpre != '.' && *ptrend != '.') { memcpy(output,ptrpre,sizeof(char)*(ptrend - ptrpre + 1)); } else if (*ptrpre == '.' && *ptrend != '.') { *output++ = '0'; memcpy(output,ptrpre,sizeof(char)*(ptrend - ptrpre + 1)); } else if(*ptrpre != '.' && *ptrend == '.' ) { memcpy(output,ptrpre,sizeof(char)*(ptrend - ptrpre)); } else { *output = '0'; } *(output+ (ptrend - ptrpre + 1)) = '\0'; } else { *output++ = '-'; Decrease(input2,input1,output); } delete[] temp1; delete[] temp2; delete[] outemp; temp1 = NULL; temp2 = NULL; outemp = NULL;}void main(){// //第一题测试// char input[100];// char output[100];// while(1)// {// cout << "input:" ;// gets(input);// convert(input,output);// cout << "output:";// cout << output << endl<
转载地址:http://njybb.baihongyu.com/