C/C++教程

C++新手入门

本文主要是介绍C++新手入门,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

用return这种形式就能定义返回值, return 0可以 终止 main() 函数,并返回值 0。
return 0 代表程序正常退出,return 1代表程序异常退出!
使用return 语句可以返回一个变量内的值或一个指针,也可用return0,表示返回为空。
return 代表调到函数外,
return 0代表函数正常终止
return 1代表函数非正常终止
而我觉得还有一个作用就是return之后你至少知道代码成功运行到最后一行,中间没有异常中止,并且return 关键字的还有一个作用是返回程序流程的控制权!其副作用是返回一个值。

std是一个命名空间(namespace),‘::’是作用域运算符,cout是std空间中的一个函数名。使用cout时,必须有使用std命名空间的说明,有两种说明方式。
方式一:每次使用时对cout说明:
std::cout << “Input two numbers:”<<endl; printf(“Input two numbers\n”);
方式二:在主函数前说明一下,后面就可以直接使用cout:
using namespace std;
cout << “Input two numbers:”;

#include
usinbg namespace std;

#include<stdio.h>

scanf("%d",&a);

cin>>a;
eg、
//x64处理器 64位window10 vs2015
#include
using namespace std;
int main()
{
bool b;
char c;short s; int i; long l; long long ll; float f; double d; long double ld;long float lf;
unsigned char uc; unsigned short us; unsigned int ui; unsigned long ul; unsigned long long ull;
cout << sizeof(bool) << endl;
cout << sizeof(char)<<" " << sizeof(short)<<" “<< sizeof(signed int) << " " << sizeof(long) << " " << sizeof(signed long long) << " " << sizeof(float) << " " << sizeof(double) << " " << sizeof(long float) << " " << sizeof(long double) << endl;
cout <<sizeof(unsigned char)<<” "<< sizeof(unsigned short) << " " << sizeof(unsigned int) << " " << sizeof(unsigned long) << " " << sizeof(unsigned long long) << endl;
cout << sizeof(unsigned) << endl;

cout << "hello World!!!" <<endl;
system("pause");
return 0;

}
结果:1
1 2 4 4 8 4 8 8 8
1 2 4 4 8
4
hello World!!!

一、输入流与输出流的基本操作
cout语句的一般格式为:
cout<<表达式1<<表达式2<<……<<表达式n;
printf(“表达式1”);
printf(“表达式2”);
printf(“表达式3”);
printf(“表达式6”);

cin语句的一般格式为:
cin>>变量1>>变量2>>……>>变量n;
scanf(“%d”,&变量1);
scanf (“%d”,&变量2);

在定义流对象时,系统会在内存中开辟一段缓冲区,用来暂存输入输出流的数据。
在执行cout语句时,先把插入的数据顺序存放在输出缓冲区中,直到输出缓冲区满或遇到cout语句中的endl(或’\n’,ends,flush)为止,
此时将缓冲区中已有的数据一起输出,并清空缓冲区。
输出流中的数据在系统默认的设备(一般为显示器)输出。

eg、
一个cout语句可以分写成若干行。如
cout<<“This is a simple C++ program.”<<endl;
可以写成
cout<<"This is " //注意行末尾无分号
<<"a C++ "
<<“program.”
<<endl; //语句最后有分号
也可写成多个cout语句,即
cout<<"This is "; //语句末尾有分号
cout <<"a C++ ";
cout <<“program.”;
cout<<endl;
以上3种情况的输出均为
This is a simple C++ program.

eg、
注意 不能用一个插入运算符“<<”插入多个输出项,如:
cout<<a,b,c; //错误,不能一次插入多项
cout<<a+b+c; //正确,这是一个表达式,作为一项

用于表示一串字符
一、char 变量名[] = “字符串值”
二、string 变量名 = “字符串值”

布尔类型:bool
#include
using namespace std; //指调用命名空间std内定义的所有标识符
#include //头文件,用于C++字符串
int main()
{
//1 代表真,0 代表假
bool flag = true;
cout << flag << endl;

flag = false;
cout << flag << endl;

cout << "bool 所占的内存空间:" << sizeof(flag) << endl;
system("pause");  //让程序暂停一下,然后按任意键继续

return 0;

}
布尔类型数据代表真或假
true — 真(1)
false — 假(0)
bool类型占1个字节大小

算数运算符:递增递减
#include
using namespace std; //指调用命名空间std内定义的所有标识符
#include //头文件,用于C++字符串

int main()
{
//1、前置递增
int a = 20;
++a;
cout << a << endl;//=21
//2、后置递增
int b = 10;
b++;
cout << b << endl;//11
//3、前置后置区别,
// 前置时变量先加1,在进行表达式运算
// 后置时先进行表达式运算,在变量先加1
int a1 = 30;
int b1 = ++a1;//=31
int a2 = 40;
int b2 = a2++;//=40
cout << b1 << endl;
cout << b2 << endl;

system("pause");  //让程序暂停一下,然后按任意键继续

return 0;

}
前置:++a
后置:a++
前置时变量先加1,在进行表达式运算
后置时先进行表达式运算,在变量先加1

