华师16-19真题

16年真题:
宿舍管理系统/学生管理系统
简单的学生管理系统

17年真题:

1.输入一个数字,为其高,一个符号*,输出该符号组成的平行四边形形状。

#include
#include 
#include
#include
using namespace std;

int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = n - i; j >= 1; j--)
			cout << " ";
		for (int k = 1; k <= n; k++)
			cout << "*";
		cout << endl;
	}
		
	return 0;
}

2.是猴子吃桃问题,每天都吃剩下的一半,再多吃一个,直到第十天剩下一个桃子,问第一天猴子有多少个桃。

#include
#include 
#include
#include
using namespace std;

int main()
{
	int n;
	cin >> n;
	int sum = 0, t = 1;
	for (int i = 1; i < n; i++)
	{
		sum = (t + 1) * 2;
		t = sum;
	}
	cout << sum << endl;
	return 0;
}

3.输入一系列的数字,输出他们正负数个数,输入0截止,要用函数实现 。


#include
#include 
#include
#include
using namespace std;

void fun()
{
	int a = 0, b = 0, n;
	while (cin >> n)
	{
		if (n == 0)
			break;
		if (n > 0)a++;
		else if (n < 0)b++;
	}
	cout << "正数个数:" << a << endl;
	cout << "负数个数:" << b << endl;
}
int main()
{
	fun();
	return 0;
}

4.是用一个类来记录学生成绩和他班级,里面有几个函数,静态成员和动态成员都有。



#include
#include 
#include
#include
using namespace std;

class Student {
private:
	string num;
	string name;
	string class_;
	float score;
	static int cnt;
	static float sum;
	static float ave;
public:
	Student(string n,string na,string c,float s)
	{
		num = n;
		name = na;
		class_ = c;
		score = s;
		cnt++;
		sum = sum + s;
	}
	static float average() {
		ave = sum / cnt;
		return ave;
	}
	static float getTotal()
	{
		return sum;
	}
	void getData()
	{
		cout << "输出 学号 姓名 班级 成绩:" << endl;
		cout << "num: " << num << endl;
		cout << "name: " << name << endl;
		cout << "class: " << class_ << endl;
		cout << "score: " << score << endl;
	}
};
 float Student::ave = 0;
 float Student::sum = 0;
 int Student::cnt = 0;

int main()
{
	Student Stu[5] = {
		Student("101","小明","十班",89.5),
		Student("102","Mike","十班",77.5),
		Student("103","Jackson","十班",87.0),
		Student("104","Cherry","十班",93.5),
		Student("105","Oscar","十班",89.0)
	};
	cout << "总分:" << Student::getTotal() << endl;
	cout << "平均分:" << Student::average() << endl;

	return 0;
}


5.重载+,++,=等简单运算符
时间相加减


#include 
#include
#include
using namespace std;

class Time {
private:
	int sec;
	int min;
public:
	//构造函数
	Time(int m = 0, int s = 0)
	{
		min = m;
		sec = s;
	}
	void operator=(Time a)
	{
		min = a.min;
		sec = a.sec;
	}
	friend ostream & operator<<(ostream & out, Time& a)
	{
		out << a.min << "/" << a.sec << endl;
		return out;
	}
	Time operator+(Time &a);
	Time operator++();//前置
	Time operator++(int);//后置
	
};
Time Time::operator+(Time &a)
{
	Time c;
	c.min = a.min + min;
	c.sec = a.sec + sec;
	if (c.sec >= 60)
	{
		c.sec -= 60;
		c.min++;
	}
	return c;
}
Time Time::operator++()
{
	sec++;
	if (sec >= 60)
	{
		sec -= 60;
		min++;
	}
	return *this;
}
Time Time::operator++(int)
{
	Time t = *this;
	sec++;
	if (sec >= 60)
	{
		sec -= 60;
		min++;
	}
	return t;
}

int main()
{
	Time a;
	cout << "a:";
	cout << a << endl;
	a++;
	cout << "a++:";
	cout << a << endl;
	Time b(10,59);
	cout<<"b: "<<b<<endl;
	++b;
	cout << "++b:" << b << endl;
	Time c = a + b;
	cout << "c = a + b:  " << c << endl;
	return 0;
}


