C++学习 第一阶段小结
基本知识点
1. C++代码的基本格式
#include <iostream>
using namespace std;
int main()
{
// Code
system("pause");
return 0;
}
2. 数据类型:int、float、double、string(注意string要搭配头文件使用#include <string>
)等
3. 选择结构
//if...else
if( condition )
{
statements
}
else
{
statements
}
//switch...case
switch( expression )
{
case constant: statements
break;
default: //Code
break;
//如果case里面要写多行代码可以
//case 1 :
//{
// //Code
//}
//break;
//不然会报错
}
4. 循环结构
//while
while( condition )
{
statements
}
//do...while
//注意do...while循环会先执行一次再判断条件
do
{
statements
}while( condition );
//for
for ( initialization; condition; increment )
{
statements
}
5. 数组
int a[3] = {a, b, c};
int a[] = {a, b, c};
int a[3];
6. 结构体
// 个人信息的结构体
struct person
{
//注意使用string要包含头文件
string name;
int age;
string sex;
}; //注意结尾的分号
7. 指针
int a = 10;
int * p;
p = &a; //指针指向变量a的地址
cout << *p << endl; //打印数据a的地址
cout << p << endl; //打印指针变量p
//通过*操作指针变量所指向的内存
8. 函数
#include <iostream>
void print()
{
cout << "Hello World" << endl;
}
int main()
{
print();
system("pause");
return 0;
}
一些有趣但比较容易劝退的问题
1. 关于a++和++a的问题
int main()
{
int a = 0;
cout << a++ << endl;
// cout << ++a << endl;
system("pause")
return 0;
}
2. const修饰指针
int main()
{
int a = 10;
int b = 10;
const int * p1 = &a;
p1 = &a; //正确
//*p = 100;//错误
int * const p2 = &a;
//p1 = &a; //错误
*p = 100; //正确
const int * const p3 = &a;
//p1 = &a; //错误
//*p = 100;//错误
// const修饰什么,什么就不能修改
system("pause");
return 0;
}
3. 值传递与地址传递
void swap1(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
void swap2(int * p1, int * p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main()
{
int a = 10;
int b = 10;
swap1(a, b); //值传递不会改变实参
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap2(&a, &b); //地址传递会改变实参
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
4. 冒泡排序
void bubbleSort(int a[])
{
for (int i = 0; i < 5-1; i++)
{
for (int j = 0; j < 5-i-1; j++)
{
if (a[j]>a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
int main()
{
int a[5] = {1, 3, 5, 4, 2};
bubbleSort(a);
for (int i=0; i<5; i++)
{
cout << a[i] << "\t";
}
system("pause");
return 0;
}
小总结
- 利用所学做一个通讯录系统
#include <iostream>
#include <string>
using namespace std;
#define MAX 1000
//联系人个人信息结构体
struct Person
{
string name;
int sex;
int age;
string phone;
string address;
};
//通讯录结构体
struct Linkmen
{
Person Array[MAX];
int arrNum;
};
//添加联系人
void addLink(Linkmen * p)
{
if (p->arrNum == MAX)
{
cout << "通讯录已满" << endl;
return;
}
else
{
cout << "请输入姓名" << endl;
string name;
cin >> name;
p->Array[p->arrNum].name = name;
cout << "请输入性别" << endl;
cout << "1----男" << endl;
cout << "2----女" << endl;
int sex;
while (true)
{
cin >> sex;
if(sex == 1 || sex == 2)
{
p->Array[p->arrNum].sex = sex;
break;
}
cout << "输入有误,重新输入" << endl;
}
cout << "请输入年龄" << endl;
int age;
cin >> age;
p->Array[p->arrNum].age = age;
cout << "phone number?" << endl;
string phone;
cin >> phone;
p->Array[p->arrNum].phone = phone;
cout << "address?" << endl;
string addr;
cin >> addr;
p->Array[p->arrNum].address = addr;
p->arrNum++;
cout << "添加成功" << endl;
}
}
//显示全部联系人
void printLink(Linkmen * p)
{
if (p->arrNum == 0)
{
cout << "记录为零,请添加联系人" << endl;
}
for (int i = 0; i < p->arrNum; i++)
{
cout << "姓名: " << p->Array[i].name << "\t";
cout << "性别: " << ( p->Array[i].sex == 1?"男":"女" ) << "\t";
cout << "年龄: " << p->Array[i].age << "\t";
cout << "电话: " << p->Array[i].phone << "\t";
cout << "住址: " << p->Array[i].address << endl;
}
}
//删除联系人
void deleteLink(Linkmen * p, string name)
{
if (p->arrNum == 0)
{
cout << "记录为零,请添加联系人" << endl;
}
for (int i = 0; i < p->arrNum; i++)
{
if(name == p->Array[i].name)
{
p->Array[i] = p->Array[i+1];
p->arrNum--;
cout << "删除成功" << endl;
return;
}
}
cout << "联系人不存在,请重新输入" << endl;
}
//查找联系人
void searchLink(Linkmen * p, string name)
{
if (p->arrNum == 0)
{
cout << "记录为零,请添加联系人" << endl;
}
for (int i = 0; i < p->arrNum; i++)
{
if(name == p->Array[i].name)
{
cout << "姓名: " << p->Array[i].name << "\t";
cout << "性别: " << ( p->Array[i].sex == 1?"男":"女" ) << "\t";
cout << "年龄: " << p->Array[i].age << "\t";
cout << "电话: " << p->Array[i].phone << "\t";
cout << "住址: " << p->Array[i].address << endl;
return;
}
}
cout << "联系人不存在,请重新输入" << endl;
}
//修改联系人
void alterLink(Linkmen * p, string name)
{
if (p->arrNum == 0)
{
cout << "记录为零,请添加联系人" << endl;
}
for (int i = 0; i < p->arrNum; i++)
{
if(name == p->Array[i].name)
{
cout << "请输入姓名" << endl;
string name;
cin >> name;
p->Array[i].name = name;
cout << "请输入性别" << endl;
cout << "1----男" << endl;
cout << "2----女" << endl;
int sex;
while (true)
{
cin >> sex;
if(sex == 1 || sex == 2)
{
p->Array[i].sex = sex;
break;
}
cout << "输入有误,重新输入" << endl;
}
cout << "请输入年龄" << endl;
int age;
cin >> age;
p->Array[i].age = age;
cout << "phone number?" << endl;
string phone;
cin >> phone;
p->Array[i].phone = phone;
cout << "address?" << endl;
string addr;
cin >> addr;
p->Array[i].address = addr;
cout << "修改成功" << endl;
return;
}
}
cout << "联系人不存在,请重新输入" << endl;
}
//清空通讯录
void clearLink(Linkmen * p)
{
for (int i = p->arrNum; i >= 0; i--)
{
p->Array[i] = p->Array[i+1];
}
p->arrNum=0;
cout << "清空成功" << endl;
}
//展示菜单
void ShowMenu()
{
cout << "************************" << endl;
cout << "***** 1,添加联系人 *****" << endl;
cout << "***** 2,显示联系人 *****" << endl;
cout << "***** 3,删除联系人 *****" << endl;
cout << "***** 4,查找联系人 *****" << endl;
cout << "***** 5,修改联系人 *****" << endl;
cout << "***** 6,清空联系人 *****" << endl;
cout << "***** 0,退出通讯录 *****" << endl;
cout << "************************" << endl;
}
int main()
{
Linkmen a;
a.arrNum = 0;
int select;
string name;
while (true)
{
ShowMenu();
cin >> select;
switch (select)
{
case 1:
addLink(&a);
break;
case 2:
printLink(&a);
break;
case 3:
cout << "输入想要删除的联系人" << endl;
cin >> name;
deleteLink(&a, name);
break;
case 4:
cout << "输入想要查找的联系人" << endl;
cin >> name;
searchLink(&a, name);
break;
case 5:
cout << "输入想要修改的联系人" << endl;
cin >> name;
alterLink(&a, name);
break;
case 6:
clearLink(&a);
break;
case 0:
cout << "欢迎下次使用" << endl;
return 0;
break;
default:
cout << "输入错误,请重新输入" << endl;
break;
}
}
return 0;
}
- 运行结果:
第一阶段结束,搞起搞起, 从入门到入土