Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 68f3158

Browse files
upload
1 parent 2b69863 commit 68f3158

19 files changed

+1542
-5
lines changed

‎3.C++程序设计/week7/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,6 @@ cout << f4 << endl << f5 << endl;
477477
- `data()`:`const char *p = str.data()`
478478
- `copy()`
479479
480-
```c++
481-
482-
```
483-
484480
### 其他特性
485481
486482
- 成员函数 `capacity()` :返回无需增加内存即可存放的字符数

‎3.C++程序设计/week7/编程练习/README.md

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,138 @@ class S
111111

112112
### 解题思路
113113

114-
没啥好说的,就是运用各种格式输出符。
114+
没啥好说的,就是运用各种格式输出符。
115+
116+
117+
118+
## 编程题#3: 整数的输出格式
119+
120+
[来源: POJ ](http://cxsjsxmooc.openjudge.cn/test/G/)(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
121+
122+
**注意: 总时间限制: 1000ms 内存限制: 1000kB**
123+
124+
### 描述
125+
126+
利用流操纵算子实现: 输入一个整数,先将该整数以十六进制输出,然后再将该整数以10个字符的宽度输出,宽度不足时在左边补0。
127+
128+
注意:在不同系统、编译器上的输出格式略有不同,但保证在程序中采用默认格式设置一定能在OJ平台上得到正确结果。
129+
130+
### 输入
131+
132+
一个正整数,保证可以用int类型存储。
133+
134+
### 输出
135+
136+
第一行:以十六进制输出该整数;
137+
138+
第二行:以10个字符的宽度输出该整数。
139+
140+
### 样例输入
141+
142+
```
143+
23
144+
```
145+
146+
### 样例输出
147+
148+
```
149+
17
150+
0000000023
151+
```
152+
153+
154+
155+
## 编程题#4: 字符串操作
156+
157+
[来源: POJ](http://cxsjsxmooc.openjudge.cn/test/U/) (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
158+
159+
**注意: 总时间限制: 1000ms 内存限制: 65536kB**
160+
161+
### 描述
162+
163+
给定n个字符串(从1开始编号),每个字符串中的字符位置从0开始编号,长度为1-500,现有如下若干操作:
164+
165+
copy N X L:取出第N个字符串第X个字符开始的长度为L的字符串。
166+
167+
add S1 S2:判断S1,S2是否为0-99999之间的整数,若是则将其转化为整数做加法,若不是,则作字符串加法,返回的值为一字符串。
168+
169+
find S N:在第N个字符串中从左开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。
170+
171+
rfind S N:在第N个字符串中从右开始找寻S字符串,返回其第一次出现的位置,若没有找到,返回字符串的长度。
172+
173+
insert S N X:在第N个字符串的第X个字符位置中插入S字符串。
174+
175+
reset S N:将第N个字符串变为S。
176+
177+
print N:打印输出第N个字符串。
178+
179+
printall:打印输出所有字符串。
180+
181+
over:结束操作。
182+
183+
其中N,X,L可由find与rfind操作表达式构成,S,S1,S2可由copy与add操作表达式构成。
184+
185+
### 输入
186+
187+
第一行为一个整数n(n在1-20之间)
188+
189+
接下来n行为n个字符串,字符串不包含空格及操作命令等。
190+
191+
接下来若干行为一系列操作,直到over结束。
192+
193+
### 输出
194+
195+
根据操作提示输出对应字符串。
196+
197+
### 样例输入
198+
199+
```
200+
3
201+
329strjvc
202+
Opadfk48
203+
Ifjoqwoqejr
204+
insert copy 1 find 2 1 2 2 2
205+
print 2
206+
reset add copy 1 find 3 1 3 copy 2 find 2 2 2 3
207+
print 3
208+
insert a 3 2
209+
printall
210+
over
211+
```
212+
213+
### 样例输出
214+
215+
```
216+
Op29adfk48
217+
358
218+
329strjvc
219+
Op29adfk48
220+
35a8
221+
```
222+
223+
### 提示
224+
225+
推荐使用string类中的相关操作函数。
226+
227+
### 解题思路
228+
229+
以下是我的解题方法:
230+
231+
观察对字符串操作需要的输入有:`int`, `string`,以此为依据把编写两个`get_int()`, `get_str()` 函数
232+
233+
然后把对字符串的操作根据返回值分为三类
234+
235+
- 返回`int`:`find()`, `rfind()`, 放到 `get_int()`里面处理
236+
- 返回`string `:`copy()`,`add()`, 放到`get_str()` 里面处理
237+
- 无返回值:是每一行操作的开始,所以放在主函数处理
238+
239+
函数操作中需要的数据就不直接用`cin`而是用`get_int()``get_str()`,这样可以处理类似 `insert copy 1 find 2 1 2 2 2` 这条语句里面的叠加操作
240+
241+
```c++
242+
insert copy 1 find 2 1 2 2 2
243+
insert (copy 1 (find 2 1 2) 2) 2
244+
```
245+
246+
- 这样分解看来有点像堆栈的操作,不过做这个作业的时候还没学算法。
247+
248+
另外还有一个要注意的是!coursera的编译器好像不支持c++11的特性,我用了`stoi()`, `to_string()` 这两个函数一直是CE,不用这两个就AC了。
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#include <iostream>
2+
#include <cctype>
3+
#include <cstdlib>
4+
#include <cstdio>
5+
#include <string>
6+
using namespace std;
7+
8+
string str[21];
9+
int NumOfStr = 0;
10+
11+
12+
bool isNum(string s)
13+
{
14+
for (int i = 0; i < s.length(); i++)
15+
{
16+
if (!isdigit(s[i]))
17+
{
18+
return false;
19+
}
20+
}
21+
return true;
22+
}
23+
24+
int find()
25+
{
26+
char s;
27+
int n;
28+
cin >> s >> n;
29+
return str[n].find(s);
30+
}
31+
32+
int rfind()
33+
{
34+
char s;
35+
int n;
36+
cin >> s >> n;
37+
return str[n].rfind(s);
38+
}
39+
40+
int get_int()
41+
{
42+
string str;
43+
cin >> str;
44+
if (isNum(str))
45+
{
46+
return atoi(str.c_str());
47+
}
48+
else
49+
{
50+
if (str == "find")
51+
{
52+
return find();
53+
}
54+
else if (str == "rfind")
55+
{
56+
return rfind();
57+
}
58+
}
59+
return 0;
60+
}
61+
62+
63+
64+
string copy()
65+
{
66+
int n, x, l;
67+
n = get_int();
68+
x = get_int();
69+
l = get_int();
70+
return str[n].substr(x, l);
71+
}
72+
73+
string add();
74+
75+
string get_str()
76+
{
77+
string str;
78+
cin >> str;
79+
if (str == "copy")
80+
{
81+
return copy();
82+
}
83+
else if (str == "add")
84+
{
85+
return add();
86+
}
87+
else
88+
{
89+
return str;
90+
}
91+
}
92+
93+
string add()
94+
{
95+
string s1 = get_str();
96+
string s2 = get_str();
97+
int n1 = atoi(s1.c_str());
98+
int n2 = atoi(s2.c_str());
99+
if (isNum(s1) && isNum(s2) && n1 >= 0 && n1 <= 99999 && n2 >= 0 && n2 <= 99999)
100+
{
101+
char str[10];
102+
int a = n1+n2;
103+
sprintf(str, "%d", a);
104+
return str;
105+
}
106+
else
107+
{
108+
return s1 + s2;
109+
}
110+
}
111+
112+
113+
114+
void insert()
115+
{
116+
string s = get_str();
117+
int n = get_int();
118+
int x = get_int();
119+
str[n].insert(x, s);
120+
}
121+
122+
void reset()
123+
{
124+
string s = get_str();
125+
int n = get_int();
126+
str[n] = s;
127+
}
128+
129+
void print()
130+
{
131+
int n = get_int();
132+
cout << str[n] << endl;
133+
}
134+
135+
void printall()
136+
{
137+
for (int i = 1; i <= NumOfStr; i++)
138+
{
139+
cout << str[i] << endl;
140+
}
141+
}
142+
143+
int main()
144+
{
145+
cin >> NumOfStr;
146+
for (int i = 1; i <= NumOfStr; i++)
147+
{
148+
cin >> str[i];
149+
}
150+
while (true)
151+
{
152+
string operation;
153+
cin >> operation;
154+
if (operation == "insert")
155+
{
156+
insert();
157+
}
158+
else if (operation == "reset")
159+
{
160+
reset();
161+
}
162+
else if (operation == "print")
163+
{
164+
print();
165+
}
166+
else if (operation == "printall")
167+
{
168+
printall();
169+
}
170+
else if (operation == "over")
171+
{
172+
break;
173+
}
174+
}
175+
return 0;
176+
177+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /