c++final exam test 2//20191222

Dec.22/2019
#include"iostream"//1.3  1.28
#include< iomanip>
using namespace std;
void main()
{ double a=1.2846; 
  cout<< setprecision(2)<<a<<endl;
  cout<<setiosflags(ios::fixed)<<setprecision(2)<<a<<endl; 
}
template <class T>//或<typename T>
T square( T  x )
{ return  x*x; }
#include"iostream"
using namespace std;
long factorial(int n)
{
	long f = 1;
	for (int i = 1; i <= n; i++)
		f = f * i;
	return f;
}
int main()
{
	long f;
	int n;
	cout << "程序功能是求n的阶乘值" << endl;
	cout << "请输入n值:";
	cin >> n;
	if (n > 0)
		cout << n << "的阶乘值=" << factorial(n) << endl;
	if (n == 0)
		cout << n << "的阶乘值=" << 1 << endl;
	if (n < 0)
		cout << n << "是负数,没有阶乘值!" << endl;
	return 1;
}
#include <iostream>   //左移
using namespace std;
void move(int arr[], int num , int digit)
	{
		int x;
		for(int i=1;i<=digit ; i++)
		{
			x=arr[0];
			for(int j=1;j<num;j++)
				  arr [j-1] = arr[j];
			arr[num-1]=x;
		}
	}
void output(int arr[] , int num)
{
	for(int i=0;i<num;i++)
		cout<<arr[i]<<" ";
	cout<<endl;
}
int main()   
{	int a[50];
		int n , w;//n表示数据个数,w要移动的位数
		cout<<"请输入数据个数:";
		cin>>n;
		cout<<"请输入"<<n<<"个数据:";
		for(int i=0;i<n;i++)
			cin>>a[i];
		cout<<"请输入要移动的位数:";
		cin>>w;
		move( a , n , w );
		cout<<"左移后:";
		output( a , n);
		return 0;
}
void func (int a , int &b )//0  2
{  	a++;   
    b++;  
}
int main()
{ int x=0,y=1;
func(x , y) ;
cout<<x<<”  ”<<y<<endl;
	 return 0;
}
#include"iostream"//3  18     3??(符合一次累加1)
using namespace std;
int main()
{
	int i, count = 0, sum = 0;
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	for (i = 0; i < 10; i++)
		if (a[i] % 3 == 0)
		{
			count++;
			sum += a[i];
		}
	cout << count << "  " << sum << endl;
	return 0;
}
#include<iostream>//0 2 2
using namespace std;
class Test
{
public:
	Test()
	{
		cnt++;
	}
	~Test()
	{
		cnt--;
	}
	static int Count()
	{
		return cnt;
	}
private:
	static int cnt;
};
int Test::cnt = 0;
int main()
{
	cout << Test::Count() <<" ";
	Test t1, t2;
	cout << Test::Count() <<" ";
	cout << Test::Count() << endl;
	return 0;
}
#include <iostream>
using namespace std;
class shape
{
	char x;
public:
	shape(char m)
	{
		x = m;
		cout << "构造" << x << endl;
	}
	~shape()
	{
		cout << "析构" << x << endl;
	}
};
int main()
{
	shape* b = new  shape('b');
	delete b;
	return 0;
}
构造b
析构b
            //此处空一行
#include <iostream>//重载,vs2019,clion的array貌似关键名,这里全部arrat改成大写ARRAY
#include<iomanip>//11 22
using namespace std;
const int N = 2;
class ARRAY
{
private:
	double a[N];
public:
	ARRAY() {};
	ARRAY(double x[N])
	{
		for (int i = 0; i < N; i++)
			a[i] = x[i];
	}
	ARRAY operator+(ARRAY& m)
	{
		ARRAY temp;
		for (int i = 0; i < N; i++)
			temp.a[i] = a[i] + m.a[i];
		return temp;
	}
	void print()
	{
		for (int i = 0; i < N; i++)
			cout << setw(4) << a[i] << endl;
		cout << endl;
	}
};
int main() {
	double arr1[N] = { 1,2 };
	double arr2[N] = { 10,20 };
	ARRAY A(arr1);
	ARRAY B(arr2);
	ARRAY C;
	C = A + B;
	cout << "结果= " << endl;
	C.print();
	return 0;
}

sss

#include<iostream>
using namespace std;
int main()
{
	int a[10],s1,s2;
	a[0] = 1;
	a[1] = 2;
	a[2] = 3;
	int* p1 = a;//等价*p1=&a[0]
	int * p2 = &a[2];
	s1=*p2- *p1;
	s2 = p2 - p1;
	cout << s1 << endl;
	cout << s2;
}
发布了38 篇原创文章 · 获赞 2 · 访问量 1187

猜你喜欢

转载自blog.csdn.net/weixin_44811068/article/details/103652598