9-2 Huawei machine trial

Source: Internet
Author: User

I. Question description (60 points ):
Enter a string of lowercase letters (~ Z. Compile a character string filter program. If multiple identical characters appear in the string, filter out non-first-time characters.
For example, the "abacacde" character string is "ABCDE ".

Required implementation functions:
Void stringfilter (const char * pinputstr, long linputlen, char * poutputstr );

[Input] pinputstr: input string
Linputlen: length of the input string
[Output] poutputstr: Output string. The space has been opened up and the length of the input string is equal to that of the input string;

[Note] You only need to complete the function algorithm, and there is no IO input or output in the middle.

Example
Input: "deefd" output: "def"
Input: "afafaf" output: "AF"

Input: "pppppppp" output: "P"

void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr){bool flag[26] = {0};int i, j = 0;for(i = 0; i < lInputLen; i++){char t = pInputStr[i];if(flag[t-'a'] == 0){pOutputStr[j] = t;j++;flag[t-'a'] = 1;}}pOutputStr[j] = '\0';}

Ii. Question description (40 points ):
Enter a string of lowercase letters (~ Z. Compile a character string compression program to compress duplicate letters that are present continuously in the string and output the compressed character string.
Compression rules:
1. Only the characters that appear repeatedly are compressed. For example, the compressed string "abcbc" is still "abcbc" because there are no consecutive duplicate characters ".
2. The format of the compressed field is "repeated characters + characters ". For example, the string "xxxyyyyyyz" becomes "3x6yz" after compression"

Required implementation functions:
Void stringzip (const char * pinputstr, long linputlen, char * poutputstr );

[Input] pinputstr: input string
Linputlen: length of the input string
[Output] poutputstr: Output string. The space has been opened up and the length of the input string is equal to that of the input string;

[Note] You only need to complete the function algorithm, and there is no IO input or output in the middle.

Example
Input: "cccddecc" output: "3c2de2c"
Input: "adef" output: "adef"
Input: "pppppppp" output: "8 P"

void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr){int num = 0, i, j = 0;for(i = 0; i <= lInputLen; i++){if(i && pInputStr[i] != pInputStr[i-1]){if(num > 1){pOutputStr[j] = (char)(num+'0');j++;}pOutputStr[j] = pInputStr[i-1];j++;num = 1;}elsenum++;}pOutputStr[j] = '\0';}

Iii. Question description (50 points ):
Enter the plus or minus formula for positive integers less than 100 on the keyboard. Write a program to output the computation result string.
The format of the input string is "operand 1 operator operand 2", and "operand" and "operator" are separated by a space.

Note:
1. the operand is a positive integer and computing result overflow is not required.
2. If the input formula format is incorrect, the output result is "0 ".

Required implementation functions:
Void Arithmetic (const char * pinputstr, long linputlen, char * poutputstr );

[Input] pinputstr: input string
Linputlen: length of the input string
[Output] poutputstr: Output string. The space has been opened up and the length of the input string is equal to that of the input string;

[Note] You only need to complete the function algorithm, and there is no IO input or output in the middle.

Example
Input: "4 + 7" output: "11"
Input: "4-7" output: "-3"
Input: "9 + + 7" output: "0" Note: Incorrect format

void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr){char ch[3][1000];int i, j = 0, k =0, a = 0, b = 0, ret = -1;for(i = 0; i <= lInputLen; i++){if(pInputStr[i] == ' ' || pInputStr[i] == '\0'){ch[j][k] = '\0';j++;k = 0;}else{ch[j][k] = pInputStr[i];k++;}}for(i = 0; ch[0][i] != '\0'; i++){if(ch[0][i] < '0' || ch[0][i] > '9'){ret = 0;break;}a = a * 10 + ch[0][i] - '0';}for(i = 0; ch[2][i] != '\0'; i++){if(ch[2][i] < '0' || ch[2][i] > '9'){ret = 0;break;}b = b * 10 + ch[2][i] - '0';}if(ch[1][1] != '\0' || (ch[1][0] != '+' && ch[1][0] != '-'))ret = 0;if(ret == 0){pOutputStr[0] = '0';pOutputStr[1] = '\0';return ;}if(ch[1][0] == '+')ret = a + b;else if(ch[1][0] == '-')ret = a - b;if(ret == 0){pOutputStr[0] = '0';pOutputStr[1] = '\0';return ;}k = 0;char temp[1000];int t = ret;j = 0;if(ret < 0){pOutputStr[0] = '-';j++;t = -t;}while(t){temp[k] = (char)(t % 10 + '0');t = t / 10;k++;}for(i = k - 1; i >= 0; i--){pOutputStr[j] = temp[i];j++;}pOutputStr[j] = '\0';}

Not familiar with string library functions, complicated to write

Test:

int main(){char a[10], b[10];while(cin.getline(a, 10, '\n')){stringFilter(a, strlen(a), b);//stringZip(a, strlen(a), b);//arithmetic(a, strlen(a), b);cout<<b<<endl;}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.