include<iostream.h>
using namespace std
int main
{
int a=1;
cout<<!a<<endl ;//0
cout<<!!a<<endl; //1
system(“pause”);
return 0;
}

C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
顺序结构:程序按照顺序执行,不发生跳转
选择结构:依据条件是否满足,有选择的执行相应功能
循环结构:依据条件是否满足,循环执行多次某段代码
if语句作用:执行满足的语句

嵌套if语句:if {} elas if{}里面嵌if{} elas if{}
#include
using namespace std; //指调用命名空间std内定义的所有标识符
#include //头文件,用于C++字符串

int main()
{
//
int score = 0;
cout << “请输入你考的分数:” << endl;
cin >> score;
cout << “你考的分数为:” << score << endl;
if (score > 600)
{
cout << “恭喜你考上了一本大学” << endl;
if (score > 700)
{
cout << “恭喜你考上了清华” << endl;

	}
	else if (score > 650)
	{
		cout << "恭喜你考上了北大" << endl;

	}
}
else if (score > 500)
{
	cout << "恭喜你考上了二本大学" << endl;
	cout << score << endl;
}
else
{
	cout << "未考上大学" << endl;
	cout << score << endl;
}

system("pause");  //让程序暂停一下,然后按任意键继续

return 0;

}

三目运算符:a>b?a:b
#include
using namespace std; //指调用命名空间std内定义的所有标识符
#include //头文件,用于C++字符串

int main()
{
//三目运算符
int a = 10;
int b = 20;
int c = 0;
c = (a>b?a:b);
cout << "c = " << c << endl;//比较ab,谁大谁赋值给c

system("pause");  //让程序暂停一下,然后按任意键继续

return 0;

}

选择结构:switch() case1: ··· break;···············default:······break;
#include
using namespace std; //指调用命名空间std内定义的所有标识符
#include //头文件,用于C++字符串

int main()
{
//
int num = 0;

cout << "请给电影打分:" << endl;
cin >> num;
cout << "你给电影打分为:" << num << endl;

switch (num)
{
case 10:
	cout << "您认为是经典电影" << endl;
	break;
case 9:
	cout << "您认为是经典电影" << endl;
	break;
case 8:
	cout << "您认为是好电影" << endl;
	break;
case 7:
	cout << "您认为是一般电影" << endl;
	break;
default:
	cout << "您认为烂片" << endl;
	break;
}

system("pause");  //让程序暂停一下,然后按任意键继续

return 0;

}

循环结构:while()
#include
using namespace std; //指调用命名空间std内定义的所有标识符
#include //头文件,用于C++字符串

int main()
{
//ctrl + k +c 多行注释
int num = 0;
while (num<10)
{
cout << num << endl;
num++;
}

system("pause");  //让程序暂停一下,然后按任意键继续

return 0;

}

猜数字eg:无限循环,随机数
#include
using namespace std; //指调用命名空间std内定义的所有标识符
#include //头文件,用于C++字符串

int main()
{
//添加随机种子,根据系统时间生成随机数,防止每次都一样
srand((unsigned int)time(NULL));
//猜数字案例
int num = rand()%100 + 1;//生成0-100的随机数
//cout << num << endl;
int val = 0;//生成0-100的随机数
while (1)
{
cin >> val;
if (val > num)
{
cout << “猜大了” << endl;
}
else if (val < num)
{
cout << “猜小了” << endl;
}
else
{
cout << “猜对了” << endl;
break;
}
}

system("pause");  //让程序暂停一下,然后按任意键继续

return 0;

}

循环结构:
do{循环语句}while(循环条件);
do…while会先执行一次循环语句,在判断循环条件

水仙花例子:(从100-999各个个位数的立方和相加等于这个数本身就是一个水仙花数))
#include
using namespace std; //指调用命名空间std内定义的所有标识符
#include //头文件,用于C++字符串
int main()
{
//
int num = 100;

do
{
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;

	a = num % 10;
	b = num / 10 % 10;
	c = num / 100;
	d = a*a*a + b*b*b + c*c*c;
	if (d == num)
	{
		cout << num << endl;
	}
	num++;
} while (num < 1000);


system("pause");  //让程序暂停一下,然后按任意键继续
return 0;

}

for(起始表达式;条件表达式;末尾循环体){循环语句;}
// An highlighted block
for(i = 0;i < 100; i++)
{
cout << i << endl;
}

嵌套循环:九九乘法口诀
#include
using namespace std; //指调用命名空间std内定义的所有标识符
#include //头文件,用于C++字符串

