将头文件,调用函数与主函数分开写

头文件如下:

#ifndef _SEQUENCE_h
#define _SEQUENCE_h
#define SIZE   10
#define Success 10001
#define Failure 10000

typedef int Element;

struct SequenceList
{
	int Length;
	Element *List;
};

typedef struct SequenceList SLT;

int SequenceInit(SLT *p);
int SequenceInsert(SLT *t, int p, Element s);
int ListLength(SLT t);
int ListEmpty(SLT t);
int ListFind(SLT t, int p, Element *e);
int ElementLocation(SLT t, Element s, void (*print)(Element));
int ListCheck(SLT t, void (*p)(Element));
int ElementDelete(SLT *t, int p, Element *e, void (*print)(Element));
int ListClear(SLT *t);
int Listdestroy(SLT *t);
int Equal(Element x, Element y);

#endif

调用函数:

#include <stdlib.h>
#include "SequenceList.h"

int Greater(Element x, Element y)
{
	return (x >= y) ? 1 : 0;
}

int Littler(Element x, Element y)
{
	return (x < y) ? 1 : 0;
}

int Equal(Element x, Element y)
{
	return (x == y) ? 1 : 0;
}

int SequenceInit(SLT *p)
{
	if(p == NULL)
	{
		return Failure;
	}
	p->Length = 0;
	p->List = (Element *)malloc(sizeof(Element)*SIZE);
	return Success;
}

int SequenceInsert(SLT *t, int p, Element s)
{
	int i;
	if(t == NULL||p < 1||p > t->Length+1||t->Length >= SIZE)
	{
		return Failure;
	}
	for(i = 0; i < t->Length-p+1; i++)
	{
		t->List[t->Length-i] = t->List[t->Length-i-1];
	}
	t->Length++;
	t->List[p-1] = s;
	return Success;
}

int ListLength(SLT t)
{
	return t.Length;
}

int ListEmpty(SLT t)
{
	return (t.Length == 0) ? Success : Failure;
}

int ListFind(SLT t, int p, Element *e)
{
	if(p < 1||p > t.Length)
	{
		return Failure;
	}
	else
	{
		*e = t.List[p-1];
		return Success;
	}
}

int ListCheck(SLT t, void (*p)(Element))
{
	int i;
	if(t.Length < 1||t.List == NULL)
	{
		return Failure;
	}
	else
	{
		for(i = 0; i < t.Length; i++)
		{
			p(t.List[i]);
		}
		return Success;
	}
}

int ElementLocation(SLT t, Element s, void (*print)(Element))
{
	int i, count = 0;
	for(i = 0; i < t.Length; i++)
	{
	/*	if(t.List[i] == s)*/
		if(Equal(s,t.List[i]))
		{
			print(i+1);
			count++;
		}
	}
	if(count == 0)
	{
		return Failure;
	}
}

int ElementDelete(SLT *t, int p, Element *e, void (*print)(Element))
{
	int i;
	if(t == NULL)
	{
		return Failure;
	}
	*e = t->List[p-1];
	for(i = 0; i < t->Length-p; i++)
	{
		t->List[p-1+i] = t->List[p+i];
	}
	t->Length--;
	for(i = 0; i < t->Length; i++)
	{
		print(t->List[i]);
	}
	return Success;
}

int ListClear(SLT *t)
{
	if(t == NULL)
	{
		return Failure;
	}
	else
	{
		t->Length = 0;
		return Success;
	}
}

int ListDestroy(SLT *t)
{
	if(t == NULL)
	{
		return Failure;
	}
	else
	{
		t->Length = 0;
		free(t->List);
		t->List = NULL;
		return Success;
	}
}

主函数:

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

void print(Element s)
{
	printf("%d ", s);
}
int main()
{
	SLT t;
	int i, ret;
	srand(time(NULL));

	ret = SequenceInit(&t);

	if(ret == Success)
	{
		printf("Init success!\n");
	}
	else if(ret == Failure)
	{
		printf("Init failure!\n");
	}

	for(i = 0; i < 5; i++)
	{
		ret = SequenceInsert(&t,i+1,rand()%10);
		if(ret == Success)
		{
			printf("Insert success!\n");
		}
		else
		{
			printf("Insert failure!\n");
		}
	}

	ret = ListLength(t);
	printf("Length = %d\n", ret);

	ret = ListEmpty(t);
	if(ret == Success)
	{
		printf("Is empty!\n");
	}
	else
	{
		printf("Is not empty!\n");
	}

	int p = 3;
	Element e;
	ret = ListFind(t, p, &e);
	if(ret == Success)
	{
		printf("%dTh is %d\n", p, e);
	}
	else
	{
		printf("Find failure!\n");
	}

	ListCheck(t, print);
	putchar('\n');

	Element s = 3;
	int q;
	ret = ElementLocation(t, s, print);
	if(ret == Failure)
	{
		printf("Locat Failure!\n");
	}
	else
	{
		putchar('\n');
	}

	int j = 3;
	Element r;
	ret = ElementDelete(&t, j, &r, print);
	if(ret == Success)
	{
		putchar('\n');
		printf("%dth is delete success!\n");
	}
	else
	{
		printf("%dth is delete failure!\n");
	}

	ret = ListClear(&t);
	if(ret == Success)
	{
		printf("ListClear Success!\n");
	}
	else
	{
		printf("ListClear Failure!\n");
	}

	ret = ListDestroy(&t);
	if(ret == Success)
	{
		printf("ListDestory Success!\n");
	}
	else
	{
		printf("ListDestory Failure!\n");
	}
	ListCheck(t,print);

	return 0;
}

一起编译执行便可实现功能。 

 

 

 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
连接sqlserver数据库需要使用ADO(ActiveX Data Objects)技术,以下是C++ ADO连接sqlserver数据库的头文件、源文件主函数调用示例: 头文件: ```c++ #include <iostream> #include <windows.h> #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "adoEOF") ``` 源文件: ```c++ using namespace std; int main() { // 定义ADO相关变量 _ConnectionPtr pConn; _RecordsetPtr pRs; HRESULT hr; // 初始化COM库 CoInitialize(NULL); try { // 创建连接对象 hr = pConn.CreateInstance(__uuidof(Connection)); if (FAILED(hr)) { throw _com_error(hr); } // 打开数据库连接 pConn->Open("Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=MyDB;User Id=sa;Password=123456;", "", "", adModeUnknown); // 创建记录集对象 hr = pRs.CreateInstance(__uuidof(Recordset)); if (FAILED(hr)) { throw _com_error(hr); } // 执行SQL查询语句 pRs->Open("SELECT * FROM MyTable", pConn.GetInterfacePtr(), adOpenStatic, adLockReadOnly, adCmdText); // 遍历记录集并输出结果 while (!pRs->adoEOF) { cout << pRs->Fields->GetItem("ID")->Value.intVal << "\t" << pRs->Fields->GetItem("Name")->Value.bstrVal << endl; pRs->MoveNext(); } // 关闭记录集 if (pRs != NULL) { pRs->Close(); pRs.Release(); } // 关闭数据库连接 if (pConn != NULL) { pConn->Close(); pConn.Release(); } } catch (_com_error e) { // 输出错误信息 cout << "Error: " << e.Description() << endl; } // 释放COM库 CoUninitialize(); return 0; } ``` 主函数调用: ```c++ int main() { // 调用连接数据库函数 connectDB(); return 0; } ``` 以上是一个简单的C++ ADO连接sqlserver数据库的示例代码,具体的连接字符串和SQL查询语句需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值