【实例】结构体转换为类

来源:互联网 发布:阿里指数能看淘宝吗 编辑:程序博客网 时间:2024/06/03 20:45
请将  结构体  转换成   类:
 c    转换成  c++ :
   谢谢!!!  80分
#include<stdio.h>
#define MAX 100
   typedef int elemtype;
   typedef struct
    { elemtype Data[MAX];
      int len;
     }SQlist;

int Insert(SQlist *L,int i,elemtype x)   /*插入*/
   { int j;
     if(i<1||i>L->len)
       { printf("/nerror,insert failure!");
    return(0);
}
     if(L->len>MAX-1)
       { printf("/nfull!");
 return(0);
       }
     for(j=L->len;j>=i;j--)
 L->Data[j+1]=L->Data[j];
     L->Data[i]=x;
     L->len++;
     return(1);
   }
int Delete(SQlist *L,int i)   /*删除*/
    { int j;
      if(i<1||i>L->len)
       { printf("/ndelete error,failure!");
  return(0);
       }
      if(L->len<1)
{ printf("/nempty,failure!");
  return(0);
}
       for(j=i;j<L->len;j++)
    L->Data[j]=L->Data[j+1];
       L->len--;
       return(1);
     }
int Output(SQlist L)   /*输出*/
   {  int i;
      if(L.len<1)
       { printf("/n empty,Output failure!");
     return(0);
       }
      for(i=1;i<=L.len;i++)
     printf("%5d",L.Data[i]);
      return(1);
   }
int Locate(SQlist L,elemtype x)   /*查找*/
   { int i;
     L.Data[0]=x;
     i=L.len;
     while(x!=L.Data[i])
        i--;
     return(i);
   }
int Create(SQlist *L)   /*建表*/
   { int i,n;
     elemtype x;
     printf("/nplease input n(n<99) for create data:");
     scanf("%d",&n);
     if(n>MAX-1)
      { printf("/nerror! n is1~99");
    return(0);
      }
     L->len=n;
     for(i=1;i<=n;i++)
       { scanf("%d",&x);
         L->Data[i]=x;
       }
     return(1);
   }
main()
 {
    SQlist L;
    int ch,i,v,a;
    elemtype x;
    printf("/n/n1--insert/n2--delete/n3--ouput/n4--locate/n");
    Create(&L);
    do
    {
       printf("/nplease input a number 1--4:/n");
       scanf("%d",&ch);
       switch(ch)
        {
 
 case 1:printf("/nplease input i,x:");
        scanf("%d%d",&i,&x);
        Insert(&L,i,x);
Output(L);break;
 case 2:printf("/nplease input i:");
                scanf("%d",&i);
Delete(&L,i);
Output(L);
break;
 case 3:Output(L);break;
 case 4:printf("/nplease input x:");
scanf("%d",&x);
v=Locate(L,x);
printf("/nbe located is %d",v);
break;
       }
    }while(ch>0&&ch<5);
 }
 
 
 
#include <iostream>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
template<typename T>
class Vector {
public:
        Vector();
        ~Vector();
        void insert(T, int); // 插入一个结点在链表中指定的位置
        void add(T); // 插入一个结点在链表尾
        T remove(int); // 从链表中删除指定位置的元素, 返回删除的元素.
        int contains(T) const; // 查找给定的元素,如果找到,返回元素在链表中的位置,没找到,返回-1
        void sort(); // 对链表进行升序排序
        void reverse(); // 反转链表
        T get(int) const; // 取得链表中第i个位置的元素
        void print() const; // 打印出链表中的所有元素
        int getSize() const { // 取得链表中元素的个数
                return this->size;
        }
       
