【C\C++】基础语法

指针

int a=0;
&a 是地址
0是 &a这个地址的值

int a=0;
int *p;
p=&a;
// *p作用就是:取指针p的指向的地址,再取这个地址的值!相当于*(&a)=a

*p=*p+1;   //相当于a=a+1 , *(&a)=*(&a)+1
// *p+1作用就是:先取指针p的指向的地址,再取这个地址的值,最后把值加1替换这个地址的原来的值!

p=p+1;  //相当于在p的值中加了一个int类型的大小存储单元,就是加4:&a+4  
// p:0x61fe14 -> 0x61fe18
// *p:6422040

交换

// 相当于复制了一个副本
void swap(int x, int y) {
    int temp;
    temp= x;
    x = y;
    y = temp;
}

int main() {
    int a = 2, b = 3;
    printf("before:%d,%d\n", a, b); // 2,3
    swap(a, b);
    printf("later:%d,%d\n", a, b);  // 2,3
    return 0;
}
// 相当于x=a=&p,int*是类型 *x=*a 
// *x就相当于取指针a的指向的地址,再取这个地址的值!
// 然后 "//2" 把这个地址的值替换为 *y,即此时*a=3了
void swap(int* x, int* y) {
    int temp; 
    temp = *x;//1
    *x = *y;  //2
    *y = temp;//3
}

int main() {
    int p = 2, q = 3;
    int* a = &p, * b = &q;
    printf("before:%d,%d\n", *a, *b); // 2,3
    swap(a, b);
    printf("later:%d,%d\n", *a, *b);  // 3,2
    return 0;
}

C++引用

#include<stdio.h>
#include<iostream>

using namespace std;
// 引用传递,可以理解为a就是x,x就是a,只不过名字不一样
void swap(int& x, int& y) {
    int temp;
    temp = x;
    x = y;
    y = temp;
}

int main() {
    int a = 2, b = 3;
    printf("before:%d,%d\n", *a, *b);  // 2,3
    swap(a, b); 
    printf("later:%d,%d\n", *a, *b);   // 3,2
    return 0;
}

字符串

#include<stdio.h>
#include<iostream>

using namespace std;


int main() {
    // char* s1;
    // s1 = "abc";

    char s2[] = "abc";
    char s3[] = { "abc" };
    char s4[] = { 'a','b','c' };
    
    // char* s5;
    // s5 = { "abc" };
    
    for (int i = 0; i < 3; i++) {
        cout << s2[i];// s2[i],s3[i],s4[i] 都可以
    }



    return 0;
}

typedef

【参考:C语言中struct typedef 以及结构体指针用法_乌鸦大大-CSDN博客
typedef是类型定义的意思 type defined

普通变量

struct Student
{
   int no;
   char name[12];
}Stu; // 定义了一个结构体Stu变量
已经定义了一个结构体Stu变量,如果要重新新建,就必须用struct Student stu1;
typedef struct
{
   int no;
   char name[12];
}Stu; // 定义了一个结构体Stu变量
如果使用了typedef,而且这里也不写Student(于是也不能struct Student stu1;了)
typedef struct Student
{
   int no;
   char name[12];
}Stu,stud; // Student写了就必须定义结构体变量,要不然有的编译器会报错
在声明变量的时候就可:Stu stu1;或者:stud stu2;(Stu 和stud 同时为Student的别名)
struct Student stu3;

举例

typedef struct Queue // 结构体名
{
    int data[maxSize];
    //设置队头和队尾
    int front; // front为当前队头元素的前一个位置
    int rear;
}Queue; // 结构体变量

Queue Q; // Queue 是结构体变量
struct Queue Q // Queue 是结构体名

结构体指针

typedef struct BTNode{
	int data;
	struct BTNode *lchild;
	struct BTNode *rchild;
}BTNode,*btnode;

BTNode *p;等价于btnode p;


一般使用
BTNode *p;
p=(BTNode *)malloc(sizeof(BTNode)); // void* malloc(int size)

引用

C++引用相当于取别名

指向普通变量的引用


