字符串操作(C++程序设计第7周)
来源:互联网 发布:思科qos端口限速配置 编辑:程序博客网 时间:2024/06/07 15:31
问题描述
给定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结束。
输出
根据操作提示输出对应字符串。
样例输入
3329strjvcOpadfk48Ifjoqwoqejrinsert copy 1 find 2 1 2 2 2print 2reset add copy 1 find 3 1 3 copy 2 find 2 2 2 3print 3insert a 3 2printallover
样例输出
Op29adfk48358329strjvcOp29adfk4835a8
提示
1.推荐使用string类中的相关操作函数。
2.这道题其实难点在递归。
3.add S1 S2这个命令需要注意itoa(”83s”)返回值为83 。
4.代码还是写的有点乱啊,有些地方写的不好,写完就懒得改了
源码
#include <iostream>#include <string>#include <vector>#include <queue>#include <cstdlib>//atoi字符串转整数的函数#include <cstring>//memset函数头文件#include <cstdio>//sprintf函数头文件using namespace std;vector<string> strGiven;//存储被给的字符串queue<string> strCommand;//存储命令字符串char* strItoa = new char[101];//存储将整数转换的字符串,并作返回值使用void strStorage(const char* ch);//将输入的一行数据分为若干个命令参数,并存入队列当中bool strProcess();//开始处理一行命令,注意开头的命令只可能是“insert”“reset”“print”“printall”和“over”string commandOp();//递归算法处理一行中的命令组合string nyCopy(int n, int x, int l);//copy N X L:取出第N个字符串第X个字符开始的长度为L的字符串string nyAdd(string s1, string s2);//add S1 S2:判断S1,S2是否为0-99999之间的整数,若是则将其转化为整数做加法,若不是,则作字符串加法,返回的值为一字符串int nyFind(int n, string s);//find S N:在第N个字符串中从左开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。int nyRFind(int n, string s);//rfind S N:在第N个字符串中从右开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。void nyInsert(int n, int x, string s);//在第N个字符串的第X个字符位置中插入S字符串void nyReset(int n, string s);//将第N个字符串变为Svoid nyPrint(int n);//打印输出第N个字符串void nyPrintAll();//打印输出所有字符串int main(){ int n; cin >> n; for (int i = 0; i < n; i++) { string str; cin >> str; strGiven.push_back(str); } cin.get(); char* ch = new char[501]; while(1) { cin.getline(ch, 500, '\n'); strStorage(ch); int t = strProcess(); if (t == false) { break; } } delete ch; delete strItoa; return 0;}/*将输入的一行数据分为若干个命令参数,并存入队列当中*/void strStorage(const char* ch){ string str; for (int i = 0; ch[i] != '\0'; i++) { if (ch[i] != ' ') { str.push_back(ch[i]); } else { strCommand.push(str); str.clear(); } } //记录最后一个字符串 strCommand.push(str);}/*开始处理一行命令,注意开头的命令只可能是“insert”“reset”“print”“printall”和“over”遇到"over"时返回false,程序结束*/bool strProcess(){ string str = strCommand.front(); strCommand.pop(); if (str == "insert") { string s = commandOp(); int n = atoi(commandOp().c_str()); int x = atoi(commandOp().c_str()); nyInsert(n, x, s); } else if (str == "reset") { string s = commandOp(); int n = atoi(commandOp().c_str()); nyReset(n, s); } else if (str == "print") { int n = atoi(commandOp().c_str()); nyPrint(n); } else if (str == "printall") { nyPrintAll(); } else if (str == "over") { return false; } return true;}/*递归算法处理一行中的命令组合*/string commandOp(){ string str = strCommand.front(); strCommand.pop(); if (str == "copy") { int n = atoi(commandOp().c_str()); int x = atoi(commandOp().c_str()); int l = atoi(commandOp().c_str()); return nyCopy(n, x, l); } else if (str == "add") { string s1 = commandOp(); string s2 = commandOp(); return nyAdd(s1, s2); } else if (str == "find") { string s = commandOp(); int n = atoi(commandOp().c_str()); sprintf(strItoa, "%d", nyFind(n, s)); return strItoa; } else if (str == "rfind") { string s = commandOp(); int n = atoi(commandOp().c_str()); sprintf(strItoa, "%d", nyRFind(n, s)); return strItoa; } else//参数 { return str; }}/*copy函数*/string nyCopy(int n, int x, int l){ string str = strGiven.at(n-1); return str.substr(x, l);}/*add函数*/string nyAdd(string s1, string s2){ //判断字符串里是否全为数字 //因为83s经过atoi后会变为83 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; } } //若全为数字,判断是否在0和99999之间 long a = atoi(s1.c_str()); long b = atoi(s2.c_str()); if (a >= 0 && a <= 99999 && b >= 0 && b <= 99999) { long c = a + b; sprintf(strItoa, "%d", c); return strItoa; } else { return s1 + s2; }}/*find函数*/int nyFind(int n, string s){ int pos = strGiven.at(n-1).find(s); if (pos == string::npos) { return s.size(); } else { return pos; }}/*rfind函数*/int nyRFind(int n, string s){ int pos = strGiven.at(n-1).rfind(s); if (pos == string::npos) { return s.size(); } else { return pos; }}/*insert函数*/void nyInsert(int n, int x, string s){ strGiven.at(n-1).insert(x, s);}/*reset函数*/void nyReset(int n, string s){ strGiven.at(n-1).swap(s);}/*print函数*/void nyPrint(int n){ cout << strGiven.at(n-1) << endl;}/*printall函数*/void nyPrintAll(){ for (unsigned int i = 0; i < strGiven.size(); i++) { cout << strGiven.at(i) << endl; }}
0 0
- 字符串操作(C++程序设计第7周)
- 字符串中次数第2多的字母(C程序设计进阶第9周)
- 编程题#4: 字符串操作(C++程序设计第9周)
- 第7周 C语言及程序设计提高例程-28 字符串、字符数组、字符指针
- 第7周 C语言及程序设计提高例程-29 实战字符串处理四例
- 第10章 C语言及程序设计提高例程-37 操作字符串数组
- 第四周《C语言及程序设计》实践项目37 操作字符串数组
- 第7周--C++“窗口”程序设计
- C02-程序设计基础提高班(C++)第14周上机任务-文件操作
- c基础第7课-数组及简单字符串操作
- 第7周 C语言及程序设计提高例程-26 实现线性表基本操作的函数
- 编程题#7:字符串排序(C++程序设计第10周)
- 第8周 C语言及程序设计提高例程-30 字符和字符串处理函数
- 第8周 C语言及程序设计提高例程-31 编制自己的字符串函数
- Linux-C程序设计大全 第4章 指针与字符串
- 《高质量程序设计指南——C/C++》第7章 C/C++指针、数组和字符串
- 文件操作与模板编程题 #1(C++程序设计第7周)
- 第10周 C语言及程序设计提高例程-35 使用指针操作二维数组
- 【JAVA笔记——道】并发编程CAS算法
- POJ 3984 迷宫问题
- 写在2015结束、2016即将开始之际
- 蓝桥杯之特殊回文数
- 关于使用while循环时,括号内用自加自减作条件的一个疑问
- 字符串操作(C++程序设计第7周)
- online_judge_1148
- 程序猿如何提高自己的搜索技能
- java根据IP地址获取本地定位
- Elastic search点点滴滴
- iOS学习笔记53-Xcode设置项之Architectures和Valid Architectures
- Ubuntu14.04 使用visualBox安装虚拟机出现VT-x is disabled in the BIOS错误
- UVA10391 Compound Words
- 使用Tomcat发布网站