C++对二进制和文本文件操作练习

#文本文件

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int a[10],max,i,order;
    ifstream infile("f1.dat",ios::in);
    {
        cout << "open error !" << endl;
        exit(1);
    }
    for (i=0;i<10;i++)
    {
        infile >> a[i];
        cout << a[i] << " ";
    }
    cout << endl;
    max=a[0];
    order=0;
    for (i=1;i<10;i++)
    {
        if(a[i]>max)
        {
            max=a[i];
            order=i;
        }
    }
    cout << "max=" << max << endl << "order=" << order << endl;
    infile.close();

    return 0;
} 

#二进制文件

#include <fstream>
#include <iostream>

using namespace std;

struct student
{
    int num;
    char name[20];
    float score;
};

int main()
{
    student stud[5]={1001,"Li",85,1002,"Fun",97.5,1004,"Wang",54,1006,"Tan",75.6,1010,"ling",96};
    fstream iofile("stud.dat",ios::in|ios::out|ios::binary);
    if(!iofile)
    {
        cerr << "open error !" << endl;
        abort();
    }
    for(int i=0;i<5;i++)
        iofile.write((char*)&stud[i],sizeof(stud[i]));
    student stud1[5];
    for (int i=0;i<5;i=i+2)
    {
        iofile.seekg(i*sizeof(stud1[i]),ios::beg);
        iofile.read((char*)&stud1[i/2],sizeof(stud1[i/2]));
        cout << stud1[i/2].num << " " << stud1[i/2].name << " " << stud1[i/2].score << endl;
    }
    cout << endl;
    stud[2].num=1012;
    strcpy(stud[2].name,"Wu");
    stud[2].score=60;
    iofile.seekp(2*sizeof(stud[0]),ios::beg);
    iofile.write((char*)&stud[2],sizeof(stud[2]));
    iofile.seekg(0,ios::beg);
    for(int i=0;i<5;i++)
    {
        iofile.read((char*)&stud[i],sizeof(stud[i]));
        cout << stud[i].num << " " << stud[i].name << " " << stud[i].score << endl;
    }
    iofile.close();

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

虚函数及捕获异常练习

#虚函数
#include <iostream>
using namespace std;

class base  //定义基类
{
    public:
        virtual void who()  //虚函数声明
        {
            cout<<"this is the class of base !"<<endl;
        }
};

class derive1: public base  //定义派生类derive1
{
    public:
        void who()  //重新定义虚函数
        {
            cout << "this is the class of derive1 !" << endl;
        }
};

class derive2: public base  //定义派生类derive2
{
    public:
        void who()  //重新定义虚函数
        {
            cout << "this is the class of derive2 !" << endl;
        }
};

int main()
{
    base obj, *ptr; //声明基类对象obj、指针ptr
    derive1 obj1;   //声明派生类1的对象obj1
    derive2 obj2;   //声明派生类2的对象obj2
    ptr= &obj;  //基类指针指向基类对象
    ptr -> who();   //调用基类成员函数
    ptr = &obj1;    //基类指针指向派生类1对象
    ptr -> who();   //调用派生类1成员函数
    ptr=&obj2;  //基类指针指向派生类2对象
    ptr -> who();   //调用派生类2成员函数
    return 0;
}

#捕获异常
#include <iostream>

using namespace std;

int Div(int x,int y);

int main()
{
    try
    {
        cout<<"5/2="<<Div(5,2)<<endl;
        cout<<"8/0="<<Div(8,0)<<endl;
        cout<<"7/1="<<Div(7,1)<<endl;   //由于8/0抛出异常,所以7/1并没有运行
    }
    catch(int)
    {
        cout<<"except of deviding zero"<< endl;;
    }
    cout << "that is ok" << endl;
    return 0;
}

int Div(int x,int y)
{
    if(y==0)
    {
        throw y;
    }
    return x/y;
}
发表在 C++ | 留下评论

基类对象与派生类对象指针

#include <iostream>
#include <string>

using namespace std;

class String
{
    char *name;
    int length;
    public:
        String(char *str)
        {
            length=strlen(str);
            name=new char[length+1];
            strcpy(name,str);
        }
        void show()
        {
            cout<<name<<endl;
        }
};

class de_string:public String
{
int age;
public:
    de_string(char *str, int age):String(str)
    {
        de_string::age=age;
    }
    void show()
    {
        String::show();
        cout<<"the age is:"<<age<<endl;
    }
};

int main()
{
    String s1("tony"),*ptr1;
    de_string s2("jack",20),*ptr2;
    ptr1=&s1;
    ptr1->show();
    ptr1=&s2;   //将ptr1指向String类的派生类de_string的对象s2
    ptr1->show();   //调用s2对象所属的基类的成员函数show()
    ptr2=&s2;
    ptr2 -> show();
    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;
}


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;
}