复数加减

#include 
#include
#include
using namespace std;

class Compare {
private:
	int real;
	int imag;
public:
	Compare(int r = 0, int i= 0)
	{
		real = r;
		imag = i;
	}
	friend ostream & operator<<(ostream & out, Compare a)
	{
		out << "(" << a.real << "," << a.imag << ")" << endl;
		return out;
	}
	friend istream & operator>>(istream & in, Compare a)
	{
		cout << "input real and imag" << endl;
		in >> a.real >> a.imag;
	}
	Compare operator+(Compare a)
	{
		return Compare(real + a.real, imag + a.imag);
	}
	
};
int main()
{
	Compare a(10, 2);
	cout <<"a="<< a;
	Compare b(1, 5);
	cout <<"b="<< b;
	Compare c = a + b;
	cout << "c=a+b="<<c;
	return 0;
}

复数相加



#include
#include 
#include
#include
using namespace std;

class Compare {
private:
	int real;
	int imag;
public:
	Compare() {
		real = 0;
		imag = 0;
	}
	Compare(int r,int i) {
		real = r;
		imag = i;
	}
	friend ostream & operator<<(ostream & out, Compare &a)
	{
		out << "(" << a.real << "," << a.imag << ")" << endl;
		return out;
	}
	friend istream & operator>>(istream & in, Compare &a)
	{
		cout << "input real and imag" << endl;
		in >> a.real >> a.imag;
		return in;
	}
	Compare operator+(Compare a)
	{
		return Compare(real + a.real, imag + a.imag);
	}
	
};
int main()
{
	Compare a;
	cin >> a;
	cout <<"a="<< a;
	Compare b;
	cin >> b;
	cout <<"b="<< b;
	Compare c = a + b;
	cout << "c=a+b="<<c;
	return 0;
}


17年真题
1, a到z的字母用ASCII码表示,显示出来(10分)


#include 
#include
#include
using namespace std;


int main()
{
	int t;
	for (char i = 'a'; i <= 'z'; i++)
	{
		t = i;
		cout << i<<'\t'<<t<< endl;
	}
	return 0;
}


2,求一元二次方程的根(10分)

#include
#include 
#include
#include
using namespace std;

int main()
{
	int a, b, c;
	double x1, x2, dis = 0;
	cout << "输入a b c:" << endl;
	cin >> a >> b >> c;
	dis = b * b - 4 * a*c;
	while (dis < 0)
	{
		cout << "输入有误 请重新输入:" << endl;
		cin >> a >> b >> c;
		dis = b * b - 4 * a*c;
	}
	x1 =( -b + sqrt(dis)) / double(2 * a);
	x2 =(-b - sqrt(dis)) / double(2 * a);
	cout << "x1=" << x1 << endl<< "x2=" << x2 << endl;
	return 0;
}

3,写一个函数,从小到大排序一个数组(10分)



#include
#include 
#include
#include
using namespace std;
//冒泡排序
void sortpop(int a[],int n)
{
	int t;
	for(int i=1;i<n;i++)
		for (int j = 0; j < n - i; j++)
		{
			if (a[j] > a[j + 1])
			{
				t = a[j];
				a[j] = a[j + 1];
				a[j + 1] = t;
			}
		}
}
//选择排序
void selectsort(int a[], int n)
{
	int t;
	for (int i = 0; i < n - 1; i++) {
		int k = i;
		for (int j = i+1; j < n; j++)
		{
			if (a[j] < a[k])
				k = j;
			t = a[k];
			a[k] = a[i];
			a[i] = t;
		}
	}
}
void put(int a[], int n)
{
	for (int i = 0; i < n; i++)
		cout << a[i] << ' ';
	cout << endl;
}
int main()
{
	int n,a[100],b[100];
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		b[i] = a[i];
	cout << "冒泡排序" << endl;
	sortpop(a, n);
	put(a, n);
	cout << "选择排序" << endl;
	selectsort(b, n);
	put(b, n);
	return 0;
}