void func(int &x){ // 这里不是取地址,而是引用
	++x;
}
int x=1;
func(x); // 传入普通变量
printf("%d",x);// 2

指向指针变量的引用
相当于给指针变量取别名
也相当于C语言中的二级指针

void func(int* &x){ // 这里不是取地址,而是引用
	++(*x);
}
int p = 1;
int* x = &p;
func(x);// 传入指针变量x
printf("%d",*x);// 2

队列

循环队

必须损失一个存储空间来区分队空和队满
顺时针转动,元素夹在(front,rear]之间

队空 Q.front == Q.rear

队满 (Q.rear + 1) % maxSize == Q.front

front为当前队头元素的前一个位置
在这里插入图片描述


c语言

// 简单写法
int Queue[maxSize];
int front,rear;
front=rear=0;

#include<stdio.h>

#define maxSize 10

// 顺序队
struct Queue
{
    int data[maxSize];
    //设置队头和队尾
    int front; // front为当前队头元素的前一个位置
    int rear;
};
// C语言没有bool类型
// 需要修改Q,所以需要传指针
int InitQueue(Queue* Q) {
    Q->front = Q->rear = 0;
    return 1;
}
// 不需要修改Q,所以不用传指针
int isEmpty(Queue Q) {
    if (Q.front == Q.rear) // 队空
        return 1;
    else
        return 0;
}

int pushQueue(Queue* Q, int e) {
    if ((Q->rear + 1) % maxSize == Q->front) { // 队满
        return 0;
    }
    Q->rear = (Q->rear + 1) % maxSize; // 后移一位
    Q->data[Q->rear] = e;
    return 1;
}
// 出队并返回该元素
int popQueue(Queue* Q, int* e) {
    if (Q->front == Q->rear) // 队空
        return 0;
    Q->front = (Q->front + 1) % maxSize; // 后移一位
    *e = Q->data[Q->front];
    return 1;
}

int length(Queue Q) {
    return (Q.rear - Q.front + maxSize) % maxSize;
}

void printQueue(Queue Q) {
    while (!isEmpty(Q))
    {
        int e;
        popQueue(&Q, &e);
        printf("%d\n", e);
    }

}

int main() {

    struct Queue Q;

    InitQueue(&Q);
    pushQueue(&Q, 1);
    pushQueue(&Q, 2);
    pushQueue(&Q, 3);
    pushQueue(&Q, 4);

    int e;
    popQueue(&Q, &e);
    printf("%d\n", e);//1
    printf("%d\n", length(Q));//3

    printQueue(Q);
    return 0;
}

c++ 引用

#include<stdio.h>

#define maxSize 10

// 顺序队
typedef struct
{
    int data[maxSize];
    //设置队头和队尾
    int front; // front为当前队头元素的前一个位置
    int rear;
}Queue;
// C语言没有bool类型
// 需要修改Q,所以需要传引用,&是引用符号
int InitQueue(Queue& Q) {
    Q.front = Q.rear = 0;
    return 1;
}
// 不需要修改Q,所以不用传引用
int isEmpty(Queue Q) {
    if (Q.front == Q.rear) // 队空
        return 1;
    else
        return 0;
}

int pushQueue(Queue& Q, int e) {
    if ((Q.rear + 1) % maxSize == Q.front) { // 队满
        return 0;
    }
    Q.rear = (Q.rear + 1) % maxSize; // 后移一位
    Q.data[Q.rear] = e;
    return 1;
}
// 出队并返回该元素
int popQueue(Queue& Q, int& e) {
    if (Q.front == Q.rear) // 队空
        return 0;
    Q.front = (Q.front + 1) % maxSize; // 后移一位
    e = Q.data[Q.front];
    return 1;
}

int length(Queue Q) {
    return (Q.rear - Q.front + maxSize) % maxSize;
}

void printQueue(Queue Q) {
    while (!isEmpty(Q))
    {
        int e;
        popQueue(Q, e);
        printf("%d\n", e);
    }

}

int main() {

    Queue Q;

    InitQueue(Q);
    pushQueue(Q, 1);
    pushQueue(Q, 2);
    pushQueue(Q, 3);
    pushQueue(Q, 4);

    int e;
    popQueue(Q, e);
    printf("%d\n", e);//1
    printf("%d\n", length(Q));//3

    printQueue(Q);
    return 0;
}

