题目:
题目描述(60分):
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。
要求实现函数:
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“deefd” 输出:“def”
输入:“afafafaf” 输出:“af”
输入:“pppppppp” 输出:“p”
*/
/*
题目描述(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”
题目描述(60分):
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。
要求实现函数:
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“deefd” 输出:“def”
输入:“afafafaf” 输出:“af”
输入:“pppppppp” 输出:“p”
*/
/*
题目描述(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” 注:格式错误
注意查下这几个函数的用法:atoi itoa sprintf
第一题答案:
#include<iostream>
#include<cstdio>
using namespace std;
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{
const char *p;
p = pInputStr;
int a[26]={0};
char *pResult = pOutputStr;
while(*p != '\0')
{
a[*p-'a']++;
if (a[*p-'a'] > 1)
{
p++;
}
else
{
*pResult = *p;
pResult++;
p++;
}
}
*pResult = '\0';
}
/*以下用于测试
int main()
{
char s[120]="accbbddeeffggwwwwwwwwwt";
char str[10];
stringFilter(s,120,str);
cout<<str<<endl;
system("pause");
return 0;
}*/
第二题答案:
#include<iostream>
#include<cstdio>
using namespace std;
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
const char *p = pInputStr;
char *pResult = pOutputStr;
char c;
int k = 1;
char s[11];
//注意是*p不是p啊,稍不注意就死这里了,艹
while(*p != '\0')
{
c = *p;
p++;
if(c == *p) k++;
else
{
if(1 != k)
{
//将整数转换成字符串,加在原字符串末尾
//注意k的范围
itoa(k,s,10);
int i = 0;
int len = strlen(s);
while (i < len)
{
*pResult++ = s[i++];
}
}
*pResult++ = c;
k = 1;
}
}
*pResult = '\0';
}
int main()
{
char s[120]="accbbddeeffffffffffff";
char str[20];
stringZip(s,120,str);
cout<<str<<endl;
system("pause");
return 0;
}
第三题答案:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
const char *p = pInputStr;
char *pResult = pOutputStr;
int len = strlen(p);
int res;
char s1[100], s2[100],s3[100];
int c1 = 0, c2 = 0,c3 = 0, k = 0;
for (int i = 0; i < len; i++,p++)
{
if (' ' == *p)
{
k++;
if (k == 1)s1[c1++] = '\0';
else if (k == 2)s2[c2++]='\0';
}
else
{
if(k == 0)s1[c1++] = *p;
else if(k == 1)s2[c2++] = *p;
else if(k == 2)s3[c3++] = *p;
}
}
s3[c3] = '\0';
int a = atoi(s1);
int b = atoi(s3);
if(s2[0] == '+') res = a+b;
else if(s2[0] == '-')res = a-b;
if (strlen(s2) != 1)
{
//cout<<"格式错误"<<endl;
res = 0;
}
itoa(res,pResult,10);//请注意itoa和atoi这两个函数的用法是不同的
}
int main()
{
char s[20]="1 + 2";
char s1[20]="333 - 111";
char s2[20]="222 ++ 11";
char result[20];
cout<<"1 + 2"<<endl;
arithmetic(s,strlen(s),result);
cout<<result<<endl;
cout<<"333 - 111"<<endl;
arithmetic(s1,strlen(s),result);
cout<<result<<endl;
cout<<"222 ++ 11"<<endl;
arithmetic(s2,strlen(s),result);
cout<<result<<endl;
system("pause");
return 0;
}