4,写一个异常处理 以c=a/b为例(10分)
5,打印9*9乘法表(10分)

#include
#include 
#include
#include
using namespace std;


int main()
{
	for (int i = 1; i <= 9; i++) {
		for (int j = 1; j <= i; j++)
		{
			cout << i << "*" << j << "=" << i * j << " ";
		}
		cout << endl;
	}
	return 0;
}

6,写一个递归函数: f(0)=0,f(1)=1,f(n)=f(n-1)+f(n-2);(10分)

#include
#include 
#include
#include
using namespace std;

int fun(int n)
{
	if (n == 0)
		return 0;
	else if (n == 1)
		return 1;
	else
		return fun(n - 1) + fun(n - 2);
}
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cout << fun(i) << '\t';
		if (i % 5 == 0)
			cout << endl;
	}
	return 0;
}

7,新建一个关于学生的类或结构体,包含学号,姓名,语文,数学,英语成绩;
并输入一个整数n,分配一个类的数组。(10分)

#include 
#include
#include
using namespace std;

class Student {
private:
	int num;
	char name[20];
	float chinese;
	float english;
	float math;
public:
	void input();
	void put();
};

void Student::input() {
	cout << "输入学生学号 姓名 语文成绩 英语成绩 数学成绩:" << endl;
	cin >> num >> name >> chinese >> english >> math;
}
void Student::put()
{
	cout << "输出学生 学号 姓名 语文成绩 英语成绩 数学成绩:" << endl;
	cout << num << " " << name << " " << chinese << " " << english << " " << math << endl;
}
int main()
{
	int n;
	cin >> n;
	Student *stu = new Student[n];
	Student *p = stu;
	for (int i = 0; i < n; i++,p++)
		p->input();
	p = stu;
	for (int i = 0; i < n; i++, p++)
		p->put();
	return 0;
}

8,运用第7题的类,要求写三个函数。(均用二进制输入输出)(30分)
(1)把一个学生信息存到一个文件中
(2)从一个文件中提取所有学生信息,并打印
(3)从一个文件中复制所有学生信息,复制到另外一个文件。

#include
#include 
#include
#include
using namespace std;

class Student {
private:
	int num;
	char name[20];
	float chinese;
	float english;
	float math;
public:
	void input();
	void put();
};

void Student::input() {
	cout << "输入学生学号 姓名 语文成绩 英语成绩 数学成绩:" << endl;
	cin >> num >> name >> chinese >> english >> math;
}
void Student::put()
{
	cout << "输出学生 学号 姓名 语文成绩 英语成绩 数学成绩:" << endl;
	cout << num << " " << name << " " << chinese << " " << english << " " << math << endl;
}
//保存到磁盘
void saveToFile(Student s)
{
	ofstream outfile("stu.dat", ios::out|ios::app);
	if (!outfile)
	{
		cerr << "保存数据时打开stu.dat文件错误!" << endl;
		exit(1);
	}
	outfile.write((char *)&s, sizeof(Student));
	outfile.close();
}
//从磁盘取出
void putFile()
{
	ifstream infile("stu.dat", ios::in);
	if (!infile)
	{
		cerr << "从磁盘提取数据时打开文件stu.dat文件错误!" << endl;
		exit(1);
	}
	Student s;
	while (infile.read((char *)&s, sizeof(Student)))
	{
		s.put();
	}
	infile.close();
}
//备份到磁盘
void save()
{
	ifstream infile("stu.dat", ios::in);
	if (!infile)
	{
		cerr << "从磁盘提取数据时打开文件stu.dat文件错误!(备份)" << endl;
		exit(1);
	}
	ofstream outfile("stu.bak", ios::out);
	if (!outfile)
	{
		cerr << "保存数据时打开stu.bak文件错误!(备份)" << endl;
		exit(1);
	}
	Student s;
	while (infile.read((char *)&s, sizeof(Student)))
	{
		outfile.write((char*)&s, sizeof(Student));
	}
	infile.close();
	outfile.close();
}
void put()
{
	ifstream infile("stu.bak", ios::in);
	if (!infile)
	{
		cerr << "从备份提取数据时打开文件stu.bak文件错误!" << endl;
		exit(1);
	}
	Student s;
	while (infile.read((char *)&s, sizeof(Student)))
	{
		s.put();
	}
	infile.close();
	
}
int main()
{
	int n;
	cin >> n;
	Student *stu = new Student[n];
	Student *p = stu;
	for (int j = 0; j < n; j++,p++)
		p->input();
	p = stu;
	cout<<"----------------------输出学生信息--------------------------"<<endl;
	for (int i = 0; i < n; i++, p++)
		p->put();
	p = stu;
	cout << "-------------------学生信息存入磁盘------------------------" << endl;
	for (int k = 0; k < n; k++,p++)
		saveToFile(*p);
	cout << "----------------从磁盘去除所有学生信息---------------------" << endl;
	putFile();
	cout << "-----------------------备份---------------------------------" << endl;
	save();
	put();

	return 0;
}

