对象指针练习

#include <iostream>
using namespace std;

class Time
{
public:
    Time()
    {
        hour=0;
        minute=0;
        sec=0;
    }
    void set_time();
    void show_time();

    int hour;
    int minute;
    int sec;
};

void Time::set_time()
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}

void Time::show_time()
{
    cout << hour << ":" << minute << ":" << sec << endl;
}

int main()
{
    Time t1;
    Time *pt;
    pt=&t1;
    t1.show_time();
    pt -> set_time();
    t1.show_time();

    int *p1;
    p1=&t1.hour;
    *p1=11;

    //pt -> show_time();
    void(Time::*p2)();
    p2=&Time::show_time;
    (t1.*p2)(); 

    return 0;
}
发表在 C++ | 留下评论

C和C++动态内存的使用

//C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
    char *p = (char *)malloc(sizeof(char)*100);
    if(p==NULL)
    {
        printf("malloc error\n");
        return;
    }
    strcpy(p,"hello");
    printf("str=%s\n",p);
    free(p);
    p=NULL;
}

//C++
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char *p = new char[100];
    strcpy(p,"hello");
    cout << "str=" << p << endl;
    delete []p;
    return 0;
}
发表在 C, C++ | 留下评论

堆栈小测试

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//栈方向
void func()
{
    printf("func:\n");
    int a,b,c,d;
    printf("%p\n",&a);
    printf("%p\n",&b);
    printf("%p\n",&c);
    printf("%p\n",&d);

    return;
}


int main()
{
    printf("main:\n");
    int a,b,c,d;
    printf("%p\n",&a);
    printf("%p\n",&b);
    printf("%p\n",&c);
    printf("%p\n",&d);

    func();

    return 0;
}

//栈溢出

void func(int n)
{
    if(n == 0)
        return;
    else
    func(n-1);
}



int main()
{
    //char arr[1024*1024*10];
    //strcpy(arr,"china");
    func(1000000000); //栈溢出,导致段错误
    printf("over\n");
    return 0;
}

//堆方向
void func()
{
    printf("in func:\n");
    char *pa,*pb,*pc,*pd;
    pa=malloc(100);
    pb=malloc(100);
    pc=malloc(100);
    pd=malloc(100);
    printf("%p\n",pa);
    printf("%p\n",pb);
    printf("%p\n",pc);
    printf("%p\n",pd);
}


int main()
{
    printf("in main:\n");
    char *pa,*pb,*pc,*pd;
    pa=malloc(100);
    pb=malloc(100);
    pc=malloc(100);
    pd=malloc(100);
    printf("%p\n",pa);
    printf("%p\n",pb);
    printf("%p\n",pc);
    printf("%p\n",pd);
    func();
    return 0;
}

//堆的简单使用
int main (int argc, char *argv[])
{
    char * p = (char*)malloc(1024*1024*1024);
    if(p == NULL)
        printf("malloc error\n");
    strcpy (p,"abcd");
    printf("over\n");
    free(p);
        p=NULL;
    return 0;
}
发表在 C | 留下评论

动态链表(头插法和尾插法)

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int data;
    struct node * next;
}Node;

//尾插法

#if 0
Node * createList()
{
    Node * head = (Node *)malloc(sizeof(Node));
    if (NULL == head)
        exit(-1);
    head->next = NULL;

    Node *t = head,*cur;
    int nodeData;
    scanf("%d",&nodeData);
    while(nodeData)
    {
        cur = (Node *)malloc(sizeof(Node));
        if(NULL == head)
            exit(-1);
        cur ->data = nodeData;  
        t -> next = cur;
        t = cur;
        scanf("%d",&nodeData);
    }

    t->next = NULL;
    return head;
}
#endif

//头插法

Node * createList()
{
    Node * head = (Node *)malloc(sizeof(Node));
    if (NULL == head)
        exit(-1);
    head->next = NULL;
    Node *cur;

    int nodeData;
    scanf("%d",&nodeData);
    while(nodeData)
    {
        cur=(Node *)malloc(sizeof(Node));
        if(NULL==head)
            exit(-1);
        cur -> data = nodeData;

        cur -> next = head -> next;
        head -> next = cur;

        scanf("%d",&nodeData);
    }
    return head;
}


void traverseList(Node *head)
{
    head = head -> next;
    while(head)
    {
        printf("%d\n",head -> data);
        head = head -> next;
    }
}

int main(int argc, char *argv[])
{
    Node *head = createList();
    traverseList(head);
    return 0;
}
发表在 C | 留下评论

静态链表练习

#include <stdio.h>

typedef struct node{
    int data;
    struct node * next;
}Node;

void traverseList(Node *head){
    while(head){
        printf("%d\n",head->data);
        head = head -> next;
    }
}

int main (int argc, char *argv[]){
    Node a,b,c,d,e,f;

    Node *head;
    head= &a;

    a.data=1;
    b.data=2;
    c.data=3;
    d.data=4;
    e.data=5;

    a.next=&b;
    b.next=&c;
    c.next=&d;
    d.next=&e;

    e.next=NULL;

    traverseList(head);
    return 0;
}
发表在 C | 留下评论

插入排序

#include <stdio.h>

int main(){
        int a[6]={21,45,58,64,71};
        int i,j,k,x;
        printf("插入前的数组:");
        for (i=0;i<6;i++){
                printf(" %d",a[i]);
        }
        printf("\n请输入要插入的数:");
        scanf("%d",&x);
        for (i=0;i<5;i++){
                if(a[i] > x)
                        break;
        }
        for (j=5;j>i;j--){
                a[j]=a[j-1];
        }
        a[i]=x;
        printf("\n插入后的数组:");
        for(i=0;i<6;i++){
                printf(" %d",a[i]);
        }
        printf("\n");
        return 0;
}
发表在 C | 留下评论