目录
4.编写一个函数,其功能是在字符串中查找某个子串,并用另一个串代替之,输出新串。
1.计算e的x次方。
原题,见11年第一题。
#include<iostream>
#include <iomanip>
#include<cmath>
using namespace std;
int main() {
double x ;
double ans=1;//结果
int n = 1;
cout << "请输入x的值:";
cin >> x;
double temp=1;
do{
temp = temp*x/n;
ans += temp;
n++;
} while (fabs(temp) >= 10e-10);
cout << "e^" <<x<<"=" << fixed << setprecision(10) << ans << endl;
return 0;
}
2.解析字符串输出
#include<iostream>
#include<string>
using namespace std;
void print(string s){
if (s.length() == 0)
return;
else {
if (s.length() == 1) {
/*const char* c = s.data(); //也可以是s.c_str()
int n = (*c)-'0';*/
int n = s[0] - '0'; //其实用s[0]就可以了。
if (n >= 0 && n <= 9)
/*cout << *c << " ";*/
cout << n;
}
else {
print(s.substr(1));
print(s.substr(0, 1));
}
}
}
int main() {
string test = "1 x 2 c 3 v 4 v";
print(test);
}
输出样例上是逆序的,其实在递归里先递归后面的就行了,所以我们看上去的一个 print(s.substr(0,1));其实是最后执行的。
补充:data()、c_str()、copy
1、c_str():生成一个const char*指针,指向以空字符终止的数组。
int main()
{
const char* c;
string s="1234";
c = s.c_str();
cout<<c<<endl; //输出:1234
s="abcd";
cout<<c<<endl; //输出:abcd
return 0;
}
我们发现c是永远指向s的,如果不需要指针这样的关系,可以这么做:
int main()
{
char* c=new char[20];
string s="1234";
//c = s.c_str();
strcpy(c,s.c_str());
cout<<c<<endl; //输出:1234
s="abcd";
cout<<c<<endl; //输出:1234
return 0;
}
2、data():与c_str()类似,但是返回的数组不以空字符终止。
//去看了一些人的文章,这里其实也没太多区别。data是c++风格,c_str()顾名思义是c风格
3、copy(p,n,size_type _Off = 0):(从size_type _Off开始,赋值n个字符到p中)。
3.骰子
#include<iostream>
#include <iomanip>
using namespace std;
int main() {
srand((unsigned)time(0));
int B[13] = {0};
int A[2][500];
for (int i = 0; i < 500; i++) {
int a = rand() % 6 + 1;
int b = rand() % 6 + 1;
A[0][i] = a;
A[1][i] = b;
B[a + b]++;
}
for (int i = 2; i < 13; i++)
cout << setw(2) << i << ":" << setw(2) << B[i]<<"次";
}
//不知道A有什么意义,仅仅是考一下二维数组存储吗?
4.编写一个函数,其功能是在字符串中查找某个子串,并用另一个串代替之,输出新串。
1.代码
#include<iostream>
#include<string>
using namespace std;
void FindRep(char str[], char f[], char r[]) {
string s1(str);
string s2(f);
string s3(r);
size_t pos = s1.find(s2);
while (pos != string::npos) {
s1.replace(pos, s2.length(), s3);
pos = s1.find(s2,pos);
}
cout << s1 << endl;
}
int main(){
char s1[] = "nan jing you dian da xue";
char s2[] = "you dian";
char s3[] = "li gong";
FindRep(s1, s2, s3);
}
nan jing li gong da xue
2.上面代码有几个细节:
1.用字符数组可以初始化字符串类型对象。
2.find函数有几种重载
size_type find(const string & str, size_type pos = 0) const | 从pos位置开始查找子串str |
size_type find(const char * s, size_type pos = 0) const | 从pos位置开始查找子串s,s为字符数组首地址 |
3.注意replace函数的三个参数 ,分别为:替换位置,要替换的长度,替换串。
当然,也有这种可能用的比较多的重载:
string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
替换位置,替换长度,替换串,替换串开始位置,替换串长度
3.size_t
size_t 是一些C/C++标准在stddef.h中定义的,size_t 类型表示C中任何对象所能达到的最大长度,它是无符号整数。find函数的返回值和replace的参数类型都是size_t,这是值得注意的。另外,string::npos表示size_t类型数据的最大值。
总之对于字符串的循环操作判断语句可以设为pos!=string::pos。
5Teacher类
#include<string>
#include<iostream>
#include <iomanip>
using namespace std;
class Teacher {
private:
int ID;
string name;
string sex;
int birth;
int hire;
public:
Teacher(int id, string n, string s, int b, int h) :ID(id), name(n), sex(s), birth(b), hire(h) {}
Teacher(const Teacher& a) {
ID = a.ID;
name = a.name;
sex = a.sex;
birth = a.birth;
hire = a.hire;
}
void judgeRetire()const {
if (sex == "Male" && (2016 - birth / 10000) >= 55)
cout << "Name:" << name << " ID:" << setw(3) << setfill('0') << ID << endl;
else if (sex == "Female" && (2016 - birth / 10000) >= 60)
cout << "Name:" << name << " ID:" << setw(3) << setfill('0') << ID << endl;
}
void keepHire()const {
if (sex == "Female" && (2016 - birth / 10000) >= 60 && (2016 - hire / 10000) <= 35)
cout << "Name:" << name << " ID:"<<setw(3)<<setfill('0') << ID << endl;
}
};
int main() {
Teacher t1(000, "tom", "Male", 19601010, 19900530);
Teacher t2(001, "lucy", "Female", 19551010, 19950530);
Teacher t3(000, "jack", "Male", 19651010, 19950530);
cout << "Teachers need retire:"<<endl;
t1.judgeRetire();
t2.judgeRetire();
t3.judgeRetire();
cout << "Teachers need keep hire:"<<endl;
t1.keepHire();
t2.keepHire();
t3.keepHire();
}
Teachers need retire:
Name:tom ID:000
Name:lucy ID:001
Teachers need keep hire:
Name:lucy ID:001