2025-03-22 19:15:32 +08:00

76 lines
1.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*----------------------------------------------------------------------
【程序设计】
------------------------------------------------------------------------
输入一个字符串,由括号(英文括号)、数字和大写字母组成,字符串中形如(2A)表示2个连续的A即AA字符串中形如(4AB)表示4个连续的AB括号无嵌套.请对其解码得到原始字符串!
示例1
输入字符串:(5A)
解码后字符串:AAAAA
示例2
输入字符串:(3AC)
解码后字符串:ACACAC
示例3
输入字符串:(3C)(4AB)
解码后字符串:CCCABABABAB
(不符合要求格式的字符串可以忽略不做处理)
------------------------------------------------------------------------
注意部分源程序给出如下。请勿改动主函数main或其它函数中给出的内容仅在
Program-End之间填入若干语句。不要删除标志否则不得分。
----------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int parseNumber(const char *str, int *index) {
int num = 0;
while (str[*index] >= '0' && str[*index] <= '9') {
num = num * 10 + (str[*index] - '0');
(*index)++;
}
return num;
}
void decodeString(char *s, char *out) {
char *p_read = s;
char *p_write = out;
char buf[64] = {0};
int digit_len = 0;
int repeats = 0;
while (*p_read){
/**********Program**********/
if(*p_read == '('){//找到左括号 //(3AC)
digit_len = 0;//数字的长度
repeats = parseNumber(++p_read, &digit_len);//取出数字部分
p_read+=digit_len;
//定位到字符
digit_len = 0;
while(*p_read != ')'){
buf[digit_len++]=*p_read++;
}//将需要输出字符存储到buf中
buf[digit_len]='\0';
//按存储的个数打印
while(repeats--){
out = strcat(out, buf);
}
}
p_read++;
/********** End **********/
}
}
int main() {
char s[32] = {0};
char result[512] = {0};
printf("输入字符串s");
scanf("%s", s);
decodeString(s, result);
printf("解码后字符串:%s", result);
return 0;
}