C++程序设计 第7周 字符串操作

遇到的问题:

1. 访问数组出现“不确定”的提示,后来是发现数组的命名与C++的某些操作命令重合了。

2. 对于“83s”这样的字符串,stoi的命令会直接得出83,需要一个个的字符去判断是否是数字。

---------------------------------以下是题目--------------------------------------------------------------

描述

给定n个字符串(从1开始编号),每个字符串中的字符位置从0开始编号,长度为1-500,现有如下若干操作:

copy N X L:取出第N个字符串第X个字符开始的长度为L的字符串。

add S1 S2:判断S1,S2是否为0-99999之间的整数,若是则将其转化为整数做加法,若不是,则作字符串加法,返回的值为一字符串。

find S N:在第N个字符串中从左开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。

rfind S N:在第N个字符串中从右开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。

insert S N X:在第N个字符串的第X个字符位置中插入S字符串。

reset S N:将第N个字符串变为S。

print N:打印输出第N个字符串。

printall:打印输出所有字符串。

over:结束操作。

其中N,X,L可由find与rfind操作表达式构成,S,S1,S2可由copy与add操作表达式构成。

输入

第一行为一个整数n(n在1-20之间)

接下来n行为n个字符串,字符串不包含空格及操作命令等。

接下来若干行为一系列操作,直到over结束。

输出

根据操作提示输出对应字符串。


代码在OpenJudge上通过了,但是Coursera并没有,估计是因为stoi这个函数。

#include<iostream>
#include<string>
using namespace std;

int n = 0;
string a[20];//收集所有字符串
string Command[500]; int index = 0;//收集所有命令,index为命令的索引

string StringCopy(); int StringFind(); int StringRfind();
string StringAdd()//判断S1,S2是否为0-99999之间的整数,若是则将其转化为整数做加法,若不是,则作字符串加法,返回的值为一字符串。
{
	index++;
	string S1, S2;
	if (Command[index] == "add") S1 = StringAdd();
	else if (Command[index] == "copy") S1 = StringCopy();
	else S1 = Command[index];
	index++;
	if (Command[index] == "add") S2 = StringAdd();
	else if (Command[index] == "copy") S2 = StringCopy();
	else S2 = Command[index];
	for (int i = 0; i < S1.size(); i++)
		if ((S1.at(i) < '0') || (S1.at(i) > '9'))
			return (S1 + S2);
	for (int i = 0; i < S2.size(); i++)
		if ((S2.at(i) < '0') || (S2.at(i) > '9'))
			return (S1 + S2);
	if ((stoi(S1) <= 99999)&(stoi(S1) >= 0)&(stoi(S2) <= 99999)&(stoi(S2) >= 0))
	{
		int result = (stoi(S1) + stoi(S2));
		return to_string(result);
	}
	else
		return (S1 + S2);
}
	
string StringCopy()//取出第N个字符串第X个字符开始的长度为L的字符串。
{
	index++;
	int N, X, L;
	if (Command[index] == "find") N = StringFind();
	else if (Command[index] == "rfind") N = StringRfind();
	else N = stoi(Command[index]);
	index++;
	if (Command[index] == "find") X = StringFind();
	else if (Command[index] == "rfind") X = StringRfind();
	else X= stoi(Command[index]);
	index++;
	if (Command[index] == "find") L = StringFind();
	else if (Command[index] == "rfind") L = StringRfind();
	else L= stoi(Command[index]);
	return a[N - 1].substr(X, L);
}

int StringFind()//在第N个字符串中从左开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。
{
	index++;
	string S; int N;
	if (Command[index] == "copy") S = StringCopy();
	else if (Command[index] == "add") S = StringAdd();
	else S = Command[index];
	index++;
	if (Command[index] == "find") N = StringFind();
	else if (Command[index] == "rfind") N = StringRfind();
	else N = stoi(Command[index]);
	if(a[N - 1].find(S)!=string::npos)
		return a[N - 1].find(S);
	else return a[N - 1].length();
}

int StringRfind()//在第N个字符串中从右开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。
{
	index++;
	string S; int N;
	if (Command[index] == "copy") S = StringCopy();
	else if (Command[index] == "add") S = StringAdd();
	else S = Command[index];
	index++;
	if (Command[index] == "find") N = StringFind();
	else if (Command[index] == "rfind") N = StringRfind();
	else N = stoi(Command[index]);
	if (a[N - 1].rfind(S))
		return a[N - 1].rfind(S);
	else return a[N - 1].length();
}

void StringInsert()//在第N个字符串的第X个字符位置中插入S字符串。
{
	index++;
	string S; int N, X;
	if (Command[index] == "copy") S = StringCopy();
	else if (Command[index] == "add") S = StringAdd();
	else S = Command[index];
	index++;
	if (Command[index] == "find") N = StringFind();
	else if (Command[index] == "rfind") N = StringRfind();
	else N = stoi(Command[index]);
	index++;
	if (Command[index] == "find") X = StringFind();
	else if (Command[index] == "rfind") X = StringRfind();
	else X = stoi(Command[index]);
	a[N - 1].insert(X, S);
}

void StringReset()//将第N个字符串变为S。
{
	index++;
	string S; int N;
	if (Command[index] == "copy") S = StringCopy();
	else if (Command[index] == "add") S = StringAdd();
	else S = Command[index];
	index++;
	if (Command[index] == "find") N = StringFind();
	else if (Command[index] == "rfind") N = StringRfind();
	else N = stoi(Command[index]);
	a[N - 1] = S;
}
void StringPrint()//打印输出第N个字符串
{
	index++;
	int N;
	if (Command[index] == "find") N = StringFind();
	else if (Command[index] == "rfind") N = StringRfind();
	else N = stoi(Command[index]);
	cout << a[N - 1]<<endl;
}
void StringPriAll()//打印输出所有字符串
{
	for (int i = 0; i < n; i++)
		cout << a[i] << endl;
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 0; Command[i-1] != "over"; i++)
		cin >> Command[i];
	for (index=0;Command[index] != "over"; index++)
	{
		if (Command[index] == "insert") StringInsert();
		else if (Command[index] == "reset") StringReset();
		else if (Command[index] == "print") StringPrint();
		else if (Command[index] == "printall") StringPriAll();
		else if (Command[index] == "over") break;
	}
	return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值