华师16-19真题_第1张图片
19年上机题

  1. 编程计算1/1!-1/3!+1/5!-…+(-1)(n+1)/(2n-1)!
#include
#include 
#include
#include
using namespace std;

double fun(int n)
{
	if (n ==1)
		return 1;
	else
		return n * fun(n - 1);
}
int main()
{
	int n;
	cin >> n;
	double t=0, sum = 0;
	int s = 1;
	for (int i = 1; i <= n; i++)
	{
		t = double(s*1) / fun(i*2-1);
		sum = sum + t;
		s = -s;
	}
	cout << sum << endl;
	return 0;
}
  1. 甲乙丙对一次竞赛名次进行预测。
    甲:A第1 B第3
    乙:C第1 D第4
    丙:D第1 B第3 (数据不是原题,记不太清,但解题方法是一样的)
    他们都说对了一半,求ABCD正确的名次(ABCD不会出现相同的名次)
    用for循环
#include
#include 
#include
#include
using namespace std;


int main()
{
	for(int a=1;a<=4;a++)
		for (int b = 1; b <= 4; b++)
		{
			if(a!=b)
				for(int c=1;c<=4;c++)
					if(c!=a&&c!=b)
						for (int d = 1; d <= 4; d++)
						{
							if(d!=a&&d!=b&&d!=c)
							if (a == 1 && b != 3 || a != 1 && b == 3)
								if (c == 1 && d != 4 || c != 1 && d == 4)
									if (d == 1 && b != 3 || d != 1 && b == 3)
									{
										cout << "A--" << a << endl;
										cout << "B--" << b << endl;
										cout << "C--" << c << endl;
										cout << "D--" << d << endl;
									}
						}
		}
	
	return 0;
}

在这里插入图片描述
3. 给定链表节点的定义
struct Node{
int data;
Node *next;
}
请编写一个函数,用递归的方式:对两个有序链表合并成一个有序链表。

#include
#include 
#include
#include
using namespace std;

struct node {
	int num;
	struct node *next;
};
node * createList()
{
	int n,x;
	cin >> n;
	node *head = new node;
	cin >> x;
	node *p, *r;
	head->num = x;
	head->next = NULL;
	r = head;
	for (int i = 2; i <= n; i++)
	{
		cin >> x;
		p = new node;
		p->num = x;
		p->next = NULL;
		r->next = p;
		r = r->next;
	}
	r->next = NULL;
	return head;
}
void putList(node *head)
{
	node *p = head;
	while (p)
	{
		cout << p->num << ' ';
		p = p->next;
	}
	cout << endl;
}
node * merge(node *a, node *b)
{
	node *head;
	if (a == NULL)
		return b;
	else if (b == NULL)
		return a;
	else
	{
		if (a->num < b->num)
		{
			head = a;
			head->next = merge(a->next, b);
		}
		else
		{
			head = b;
			head->next = merge(a, b->next);
		}
	}
	return head;
}
int main()
{
	node *a, *b,*c;
	a = createList();
	b = createList();
	c = merge(a, b);
	putList(c);
	return 0;
}