int main()
{
//
int i = 0;
int j = 0;
for (i = 1; i < 10; i++)
{
for (j = 1; j <= i; j++)
{
cout << j << “*” << i << “=” << j * i << " ";
}
cout << endl;
}

system("pause");  //让程序暂停一下,然后按任意键继续
return 0;//表明程序正常退出,返回到主程序继续往下执行

}

跳转语句:break continue goto

break:退出当前循环
continue:继续执行下面的指令
goto:跳转到指定语句

数组冒泡排序:(对数据内元素进行升序排列 排序总论数=元素个数-1 每轮对比数= 元素个数-排序轮数-1)
#include
using namespace std;//声明使用std空间里的所有标识符

int main() {
//数组,冒泡排序
int arr[9] = { 4,2,8,0,5,7,1,3,9};

cout << "排序前顺序" << endl;
for (int i = 0; i < 9; i++)
{
	cout << arr[i] << endl;
}

//开始排序
for (int i = 0; i < 9-1; i++)
{
	for (int j = 0; j < 9-i-1; j++)
	{
		if (arr[j] < arr[j+1])
		{
			int temp = arr[j + 1];
			arr[j+1] = arr[j];
			arr[j] = temp;
		}
	}
}

cout << "排序后顺序" << endl;
for (int i = 0; i < 9; i++)
{
	cout << arr[i] << endl;
}

system("pause");//让程序暂停一下,然后按任意键继续
return 0;//表明程序正常退出,返回到主程序继续往下执行

}

函数的定予:
1、返回值
2、函数名
3、参数列表
4、函数体语句
5、return表达式

函数调用:
#include
using namespace std;//声明使用std空间里的所有标识符
#include

//函数
int add(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}

int main() {

int a = 10;
int b = 20;
int c = add(a, b);
cout << "c=" << c << endl;

system("pause");//让程序暂停一下,然后按任意键继续
return 0;//表明程序正常退出,返回到主程序继续往下执行

}
.

函数样式:
1、无参无返
2、有参无返
3、无参有返
4、有参有返
#include
using namespace std;

//1、无参无返
void test1()
{
cout << “this is test1.” << endl;
}

//2、有参无返
void test2(int a)
{
cout << "this is test2. " << a << endl;
}

//3、无参有返
int test3()
{
cout << “this is test3.” << endl;
return 1000;
}

//3、有参有返
int test4(int a)
{
cout << "this is test4. " << a << endl;
return a;
}

int main()
{
system(“pause”);
return 0;
}

分文件的编写:
1、创建后缀名为.h的头文件
2、创建后缀名为.cpp的源文件
3、在头文件中写函数的声明
4、在源文件中写函数的定义

指针的定义与使用:
#include
using namespace std;//声明使用std空间里的所有标识符
#include
int main() {

//1、定义指针
int a = 10;
//指针定义语法,数据类型*指针变量名
int *p;
//让指针记录变量a的地址
p = &a;
cout << "a的地址为" << &a << endl;
cout << "指针p为" << p << endl;

//2、使用指针
//可以通过解引用的方式来找到指针指向的内存
//指针前加*代表解引用,找到指针指向的内存中数据
*p = 1000;
cout << "a=" << a << endl;
cout << "*p=" << *p << endl;

system("pause");//让程序暂停一下,然后按任意键继续
return 0;//表明程序正常退出,返回到主程序继续往下执行

}

指针所占内存空间大小:
#include
using namespace std;//声明使用std空间里的所有标识符
#include

int main() {

//1、定义指针
int a = 10;
int *p = &a;

//在32位操作系统下,指针是占用4个字节空间大小,不管什么数据类型
//在64位操作系统下,指针是占用8个字节空间大小
cout << "sizeof(int *)=" << sizeof(int *) << endl;
cout << "sizeof(int *)=" << sizeof(double *) << endl;

system("pause");//让程序暂停一下,然后按任意键继续
return 0;//表明程序正常退出,返回到主程序继续往下执行

}
在32位操作系统下,指针是占用4个字节空间大小,不管什么数据类型
在64位操作系统下,指针是占用8个字节空间大小

const修饰指针有三种情况:
1、const修饰指针—常量指针
2、const修饰常量—指针常量
3、const即修饰指针又修饰常量

#include
using namespace std;//声明使用std空间里的所有标识符
#include
int main() {

//1、const修饰指针 常量指针
int a = 10;
int b = 10;

const int *p = &a;
//指针指向的值不可以改,指针的指向可以改
//*p = 20;错误
p = &b;

//2、const修饰常量 指针常量
//指针的指向不可以改,指针指向的值可以改
int  * const p2 = &a;
*p2 = 20;//正确
//p2 = &b;错误,指针的指向不可以改

//2、const修饰常量 指针常量
//指针的指向不可以改,指针指向的值不可以改
const int  * const p3 = &a;
//*p3 = 20;//错误
//p3 = &b;错误,指针的指向不可以改

system("pause");//让程序暂停一下,然后按任意键继续
return 0;//表明程序正常退出,返回到主程序继续往下执行

}

