《计算机程序设计》
计算机学院计算机研究所编译系统研究室
编程实现
已知a
、b
是两个01串,则规定a和b进行“逻辑或”运算的过程为:
例如,a为”0100111”、b为”10010101”时,a先在高位补0,变为”00100111”,然后两者计算得”10110111”。
补全函数void OR(char *a, char *b, char *c)
代码,其中,参数a
、b
长度不超过20,将a
、b
逻辑或结果存在c中。
注意:若计算结果c的高位是’0’,注意保留,不要省去。
提示
本题可能用到的字符串函数如下:
int strlen(char *s);
返回字符串s的长度char *strcpy(char *s1, char *s2);
把字符串s2复制到s1,返回s1char *strcat(char *s1, char *s2);
把字符串s2拼接到s1后面,返回s1int strcmp(char *s1, char *s2);
比较字符串s1和s2的大小,相等时返回0提示
本题可能用到的字符串函数如下:
int strlen(char *s);
返回字符串s的长度char *strcpy(char *s1, char *s2);
把字符串s2复制到s1,返回s1char *strcat(char *s1, char *s2);
把字符串s2拼接到s1后面,返回s1int strcmp(char *s1, char *s2);
比较字符串s1和s2的大小,相等时返回0逻辑或运算:解法1
void OR(char* a, char* b, char* c) {
int al = strlen(a), bl = strlen(b);
int len = al > bl ? al : bl;
char acopy[len+1] = {0}, bcopy[len+1] = {0};
strcpy(acopy + len - al, a);
strcpy(bcopy + len - bl, b);
for (int i = 0; i < len; i++)
c[i] = (acopy[i] == '0' and bcopy[i] == '0' ? '0' : '1');
c[len] = '\0';
}
提示
本题可能用到的字符串函数如下:
int strlen(char *s);
返回字符串s的长度char *strcpy(char *s1, char *s2);
把字符串s2复制到s1,返回s1char *strcat(char *s1, char *s2);
把字符串s2拼接到s1后面,返回s1int strcmp(char *s1, char *s2);
比较字符串s1和s2的大小,相等时返回0逻辑或运算:解法2
void OR(char* a, char* b, char* c) {
int al = strlen(a), bl = strlen(b);
int len = al > bl ? al : bl;
char *atail = a + al - 1;
char *btail = b + bl - 1;
char *ctail = c + len - 1;
while (ctail >= c) {
char ac = (atail >= a) ? *atail : '0';
char bc = (btail >= b) ? *btail : '0';
*ctail = (ac == '0' && bc == '0') ? '0' : '1';
atail--;
btail--;
ctail--;
}
}
编程实现
完成函数int compareString(char a[], char b[])
,参数a
和b
是2个长度相等的仅由大小写字母组成的字符串,该函数比较字符串a和b并返回比较结果,它们之间的关系是以下3种情况之一:
提示:大写字母的ASCII码比相应的小写字母的ASCII码小32
字符串比较
考考你
更改题目要求,要求支持比较长度不同的字符串,若字符串长度不同则返回4,应该如何更改程序?
编程实现
行程编码是一种简单有效的压缩算法,它可将连续的重复字符压缩成“重复次数+字符”的形式,从而减少存储开销。例如,“AAAABBCDEE”压缩后为“4A2B1C1D2E”,“aaaBCCeFF”压缩后为“3a1B2C1e2F”。 完成函数run_length_coding(char *src, char *dst)
,功能是按行程编码算法压缩字符串,其中参数src
是待压缩的字符串(仅包含字母),压缩后的结果保存在参数dst
中。
行程编码
#include <iostream>
#include <string>
using namespace std;
void run_length_coding(char* src, char* dst) {
/**********Program**********/
/********** End **********/
}
int main() {
char s[1000], t[1000];
cout << "请输入一个字符串:";
cin >> s;
run_length_coding(s, t);
cout << "压缩结果为:" << t << endl;
return 0;
}
提示
提示:可能用到的字符串函数说明如下
sprintf(char *str, const char *format, …);
发送格式化输出到str所指向的字符串,并返回打印的字符数。提示
提示:可能用到的字符串函数说明如下
sprintf(char *str, const char *format, …);
发送格式化输出到str所指向的字符串,并返回打印的字符数。行程编码
考考你
如果你不会使用sprintf函数,怎么办?
编程实现
完成函数digit(char str[])
,其功能是抽取字符串str
中的数字,用抽取到的数字组成整数,并返回该整数。 字符串str中可能包含小写英文字母和数字字符0-9,函数digit只抽取其中的数字字符组成整数。
如str为”asd123fgh34sxc1”,则抽取到的数字组成的整数为123341,请完成该函数的编写。
说明:测试用例保证不会发生整数越界。
抽取整数
计算机程序设计