c++final exam test 1//20191221

#include<iostream>//15
using namespace std;
int sub(int);
int main()
{
    int  i = 5;
    cout << sub(i) << endl;
}
int sub(int  n)
{
    int  a;
    if (n == 1)   return  1;
    a = n + sub(n - 1);
    return   a;
}
#include<iostream>//print->012345543210
using namespace std;//因为你的if语句前面和后面一共用了两个cout所以会输出两组数据
void recur(char c)
{
    cout << c;
    if (c <'5') recur(c + 1);
    cout << c;
}
int main(void)
{
    recur('0');
}

#include<iostream>//1221
using namespace std;
int main()
{
	int m = 1, n = 2;
	int* p = &m, * q = &n, * r;
	r = p; p = q; q = r;
	cout << m << n << *p << *q << endl;
}

#include<iostream>//d
using namespace std;
void fun(char *s)
{   while(*s)
  {
     if(*s%2==0)
        cout<<*s;
     s++;
  }
}
void main()
{   char a[]="good ";
     fun(a);
}
#include <iostream>
using namespace std;
void fun(int a = 4, int b = 12, int c = 13)
{
	cout << a << ',' << b << ',' << c << endl;
}
void main()
{
	fun(); fun(8);
}
#include <iostream>
using namespace std;
void main()
{
	int b[][3] = { {1,5,6},{3},{0,2} };
	cout << b[1][1];
}
#include <iostream>//是否相等的正确表达式->strcmp
#include<string.h>
using namespace std;
void main()
{
	char s1[] = "ch2na";//china
	char s2[]= "china";
	cout << (s1 == s2) << endl;
	cout << strcmp(s1, s2);
}
#include <iostream>   //1  区分上面,char与string类型
#include <string>   
using namespace std;
int main()
{
	string  a = "boot" , b = "book";
	if (a == b)  cout << '1' << endl;
	else  cout << '0' << endl;
	return  0;
}
#include <iostream>
#include<string.h>
using namespace std;
void main()
{
	char s1[] = "abcd";//✔
	char s4[2][3] = { "xyz","mnp" };//❌
	char s3[][3] = { 'a' , 'x' , 'y' };//❌第一维的长度是不定的,s[3][]=xx,正确
	char s2[3] = "xyz";//❌
}
#include<iostream>//oC+
#include<string>
using namespace std;
int main()
{
	string str = " HelloC++!";
	cout << str.substr(5, 3) << endl;
}
#include<iostream>//5  20
#include<string>
using namespace std;
int main()
{
	char a[20] = "CHINA";
	cout << strlen(a) << "  " << sizeof(a) << endl;
}
#include<iostream>//待解决,strcpy()函数问题
#include<string>
#include<stdlib.h>
using namespace std;
int main()
{
	char a[7] = "abcdef";
	char b[4] = "ABC";
	strcpy(a, b);
	cout << a[5] << endl;
}

#include <iostream> //求二(三)维方阵主对角线元素之和 此处/**/表填空
#include <string>   
using namespace std;
int main()
{
	int a[3][3] = { 9,8,7,6,5,4,3,2,1 }, s = 0;
	/*for (int i = 0; i < 3; i++)
		for (int j = 0; j< 3; j++)
		{
			if (i == j)
				s += a[i][j];
		}*/
		
	cout << s << endl;
}
#include <iostream>  //以下程序用于从键盘上输入若干个学生的成绩,统计出平均成绩,
#include <string>   //并输出低于平均成绩的学生成绩。输入负数结束 
using namespace std;
int main()
{
	float  x[100], sum = 0, ave, a;
	int n = 0, i;
	cout << "Input score\n";
	cin>>a;/**/
	while (a>=0.0 && n<1000)
	{
		x[n] = a;
		sum+=a;/**/
		n++;/**/
		cin >> a;
	}
	ave = sum / n;//sum/n或sum/(float)n或sum/(double)n
	cout << "ave = " << ave << endl;
	for (i = 0; i < n; i++)/**/
	{
		if (x[i] < ave)//x[i]<ave或!(x[i]>=ave)或ave>x [i]/**/
			cout << "x[" << i << "]"<< x[i] << endl;
	}
}
#include<iostream>//200
using namespace std;
int main()
{
	int hot = 100;
	int& rad = hot;
	hot += 100;
	cout << rad << endl;
}
	
#include<iostream>//48
using namespace std;
void main()
{
	int a[2][3] = { {1,2,3},{4,5,6} };
	int k, * p;
	p = a[0] + 1;
	k = (*p) * (*(p + 2)) * (*(p + 4));
	cout << k << endl;
}
	
#include<iostream>//a 下标从0开始
using namespace std;
int main()
{
	string a = "beautiful";
	cout << a[2];
}
	
#include <iostream>//输入helloworld
#include <string>
using namespace std;
int main()
{
	string str;
	cout << "请输入一个字符串:"; getline(cin, str);
	cout << str[2] << str[5] << endl;//lw
	str[5] = '\0';
	cout << str << endl;//hello ord(中间空的)
	return 0;
}
#include<iostream>//输入 12  5  9 打印26
using namespace std;
void main()
{
int a,b,c,s=0;
int *p[3]={&a,&b,&c};
for(int i=0;i<3;i++)
cin>>*p[i];
for(i=0;i<3;i++)
s=s+*p[i];
cout<<s<<endl;
}
#include "iostream"//构造函数与析构函数***
#include<iostream >
using namespace std;
class AA
{
public:
	AA() { cout << '1' << ""; }
	~AA() { cout << "2" << ""; }
};
class BB : public AA
{
	int k;
public:
	BB(int n) :k(n) { cout << k << ""; }
	~BB() { cout << "4" << ""; }
};
int  main()
{
	BB b(6);
	return 0;
}
#include<iostream>
using namespace std;
#define MAX  5
int main()
{
	int a[MAX + 1], x, i;
	for (i = 1; i <= MAX; i++)
		cin >>a[i];
	cout << "Enter x : ";
	cin >> x;
	a[0] = x;  i = MAX;
	while (x != i)
		i--;
	if (x==i)
		cout << x << "位置:" << i << endl;
	else
		cout << "Not found" << endl;
}
发布了38 篇原创文章 · 获赞 2 · 访问量 1188

猜你喜欢

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