/*
题目描述(60分):
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。
要求实现函数:
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“deefd” 输出:“def”
输入:“afafafaf” 输出:“af”
输入:“pppppppp” 输出:“p”
*/
/* main函数已经隐藏,这里保留给用户的测试入口,在这里测试你的实现函数,可以调用printf打印输出*/
/* 当前你可以使用其他方法测试,只要保证最终程序能正确执行即可 */
/* 该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响*/
/*
题目描述(40分):
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
压缩规则:
1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"
要求实现函数:
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“cccddecc” 输出:“3c2de2c”
输入:“adef” 输出:“adef”
输入:“pppppppp” 输出:“8p”
*/
/*
题目描述(50分):
通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。
补充说明:
1. 操作数为正整数,不需要考虑计算结果溢出的情况。
2. 若输入算式格式错误,输出结果为“0”。
要求实现函数:
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“4 + 7” 输出:“11”
输入:“4 - 7” 输出:“-3”
输入:“9 ++ 7” 输出:“0” 注:格式错误
*/
第一题:
#include <stdio.h>
void stringFilter(const char *p_str, long len, char *p_outstr)
{
int array[256]={0};
const char *tmp = p_str;
for(int j=0;j<len;j++)
{
if(array[tmp[j]]==0)
*p_outstr++= tmp[j];
array[tmp[j]]++;
}
*p_outstr = '\0';
}
void main()
{
char *str = "cccddecc";
int len = strlen(str);
char * outstr = (char *)malloc(len*sizeof(char));
stringFilter(str,len,outstr);
printf("%s",outstr);
free(outstr);
outstr = NULL;
}
第二题:
#include <stdio.h>
void stringZip(const char *p_str, long len, char *p_outstr)
{
int count=0;
for(int i=0;i<len;i++)
{
if(p_str[i]==p_str[i+1])
{
count++;
}
else
{
count++;
if(count>1)
{
*p_outstr++ = count +'0';
*p_outstr++ =p_str[i];
}
else
{
*p_outstr++ =p_str[i];
}
count = 0;
}
}
*p_outstr = '\0';
}
void main()
{
char *str = "cccddecc";
int len = strlen(str);
char * outstr = (char*)malloc(len*sizeof(char));
// char outstr[100];
stringZip(str,len,outstr);
printf("%s",outstr);
free(outstr);
outstr = NULL;
}
第三题:
#include <stdio.h>
void arithmetic(const char *input, long len, char *output)
{
char *p1=(char*)malloc(4);
char *p2=(char*)malloc(4);
char *p3=(char*)malloc(4);
char * i1=p1;
char * i2=p3;
int count = 0;
while(*input!=' ')
{
*p1 = *input;
if(*p1<'0'||*p1>'9')
{
*output++ = '0';
*output = '\0';
return;
}
p1++;
input++;
}
while(*input==' ')
input++;
while(*input!=' ')
{
*p2 = * input;
count++;
if(count>1)
{
*output++ = '0';
*output = '\0';
return;
}
p2++;
input++;
}
p2--;//这句话不能少
while(*input==' ')
input++;
while(*input!=' '&&*input!='\0')
{
*p3 = * input;
if(*p3<'0'||*p3>'9')
{
*output++ = '0';
*output = '\0';
return;
}
p3++;
input++;
}
int x=atoi(i1);
int y=atoi(i2);
if(*p2 == '+')
{
int result = x + y;
itoa(result,output,10);
return;
}
else if(*p2 == '-')
{
int result = x - y;
itoa(result,output,10);
return ;
}
else
{
*output++ = '0';
*output = '\0';
return;
}
}
void main()
{
char *str = "12 - 23";
int len = strlen(str);
char *outstr = (char*)malloc(sizeof(str));
arithmetic(str,len,outstr);
printf("%s\n",str);
printf("%s\n",outstr);
}
附上别人写的第三题答案(更切合题意)
#include <stdio.h>
void arithmetic(const char *input, long len, char *output)
{
char s1[10];
char s2[10];
char s3[10];
int cnt = 0;
for(int i=0;i<strlen(input);++i)
{
if(input[i]==' ')
cnt++;
}
if(cnt!=2)
{
*output++ = '0';
*output = '\0';
return;
}
sscanf(input,"%s %s %s",s1,s2,s3);
if(strlen(s2)!=1||(s2[0]!='+'&&s2[0]!='-'))
{
*output++ = '0';
*output = '\0';
return;
}
for(int i=0;i<strlen(s1);i++)
{
if(s1[i]<'0'||s1[i]>'9')
{
*output++ = '0';
*output = '\0';
return;
}
}
for(int i=0;i<strlen(s3);i++)
{
if(s3[i]<'0'||s3[i]>'9')
{
*output++ = '0';
*output = '\0';
return;
}
}
int x = atoi(s1);
int y = atoi(s3);
if(s2[0]=='+')
{
int result = x+y;
itoa(result,output,10);
}
else if(s2[0]=='-')
{
int result = x-y;
itoa(result,output,10);
}
else
{
*output++ = '0';
*output = '\0';
return;
}
}
void main()
{
char str[] = {"10 - 23"};
char outstr[10];
int len = strlen(str);
arithmetic(str,len,outstr);
printf("%s\n",str);
printf("%s\n",outstr);
}