【实例】结构体转换为类
来源:互联网 发布:阿里指数能看淘宝吗 编辑:程序博客网 时间:2024/06/03 20:45
请将 结构体 转换成 类:
c 转换成 c++ :
c 转换成 c++ :
谢谢!!! 80分
#include<stdio.h>
#define MAX 100
typedef int elemtype;
typedef struct
{ elemtype Data[MAX];
int len;
}SQlist;
#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);
}
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);
}
{ 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);
}
{ 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");
{
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);
}
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>
#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;
}
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; // 每次增加链表容量的个数.
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)); // 非法链表值
}
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;
}
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;
}
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++;
}
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;
}
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;
}
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;
}
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;
}
}
}
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;
}
}
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;
}
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();
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;
}
- 【实例】结构体转换为类
- 将结构转换为数组
- 将ORACLE数据库的表转换为C++类与结构体的想法及实现
- int stirng互轉;转换时间结构体为YYYYMMDDhhmmss字符串
- asp.net将类实例转换为JSON格式
- Java json类实例 中文自动转换为unicode
- Gson 转换树型结构的实例
- 将HBITMAP类型转换为BITMAP结构
- [js]表格转换为树形结构
- java 树形结构转换为json字符串
- C++实例 将数字转换为汉字
- Oracle RAC 转换为单实例
- 将MySQL转换为MMSQLServer2008实例过程
- 对象类型转换为数组类型实例
- 将给定格式的线性结构转换为树形结构
- C#调用C++写的dll,包含结构体转换和C#动态调用dll(头文件转换为类)
- C++结构体实例和类实例的初始化
- C++结构体实例和类实例的初始化
- Error establishing socket的解决方法
- python subprocess
- ipv6 socket 编程中遇到的问题
- ibus and fcitx
- 基于ipv6的socket通信(TCP)
- 【实例】结构体转换为类
- 我怎么和别人不一样
- 【图论】【RQNOJ】拜年
- 大家好
- 今天总算可以开通博客了
- Android屏幕禁止休眠的方法
- gewe
- 初学者看过来:简单谈谈 C/C++ 递归的思想,实现,以及和循环的关系
- Linux Kernel and Android 休眠与唤醒