以下答案本人在linux vscode中均已亲自测试编译通过,完美运行.
复习题8:
#include<iostream>
using namespace std;
int replace(char * str, char c1, char c2);
int main()
{
const int size = 20;
char str[size] = "ajajjaca";
cout << str << endl;
replace(str,'a','@');
cout << str << endl;
}
int replace(char * str, char c1, char c2)
{
int count =0;
for(int i=0; str[i] != '\0'; i++ )
{
if(str[i] == c1)
{
str[i] = c2;
count++;
}
}
cout << count << endl;
}
复习题12:
#include<iostream>
using namespace std;
struct appliant
{
char name[30];
int credit_ratings[3];
};
void show1(appliant see);
void show2(appliant * see);
int main()
{
appliant wu{"wu1234456",1,2,3};
show1(wu);
show2(&wu);
}
void show1(appliant see)
{
cout << see.name << endl;
for(int i=0; i < 3; i++ )
cout << see.credit_ratings[i] << endl;
}
void show2(appliant * see)
{
cout << see->name << endl;
for(int i=0; i < 3; i++ )
cout << see->credit_ratings[i] << endl;
}
编程练习:
7.1
#include<iostream>
using namespace std;
double funave(double x, double y);
int main()
{
double x, y,ave;
cout << "请输入两个数:";
while(cin >> x >> y)
{
if(0 == x || y == 0)
break;
ave = funave(x, y);
cout << "调和平均数=" << ave << endl;
cout << "请输入两个数:";
}
}
double funave(double x, double y)
{
double ave = 0.0;
ave = 2.0 * x * y / (x + y);
return ave;
}
7.2
#include<iostream>
using namespace std;
void input(double score[],int size);
void show(double score[]);
double funave(double score[]);
int main()
{
const int size = 10;
double score[size]{0};
double ave = 0;
input(score,size);
show(score);
ave = funave(score);
cout << "\n平均成绩:" << ave << endl;
}
void input(double score[], int size)
{
int i = 0;
cin >> score[i];
while((cin.get() != '\n') && (i < size)) //可以提前结束输入
{
i++;
cin >> score[i];
}
}
void show(double score[])
{
int i = 0;
while(score[i] != 0