class NewStr(str): def add(self, other): new_str = '' for char in other: if char not in self: new_str += char return self + new_str

def __sub__(self, other):
    new_str = ''
    for char in self:
        if char not in other:
            new_str += char
    return new_str

string1 = NewStr(input()) string2 = NewStr(input())

print(string1 + string2) print(string1 - string2)

利用Python写出代码 重新定义一个str类使其可以完成加法和减法的操作。其中加法操作执行如下:abcdefg +acdi = abcdefgi即把加号后面的字符串没出现在前面字符串中的字符连接到字符串后面得到的新字符串为加法运算的结果。abcdefg - acdi = befg即把减号前面字符串去掉后面字符串中出现的字符剩余的字符为减法运算的结果。并可以对字符串做打印输出操作。从键盘输入两行

原文地址: https://www.cveoy.top/t/topic/eRvN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录