C++基础题目练习

文章详细介绍了C++编程中的基本概念,包括多线程的使用、不同数据类型(如值传递、引用传递和移动构造)、内存管理(如常量、指针和数组),以及结构体的定义、操作和作为函数参数的应用。
摘要由CSDN通过智能技术生成

代码已经注释,每一段都有标题,如图形式:

代码如下:

​
//多线程,暂时不用管
//#include <iostream>
//#include <utility>
//#include <thread>
//#include <chrono>
//#include <functional>
//#include <atomic>
//
//void f1(int n)
//{
//    for (int i = 0; i < 5; ++i) {
//        std::cout << "Thread 1 executing\n";
//        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
//    }
//    std::cout << "\t";
//}
//
//void f2(int& n)
//{
//    for (int i = 0; i < 5; ++i) {
//        std::cout << "Thread 2 executing\n";
//        ++n;
//        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
//    }
//}
//
//int main()
//{
//    int n = 0;
//    std::thread t1;                   // t1 is not a thread
//    std::thread t2(f1, n + 1);        // 值传递
//    std::thread t3(f2, std::ref(n));  // 引用传递
//    std::thread t4(std::move(t3));    // 移动构造函数
//    t2.join();
//    t4.join();
//    std::cout << "Final value of n is " << n << '\n';
//}
//#include <iostream>
//using namespace std;
//int main(void)
//{
//	cout << "Hellow world" << endl;
//	system("pause");
//	return 0;
//}
//******从此处开始*******

#include<iostream>
#include<math.h>
#include<string>
#include"test1.h"
using namespace std;
#define day 7//常量1