顺序栈

// 简单写法
int Stack[maxSize];
int top-1;

stack[++top]=x;// 进栈
x=stack[top--];// 出栈

#include<stdio.h>

#define maxSize 10

// 顺序队
struct stack
{
    int data[maxSize];
    int top; // 栈顶
};

int Initstack(stack* st) {
    st->top = -1; // 栈空
    return 1;
}

int isEmpty(stack st) {
    if (st.top == -1)
        return 1;
    else
        return 0;
}

int pushstack(stack* st, int e) {
    if (st->top == maxSize - 1) { //栈满 0~maxSize - 1
        return 0;
    }
    (st->top)++;
    st->data[st->top] = e;
    return 1;
}
// 出队并返回该元素
int popstack(stack* st, int& e) {
    if (st->top == -1)
        return 0;
    e = st->data[st->top];
    (st->top)--;
    return 1;
}

int length(stack st) {
    return st.top + 1; // 加上data[0]	
}

void printstack(stack st) {
    while (!isEmpty(st))
    {
        int e;
        popstack(&st, e);
        printf("%d\n", e);
    }

}

int main() {

    struct stack st;

    Initstack(&st);
    pushstack(&st, 1);
    pushstack(&st, 2);
    pushstack(&st, 3);
    pushstack(&st, 4);

    int e;
    popstack(&st, e);
    printf("%d\n", e);//4
    printf("%d\n", length(st));//3

    printstack(st);
    return 0;
}

c++引用

#include<stdio.h>

#define maxSize 10

// 顺序队
struct stack
{
    int data[maxSize];
    int top; // 栈顶
};

int Initstack(stack& st) {
    st.top = -1; // 栈空
    return 1;
}

int isEmpty(stack st) {
    if (st.top == -1)
        return 1;
    else
        return 0;
}

int pushstack(stack& st, int e) {
    if (st.top == maxSize - 1) { //栈满 0~maxSize - 1
        return 0;
    }
    (st.top)++;
    st.data[st.top] = e;
    return 1;
}
// 出队并返回该元素
int popstack(stack& st, int& e) {
    if (st.top == -1)
        return 0;
    e = st.data[st.top];
    (st.top)--;
    return 1;
}

int length(stack st) {
    return st.top + 1; // 加上data[0]
}

void printstack(stack st) {
    while (!isEmpty(st))
    {
        int e;
        popstack(st, e);
        printf("%d\n", e);
    }

}

int main() {

    struct stack st;

    Initstack(st);
    pushstack(st, 1);
    pushstack(st, 2);
    pushstack(st, 3);
    pushstack(st, 4);

    int e;
    popstack(st, e);
    printf("%d\n", e);//4
    printf("%d\n", length(st));//3

    printstack(st);
    return 0;
}

C++

参考《C++面向对象程序设计教程》第三版 陈维兴 林小茶 清华大学出版社

输出

#include<iostream>
using namespace std;


int main()
{
    int x = 30;
    cout << dec << x << endl; // 30 十进制
    cout << hex << x << "\n"; // 1e 八进制
    cout << oct << x << endl; // 36 十六进制
    return 0;
}

const

  • 1.指向常量的指针:一个指向常量的指针变量 (地址可变)
    const char* name=“chen”;
    不允许改变指针所指地址中的常量:name[3]=‘a’; ×
    但可以改变name所指的地址:name=“zhang”;

  • 2.常指针:把指针所指的地址声明为常量,即不能移动的指针(数据可变
    char* const name=“chen”;
    允许改变指针所指地址中的数据:name[3]=‘a’;
    不允许改变name所指的地址:name=“zhang”; ×

  • 3.指向常量的常指针:(数据和地址都不可变
    const char* const name=“chen”;
    不允许改变指针所指地址中的数据:name[3]=‘a’; ×
    不允许改变name所指的地址:name=“zhang”; ×

int可以省略
const int LIMIT=100;
const LIMIT=100;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值