指针和函数:
值传递不会改变实参
地址传递会改变实参
指针,数组,函数,冒泡排序:
#include
using namespace std;//声明使用std空间里的所有标识符
#include

void maopao(int * arr, int len)
{
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len - i -1; j++)
{
//如果j>j+1,交换
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printarray(int *arr, int len)
{
for (int i = 0; i < len; i++)
{
cout << arr[i] << endl;
}

}
int main() {
int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };

int len = sizeof(arr) / sizeof(arr[0]);

maopao(arr, len);

printarray(arr, len);

system("pause");//让程序暂停一下,然后按任意键继续
return 0;//表明程序正常退出,返回到主程序继续往下执行

}

结构体:
结构体属于用户自定义的数据类型,允许用户存储不同的数据类型
语法:struct 结构体名{结构体成员列表};

通过结构体创建变量的方式有三种:
struct 结构体名 变量名
struct 结构体名 变量名 = {成员1值,成员2值…}
定义结构体时顺便创建变量
#include
using namespace std;//声明使用std空间里的所有标识符
#include

struct student {
//结构体
string name;
int age;
int score;
};
int main() {
struct student stu1[3] =
{
{“zhang”,21,84},
{“li”,22,87},
{“wang”,23,95}
};
for (int i = 0; i < 3; i++)
{
cout << “姓名:” << stu1[i].name << “年龄:” << stu1[i].age << “分数:” << stu1[i].score << endl;
}

system("pause");//让程序暂停一下,然后按任意键继续
return 0;//表明程序正常退出,返回到主程序继续往下执行

}

结构体指针:(操作符->可以通过结构体指针访问结构体属性)
// An highlighted block
#include
using namespace std;//声明使用std空间里的所有标识符
#include

struct student {
//结构体
string name;
int age;
int score;
};
int main() {
struct student stu1= {“zhang”,21,84};

struct student *p = &stu1;

cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;

system("pause");//让程序暂停一下,然后按任意键继续
return 0;//表明程序正常退出,返回到主程序继续往下执行

}

结构体作函数参数:
#include
using namespace std;//声明使用std空间里的所有标识符
#include

struct student
{
//结构体
string name;
int age;
int score;
};
//值传递
void printStudent1(struct student stu1)
{
cout << “姓名:” << stu1.name << “年龄:” << stu1.age << “分数:” << stu1.score << endl;
}
//地址传递
void printStudent2(struct student *p)
{
cout << “姓名:” << p->name << “年龄:” << p->age << “分数:” << p->score << endl;
}
int main() {

struct student stu1;
stu1.name = "zhangsan";
stu1.age = 20;
stu1.score = 90;

printStudent1(stu1);
printStudent2(&stu1);

system("pause");//让程序暂停一下,然后按任意键继续
return 0;//表明程序正常退出,返回到主程序继续往下执行

}

// An highlighted block
#include
using namespace std;//声明使用std空间里的所有标识符
#include
#include

struct student
{
//结构体
string sname;
int score;
};

struct teacher
{
string tname;
struct student s[5];
};
//给老师和学生赋值的函数,len是计算老师个数
void allocateSpace(struct teacher t[], int len)
{
string nameSeed = “ABCDE”;

for (int i = 0; i < len; i++)
{
	t[i].tname = "teacher_";
	t[i].tname += nameSeed[i];
	for (int j = 0; j < 5; j++)
	{
		t[i].s[j].sname = "student_";
		t[i].s[j].sname += nameSeed[j];

		int random = rand() % 61 + 40;//40~100
		t[i].s[j].score = random;
	}
}

}
void printInfo(struct teacher t[],int len)
{
for (int i = 0; i < len; i++)
{
cout << "老师姓名: " << t[i].tname << endl;
for (int j = 0; j < 5; j++)
{
cout << “\t学生姓名:” << t[i].s[j].sname
<< “考试分数:” << t[i].s[j].score << endl;
}
}

}
int main() {
//随机数种子
srand((unsigned int)time(NULL));

//1、创建3名老师的数组
struct teacher t[3];

//2、通过函数给3名老师的信息赋值,并给老师带的学生信息赋值
int len = sizeof(t) / sizeof(t[0]);
allocateSpace(t, len);

//3、打印所有老师及所带的学生信息
printInfo(t, len);

system("pause");//让程序暂停一下,然后按任意键继续
return 0;//表明程序正常退出,返回到主程序继续往下执行

}

这篇关于C++新手入门的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!