在这里插入图片描述
4. 现有一个酒店场景。定义一个客人类Guest。包含成员属性:编号Num、姓名Name、房费Fee、当前酒店入住人数Count。其中编号Num需要程序自动生成。现在要求实现以下Guest的成员函数:构造函数、Show()显示Guest的信息、GetCount()返回当前酒店入住的人数、GetTotalIncome()返回当前酒店的总收入。并定义3个Guest对象来对成员函数进行测试。

#include
#include 
#include
#include
using namespace std;

int total = 1000;
float totalincome = 0;
class Guest {
private:
	int num;
	string name;
	float free;
	static int count;
public:
	Guest(string n,float f)
	{
		count++;
		num =++total;
		free = f;
		totalincome += f;
		
	}
	void Show()
	{
		cout << "客人信息:" << endl;
		cout << "num: " << num << "  name: " << name << "  free: " << free << endl;
	}
	float getTotalIncome()
	{
		return totalincome;
	}
	static int GetCount()
	{
		return count;
	}
};
int Guest::count = 0;
int main()
{
	Guest g[3] = { 
		Guest("小明",342),
		Guest("丽丽",240),
		Guest("琳娜",360)
	};
	for (int i = 0; i < 3; i++)
		g[i].Show();
	cout << "总收入:" << g[0].getTotalIncome() << endl;
	cout << "总客人数:" << Guest::GetCount() << endl;
	return 0;
}

华师16-19真题_第2张图片

  1. 现有一抽象类Shape,它拥有一系列虚函数:Input()输入类需要的信息、Show()显示类的信息、Perimeter()计算周长、Area()计算面积。先定义Circle、Square、Triangle来继承Shape并实现其虚函数。要求创建Circle、Square、Triangle的对象,用基类指针指向这些对象,并调用成员函数进行测试。
#include
#include 
#include
#include
using namespace std;

class Shape {
public:
	virtual void input() = 0;
	virtual void show() = 0;
	virtual double Perimeter() = 0;
	virtual double Area() = 0;
};

class Circle:public Shape{
private:
	double r;
public:
	void input()
	{
		cout << "输入圆形的半径:" << endl;
		cin >> r;
	}
	void show()
	{
		cout << "圆的半径:" << r << endl;
		cout << "周长:" << Perimeter() << endl;
		cout << "面积:" << Area() << endl;
	}
	double Area() {
		return r * r*3.14;
	}
	double Perimeter() {
		return 2 * 3.14*r;
	}
};

class Square :public Shape {
private:
	double width;
	double lenth;
public:
	void input()
	{
		cout << "输入长方形的长和宽:" << endl;
		cin >> lenth >> width;
	}
	void show()
	{
		cout << "长方形的长和宽:" << lenth << " " << width << endl;
		cout << "周长:" << Perimeter() << endl;
		cout << "面积:" << Area() << endl;
	}
	double Perimeter() {
		return 2 * (width + lenth);
	}
	double Area() {
		return width * lenth;
	}
};
class Triangle :public Shape {
private:
	double length_A, length_B, length_C; //三条边的长度
public:
	void input()
	{
		cout << "输入三角形三边长度:" << endl;
		cin >> length_A >> length_B >> length_C;
	}
	void show()
	{
		cout << "三角形的三边长度分别为:" << length_A << " " << length_B << " " << length_C << endl;
		cout << "面积:" << Area() << endl;
		cout << "周长:" << Perimeter() << endl;
	}
	double Perimeter() {
		return length_A + length_B + length_C;
	}
	double Area() {
		double p = 0.5*(length_A + length_B + length_C);
		return sqrt(p*(p - length_A)*(p - length_B)*(p - length_C));
	}

};
int main()
{
	Shape *s;
	s = new Circle();
	s->input();
	s->Area();
	s->Perimeter();
	s->show();

	s = new Triangle();
	s->input();
	s->Area();
	s->Perimeter();
	s->show();

	s = new Square();
	s->input();
	s->Area();
	s->Perimeter();
	s->show();

	return 0;
}



华师16-19真题_第3张图片

你可能感兴趣的:(华师上机真题)