        int getCapacity() const { // 取得链表最大能容纳元素的个数
                return this->capacity;
        }
private:
        int size; // 链表元素的个数
        int capacity; // 链表能容纳元素的最大数
        T* data; // 存储链表的元素数组
        const static int CREASE_LENGTH = 32; // 每次增加链表容量的个数.
        T NO_ELEMENT; // 当给定非法的下标从链表中取元素时,返回此标志,表示取得的值非法.
};
template<typename T>
Vector<T>::Vector():size(0), capacity(16) {
        data = new T[capacity]; // 为链表元素初始化一段空间
        memset(&NO_ELEMENT, -1111111, sizeof(T)); // 非法链表值
}
template<typename T>
Vector<T>::~Vector() {
        delete[] data;
}
template<typename T>
void Vector<T>::add(T d) {
        if (size + 1 > capacity) { // 当增加元素时,可以链表容量不够,重新分配空间
                T* temp = new T[capacity + CREASE_LENGTH];
                if (NULL == temp) { // 如果分配内存不成功,则函数返回
                        cout << "Cann't allocation Memory." << endl;
                        return;
                }
                capacity += CREASE_LENGTH;
                memcpy(temp, data, capacity * sizeof(T));
                delete[] data; // 记得释放以前链表的空间,以免造成内存泄漏
                data = temp;
                temp = NULL;
                cout << "reallocation: [capacity " << capacity << ", size" << size << "]" << endl;
        }
        data[size++] = d;
}
template<typename T>
void Vector<T>::insert(T d, int loc) {
        if (loc < 0 || loc > size) {
                cout << "Insert Exception: Bad allocation." << endl;
                return;
        }
       
        if (size + 1 > capacity) {
                T* temp = new T[capacity + CREASE_LENGTH];
                if (NULL == temp) {
                        cout << "Cann't allocation Memory." << endl;
                        return;
                }
                capacity += CREASE_LENGTH;
                memcpy(temp, data, capacity * sizeof(T));
                delete[] data;
                data = temp;
                temp = NULL;
                cout << "Reallocation: [capacity " << capacity << ", size" << size << "]" << endl;
        }
       
        for (int i = size; i > loc; i--) {
                data[i] = data[i - 1];
        }
        data[loc] = d;
        size++;
}
template<typename T>
T Vector<T>::remove(int loc) {
        if (loc < 0 || loc >= size) { // 判断给定的位置是否合法,不合法,则函数返回
                cout << "Remove Exception: Bad allocation." << endl;
                return NO_ELEMENT;
        }
       
        T temp = data[loc];
        for (int i = loc; i < size - 1; i++) {
                data[i] = data[i+1]; // 删除元素后,要把链表中的元素移动.
        }
        size--;
       
        return temp;
}
template<typename T>
int Vector<T>::contains(T d) const {
        for (int i = 0; i < size; i++) {
                if (data[i] == d) {
                        return i; // 返回给定元素在链表中的下标位置
                }
        }
       
        return -1;
}
template<typename T>
T Vector<T>::get(int loc) const {
        if (loc < 0 || loc >= size) {
                cout << "Get Exception: Bad allocation." << endl;
                return NO_ELEMENT;
        }
        return data[loc];
}
template<typename T>
void Vector<T>::sort() {
        // 选择法对链表进行排序.
        for (int i = 0; i < size - 1; i++) {
                int k = i;
                for (int j = i + 1; j < size; j++) {
                        if (data[j] < data[k]) {
                                k = j;
                        }
                }
               
                if (k != i) {
                        T temp = data[k];
                        data[k] = data[i];
                        data[i] = temp;
                }
        }
}
template<typename T>
void Vector<T>::reverse() {
        // 反转链表,只需要计算一半位置,左右两边的值进行交换.
        int middle = size / 2;
        T temp;
        for (int i = 0; i < middle; i++) {
                temp = data[i];
                data[i] = data[size - 1 - i];
                data[size - 1 - i] = temp;
        }
}
template<typename T>
void Vector<T>::print() const {
        cout << "[";
        for (int i = 0; i < size - 1; i++) {
                cout << data[i] << ", ";
        }
        cout << data[size - 1] << "]" << endl;
}
int main() {
        Vector<int> vec;
        cout << "Initial size: " << vec.getSize() << endl;
        cout << "Initial capacity: " << vec.getCapacity() << endl;
       
        srand(time(0));
        for (int i = 0; i < 20; i++) {
                vec.add(rand() % 100);
        }
        vec.print();
        cout << "Current size: " << vec.getSize() << endl;
        cout << "Current capacity: " << vec.getCapacity() << endl;
       
        cout << "Random remove: " << endl;
        vec.remove(vec.getSize() - rand() % (vec.getSize() + 1));
        vec.print();
       
        cout << "After sort: " << endl;
        vec.sort();
        vec.print();
       
        cout << vec.get(-1) << endl;
        cout << "Random insert: " << endl;
        vec.insert(rand() % 500, rand() % vec.getSize());
        vec.insert(rand() % 500, rand() % vec.getSize());
        vec.print();
       
        cout << "After sort: " << endl;
        vec.sort();
        vec.print();
       
        cout << "After reverse: " << endl;
        vec.reverse();
        vec.print();

        return 0;
}
原创粉丝点击