## 5.8.14 编写一个函数，它从字符串s1中删除所有在s2中出现的字符，函数返回删除的字符个数。

``````/*
P166 5.8.14

*/
#include <iostream>
using namespace std;

bool isin(char a, char b[])
{
int i = 0;
while (b[i] != '\0')
{
if (b[i++] == a)
return true;
}
return false;
}

int main()
{
char s1[100], s2[100];
int count;
while (1)
{
count = 0;
cout << "string1:";
cin >> s1;
cout << "string2:";
cin >> s2;
int i, j;
i = j = 0;
while (s1[i] != '\0')
{
if (isin(s1[i], s2))
{
i++;
}
else
{
s1[j++] = s1[i++];
}
}
s1[j] = '\0';
count = i - j;
cout << "result:" << s1 <<"    删除字符数："<<count<< endl;
}
}``````

## 5.8.15 编写一个函数；要求该函数能够完成把字符串str中的所有子串都替换成指定字符串，返回值为替换的次数。

``````/*
P166 5.8.15

*/
#include <iostream>
using namespace std;

int find(char a, char b[])
{
int  i = 0;
while (b[i] != '\0')
{
if (b[i++] == a)
return true;
}
return false;
}

bool cmp(char a[],const char b[], int n)
{
int i = 0;
while (b[i] != '\0'&&a[n++] == b[i++]);
if (b[i] == '\0' || b[i - 1] == '\0')
return true;
return false;
}
int strlen1(const char a[])
{
int i=0;
while (a[i] != '\0')
i++;
return i;
}

int find_replace_str(char str[], const char find_str[], const char replace_str[])
{
char t[100];
int i, k;
i = k = 0;
int count = 0;
int lf = strlen1(find_str);
while (str[i] != '\0')
{
if (cmp(str, find_str, i))
{
count++;
int l = 0;
while (replace_str[l] != '\0')
t[k++] = replace_str[l++];
i +=lf;
}
else
{
t[k++] = str[i++];
}
}
t[k] = '\0';
i = 0;
while (t[i] != '\0')
{
str[i] = t[i];
i++;
}
str[i] = '\0';
return count;
}

int main()
{
char a[100], b[100], c[100];
int count;
while (1)
{
cout << "originalString:";
cin >> a;
cout << "searchString:";
cin >> b;
cout << "replaceString:";
cin >> c;
count = find_replace_str(a, b, c);
cout << "transmittedString:" << a << "  deleted:" << count << " times." << endl;
}
}``````

## 5.8.16 编写一个程序，从键盘输入一批学生的成绩信息，每个学生的成绩信息爱包括：学号、姓名以及8们课程的成绩。然后按照平均成绩由高到低顺序输出学生的学号、姓名以及平均成绩。

``````/*
P166 5.8.16

*/
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;

struct Student
{
string id;
string name;
int s1, s2, s3, s4, s5, s6, s7, s8;
int av;
};

bool cmp(Student s1,Student s2)
{
if (s1.av > s2.av)
return true;
return false;
}

int main()
{
int n;
Student stu[100];
while (1)
{
cout << "学生数：";
cin >> n;
for (int i = 1; i <= n; i++)
{
cout << "学生" << i << ":" << endl;
cout << "学号：";
cin >> stu[i].id;
cout << "姓名：";
cin >> stu[i].name;
cout << "课程1成绩：";
cin >> stu[i].s1;
cout << "课程2成绩：";
cin >> stu[i].s2;
cout << "课程3成绩：";
cin >> stu[i].s3;
cout << "课程4成绩：";
cin >> stu[i].s4;
cout << "课程5成绩：";
cin >> stu[i].s5;
cout << "课程6成绩：";
cin >> stu[i].s6;
cout << "课程7成绩：";
cin >> stu[i].s7;
cout << "课程8成绩：";
cin >> stu[i].s8;
stu[i].av = (stu[i].s1 + stu[i].s2 + stu[i].s3 + stu[i].s4 + stu[i].s5 + stu[i].s6 + stu[i].s7 + stu[i].s8) / 8;
}
sort(stu+1, stu+1+n, cmp);
cout << "平均成绩排名：" << endl;
for (int i = 1; i <= n; i++)
{
cout << setw(4) << i << "学号：" << stu[i].id << "    姓名：" << stu[i].name << "    平均成绩：" << stu[i].av << endl;
}
}
}``````

## 5.8.18 写一个函数map，它有三个参数。第一个参数是一个一维double型数组，第二个参数为数组元素个数，第三个参数是一个函数指针，它指向带有一个double型参数、返回值类型为double的函数。函数map的功能是把数组的每个元素替换成：用它原来的值（作为参数）调用第三个参数所指向的函数得到的值。

``````/*
P166 5.8.18

*/
#include <iostream>
using namespace std;

void map(double a[], int n, double (*fp)(double &b))
{
for (int i = 0; i < n; i++)
a[i] = fp(a[i]);
}

{
return ++a;
}

int main()
{
double a[100];
int n;
while (1)
{
cout << "数组长度：";
cin >> n;
cout << "数组：";
for (int i = 0; i < n; i++)
cin >> a[i];
cout << "转换后：" << endl;
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << endl;
}
}``````

## 5.8.19 把在链表中插入一个新结点的操作写成一个函数：

bool insert(Node *&h,int a,int pos);

``````/*

bool insert(Node *&h,int a,int pos);

true，否则返回false。
*/
#include <iostream>
using namespace std;

struct Node
{
int val;
Node *next;
};

Node *insert(Node *&h, int a, int pos=0)
{
if (h == NULL)
{
h = new Node;
h->val = a;
h->next = NULL;
return h;
}
Node *p = new Node;

p->val = a;
p->next = NULL;
if (pos == 0)
{
p->next = h;
h = p;
return h;
}
Node *q=h;
while (pos - 1)
{
if (q == NULL)
{
cout << "can't add" << endl;
return h;
}
q = q->next;
pos--;
}
p->next = q->next;
q->next = p;
return h;
}

void print(Node *h)
{
while (h != NULL)
{
cout << h->val;
h = h->next;
}
cout << endl;
}

int main()
{
while (1)
{
int t;
int pos;
Node *h = NULL;
while (1)
{
cout << "插入值和位置（最后一个以-1结束）：";
cin >> t >> pos;
if (t == -1)
break;
insert(h, t,pos);
}
cout << "链表："<<endl;
print(h);
}
}``````