Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 字符串操作_AC.cpp #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 196 additions & 0 deletions 3.C++程序设计/week7/编程练习/字符串操作_AC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sstream> // Required for stringstreams
using namespace std;

string str[21];
int NumOfStr = 0;

int StringToInteger(string NumberAsString)
{
int NumberAsInteger=0;
for (int i = 0; i < NumberAsString.size(); i++)
NumberAsInteger = NumberAsInteger * 10 + (NumberAsString[i] - '0');

return NumberAsInteger;
}

string NumToString ( int number )
{
std::ostringstream oss;

// Works just like cout
oss<< number;

// Return the underlying string
return oss.str();
}


bool isNum(string s)
{
for (int i = 0; i < s.length(); i++)
{
if (!isdigit(s[i]))
{
return false;
}
}
return true;
}

int find()
{
char s;
int n;
cin >> s >> n;
return str[n].find(s);
}

int rfind()
{
char s;
int n;
cin >> s >> n;
return str[n].rfind(s);
}

int get_int()
{
string str;
cin >> str;
if (isNum(str))
{
//return stoi(str);
return StringToInteger(str);
}
else
{
if (str == "find")
{
return find();
}
else if (str == "rfind")
{
return rfind();
}
}
return 0;
}



string copy()
{
int n, x, l;
n = get_int();
x = get_int();
l = get_int();
return str[n].substr(x, l);
}

string add();

string get_str()
{
string str;
cin >> str;
if (str == "copy")
{
return copy();
}
else if (str == "add")
{
return add();
}
else
{
return str;
}
}

string add()
{
string s1 = get_str();
string s2 = get_str();
//if (isNum(s1) && isNum(s2) && stoi(s1) >= 0 && stoi(s1) <= 99999 && stoi(s2) >= 0 && stoi(s2) <= 99999)
if (isNum(s1) && isNum(s2) && StringToInteger(s1) >= 0 && StringToInteger(s1) <= 99999 && StringToInteger(s2) >= 0 && StringToInteger(s2) <= 99999)
{
//return to_string(stoi(s1) + stoi(s2));
return NumToString(StringToInteger(s1) + StringToInteger(s2));
}
else
{
return s1 + s2;
}
}



void insert()
{
string s = get_str();
int n = get_int();
int x = get_int();
str[n].insert(x, s);
}

void reset()
{
string s = get_str();
int n = get_int();
str[n] = s;
}

void print()
{
int n = get_int();
cout << str[n] << endl;
}

void printall()
{
for (int i = 1; i <= NumOfStr; i++)
{
cout << str[i] << endl;
}
}

int main()
{
cin >> NumOfStr;
for (int i = 1; i <= NumOfStr; i++)
{
cin >> str[i];
}
while (true)
{
string operation;
cin >> operation;
if (operation == "insert")
{
insert();
}
else if (operation == "reset")
{
reset();
}
else if (operation == "print")
{
print();
}
else if (operation == "printall")
{
printall();
}
else if (operation == "over")
{
break;
}
}
return 0;

}