华为机考MySQL_2013华为校园招聘机试题2021年01月27日题(杭州)

#include #include #include #define MAXCHAR 256//必须规定均为小写字母void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr){ int a[26] = {0}; int pos = 0; long i, j; const char *p = pInputStr; for(i = 0, j = 0; i < lInputLen; i++) { pos = pInputStr[i] - 'a'; //将出现的字符标记为1,下次出现同样的字符时就不再存储 if(a[pos] == 0) { a[pos]++; pOutputStr[j++] = pInputStr[i]; } }pOutputStr[j] = ' ';}void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr){ int i, j, k = 0, cnt = 0; char buffer[30]; for(i = 0; i < lInputLen;) { for(j = i + 1;;j++) { if(pInputStr[j] == pInputStr[i]) cnt++; else break; } if(cnt != 0) { memset(buffer, 0, sizeof(buffer)); itoa(cnt + 1, buffer, 10); strcat(pOutputStr, buffer); k += strlen(buffer); i += cnt; } pOutputStr[k++] = pInputStr[i++]; cnt = 0; }}void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr){ int i, cnt = 0, a, b, result; char ch[1] = {'0'}; char op1[MAXCHAR], op[MAXCHAR], op2[MAXCHAR], buffer[4]; for(i = 0; i < lInputLen; i++) if(pInputStr[i] == ' ') cnt++; if(cnt != 2) //空格数不等于2 { strcat(pOutputStr, ch); return; } sscanf(pInputStr, "%s %s %s", op1, op, op2); if(strlen(op) > 1 || (op[0] != '+' && op[0] != '-')) // 操作符有多个 { strcat(pOutputStr, ch); return; } for(i = 0; i < strlen(op1); i++) //操作数1是否有其他字符 { if(op1[i] < '0' || op1[i] > '9') { strcat(pOutputStr, ch); return; } } for(i = 0; i < strlen(op2); i++) //操作数2是否有其他字符 { if(op2[i] < '0' || op2[i] > '9') { strcat(pOutputStr, ch); return; } } a = atoi(op1); b = atoi(op2); switch(op[0]) { case '+': result = a + b; itoa(result, buffer, 10); strcat(pOutputStr, buffer); break; case '-': result = a - b; itoa(result, buffer, 10); strcat(pOutputStr, buffer); break; default: break; }}int main(){ char pInputStr1[] = {"aaabbbcccdde"}; char pInputStr2[] = {"aaabbcddde"}; char pInputStr3[] = {"3 + 4"}; char pOutputStr1[MAXCHAR] = {0}; char pOutputStr2[MAXCHAR] = {0}; char pOutputStr3[MAXCHAR] = {0}; /* TODO: 调用被测函数 */ stringFilter(pInputStr1, strlen(pInputStr1), pOutputStr1); stringZip(pInputStr2, strlen(pInputStr2), pOutputStr2); arithmetic(pInputStr3, strlen(pInputStr3), pOutputStr3); /* TODO: 执行完成后可比较是否是你认为正确的值 */ printf(pOutputStr1); //abcde printf(pOutputStr2); //3a3b3c2de printf(pOutputStr3); //7 return;}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值