“小课堂”系列旨在针对蓝桥杯等比赛、考试进行快速入门,以及帮助「学过但记忆模糊」的同学快速唤醒脑海中的知识。
注意事项: 1.推荐使用 Dev-C++ 4.9 或 Dev-C++ 5.4 (蓝桥杯使用此IDE),不推荐使用 Visual Studio。 2.推荐参考资料:菜鸟教程 (系统、详细的教程,可作为补充学习),CSDN、CnBlog(有什么问题可以搜了看这上面的解答) 3.教程所讲内容应全部掌握,记在脑中 4.课程中的题目务必完成
输入输出 cin 和 cout 示例:
1 2 3 4 5 6 7 8 9 10 11 #include <iostream> // 包含在iostream中 using namespace std ; int main () { int x; cin >>x; cout <<"hello,world" <<endl ; cout <<"The value of x is: " <<x; cout <<endl ; return 0 ; }
关于cin和cout的输入输出格式控制,会在下一节课讲。
printf 和 scanf 示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <cstdio> // 包含在cstdio中,相当于stdio.h,但C++中应该使用cstdio,没有后缀! int main () { int x = 5 ; printf ("Number = %d" , x); float f; scanf ("%f" , &f); double d=2.33 ; printf ("%lf" , d); char c; scanf ("%c" , &c); string s="hello" ; printf ("%s" , s); printf ("\n" ); return 0 ; }
一定要牢记上述几种常用类型的控制符 ,输入时后面跟地址 ,输出时后面跟变量 ,并且控制符要包含在引号中 。
关于printf和scanf的输入输出格式控制,会在下一节课讲。
文件输入输出 fprintf 和 fscanf 示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <cstdio> #include <string> // 因为用到了string,所以要包含这个头文件,具体用法会在后面讲到 int main () { FILE * fp; fp = fopen ("file.txt" , "w+" ); fprintf (fp, "%s %d" , "hello,world" , 2019 ); string str; int year; fscanf (fp, "%s%d" , str, &year); fclose(fp); return 0 ; }
fopen常用的文件访问模式(即第二个参数)
w+ :创建一个可读写的空文件,如果已存在同名文件,则会删除已有文件的内容
a+ :打开一个用于读取和追加 的文件,如果文件不存在,则创建文件
注意点
使用方法基本与printf、scanf一样,但要注意以下几点:
在使用文件前要调用fopen 打开文件,并且将返回值复制给一个FILE* 类型的变量fp
文件使用完后要调用fclose,注意要传入一个FILE* 类型的变量fp
调用fprintf和fscanf时传入的第一个参数是fp
fstream 中的函数(C++特有) 示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <fstream> // 包含在头文件fstream中 #include <string> #include <iostream> using namespace std ; int main () { ofstream fout; fout.open("file.txt" ); fout << “hello,world” << 2019 << endl ; fout.close(); ifstream fin; fin.open("file.txt" ); string str; int year; fin >> str >> year; fin.close(); return 0 ; }
注意点
使用方法和cin、cout基本一样,但有以下注意点:
读写要用不同的流对象(ofstream 和 ifstream ),对象的名字可以随意起,但要注意与文件、读写模式对应
创建好流对象后,要用「成员函数 open」 打开指定文件,一定注意要以 「fout.open(PATH)」 形式调用,不要和上面的 fopen 搞混了
循环语句 for 1 for (变量赋值; 循环继续的条件; 每次迭代的变量更新)
break 和 continue break :跳出break语句所在的循环体continue :不执行continue语句后的语句,直接开始下一轮循环
while 和 do while 1 2 3 4 5 6 7 8 while (循环的条件){ } do { } while (循环继续的条件);
指针 示例(如果太绕,可以先忽略小括号中的内容):
1 2 3 4 int x=2 ; int * p=&x; cout <<p<<endl ; cout <<*p<<endl ;
⚠️注意: void*不是「指向void变量的指针」,而是「可以指向任意类型变量的指针」,例如:
指针看似简单,实则复杂,会在后续课程中陆续补充。
数组 1 2 3 4 5 6 7 8 9 #include <iostream> using namespace std ;int num1[4 ]; int main () { int num2[4 ]={0 }; int *num3=new int [4 ](0 ); }
考虑这样一个问题:读入十个整数,逆序输出到一行中。(动手写一写)
思考以下问题(下节课会解答):
1 2 3 int nums[10]; nums; &nums[0]; //nums和&nums[0]有何异同?
字符串 C风格字符串 下堂课讲
string 示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <iostream> #include <string> // 包含在string头文件中,不能忘! using namespace std ; int main () { string str1 = "Hello" ; string str2 = "World" ; string str3; int len ; cout <<str1[0 ]<<endl ; str3 = str1; cout << "str3 : " << str3 << endl ; str3 = str1 + str2; cout << "str1 + str2 : " << str3 << endl ; len = str3.size(); cout << "str3.size() : " << len << endl ; return 0 ; }
string使用起来十分简单方便,一般情况下用string作为字符串就行。
函数 函数返回类型 函数名(参数类型1 形参名1,参数类型2 形参名2) {代码段}
示例:
1 2 3 4 5 6 int max (int a, int b) { if (a>b) return a; else return b; }
⚠️注意: C++中的函数参数都是按值传递 !
什么是按值传递:
1 2 3 4 5 6 7 8 9 10 void func (int x) { x=x+1 ; cout <<x; } int main{ int a=2 ; func(a); cout <<a; }
题目
从键盘输入一个任意长的字符串,设计一个将函数倒序排列的函数stringReverse,将倒序后的结果存入“ans.txt”文件
https://www.luogu.com.cn/problem/P1425
修改以下代码,使得a变量的值会因为add函数的执行而加1(使用指针,不要改变函数的返回类型):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std ;void add (int x) { x=x+1 ; cout <<"x: " <<x<<endl ; } int main () { int a=2 ; add(a); cout <<"a: " <<a<<endl ; return 0 ; }