【c++】黑马·1 程序基础——代码练习

        入了cs的坑4个月入了C语言,用1个月学黑马程序员的c++至重载,挺合口味爽的一批。 

目录

黑马32(循环随机)猜数

黑马34 水仙数

黑马036敲桌子

黑马038乘法表

黑马044(数组)小猪称重

黑马044数组逆序

黑马045冒泡排序

黑马049(二维数组)打印成绩

黑马057指针大小

黑马063(函数指针)冒泡排序

黑马066结构体指针

黑马070结构体老师学生

黑马071结构体英雄

黑马p87堆区于函数创建

黑马p89引用

黑马p92函数返回引用


黑马32(循环随机)猜数

#include<iostream>
#include<time.h>
#include<random>
using namespace std;
int main()
{
	srand((unsigned int)time(NULL));
	int num=rand() % 100,i=10,guess;
	while (i--)
	{
		cout << "第"<<10-i<<"次,请猜1 - 100一个数:";
		cin >> guess;
		if (guess == num)
		{
			cout << "你猜对啦,游戏结束";
			return 0;
		}
		cout << "猜错啦," ;
		if (guess > num)
			cout << "猜大了" << endl;
		else
		    cout << "猜小了" << endl;
	}
	cout << "不好意思,10次机会用完了,游戏结束" << endl;
	return 0;
}

黑马34 水仙数

#include<iostream>
#include<time.h>
#include<random>
using namespace std;
int main()
{
	srand((unsigned int)time(NULL));
	int num=rand() % 100,i=10,guess;
	while (i--)
	{
		cout << "第"<<10-i<<"次,请猜1 - 100一个数:";
		cin >> guess;
		if (guess == num)
		{
			cout << "你猜对啦,游戏结束";
			return 0;
		}
		cout << "猜错啦," ;
		if (guess > num)
			cout << "猜大了" << endl;
		else
		    cout << "猜小了" << endl;
	}
	cout << "不好意思,10次机会用完了,游戏结束" << endl;
	return 0;
}

黑马036敲桌子

/*打印1-100,含7或7倍数打印*/
#include<iostream>
using namespace std;
int main()
{
	for (int i = 1; i < 101; i++)
	{
		cout << i;
		if (!(i % 7) || i % 10 == 7 || i / 10 == 7)
			cout << "敲桌子";
		cout << endl;
	}
	return 0;
}

黑马038乘法表

#include<iostream>
using namespace std;
int main()
{
	for (int i = 1; i < 10; i++)
	{
		for (int j = 1; j <= i; j++)
			cout << j << '*' << i << '=' << i * j << ' ';
		cout << endl;
	}
	return 0;
}

黑马044(数组)小猪称重

/*数组中记录5小猪体重,并输出最大*/
#include<iostream>
using namespace std;
int main()
{
	cout << "请分别输入5只小猪的体重:";
	int arr[5],max1=0xc0c0c0c0;
	for (int i = 0; i < 5; i++)
	{
		cin >> arr[i];
		max1 = max(max1, arr[i]);
	}
	cout << "最重为" << max1 << endl;
	return 0;
}

黑马044数组逆序

/*数组中记录5小猪体重,并输出最大*/
#include<iostream>
using namespace std;
int main()
{
	cout << "请分别输入5数:";
	int arr[5];
	for (int i = 0; i < 5; i++)
		cin >> arr[i];
	for (int i = 0; i < 2; i++)
		swap(arr[4 - i], arr[i]);
	cout << "逆序后:";
	for (int i = 0; i < 5; i++)
		cout<<arr[i]<<' ';
	return 0;
}

黑马045冒泡排序

#include<iostream>
using namespace std;
int main()
{
	cout << "请分别输入5数:";
	int arr[5];
	for (int i = 0; i < 5; i++)
		cin >> arr[i];
	for (int i = 1; i < 5; i++)
		for (int j = 0; j < 5 - i; j++)
			if (arr[j] > arr[j + 1])
				swap(arr[j], arr[j + 1]);
	cout << "冒泡排序后:";
	for (int i = 0; i < 5; i++)
		cout << arr[i] << ' ';
	return 0;
}

黑马049(二维数组)打印成绩

/*打印三名同学3科成绩*/
#include<iostream>
using namespace std;
int main()
{
	char name[3][20];
	int score[3][3];
	for (int i = 0; i < 3; i++)
	{
		cout << "请输入第" << i + 1 << "名学生的姓名和语数英三科成绩:";
		cin >> name[i]>>score[i][0]>>score[i][1] >> score[i][2];
	}
	cout << "     语文 数学 英语" << endl;
	for (int i = 0; i < 3; i++)
		cout << name[i] << "  " << score[i][0] << "   " << score[i][1] << "   " << score[i][2] << endl;
	return 0;
}