#endif

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


void insertList(Node *head,int nodeData)
{
    Node *cur = (Node *)malloc(sizeof(Node));
    if(NULL == head)
        exit(-1);
    cur -> data=nodeData;
    cur -> next = head -> next;
    head -> next = cur;

}

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

int lenList(Node *head)
{
    int len=0;
    head =head -> next;
    while(head)
    {
        len++;
        head=head -> next;
    }
    return len;
}

Node *searchList(Node *head,int findData)
{
    head = head -> next; 
    while(head)
    {
        if(head -> data == findData)
        {   
            break;
        }
        head = head -> next;
    }
    return head;
}

void deleteNodeOfList(Node *head, Node *pfind)
{
    if(pfind ->next ==NULL)
    {
        while(head -> next != pfind)
            head=head ->next;
        head -> next= pfind -> next;
        free(pfind);
        pfind = NULL;
    }
    else
    {
        Node *t = pfind -> next;
        pfind -> data = pfind -> next -> data;
        pfind -> next = pfind -> next -> next;
        free(t);
    }

}

#if 0
void popSortList(Node *head)
{
    int len = lenList(head);
    head = head -> next;
    Node *p,*q;
    int i,j;
    for (i=0;i<len-1;i++)
    {
        p=head;
        q=p -> next;
        for (j=0;j<len-1-i;j++)
        {
            if(p -> data > q -> data)
            {
                p -> data ^= q -> data;
                q -> data ^= p -> data;
                p -> data ^= q -> data;
            }
            p = p-> next;
            q = p -> next;
        }
    }
}
#endif

void popSortList(Node *head)
{
    int len =lenList(head);
    int i,j;
    Node *prep,*p,*q,*t;
    for (i=0;i<len-1;i++)
    {
        prep=head;
        p=head -> next;
        q=p -> next;
        for(j=0;j<len-1-i;j++)
        {
            if(p -> data > q -> data)
            {
                prep -> next = q;
                p -> next = q -> next;
                q -> next = p;

                t=p;
                p=q;
                q=t;

            }
            prep = prep -> next;
            p = p -> next;
            q = q -> next;
        }
    }   
}

void reverseList(Node *head)
{
    Node * cur = head -> next;
    head -> next = NULL;
    Node *t;
    while(cur)
    {
        t = cur;
        cur = cur -> next;

        t -> next = head -> next;
        head -> next =t;
    }   
}


void destroyList(Node *head)
{
    Node *t;
    while(head)
    {
        t=head;
        head=head -> next;
        free(t);
    }
}


int main(int argc, char *argv[])
{
    Node *head = createList();

    srand(time(NULL));

    int i;
    for (i=0;i<10;i++)
    {
        //insertList(head,i);
        insertList(head,rand()%100);
    }
    traverseList(head);

    printf("len of list = %d\n",lenList(head));

    Node *pfind = searchList(head,9);

    if (pfind == NULL)
    {
        printf("find none\n");
    }
    else
    {
        pfind ->data=1000;
        traverseList(head);

        printf("your find in list\n");
        deleteNodeOfList(head,pfind);
    }

    traverseList(head);

    popSortList(head);
    printf("after popsort\n");
    traverseList(head);

    reverseList(head);  
    printf("after reverse\n");
    traverseList(head);

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

对象指针练习

#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++ | 留下评论