入门:
void hello() {
cout << "!!!hello world!!!" << endl;
int a = 1;
cout << a << endl;
int b;
cout << "enter a number:" << endl;
cin >> b;
cout << "xxxxxxxxxxxx: " << b << endl;
}
数据类型:
/**
* unsigned long:
* long:
* unsigned int:
* int:
* unsigned short:
* short:
* unsigned char:
* char:
* bool:
*
* C11新增两个类型:
* unsigned long long:
* long long
*/
int dataType() {
unsigned long ul = ULONG_MAX;
cout << "unsigned long: " << sizeof(ul) << endl; //unsigned long: 4
cout << ul << endl; //4294967295
char a = 'A';
cout << "A:" << a + 1 << endl;
cout << a << endl;
cout.put(a);
return 0;
}
数组:
/**
* array
*/
int arrayDemo() {
int nums[3] = { 2 };
cout << nums[0] << endl;
cout << nums << endl; //0x62ff04
cout << sizeof(nums) << endl; //12
return 0;
}
字符串:
int stringDemo() {
// C 语言中的字符串 形式1
char chs01[8] = { 'a', 'b', 'c', 'd', 'e', '2', '3', 'g' };
char chs02[8] = { '1', '2', '3', '4', '\0' };
cout << chs01[5] + 0 << endl;
cout << chs01[6] + 0 << endl;
cout << chs01[7] + 0 << endl;
cout << chs01[8] + 0 << endl;
cout << chs01[9] + 0 << endl;
cout << chs01[10] + 0 << endl;
cout << chs01[11] + 0 << endl;
cout << chs01 << endl; //abcde23g`�@
cout << chs02 << endl; //1234
// C 语言中的字符串 形式2
char chs03[8] = { "hello" };//unused variable 'chs03' [-Wunused-variable]
char chs04[8] { "hello" };
cout << chs03 << endl;
cout << chs04 << endl;
return 0;
}
int stringDemo02() {
const int Size = 15;
char name1[Size];
char name2[Size] = { "HelloWorldwer" };
char name3[Size];
char name4[Size];
//array must be initialized with a brace-enclosed initializer
//char name2[15] = "HelloWorld"; //编译通过不了
//char name2[15] = {"HelloWorldwer"};
//char name3[Size];
cout << sizeof(name1) << endl; //获取存储字符串声明的长度
cout << strlen(name1) << endl; //strlen获取字符串实际的长度 10
cout << strlen(name2) << endl; //13
cout << strlen(name3) << endl; //0
cout << strlen(name4) << endl; //7
//输出内容
cout << name1 << endl; //?_v\r\n乢v?�@
cout << name2 << endl; // HelloWorldwer
cout << name3 << endl; //没内容
cout << name4 << endl; //孒?*?�@
cout << &name1 << endl; //0x62fefd
cout << &name2 << endl; //0x62feee
cout << &name3 << endl; //0x62fedf
cout << &name4 << endl; //0x62fed0
//cout << strlen(name3) << endl;
//cout << sizeof(name2) << endl;
//cout << strlen(name2) << endl;
cout << "----------------------------" << endl;
cout << "前--第6个位置的元素:" << name1[5] + 0 << endl;
cout << "前--第7个位置的元素:" << name1[6] + 0 << endl;
cout << "前--第8个位置的元素:" << name1[7] + 0 << endl;
//给name1赋值,会修改赋值字符串紧跟的那个字符的值
//赋值前,需要结束的位置的元素值并不是\0
//赋值前,需要结束的位置的元素值变为\0
//但是我们并没有输入\0,我们输入的回车换行
//C++ 会将 空白,制表符,换行符作为字符串输入的结束
cout << "enter name1:" << endl;
cin >> name1;
cout << strlen(name1) << endl;
cout << name1 << endl;
cout << "后--第6个位置的元素:" << name1[5] + 0 << endl;
cout << "后--第7个位置的元素:" << name1[6] + 0 << endl;
cout << "后--第8个位置的元素:" << name1[7] + 0 << endl;
// C 语言中的字符串
//char chs01[8]={'a','b','c','d','e','2','3','g'};
//char chs02[8]={'1','2','3','4','\0'};
return 0;
}
//面向行的输入getline() -- 换行符不在输入队列中
int string_demo03() {
const int Size = 15;
char name1[Size];
char name2[Size];
cout << "enter your name1: " << endl;
cin.getline(name1, Size);
cout << "name1: " << name1 << endl;
cout << "enter your name2: " << endl;
cin.getline(name2, Size);
cout << "name2: " << name2 << endl;
return 0;
}
//面向行的输入get() -- 换行符仍旧在输入队列中
void string_demo04() {
const int Size = 15;
char name1[Size];
char name2[Size];
cout << "enter your name1: " << endl;
cin.get(name1, Size).get();
cout << "name1: " << name1 << endl;
cout << "enter your name2: " << endl;
cin.get(name2, Size).get();
cout << "name2: " << name2 << endl;
//return 0;
}
/**
* 混合输入的问题:
*/
void string_demo05() {
const int Size = 15;
int age;
char name2[Size];
cout << "enter your age: " << endl;
cin >> age;
cin.get();
cout << "age: " << age << endl;
cout << "enter your name2: " << endl;
cin.getline(name2, Size);
cout << "name2: " << name2 << endl;
}
/**
* string 类的简介
*/
void string_demo06() {
//1. C 风格给字符串对象赋值
string s1 = "hello";
string s2;
//2. 使用cin接收字符串对象的值
cout << "请输入s2字符串的值:" << endl;
cin >> s2;
//3. 使用cout输出字符串的值
cout << s2 << endl;
//4. 访问字符串中的元素,可以类似字符数组访问
cout << s2[1] << endl;
//5. 赋值
string s3 = s2;
cout << s3 << endl;
//6. 拼接
string s4 = s1+s2;
cout << s4 << endl;
//7. 追加
s1 += s2;
cout << s1 << endl;
//C 风格 字符串操作
//8. strcpy(charr1,charr2);//copy charr2 to charr1
char arr1[18] = {"HelloWorld"};
cout << arr1 << endl;
char arr2[5];
strcpy(arr2,arr1);//疑问??
cout << arr2 << endl;//HelloWorld,超过arr2的长度就发生意想不到的情况。
cout << "xxxxxxxxxxxxxxxxxxxx: " << arr1 << endl;//World
//9. strcat(charr1,charr2);//append content of charr2 to charr1
char arr3[18] = {"333"};
char arr4[18] = {"HelloWorld"};
strcat(arr4,arr3);
cout << arr4 << endl;
}
结构体:
#include <iostream>
//#include <climits>
//#include <cstring>
struct student {
char name[20];
int age;
double score;
//使用字符串作为成员变量
std::string addr;
};
/**
* 枚举主要用来定义相关的符号常量,而不是用来定义新的类型
*/
enum spectrum {red,orange,yellow,green,blue,violet,indigo};
// 如果只使用符号常量,可以省略枚举名字
enum {red1,orange1,yellow1,green1,blue1,violet1,indigo1};
//指定枚举元素对应的数值
enum spectrum2 {red2=3,orange2=6,yellow2=9};
void mail();
int main2() {
using namespace std;
spectrum band = red;
//spectrum band2 = kkk;//invalid conversion from 'int' to 'spectrum' [-fpermissive]
//band = red+blue;//invalid conversion from 'int' to 'spectrum' [-fpermissive]
int a = red2;
cout << a << endl;
struct person {
char name[20];
int age;
double score;
string addr;
};
/**
* 声明一个结构体,struct 关键字在C++中可以省略,C语言不能省略
*/
struct student stu1 = { "hello", 23, 34.5 };
person p = { "hello", 23, 34.5 };
cout << stu1.age << stu1.name << stu1.score << endl;
cout << stu1.name[1] << endl;
return 0;
}
void mail() {
struct student stu1 = { "hello", 23, 34.5 };
//person p = {"hello",23,34.5};//Type 'person' could not be resolved
}
指针:
/*
* pointer_demo.cpp
*
* Created on: 2019年5月19日
* Author: guo20
*/
#include <iostream>
/**
* 计算机程序在存储数据的时候必须跟踪的3中基本属性:
* 1. 信息存储在何处
* 2. 存储的值是多少
* 3. 存储的信息是什么类型
*
* 如何实现?
* 1. 定义一个变量,声明出类型和符号名,让程序分配内存,并在内存中跟踪该内存单元。
* 2. 指针:指针是一个变量,是存储了变量的地址,而不是值。
*
* 指针策略:内存管理编程理念的核心
* 指针:用于存储值的地址,因此:指针名表示的是地址, * 指针名可以得到该地址的处存储的值
*/
int main3() {
using namespace std;
int num1 = 3;
double num2 = 2.3;
//获取变量的地址
cout << &num1 << endl;
cout << &num2 << endl;
return 0;
}
#include <iostream>
using namespace std;
void string_demo();
void getAddress();
void pointer01();
void pointer02();
void pointer03();
void pointer04();
void pointer05();
struct student
{
char name[20];
float weight;
int age;
};
int main1() {
cout << (3 < 5) << endl;//1 true
cout << (3 > 5) << endl;//0 false
cout.setf(ios_base::boolalpha);//设置标记
cout << (3 < 5) << endl;//true
cout << (3 > 5) << endl;//false
return 0;
}
void pointer05(){
student stu01,stu02,stu03;
//创建指针数组
//stus 是一个指针数组,stus[1] 就是一个指针
const student * stus[3] = {&stu01,&stu02,&stu03};
cout << stus[1] -> age << endl;
//可以创建上述数组的指针
//stus 是一个数组的名称,它是第一个元素的地址。
//但是其第一个元素为指针,因此 ppa也是一个指针
// 指向一个 const student 的指针。这种声明很容易出错。
const student ** ppa = stus;
}
void pointer04(){
struct student stu = {"aa",34.5,21};
cout << "name:" << stu.name << ",age:" << stu.age << ",weight:" << stu.weight << endl;
student * p_stu = new student;
p_stu -> weight = 56.7;
p_stu -> age = 56;
cout << "enter p_stu name: " << endl;
cin.get(p_stu->name,20);
cout << "name:" << (* p_stu).name << endl;//expression must be a modifiable lvalue
cout << "weight:" << p_stu -> weight << endl;
cout << "age:" << p_stu -> age << endl;
}
void pointer03(){
int * pn = new int[10];
pn[0] = 1;
pn[1] = 2;
pn[2] = 3;
cout << sizeof(pn) << endl;//4
cout << pn[0] << endl;
cout << pn[1] << endl;
cout << &pn[1] << endl;
cout << pn[2] << endl;
cout << pn << endl;
//将指针变量加1后,其增加的值等于指向的类型占用的字节数
//pn[1] 获取的第二个元素的值,可以看成是 * (pn+1)
// 这意味:先计算数组第二个元素的地址,然后找到存储在那里的值。
// 所以: pn[1] 等价于 * (pn+1)
// 数组的通用写法: arrayname[i] 等价于 *(arrayname+i)
// 指针的通用写法: pointername[i] 等价于 *(pointername+i)
//区别1: 指针可以写 pn = pn+1,数组不能这么写
//区别2:sizeof (数组) 得到的数组的长度, sizeof(指针) 得到的指针的长度。
pn = pn + 1;
cout << pn << endl;
cout << pn[0] << endl;
pn = pn - 1;
delete pn;
}
/**
* 使用new 分配内存
*/
void pointer02(){
//分配内存,C++提供了检测并处理内存分配失败的工具
int * pt = new int;
double * pd = new double;
cout << pt << endl;
cout << pd << endl;
//赋值
* pt = 3;
* pd = 3.4;
cout << *pt << endl;
cout << *pd << endl;
}
/**
* 使用指针修改变量的值
*/
void pointer01(){
using namespace std;
int updates = 3; //声明一个变量
int * p_updates; //声明指针指向int类型
/**
*
* int* 是指向int类型的指针
* */
p_updates = &updates; //将updates的地址保存p_updates
//p_updates 保存了 updates 的地址,可以说:p_updates 指向了 updates
//输出两个的值
cout << "Value: updates = " << updates << endl;//Value: updates = 3
cout << "Value: * p_updates = " << *p_updates << endl;//Value: * p_updates = 3
//输出两个地址
cout << "Value: &updates = " << &updates << endl;//Value: &updates = 0x61ff08
cout << "Value: p_updates = " << p_updates << endl;//Value: p_updates = 0x61ff08
//使用指针修改变量的值
* p_updates = *p_updates + 1;
cout << "Value: updates = " << updates << endl;//Value: updates = 4
cout << "Value: * p_updates = " << *p_updates << endl;//Value: * p_updates = 4
}
//获取变量的地址
void getAddress(){
using namespace std;
int num1 = 3;
double num2 = 2.3;
//获取变量的地址
cout << &num1 << endl;
cout << &num2 << endl;
}
#include<iostream>
using namespace std;
struct student {
int age;
};
int main4() {
student stu01, stu02, stu03;
stu01.age = 1998;
student * pa = &stu02;
pa->age = 2003;
student * pa02 = &stu03;
pa02->age = 1200;
student stus[3];
stus[0].age = 1;
stus[1].age = 2;
stus[2].age = 3;
cout << stus->age << endl;
cout << (stus + 1)->age << endl;
cout << (stus + 2)->age << endl;
stu01.age = 1998;
//创建指针数组
const student * arp[3] = { &stu01, &stu02, &stu03 };
cout << arp[1]->age << endl;
cout << arp[2]->age << endl;
cout << arp[0]->age << endl;
//创建指针,ppa 保存了arp的首元素地址
const student ** ppa = arp;
cout << (*ppa)->age << endl;
cout << (*(ppa+1))->age << endl;
cout << (*(ppa+2))->age << endl;
//auto ppb = arp;//C++ automatic type deduction
//or else use const student ** ppb = arp;
return 0;
}
循环:
/*
* loop_demo.cpp
*
* Created on: 2019年5月21日
* Author: guo20
*/
#include <iostream>
#include <cstring>
using namespace std;
const int ArSize = 16;
void loop02();
int main_loop(){
loop02();
return 0;
}
void loop02(){
string word = "hello";
//如果两个字符串相等,则下面的结果为 true
bool b = strcmp("aa","aa") == 0;
cout.setf(ios_base::boolalpha);
cout << strcmp("","") << endl;
cout << b << endl;//false
//cout << "enter a word: " << endl;
//cin >> word;
/*for (unsigned int i = 0; i < word.size(); i++)
{
cout << word[i];
}
*/
}
if:
#include <iostream>
#include <fstream>
#include <cctype>
void if_demo() {
while (true) {
cout << "enter a char letter: " << endl;
char a;
//cin >> a;
cin.get(a);
if (isalpha(a)) {
cout << "字母" << endl;
} else if (isdigit(a)) {
cout << "数字" << endl;
} else if (ispunct(a)) {
cout << "punct" << endl;
} else if (isspace(a)) {
cout << "space" << endl;
} else {
cout << "other" << endl;
exit(EXIT_FAILURE);
}
}
}
网友评论