黑马057指针大小

#include<iostream>
using namespace std;
int main()
{
	cout << sizeof(int*);
	return 0;
}

黑马063(函数指针)冒泡排序

#include<iostream>
using namespace std;
void getnum(int* arr)
{
	cout << "请分别输入10数:";
	for (int i = 0; i < 10; i++)
		cin >> arr[i];
}
void BubbleSort(int* arr)
{
	for (int i = 1; i < 10; i++)
		for (int j = 0; j < 10 - i; j++)
			if (arr[j] > arr[j + 1])
				swap(arr[j], arr[j + 1]);
}
void show(int* arr)
{
	cout << "冒泡排序后:";
	for (int i = 0; i < 10; i++)
		cout << arr[i] << ' ';
}
int main()
{
	int arr[10];
	getnum(arr);
	BubbleSort(arr);
	show(arr);
	return 0;
}

黑马066结构体指针

#include<iostream>
#include<string>
using namespace std;
struct student
{
	string name;
	int age;
	int score;
};
int main()
{
	student stuarr[3] ={ {"张三",20,99},{"李四",19,88},{"王五",22,78}};
	cout << stuarr->name <<' ' << stuarr->age<<' ' << stuarr->score;
	return 0;
}

黑马070结构体老师学生

/*老师:姓名加5学生数组,数组存3老师,学生:姓名分数,
函数给老师学生赋值并打印*/
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
struct student
{
	string name;
	int score;
};
struct teacher
{
	string name;
	student stu[5];
};
void gettea(teacher* tea)
{
	for (int i = 0; i < 3; i++)
	{
		cout << "请输入第" << i + 1 << "名老师的姓名:";
		cin >> tea[i].name;
		for (int j = 0; j < 5; j++)
		{
			cout << "请输入该老师第" << j + 1 << "名学生的姓名及成绩:";
			cin >> tea[i].stu[j].name>> tea[i].stu[j].score;
		}
		cout << endl;
	}
}
void show(teacher* tea)
{
	cout << "老师姓名 所带学生姓名及分数" << endl;
	for (int i = 0; i < 3; i++)
	{
		cout<<setw(8) << tea[i].name<<' ';
		for (int j = 0; j < 5; j++)
			cout << tea[i].stu[j].name << tea[i].stu[j].score<<' ';
		cout << endl;
	}
}
int main()
{
	teacher tea[3];
	gettea(tea);
	show(tea);
	return 0;
}

黑马071结构体英雄

/*5英雄:名 岁。冒泡年龄打印*/
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
struct hero
{
	string name;
	int age;
	string sex;
};
void gethero(hero* h)
{
	for (int i = 0; i < 5; i++)
	{
		cout << "请输入第" << i + 1 << "名英雄的姓名、年龄和性别:";
		cin >> h[i].name >> h[i].age>>h[i].sex;
	}
}
void BubbleSort(hero* h)
{
	for (int i = 1; i < 5; i++)
		for (int j = 0; j < 5 - i; j++)
			if (h[j].age > h[j + 1].age)
				swap(h[j], h[j + 1]);
}
void show(hero* h)
{
	cout << "姓名 年龄 性别" << endl;
	for (int i = 0; i < 5; i++)
		cout << setw(4) << h[i].name <<setw(5)  << h[i].age << setw(5) <<h[i].sex << endl;
}
int main()
{
	hero h[5];
	gethero(h);
	BubbleSort(h);
	show(h);
	return 0;
}

黑马p87堆区于函数创建

/*黑马p87堆区于函数创建*/
#include<iostream>
using namespace std;
int* func()
{
	int* p = new int(10);
	return p;
}
int main()
{
	int* p = func();
	cout << *p << endl << *p;
	delete p;
	int* arr = new int[10];
	for (int i = 0; i < 10; i++)
	{
		arr[i] = i;
		cout << arr[i] << endl;
	}
	delete[] arr;
	return 0;
}

黑马p89引用

/*黑马p89引用*/
#include<iostream>
using namespace std;
int main()
{
	int a = 10, & b = a;
	cout << a << endl << b << endl;
	b = 200;
	cout << a << endl << b;
	return 0;
}

黑马p92函数返回引用

/*黑马p92函数返回引用*/
#include<iostream>
using namespace std;
int& func()
{
	static int a = 10;
	return a;
}
int main()
{
	int& b = func();
	cout << b << endl << b << endl << func() << endl;
	func() = 1000;
	cout << b << endl << b << endl << func() << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_51695846/article/details/125364006