社区讨论

*如果有bug那就有bug,蒟蒻求救!*

P5734【深基6.例6】文字处理软件参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lo27bzst
此快照首次捕获于
2023/10/23 09:11
2 年前
此快照最后确认于
2023/11/03 09:26
2 年前
查看原帖
写了批注也找不出个bug来(⊙︿⊙)
CPP
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
    long long int n;  // 操作数,即要执行的总操作数
    long long int operation;  // 操作类型
    string raw_string;  // 原始字符串
    string str;  // 输入的字符串
    long long int a, b;  // 从第a个字符往后的b个字符
    long long int number_of_characters;  // 原始字符串的字符数量
    cin >> n >> raw_string;  // 输入操作数和原始字符串

    for (int i = 0; i < n; i++) {  // 遍历每个操作
        cin >> operation;  // 输入操作类型

        if (operation == 1) {  // 操作类型1:在字符串末尾添加字符串
            cin >> str;  // 输入待添加的字符串
            raw_string = raw_string + str;  // 将待添加的字符串追加至原字符串末尾
            cout << raw_string << endl;
        }
        else if (operation == 2) {  // 操作类型2:删除指定范围内的字符
            cin >> a >> b;  // 输入待删除的起始位置和删除字符的数量
            string cup = "";  // 用来存放这里不删除的字符
            number_of_characters = raw_string.length();

            for (int i = 0; i < number_of_characters; i++) {
                if (i >= a && i <= a + b) {
                    cup = cup + raw_string[i];
                }
            }
            raw_string = cup;  // 更新原始字符串
            cout << raw_string << endl;
        }
        else if (operation == 3) {  // 操作类型3:在指定位置插入字符串
            long long int inserted_string_position;  // 插入的字符串位置
            string left_side_of_string, right_side_of_string;  // 分别为字符串的左右两边
            cin >> inserted_string_position >> str;

            for (int i = 0; i < inserted_string_position; i++) {
                left_side_of_string = left_side_of_string + raw_string[i];  // 提取左侧字符串
            }

            for (int i = inserted_string_position; i < raw_string.length(); i++) {
                right_side_of_string = right_side_of_string + raw_string[i];  // 提取右侧字符串
            }

            raw_string = left_side_of_string + str + right_side_of_string;  // 拼接字符串
            cout << raw_string << endl;
        }
        else if (operation == 4) {  // 操作类型4:搜索字符串并返回第一个匹配位置
            long long int length_of_str;  // 查找字符串str的长度
            long long int number_of_searches = 0;  // 查找的次数
            long long int on_off = 0;  // 开关
            string extracted_string = "";  // 抽出的字符串
            cin >> str;
            length_of_str = str.length();

            for (int i = 0; i < raw_string.length(); i++) {
                extracted_string = "";
                long long int j1 = i;
                long long int j2 = j1;

                while (j1 < j2 + length_of_str) {
                    extracted_string = extracted_string + raw_string[j1];  // 提取长度为length_of_str的子串
                    j1++;
                }

                if (str == extracted_string) {
                    on_off++;  // 找到匹配位置
                    break;
                }

                number_of_searches++;
            }

            if (on_off == 1) {
                cout << number_of_searches << endl;
            } else {
                cout << "-1" << endl;
            }
        }
    }

    return 0;
}

不喜勿喷

回复

2 条回复,欢迎继续交流。

正在加载回复...