版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_24994943/article/details/82260225
1、
#include <iostream>
using namespace std;
double HarmonicMean(const double &x, const double &y);
int main()
{
double x, y;
cin >> x >> y;
//输入两个数,直到其中一个为0结束程序
while (x != 0 && y != 0)
{
cout << HarmonicMean(x, y) << endl;
cin >> x >> y;
}
return 0;
}
//计算两个数的调和平均数
double HarmonicMean(const double & x, const double & y)
{
return 2.0*x*y / (x + y);
}
2、
#include <iostream>
using namespace std;
//将用户输入的数据保存在数组中
void inNumber(int a[], const int &b, const int &i);
//将保存的数组数据显示出来
void printNumber(const int a[], const int &n);
//计算数组中的平均成绩
double clacNumber(const int a[], const int &n);
int main()
{
int a[10];
int tmp;
int i;
cin >> tmp;
for (i = 0; i < 9 || tmp < 0; i++)
{
inNumber(a, tmp, i);
cin >> tmp;
}
inNumber(a, tmp, i);
printNumber(a, i + 1);
cout << clacNumber(a, i + 1);
return 0;
}
void inNumber(int a[], const int & b, const int & i)
{
a[i] = b;
}
void printNumber(const int a[], const int & n)
{
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
double clacNumber(const int a[], const int & n)
{
int count = 0;
for (int i = 0; i < n; i++)
{
count += a[i];
}
return count / n;
}
3、
#include <iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
//按值传递,显示b的成员的值
void Show(box b);
//按地址传递,设置volume的值
void Add(box *b);
int main()
{
box test = { "testBox",5,4,3,20 };
Show(test);
Add(&test);
cout << endl;
Show(test);
return 0;
}
void Show(box b)
{
cout << "maker: " << b.maker << endl;
cout << "height: " << b.height << endl;
cout << "width: " << b.width << endl;
cout << "length: " << b.length << endl;
cout << "volume: " << b.volume << endl;
}
void Add(box * b)
{
b->volume = b->height*b->width*b->length;
}
5、
#include <iostream>
using namespace std;
//计算一个数的阶层
int jieceng(const int &n);
int main()
{
int n;
cout << "请输入一个数(输入-1退出):";
cin >> n;
while (n != -1)
{
cout << n << "!= " << jieceng(n) << endl;
cin >> n;
}
return 0;
}
int jieceng(const int & n)
{
if (n == 1) return 1;
else return n * jieceng(n - 1);
}
6、
#include <iostream>
using namespace std;
//将值存入数组当中,返回实际输入多少个数字(输入非数字则停止输入)
int Fill_array(double a[], const int &n);
//显示数组内容
void Show_array(const double a[], const int &n);
//将存储在数组中的值反转
void Reverse_array(double a[], const int &n);
int main()
{
double a[6];
int n = Fill_array(a, 6);
Show_array(a, n);
Reverse_array(a, n);
Show_array(a, n);
return 0;
}
int Fill_array(double a[], const int & n)
{
double tmp;
int i = 0;
while (cin >> tmp && i < n)
{
a[i] = tmp;
i++;
}
return i;
}
void Show_array(const double a[], const int & n)
{
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
void Reverse_array(double a[], const int & n)
{
int i;
double tmp;
for (i = 0; i < n / 2; i++)
{
tmp = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = tmp;
}
}
7、
#include <iostream>
using namespace std;
double* Fill_array(double ar[], const int &limit);
void Show_array(const double ar[], const double *p);
void Revalue(double r, double ar[], const double *p);
int main()
{
double a[6];
double *p = Fill_array(a, 6);
Show_array(a, p);
Revalue(3.0, a, p);
Show_array(a, p);
return 0;
}
double* Fill_array(double ar[], const int & limit)
{
double tmp;
double *p = ar;
for (int i = 0; i < limit; i++, p++)
{
cout << "Enter value #" << (i + 1) << ":";
cin >> tmp;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input!input process terminated.\n";
break;
}
else if (tmp < 0)
break;
ar[i] = tmp;
}
return p;
}
void Show_array(const double ar[], const double *p)
{
const double *q = ar;
for (int i = 0; q != p; i++, q++)
{
cout << "Property #" << (i + 1) << ": $";
cout << ar[i] << endl;
}
cout << endl;
}
void Revalue(double r, double ar[], const double *p)
{
double *q = ar;
for (int i = 0; q != p; i++, q++)
ar[i] *= r;
}
8、
9、
#include <iostream>
#include <string>
using namespace std;
const int SLEN = 30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int opplevel;
};
//提示用户输入数据,并将数据存入pa数组当中,返回数组中元素的个数,若数组满或用户输入空白行结束
int getinfo(student pa[], int n);
//显示单个学生信息
void display1(student st);
//显示单个学生信息
void display2(const student * ps);
//显示一组学生的信息
void dispaly3(const student pa[], int n);
int main()
{
cout << "Enter class size: ";
int class_size;
cin >> class_size;
while (cin.get() != '\n')
continue;
student *ptr_stu = new student[class_size];
int entered = getinfo(ptr_stu, class_size);
for (int i = 9; i < entered; i++)
{
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
dispaly3(ptr_stu, entered);
delete[] ptr_stu;
cout << "Done.\n";
return 0;
}
int getinfo(student pa[], int n)
{
int i;
char temp[SLEN];
for (i = 0; i < n; i++)
{
cin.getline(temp, SLEN);
if (temp[0] == ' ') break;
int j;
for (j = 0; j < strlen(temp); j++)
{
pa[i].fullname[j] = temp[j];
}
pa[i].fullname[j] = '\0';
cin.getline(temp, SLEN);
for (j = 0; j < strlen(temp); j++)
{
pa[i].hobby[j] = temp[j];
}
pa[i].hobby[j] = '\0';
cin >> pa[i].opplevel;
while (cin.get() != '\n')
continue;
}
return i;
}
void display1(student st)
{
cout << "name: " << st.fullname << " hobby: " << st.hobby << " opplevel:" << st.opplevel << endl;
}
void display2(const student * ps)
{
cout << "name: " << ps->fullname << " hobby: " << ps->hobby << " opplevel:" << ps->opplevel << endl;
}
void dispaly3(const student pa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "第" << i + 1 << "个学生: ";
display1(pa[i]);
}
}
10、
#include <iostream>
using namespace std;
double add(double x, double y);
double sub(double x, double y);
double calculate(double x, double y, double(*pf)(double, double));
int main()
{
double x, y;
cin >> x >> y;
cout << x << " + " << y << " = " << calculate(x, y, add) << endl;
cout << x << " - " << y << " = " << calculate(x, y, sub) << endl;
return 0;
}
double add(double x, double y)
{
return x + y;
}
double sub(double x, double y)
{
return x - y;
}
double calculate(double x, double y, double(*pf)(double, double))
{
return pf(x, y);
}