【输入形式】 一个以‘#’为结尾的字符串
【输出形式】 经题中所述规则处理后生成的新字符串
【样例输入】 23s0_d41#
【样例输出】 333_ssss_s_0_\UL_d_11111_1
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define OK 1
#define OVERLOW 0
#define LIST_INIT_SIZE 100
typedef struct SqList{
char *elem;
int length;
}String;
int InitList_Sq(String *L)
{
L->elem = (char*)malloc(LIST_INIT_SIZE*sizeof(char));
if(!L->elem)
return OVERLOW;
L->length = 0;
return OK;
}
int ClearList_Sq(String *L)
{
if(!L->elem)
return OVERLOW;
L->elem = NULL;
L->length = '0';
return OK;
}
int ListLength_Sq(String *L)
{
if(!L->elem)
return OVERLOW;
return L->length;
}
void putinto(String *p)
{
if(p ->length == 0)
printf("顺序列表为空");
int i = 0;
for(i = 0; i < p->length; i++)
{
printf("%c", p->elem[i]);
}
}
void Into(String *p, char x)
{
p->elem[p->length] = x;
p->length++;
}
void Swith(char ch, int *s)
{
*s = (int)ch - 48;
}
void Decide(char ch,String *p, String *q, int i)
{
int a;
Swith(ch, &a);
char s;
if(a > 0 && a < 9 && i != (p->length - 1))
{
s = p->elem[i+1];
for(i = 0; i < a+1; i++)
{
Into(q, s);
}
s = '_';
Into(q, s);
}
else
{
if(ch == '\\'|| ch == 'U' ||ch == 'L')
{
Into(q, ch);
if(ch == 'L')
{
s = '_';
Into(q, s);
}
}
else
{
if(i != (p->length - 1))
{
Into(q, ch);
s = '_';
Into(q, s);
}
else
{
Into(q, ch);
}
}
}
}
void Function(String *p, String *q)
{
int i, a, n;
for(i = 0; i < p->length; i++)
{
Decide(p->elem[i], p ,q ,i );
}
}
void Cat(char ch[], String *p)
{
int size = 0;
size = p->length + strlen(ch);
p->elem = realloc(p->elem, size);
strcat(p->elem, ch);
}
int main()
{
int i, n;
String *q = (String*)malloc(sizeof(String));
String *p = (String*)malloc(sizeof(String));
InitList_Sq(p);
InitList_Sq(q);
char arr[100];
gets(arr);
n = strlen(arr);
for(i = 0; i < n; i++)
{
if(arr[i] == '#')
break;
if(arr[i] == '_')
{
Into(q, '\\');
Into(q,'U');
Into(q,'L');
continue;
}
Into(q, arr[i]);
}
Function(q, p);
putinto(p);
return 0;
}