如何用C语言做到增删改查 内详
很早前帮朋友写的,跟你的需求很像,给你用吧。
创新互联从2013年创立,是专业互联网技术服务公司,拥有项目成都网站设计、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元弥勒做网站,已为上家服务,为弥勒各地企业和个人服务,联系电话:18982081108
你可改下main函数,使它更贴近你的需求。
如果你不会改,再帮你改吧。
-------------------------------------------
#include time.h
#include stdio.h
#define NULL -2
#define ERROR -1
#define OK 1
#define TRUE 2
#define FALSE 3
#define Boolen int
#define Status int
#define LIST_INIT_SIZE 3
#define LIST_INCREMENT 2
#define NAME_LEN 13
#define DES_LEN 30
char ErrDescription[DES_LEN];
typedef struct{
int NO;
char Name[NAME_LEN];
enum{male,female} Sex;
int Age;
char Tel[15];
char Inserttime[64];
}ElemType,*ElemPointer;
typedef struct{
ElemPointer base; //基址
int length; //表长
int listsize; //内存占用
int elemcount; //记录数
}SqList,*SqPointer;
int ErrorEXP(int i)
{
switch(i)
{ case 1: strcpy(ErrDescription,"InitList::(ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType)) 空间申请失败");break;
case 2: strcpy(ErrDescription,"IncreaseList::(ElemType *)realloc(L-base,(L-length + LIST_INCREMENT) * sizeof(ElemType)) 空间申请失败");break;
case 3: strcpy(ErrDescription,"if(!L-base) return Error; SqList不存在");break;
case 4: strcpy(ErrDescription,"GetElem:: i 越界");break;
case 5: strcpy(ErrDescription,"ListInsert:: i 越界");break;
case 6: strcpy(ErrDescription,"ListInsert:: CALL IncreaseList(L)==ERROR return Error 邻接空间申请失败,由ListInsert返回");break;
case 7: strcpy(ErrDescription,"ListDelete:: i 越界");break;
case 8: strcpy(ErrDescription,"KeyInList:: i 越界");break;
case 9: strcpy(ErrDescription,"KeyInList:: CALL ListInsert(L,i,temp)==ERROR return Error 邻接空间申请失败,由KeyInList返回");break;
case 10: strcpy(ErrDescription,"ScanfList:: CALL KeyInList(L,i++)==ERROR return Error");break;
}
puts("!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!\n");
puts(ErrDescription);
puts("\n!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!\n");
return ERROR;
}
Status InitList(SqPointer L)
{
L-base = 0; //不可不要!!! 去掉后即使(ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType))失败,系统也会认为正常
L-base = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(!L-base) return ErrorEXP(1); //空间申请失败,返回
L-length = LIST_INIT_SIZE;
L-listsize = L-length * sizeof(ElemType);
L-elemcount = 0;
return OK;
}
Status IncreaseList(SqPointer L)
{
ElemPointer newbase;
newbase = (ElemType *)realloc(L-base,(L-length + LIST_INCREMENT) * sizeof(ElemType));
if(!newbase) return ErrorEXP(2);
L-base = newbase;
L-length += LIST_INCREMENT;
L-listsize = L-length * sizeof(ElemType);
return OK;
}
Status DestroyList(SqPointer L)
{
if(!L-base) return ErrorEXP(3); //L不存在,返回
free(L-base);
L-length = NULL;
L-listsize = NULL;
L-elemcount = NULL;
return OK;
}
Status ClearList(SqPointer L)
{
if(!L-base) return ErrorEXP(3); //L不存在,返回
L-elemcount = 0;
return OK;
}
Boolen ListEmpty(SqPointer L)
{
if(!L-base) return ErrorEXP(3); //L不存在,返回
if(L-elemcount == 0)
return TRUE;
else
return FALSE;
}
int ListElemCount(SqPointer L)
{
if(!L-base) return ErrorEXP(3); //L不存在,返回
return L-elemcount;
}
Status GetElem(SqPointer L,int i,ElemType *ret) //调用此函数需将ret指向main函数域某一ElemType变量
{
if(!L-base) return ErrorEXP(3); //L不存在,返回
if(i L-elemcount) return ErrorEXP(4); //i越界,返回
*ret = L-base[i-1]; //i 从1开始 此种方法在main中改变*ret会直接更改链表中数据
return OK;
}
//重大发现 指针型 temp-base 普通型L.base
int LocateElem(SqPointer L,char Locatename[]) //返回的i从1开始
{
int i=0;
ElemType *temp;
if(!L-base) return ErrorEXP(3); //L不存在,返回
while(iL-elemcount)
{
temp=(L-base[i]); //改为temp=L-base[i++];并去除下面的i++; ??
if(strcmp(temp-Name,Locatename) == 0) return i+1; //不能用temp-Name==locatename来试图比较字符串
i++;
}
return 0;
}
Status ListInsert(SqPointer L,int i,ElemType newelem) //插入位置1=i=elemcount+1
{
ElemPointer newbase;
ElemType *temp,*flag;
if(!L-base) return ErrorEXP(3); //L不存在,返回
if(i1 || iL-elemcount + 1) return ErrorEXP(5);
if(L-elemcount == L-length)
if(IncreaseList(L)==ERROR) return ErrorEXP(6);
flag=(L-base[i-1]); //插入位置
for(temp=(L-base[L-elemcount-1]);temp=flag;temp--)
*(temp+1)=*temp;
*flag=newelem;
L-elemcount++;
return OK;
}
Status ListDelete(SqPointer L,int i,ElemType *ret) //调用此函数需将ret指向main函数域某一ElemType变量
{
ElemType *temp;
if(!L-base) return ErrorEXP(3); //L不存在,返回
if(i1 || iL-elemcount) return ErrorEXP(7);
*ret=L-base[i-1]; //删除位置,这里先返回该值
for(temp=(L-base[i]);temp=(L-base[L-elemcount-1]);temp++)
*(temp-1)=*temp;
L-elemcount--;
return OK;
}
Status KeyInList(SqPointer L,int i)
{
ElemType temp;
time_t t;
char tmp[64];
char S;
if(!L-base) return ErrorEXP(3); //L不存在,返回
if(i1 || iL-elemcount + 1) return ErrorEXP(8);
printf("正在输入第%d个元素的值:",i);
printf("\n编号:(int)\n");
scanf("%d",temp.NO);
printf("\n姓名:(char *)\n");
scanf("%s",temp.Name);
printf("\n性别:(m or f)\n");
do{
S=getch();
if(S=='m')
temp.Sex=male;
else if(S=='f')
temp.Sex=female;
else
puts("Key in 'm' or 'f'.\n");
}while(S!='m' S!='f');
putchar(S);
printf("\n年龄:(int)\n");
scanf("%d",temp.Age);
printf("\n电话:(char *)\n");
scanf("%s",temp.Tel);
printf("\n记录时间:\n");
t=time(0);
strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A 本年第%j天 %z",localtime(t));
puts(tmp);
strcpy(temp.Inserttime,tmp);
if(ListInsert(L,i,temp)==OK)
return OK;
else
return ErrorEXP(9);
}
ElemType ScanfElem()
{
ElemType temp;
time_t t;
char tmp[64];
char S;
printf("正在录入元素:");
printf("\n编号:(int)\n");
scanf("%d",temp.NO);
printf("\n姓名:(char *)\n");
scanf("%s",temp.Name);
printf("\n性别:(m or f)\n");
do{
S=getch();
if(S=='m')
temp.Sex=male;
else if(S=='f')
temp.Sex=female;
else
puts("Key in 'm' or 'f'.\n");
}while(S!='m' S!='f');
putchar(S);
printf("\n年龄:(int)\n");
scanf("%d",temp.Age);
printf("\n电话:(char *)\n");
scanf("%s",temp.Tel);
printf("\n记录时间:\n");
t=time(0);
strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A 本年第%j天 %z",localtime(t));
puts(tmp);
strcpy(temp.Inserttime,tmp);
return temp;
}
Status ScanfList(SqPointer L,int i)
{
char p='c';
while(putchar('\n'),p=='c'||p=='C')
{ p='\0';
if(KeyInList(L,i++)==ERROR) return ErrorEXP(10);
printf("\nPress ESC key to exit or 'C' to continue...");
while(p!='c' p!='C' (int)p!=27)
p=getch();
}
return OK;
}
Status PrintListProperty(SqPointer L)
{
puts("SqList L Property:");
if(!L-base)
{ puts("链表不存在!");
return OK;}
else
puts("链表已初始化...\n");
printf("%d/%d BASE=%d,MemoryStatus=%d\n",L-elemcount,L-length,L-base,L-listsize);
return OK;
}
Status PrintOnScreen(SqPointer L)
{
int i;
char Stmp[7],t;
if(!L-base) return ErrorEXP(3); //L不存在,返回
puts("Push 'C' shell CLS or other key to skip.");
t=getch();
if(t=='c' || t=='C')
system("cls");
puts("数据表打印:");
for(i=0;i=L-elemcount-1;i++)
{ printf("\nElem %d st:\n",i+1);
if(L-base[i].Sex == male)
strcpy(Stmp,"male");
else if(L-base[i].Sex == female)
strcpy(Stmp,"female");
else
strcpy(Stmp,"Unknow");
printf("NO:%d\tName:%s\t\tSex:%s\tAge:%d\n\tTel:%s\n\tInsertTime:%s\n",L-base[i].NO,L-base[i].Name,Stmp,L-base[i].Age,L-base[i].Tel,L-base[i].Inserttime);
}
return OK;
}
Status PrintElem(ElemPointer elem)
{
char Stmp[7];
printf("\nPrintElem:\n");
if(elem-Sex == male)
strcpy(Stmp,"male");
else if(elem-Sex == female)
strcpy(Stmp,"female");
else
strcpy(Stmp,"Unknow");
printf("NO:%d\tName:%s\t\tSex:%s\tAge:%d\n\tTel:%s\n\tInsertTime:%s\n",elem-NO,elem-Name,Stmp,elem-Age,elem-Tel,elem-Inserttime);
return OK;
}
void main() //把以上所有函数都串了起来
{
SqList TheList;
SqPointer ListP;
ElemType mylistelem,*elemtemp;
ElemPointer mylist;
int i;
char nameT[20];
elemtemp=mylistelem; //*ret
ListP=TheList;
if(InitList(ListP)==OK) puts("InitList(TheList)==OK");
PrintListProperty(ListP);
if(ListEmpty(ListP)==TRUE) puts("ListEmpty==True");
else puts("ListEmpty==False");
ScanfList(ListP,1);
PrintListProperty(ListP);
PrintOnScreen(ListP);
printf("ListElemCount return %d.",ListElemCount(ListP));
puts("\nGetElem index? ");
scanf("%d",i);
if(GetElem(ListP,i,elemtemp)==OK) PrintElem(elemtemp);
puts("\nLocateElem name? ");
scanf("%s",nameT);
printf("LocateElem return %d.",LocateElem(ListP,nameT));
puts("\nListDelete index? ");
scanf("%d",i);
if(ListDelete(ListP,i,elemtemp)==OK) PrintElem(elemtemp);
puts("\nListInsert index? ");
scanf("%d",i);
puts("\nListInsert NEWELEM? ");
ListInsert(ListP,i,ScanfElem());
PrintListProperty(ListP);
PrintOnScreen(ListP);
if(ClearList(ListP)==OK) puts("ClearList==OK");
if(ListEmpty(ListP)==TRUE) puts("ListEmpty==True");
if(DestroyList(ListP)==OK) puts("DestroyList==OK");
getch();
}
/* 函数列表
类型 名称 参数 说明
int ErrorEXP (int i) 错误描述符
Status InitList (SqPointer L) 初始化SqPointer L... 通过L返回base
Status IncreaseList (SqPointer L) L当前满时,继续申请空间
Status DestroyList (SqPointer L) 销毁L
Status ClearList (SqPointer L) 把L置为空表
Boolen ListEmpty (SqPointer L) 判断L是否为空表,是则返回TRUE
int ListElemCount (SqPointer L) 返回当前L中记录的元素个数
Status GetElem (SqPointer L,int i,ElemType *ret) 通过*ret返回i号元素
int LocateElem (SqPointer L,char Locatename[]) 顺序查找表,根据name字段,返回首个匹配元素的i,无则返回0
Status ListInsert (SqPointer L,int i,ElemType newelem) 在L中的i号位置插入newelem元素
Status ListDelete (SqPointer L,int i,ElemType *ret) 删除L中第i号元素,并用*ret返回该元素
Status KeyInList (SqPointer L,int i) 从键盘输入单个元素并插入到i号位置
ElemType ScanfElem () 从键盘输入单个元素返回一个ElemType类型的节点
Status ScanfList (SqPointer L,int i) 从i号开始递增顺序录入元素到L,直到按'ESC'
Status PrintListProperty(SqPointer L) 打印L的属性,打印格式为(已用空间/已申请空间 基址 内存占用)
Status PrintOnScreen (SqPointer L) 打印整张L表到屏幕
Status PrintElem (ElemPointer elem) 打印单个ElemType类型的元素
时间仓促,所以乱了些,书上2章开头 动态线性的顺序表 的基本操作几乎都写了
不知你说的是不是这个,mian函数比较乱,只是把所有的基本操作都串了起来,你
可以根据情况改改主函数的调用过程,就会比较清楚是怎么实现的了。你可以按F10
进行单部跟踪,F11可以进入调用过程,一步一步跟着程序走一遍就好了。
关于动态链表的我之前写过一个,也好象给你看过,这里再附上一起发过去。文件LinkList.c
只实现了构造链表,并打印出来的功能。
*/
C语言的题用函数实现数组数据的增加,删除,修改,查找和排序
#includestdio.h
#define maxn 1024
int arr[maxn], n;
void add(int value)
{
arr[n - 1] = value;
return;
}
void del(int index)
{
int i, j;
for(i = index; i n - 1; i++)
arr[i] = arr[i + 1];
n--;
return;
}
void update(int index, int value)
{
arr[index] = value;
return;
}
void search(int index)
{
printf("%d\n", arr[index]);
return;
}
void BubbleSort(int count)
{
int i, j;
for(i = 0; i count; i++)
{
for(j = count - 1; j i; j--)
{
if(arr[j] arr[j-1])
{
int temp = arr[j-1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
return;
}
int main(void)
{
n = 0;
return 0;
}
谁知道用c语言向数据库做增删改查吗?
我曾经写过C语言的数据库系统..
给你部分代码(一个销售函数)以作参考...
void
book_out()
//销售函数
{
char
temp;
EXEC
SQL
BEGIN
DECLARE
SECTION;
/*主变量定义开始.*/
int
Hout_shuliang;
int
Hshuliang;///////////
char
Hbook_id[11];
EXEC
SQL
END
DECLARE
SECTION;
/*主变量定义结束*/
lab3:
printf("请输入图书编号:");
scanf("%s",Hbook_id);
printf("请输入卖出本数:");
scanf("%d",Hout_shuliang);
//先将库存量取出到主变量
EXEC
SQL
select
book_shuliang
into
:Hshuliang
from
book_kucun
where
book_id=:Hbook_id;
if(HshuliangHout_shuliang)
//假如库存不足,销售不成功.
{
printf("输入有误.没那么多库存,请重新输入.\n");
goto
lab3;
}
//将销售记录插入到book_out(销售表)数据表.
EXEC
SQL
insert
into
book_out
values(:Hbook_id,:Hout_shuliang,GETDATE());
EXEC
SQL
COMMIT
TRANSACTION;
/*事务提交*/
printf("售出成功,输入Y继续输入其他要售出的书.其他键返回主菜单:");
getchar();//////////////////////////
scanf("%c",temp);
if(temp=='y'||temp=='Y')
goto
lab3;
}
分享标题:c语言函数增删 c语言增加函数怎么写
当前路径:http://scgulin.cn/article/doghhog.html