int main()
{
	//基础练习
	/*const int month = 30;//常量2
	char st1[] = "hello world";//c字符串
	string st2 = "lhr";//c++字符串
	bool target = false;//0/1
	int a = 9, b = 10, c = 0;
	cout<<"day="<<day<< "day占用空间"<<sizeof(day)<<endl;//sizeof占用空间
	cout << "字符串1=" << st1 << endl;
	cout << "name=" << st2 << endl;
	cout << "target=" <<target<<"\nbool占用空间为"<<sizeof(target)<< endl;//bool
	c = a > b ? a : b;
	cout << c << endl;

	cin >> target;
	cout << target << endl;

	switch (target)
	{
	case 1:
		cout << "true" << endl;
		break;
	case 0:
		cout << "false" << endl;
		break;
	default:

		break;
	}*/

	//猜数字
	/*int num = 0;
	srand(time(0));

	num = rand() % 100;//生成0-10的随机整数
	cout << num << endl;
	int usr = 0;
	while (cin >> usr) {
		if (usr > num) {
			cout << "大了" << endl;
		}
		else if(usr < num) {
			cout << "小了" << endl;
		}
		else if (usr == num) {
			cout << "猜对了" << endl;
			break;
		}
	}*///猜数字

	//水仙花数1
	/*int ge = 0;
	int shi = 0;
	int bai = 0;
	int i = 100;
	do
	{
		ge = i % 10;
		shi = (i / 10) % 10;
		bai = i / 100;
		if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai)
		{
			cout << i << endl;
		}
		i++;
	} while (i < 1000);//水仙花数*/

	//水仙花数2,
	/*int ge = 0;
	int shi = 0;
	int bai = 0;
	int i = 100;
	do {
		ge = i % 10;
		shi = (i / 10) % 10;
		bai = i / 100;
		int ge3 = pow(ge, 3);
		int shi3 = pow(shi, 3);
		int bai3 = pow(bai, 3);
		if (i == ge3 + shi3 + bai) {
			cout << i << endl;
		}
		i++;
	} while (i < 1000);*///水仙花数2

	//7倍数
	/*int ge = 0;
	int shi = 0;
	int num = 0;
	for (num = 0; num < 100; num++) {
		ge = num % 10;
		shi = num / 10;
		if (ge == 7 || shi == 7 || num % 7 == 0) {
			cout << num << endl;
		}

	}*///7的倍数

	//正方形*
	/*for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 10; j++) {
			cout << "*";
		}
		cout << endl;//输出一个换行符
	}*/

	//99乘法表
	/*for (int i = 1; i <= 9; i++) {
		for (int j = 1; j <= 9; j++) {
			cout << i << "*" << j << "=" << i * j<<"   ";
		}
		cout << endl;
	}*/

	//最大数
	/*int weight[5] = {100,250,150,500,300};
	int value = 0;
	for (int i = 0; i <= 5; i++) {
		if (weight[i] > value){
			value = weight[i];
		}
	}
	cout << "最大的数为" << value << endl*/

	//数组逆序
	/*int nums[5] = {1,2,3,4,5};
	int temp = 0;
	int start = 0;
	int end = sizeof(nums) / sizeof(nums[0]) - 1;
	while (start < end)
	{
		temp = nums[start];
		nums[start] = nums[end];
		nums[end] = temp;
		end--;
		start++;
	}
	for (int i = 0; i < 5; i++)
	{
		cout << nums[i] << endl;
	}*/

	//冒泡排序
	/*int temp = 0;
	int n[9] = { 4,7,3,9,1,6,2,5,8 };
	for (int i = 0; i < 8; i++) {
		for (int j = 0; j < 9 - i - 1; j++) {
			if (n[j] > n[j + 1]) {
				temp = n[j];
				n[j] = n[j + 1];
				n[j + 1] = temp;
			}
		}
	}
	for (int i = 0; i <= 8; i++) {
		cout << n[i];
	}*/

	//二维数组基础
	/*int score[3][3] = {{60,50,40},{30,20,10},{90,80,70}};
	for (int i = 0; i < 3; i++)
	{
		int temp = 0;
		for (int j = 0; j < 3; j++)
		{
			temp += score[i][j];
		}
		cout << temp << endl;
	}*/

	//函数举例
	/*int max(int a, int b) {
		return a > b ? a : b;
	///主函数前,若写在主函数后需要在主函数前声明。或者用头文件声明
	int a = max(10, 20);
	cout << a << endl;*///调用

	//指针基础
	/*1.定义指针->指针变量定义语法:数据类型* +变量名
	int a = 10;
	int* p;
	p = &a;//取变量a的地址,&取指符号
	cout << "a的地址为" << &a << endl;
	cout << "p为" << p << endl;

	//2.解引用:指针前加*,找到指针指向的内存中的数据
	*p = 1000;//可访问地址并进行修改
	cout << "a=" << a << endl;
	cout << "*p=" << *p << endl;//指针指向的值
	cout << "现在p=" << p << endl;//地址不变
	//3.在32位操作系统下无论是什么类型的指针,都占4个字节的内存空间。64位占8个字节空间。
	//4.空指针:用于给指针变量初始化,是不可进行访问的(0-255为系统内存占用,不允许用户访问)
	int* p = NULL;
	//5.野指针(错误,危险)
	int* p = (int*)0x1100;*/

	//*********const修饰指针**********
	/*//const修饰指针有3种情况
	//1.const修饰指针—常量指针
	//const修饰的是指针,指针指向可以改,指针指向的值不可以改
		int a = 10;
		int b = 10;
		const int* p1 = &a;
		//*p1 = 20;//错误
		p1 = &b;//正确
	//2.const修饰常量—指针常量
	//const修饰的是常量,指针指向不可以改,指针指向的值可以更改
		int* const p2 = &a;
		*p2 = 20;//正确
		//p2 = &b;//错误
	//3.const既修饰指针,又修饰常量
	//const既修饰指针,又修饰常量,指针的指向和指针指向的值都不可以改变
		const int* const p = &a;
						*/

	//指针和数组
	/*int arr[10] = {1,2,3,4,5,6,7,8,9,10};
	int* p = arr;//arr就是数组首地址
	//cout << "指针第一个元素 " << *p << endl;
	//p++;//指针向后偏移四个字节
	//cout << "指针第2个元素 " << *p << endl;
	for (int i = 0; i < 10; i++) {
		cout << *p << endl;
		p++;
	}*/

	//指针和函数:利用指针函数冒泡排序
	/*/void popsort(int* a, int len) {
		for (int i = 0; i < len; i++) {
			for (int j = 0; j < len - i - 1; j++) {
				int temp = 0;
				if (a[j] > a[j + 1]) {
					temp = a[j + 1];
					a[j + 1] = a[j];
					a[j] = temp;
				}
			}
		}
	}//放在主函数外
	int array[7] = { 8,3,2,9,7,6,5 };
	popsort(array, 7);
	for (int i = 0; i < 7; i++) {
		cout << array[i];
	}
	*/

	//结构体 
	/*//语法:struct 结构体名称{    结构体成员列表};struct关键字可以省略
	//1.struct 结构体名 变量名
	struct Student {
		string name;
		int age;
		int score;
	}s3;
	struct Student s1;//访问具体学生
	s1.name = "zz";
	s1.age = 18;
	s1.score = 100;
	//2.struct 结构体名 变量名 = (成员1值,成员2值…)
	struct Student s2 = { "xx",19,80 };
	cout << s2.name << s2.age << s2.score << endl;
	//3.定义结构体时顺便创建变量
	s3.name = "cc"
	//结构体属于用户自定义的数据类型,允许用户存储不同的数据类型。*/

	//结构体数组
	/*//作用 : 将自定义的结构头放入到数组中方便维护
	//语法 :struct 结构体名 数组名[元素个数] = { {},{}...{} };

	struct Student stuarr[3] = {
		{"zz",18,50},
		{"xx",19,60},
		{"cc",20,30 }
	};*/

	//*****结构体指针*****
	/*//作用:通过指针访问结构体中的成员
	//利用操作符->可以通过结构体指针访问结构体属性
	struct Student {
		string name;
		int age;
		int score;
	};
	struct Student s1 = { "zz",18,20 };//struct可以省略
	struct Student* p = &s1;
	cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;*/

	//*****结构体作函数参数*****
	/*//struct Student {
	//string name;
	//int age;
	//int score;
	//};
	//********结构体在主函数外**********
//void printstu(struct Student* p) {
//	//cout << "姓名: " << s.name << " 年龄: " << s.age << " 分数: " << s.score << endl;
//	cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;
//}//函数也在主函数外
//struct Student s;
//s.name = "zz";
//s.age = 18;
//s.score = 80;

//printstu(&s);*/
	

system("pause");
return 0;
}






​

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值