【C++100问】一篇文章(16个小例子)带你入门C++的编程世界(基础篇)

本文是一篇C++基础教程,通过16个小例子介绍C++编程,涵盖数据类型、算术运算符、复合类型、指针、模板类、循环、分支及简单文件处理等内容,适合初学者入门。
摘要由CSDN通过智能技术生成

📢 声明:

1)本文仅供学术交流,非商用。
2)博主才疏学浅,文中如有不当之处,请各位指出,共同进步,谢谢。
3)此属于第一版本,若有错误,还需继续修正与增删。还望大家多多指点。
4)大家都共享一点点,一起为祖国科研的推进添砖加瓦。

👉 学习路线

〇、✏ 前言

为了更好地记录学过的C++知识,作者在过去的两个月里,将每个知识点对应的小例子总结下来,分享给大家 💖,希望大家喜欢 😁。

下面开始正文,全程英文注释:

⌛ 加载中 ▶▶▶ ⏳ ...

一、🚀 你好,世界

牙牙学语的开始。

#include <iostream>
int main()
{
   
	using namespace std;
	
	cout << "Hello,World" << endl;

	system("pause");
	return 0;
}

在这里插入图片描述

二、🍄 数据类型

int、short、long、long long、char、float、double、long double 的位数,最大值,最小值。

#include <iostream>
#include <climits>
int main()
{
   
	using namespace std;
	
	int n_int = INT_MAX;			// initialize n_int to max int value
	short n_short = SHRT_MAX;		// symbols defined in climits file
	long n_long = LONG_MAX;
	long long n_llong = LLONG_MAX;
	char n_char = CHAR_MAX;
	float n_float = FLT_MAX;
	double n_double = DBL_MAX;
	long double n_ldouble = LDBL_MAX;

	// sizeof operator yields size of type or of variable
	cout << "Sizeof:" << endl;
	cout << "int is " << sizeof (int) << " bytes." << endl;
	cout << "short is " << sizeof n_short << " bytes." << endl;
	cout << "long is " << sizeof n_long << " bytes." << endl;
	cout << "long long is " << sizeof n_llong << " bytes." << endl;
	cout << "char is " << sizeof n_char << " bytes." << endl;
	cout << "float is " << sizeof n_float << " bytes." << endl;
	cout << "double is " << sizeof n_double << " bytes." << endl;
	cout << "long double is " << sizeof n_ldouble << " bytes." << endl;
	cout << endl;

	cout << "Maximum values:" << endl;
	cout << "int: " << n_int << endl;
	cout << "short: " << n_short << endl;
	cout << "long: " << n_long << endl;
	cout << "long long: " << n_llong << endl;
	cout << "char is " << n_char << endl;
	cout << "float is " << n_float << endl;
	cout << "double is " << n_double << endl;
	cout << "long double is " << n_ldouble << endl;
	cout << endl;

	cout << "Minimum values:" << endl;
	cout << "int: " << INT_MIN << endl;
	cout << "short: " << SHRT_MIN << endl;
	cout << "long: " << LONG_MIN << endl;
	cout << "long long: " << LLONG_MIN << endl;
	cout << "char is " << CHAR_MIN << endl;
	cout << "float is " << FLT_MIN << endl;
	cout << "double is " << DBL_MIN << endl;
	cout << "long double is " << LDBL_MIN << endl;
	cout << endl;

	cout << "Bits per byte = " << CHAR_BIT << endl;

	system("pause");
	return 0;
}

在这里插入图片描述

三、🍅 算术运算符

加法、减法、乘法、除法、求模。

#include <iostream>
int main()
{
   
	using namespace std;
	
	int hats, heads;
	cout << "Enter a number: ";
	cin >> hats;
	cout << "Enter another number: ";
	cin >> heads;

	cout << "hats = " << hats << "; heads = " << heads << endl;
	cout << "hats + heads = " << hats + heads << endl;
	cout << "hats - heads = " << hats - heads << endl;
	cout << "hats * heads = " << hats * heads << endl;
	cout << "hats / heads = " << hats / heads << endl;
	cout << "hats % heads = " << hats % heads << endl;

	system("pause");
	return 0;
}

在这里插入图片描述

四、🍆 复合类型

4.1、🍉 一维数组、字符串、string
#include <iostream>
#include <cstring>
#include <string>		// make string class available
const int Size = 15;
int main()
{
   
	using namespace std;

	int yams[3];		// creates array with three elements
	yams[0] = 7;		// assign value to first element
	yams[1] = 8;
	yams[2] = 6;

	cout << "Total yams = ";
	cout << yams[0] + yams[1] + yams[2] << "." << endl;
	cout << "The second yams = ";
	cout << yams[1] << ".";
	cout << "\nSize of yams array = " << sizeof yams;
	cout << " bytes.\n";

	cout << endl;
	cout << "********************************分割线********************************" << endl;
	cout << endl;

	char name[Size] = "C++owboy";		// initialized array

	cout << "Well, " << name << ", your name ";
	cout << "is stored in an array of " << sizeof(name) << " bytes.\n";
	cout << "Your initial is " << name[0] << ".\n";
	name[3] 
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是管小亮

一口吃掉你的打赏,嗝~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值