试卷一
五、编程题
建立一个数组类ARR,求一个整型数组的平均值。
#include<iostream>
using namespace std;
class ARR
{
int n;//数组实际元素个数
int a[100] = {};//存放数组元素
double aver;//存放整型数组的平均值
public :
ARR(int x[], int size);
void Average();
void Show();
};
ARR::ARR(int x[],int size)//构造函数,用参数size初始化n,用x数组初始化a数组
{
n = size;
for (int i = 0; i < 100; i++)
a[i] = x[i];
aver = 0;
}
void ARR::Average()//求整形数组的平均值
{
double s = 0;
for (int i = 0; i < n; i++)
s = s + a[i];
aver = s / n;
}
void ARR::Show()//每行5个输出,同时输出平均值
{
for (int i = 0; i < n; i++)
{
cout << a[i] << '\t';
if ((i + 1) % 5 == 0)cout << endl;
}
cout << endl;
cout << "aver=" << aver << endl;
}
int main()
{
int b[] = { 3,4,6,8,10,34,2 };
ARR v(b, sizeof(b) / sizeof(int));
v.Average();
v.Show();
return 0;
}
试卷二
五、编程题
建立一个数组类ARR,求一个整型数组中正数和负数的个数。
#include<iostream>
using namespace std;
class ARR
{
int n;//数组实际元素个数
int a[100] = {};//存放数组元素
int pos;//存放整形数组中正数的个数
int neg;//存放整形数组中负数的个数
public:
ARR(int x[], int size);
void Num();
void Show();
};
ARR::ARR(int x[], int size)//构造函数,用参数size初始化n,用x数组初始化a数组 pos neg初始化为0
{
n=size;
for (int i = 0; i < 100; i++)
a[i] = x[i];
pos = 0; neg = 0;
}
void ARR::Num()//求整形数组中正数和负数的个数
{
for (int i = 0; i < n; i++)
if (a[i] > 0) pos++;
neg = n - pos;
}
void ARR::Show()//每行5个输出 输出正数和负数的个数
{
for (int i = 0; i < n; i++)
{
cout << a[i] << '\t';
if ((i + 1) % 5 == 0)cout << endl;
}
cout << endl;
cout << "pos=" << pos << '\t' << "peg=" << neg << endl;
}
int main()
{
int b[] = { 3,-4,6,-8,10,-34,2,80,-100 };
ARR v(b, sizeof(b) / sizeof(int));
v.Num();
v.Show();
return 0;
}
试卷三
五、编程题
建立一个数组类ARR,求一个整形数组中所有是素数的元素及这些元素的个数。
#include<iostream>
using namespace std;
class ARR
{
int n;//数组实际元素个数
int a[100] = {}, b[100] = {};//存放数组元素
int nprime;//存放整型数组a中素数的个数
public :
ARR(int x[], int size);
int Prime(int x);
void Fun();
void Show();
};
ARR::ARR(int x[],int size)//构造函数,用参数size初始化n,用x数组初始化a数组,将nprime初始化为0
{
n = size;
for (int i = 0; i < 100; i++)
a[i] = x[i];
nprime = 0;
}
int ARR::Prime(int x)//判断整数x是否是素数,若是则返回1,否则返回0
{
for (int i = 2; i < x; i++)
if (x % i == 0)
return 0;
return 1;
}
void ARR::Fun()//求整形数组中所有是素数的元素及素数的个数
{
int j = 0;
for (int i = 0; i < n; i++)
if (Prime(a[i]))
{
b[j] = a[i];
j++;
}nprime = j;
}
void ARR::Show()//每行5个输出,同时输出素数的个数
{
for (int i = 0; i < nprime; i++)
{
cout << b[i] << '\t';
if ((i + 1) % 5 == 0)cout << endl;
}
cout << endl << "nprime=" << nprime << endl;
}
int main()
{
int b[] = { 13,14,15,16,17,18,19,20,21,22 };
ARR arr(b, sizeof(b) / sizeof(int));
arr.Fun();
arr.Show();
return 0;
}
试卷四
五、编程题
建立一个数组类ARR,求一个整型数组所有元素中的最大值及该最大值在数组中的序号(从1开始)
#include<iostream>
using namespace std;
class ARR
{
private:
int n;//数组实际元素个数
int a[100] = {};//存放数组元素
int max;//存放整形数组中的最大值
int maxindex;//存放整形数组中最大值的序号
public:
ARR(int x[], int size)//构造函数,用参数size初始化n,用x数组初始化a数组
{
n = size;
for (int i = 0; i < n; i++)
a[i] = x[i];
max = a[0];
maxindex = 1;
}
void Findmax()//求整形数组元素中的最大值及最大值的序号
{
for (int i = 0; i < n; i++)
if (max < a[i])
{
max = a[i];
maxindex = i + 1;
}
}
void Show()//输出
{
for (int i = 0; i < n; i++)
{
cout << a[i] << '\t';
if ((i + 1) % 5 == 0)cout << endl;
}
cout << "\nmax=" << max << '\t' << "maxindex=" << maxindex << endl;
}
};
int main()
{
int b[] = { 3,4,6,8,10,34,2 };
ARR arr(b, sizeof(b) / sizeof(int));
arr.Findmax();
arr.Show();
return 0;
}
试卷五
五、编程题
建立一个梯形法求f(x)在a-b的定积分的类integral
#include<iostream>
#include<cmath>
using namespace std;
class integral
{
private:
double a, b, area;
int n;
public:
integral();
double Calcul(double aa, double bb,int nn, double(*f)(double));
void Show();
};
integral::integral() { a = 0; b = 0; n = 1; }
double integral::Calcul(double aa,double bb, double nn, double(*f)(double))
{
a = aa; b = bb; n = nn;
double h = (b - a) / n;
area = (f(a) + f(b)) / 2;
for (int i = 0; i < n; i++) area = area + f(a + i * h);
area = h * area;
return area;
}
void integral::Show()
{
cout << "area=" << area << endl;
}
int main()
{
integral jf;
jf.Calcul(2, 4,1000, sin);
jf.Show();
return 0;
}
试卷六
五、编程题
定义一个类ARRAY,实现对一维整形数组的排序,将一维数组中各元素按其各位的数字之和从小到大排序。
#include<iostream>
#include<cmath>
using namespace std;
class ARRAY
{
int a[100] ={};
int n;
public:
ARRAY(int t[], int n);
int sum(int(x));
void fun();
void print();
};
ARRAY::ARRAY(int t[], int m)
{
n = m;
for (int i = 0; i < n; i++)a[i] = t[i];
}
int ARRAY::sum(int x)//求整数x的各位数字之和,并返回该值,此函数供成员函数fun调用
{
int s = 0;
while (x)
{
s = s + x % 10;
x = x / 10;
}
return s;
}
void ARRAY::fun()//按要求对数组a的元素排序
{
int i, j;
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(sum(a[j])>sum(a[j+1]))
{
int t;
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
void ARRAY::print()//输出
{
for (int i = 0; i < n; i++)cout << a[i] << '\t';
cout << endl;
}
int main()
{
int a[] = { 297,735,624,158,312,900 };
ARRAY arr(a, sizeof(a) / sizeof(int));
arr.print();
arr.fun();
arr.print();
return 0;
}