📄 new.c
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "database.h"
/////////////////////////////////////////////////////////////////////
// GetTime Function:
// Get the current time of the system
TIME GetTime()
{
struct tm t;
TIME time;
// asctime(&t);
_getsystime(&t);
time.year = t.tm_year+1900;
time.month = t.tm_mon+1;
time.day = t.tm_mday;
time.hour = t.tm_hour;
time.minute = t.tm_min;
return time;
}
void CreateDatabase(void)
{
struct database * dhead;
struct database * p1,*p2;
char copy[MAX_NAME];
int namecontrol;
// int tablenumber;
// int count;
namecontrol=0;
//////////记住这里首先要负值NULL//////////////////////////////////
if(databasehead==NULL)
{
databasehead=(struct database *)(malloc)(LEN_DATABASE);
printf("************* start ***************************\n");
printf("please input the infomation of this database \n");
printf("********** databasename ********************\n");
printf("name the database :");
scanf("%s",databasehead->cName);
getchar();
// printf("please input the table number of the database\n ");
// scanf("%d",&databasehead->iTableNum);
databasehead->tCreateTime=GetTime();
databasehead->iTableNum=0;
printf("your name:");
scanf("%s",databasehead->cCreatorName);
databasehead->tablehead=NULL;
databasehead->next=NULL;
}
else
{
dhead=databasehead;
p2=databasehead;
while(dhead!=NULL)
{
p1=dhead;
dhead=dhead->next;
}
dhead=(struct database *)(malloc)(LEN_DATABASE);
dhead->tablehead=NULL;
dhead->next=NULL;
p1->next=dhead;
printf("please input the infomation of this database \n");
printf("********** databasename ********************\n");
// printf("********** tablenumber *********************\n");
while(namecontrol==0)
{
printf("please input the name of the database \n");
scanf("%s",copy);
p2=databasehead;
while(p2!=NULL)
{
if(strcmp(p2->cName,copy)==0)
{
printf("sorry the name has been used\n");
printf("please input another name\n");
break;
}
p2=p2->next;
}
if(p2==NULL)
{
namecontrol=1;
strcpy(dhead->cName,copy);
}
getchar();
}
// printf("please input the table number of the database\n");
// scanf("%d",&dhead->iTableNum);
dhead->iTableNum=0;
getchar();
dhead->tCreateTime=GetTime();
printf(" your name:");
scanf("%s",dhead->cCreatorName);
}
New();
}
void CreateTable(void)
{
struct database *searchdatabase;
struct table * thead,*tabletail,*p;
char databasename[MAX_NAME];
char copy[MAX_NAME];
int find;
int namecontrol;
namecontrol=find=0;
printf("************* start ***************************\n");
printf("input the database name the table belongs to\n");
scanf(" %s",databasename);
searchdatabase=databasehead;
while(find==0)
{
while(searchdatabase!=NULL)
{
if(strcmp(searchdatabase->cName,databasename)==0)
{
find=1;
break;
}
else
searchdatabase=searchdatabase->next;
}
if(searchdatabase==NULL)
{
printf("sorry can not find the database \n");
printf("please input again\n");
//注意 会不会擦掉*/
scanf(" %s",databasename);
searchdatabase=databasehead;
}
}
tabletail=searchdatabase->tablehead;
p=searchdatabase->tablehead;
if(tabletail==NULL)
{
tabletail=(struct table *)(malloc)(LEN_TABLE);
tabletail->next=NULL;
tabletail->columnhead=NULL;
tabletail->iColNum=0;
printf("please input the infomation of the table \n");
printf("******************* tablename ********************\n");
// printf("******************* columnnumber ******************\n");
printf("now the table name\n");
scanf(" %s",tabletail->cName);
tabletail->tCreateTime=GetTime();
getchar();
// printf("now the columnnumber\n");
// scanf("%d",&tabletail->iColNum);
searchdatabase->tablehead=tabletail;
/////////////////增加数据库表的数量///////////
searchdatabase->iTableNum=searchdatabase->iTableNum+1;
}
else
{
while(tabletail->next!=NULL)
tabletail=tabletail->next;
thead=(struct table*)(malloc)(LEN_TABLE);
thead->next=NULL;
thead->columnhead=NULL;
thead->iColNum=0;
tabletail->next=thead;
printf("please input the infomation of the table \n");
printf("************* start ***************************\n");
printf("******************* tablename ********************\n");
while(namecontrol==0)
{
printf("now the table name\n");
scanf(" %s",copy);
p=searchdatabase->tablehead;
while(p!=NULL)
{
if(strcmp(p->cName,copy)==0)
{
printf("sorry the name has been used\n");
printf("please input another name\n");
break;
}
p=p->next;
}
if(p==NULL)
{
namecontrol=1;
strcpy(thead->cName,copy);
thead->tCreateTime=GetTime();
}
getchar();
}
searchdatabase->iTableNum=searchdatabase->iTableNum+1;
// printf("now the columnnumber\n");
// scanf("%d",&thead->iColNum);
}
New();
}
void CreateColumn(void)
{
struct database *searchdatabase;
struct table *searchtable;
struct column *columntail,*newcolumn,*p;
char databasename[MAX_NAME];
char tablename[MAX_NAME];
int find;
// int columnnumber;
char copy[MAX_NAME];
int namecontrol;
namecontrol=find=0;
///////find database//////////////////////////////////////////
printf("input the database name the column belongs to\n");
scanf("%s",databasename);
getchar();
searchdatabase=databasehead;
while(find==0)
{
while(searchdatabase!=NULL)
{
if(strcmp(searchdatabase->cName,databasename)==0)
{
find=1;
break;
}
else
searchdatabase=searchdatabase->next;
}
if(searchdatabase==NULL)
{
printf("sorry can not find the database \n ");
printf("please input again\n");
scanf("%s",databasename);
searchdatabase=databasehead;
}
}
searchtable=searchdatabase->tablehead;
find=0;
printf("input the table name the column belongs to\n ");
scanf("%s",tablename);
getchar();
while(find==0)
{
while(searchtable!=NULL)
{
if(strcmp(searchtable->cName,tablename)==0)
{
find=1;
break;
}
else
searchtable=searchtable->next;
}
if(searchtable==NULL)
{
printf("sorry can not find the table \n ");
printf("please input again\n");
scanf("%s",tablename);
searchtable=searchdatabase->tablehead;
}
}
// columnnumber=searchtable->iColNum;
// printf("%d",columnnumber);
// printf("columns exist int the table\n");
newcolumn=searchtable->columnhead;
columntail=searchtable->columnhead;
if(newcolumn==NULL)
{
newcolumn=(struct column*)malloc(LEN_COLUMN);
newcolumn->datahead=NULL;
newcolumn->iLineNum=0;
newcolumn->next=NULL;
printf("please input the infomation the column\n");
printf("**************column name*****************\n");
printf("**************datatype********************\n");
printf("Integar------------i\n");
printf("char---------------c\n");
printf("String-------------s\n");
printf("float--------------f\n");
printf("now the name of the column \n");
scanf("%s",newcolumn->cName);
getchar();
printf("now the datatype of the column \n");
scanf("%c",&newcolumn->cType);
searchtable->columnhead=newcolumn;
///////////////////所属的表增加一列/////////
searchtable->iColNum=searchtable->iColNum+1;
}
else
{
while(newcolumn!=NULL)
{
columntail=newcolumn;
newcolumn=newcolumn->next;
}
newcolumn=(struct column*)malloc(LEN_COLUMN);
newcolumn->datahead=NULL;
newcolumn->iLineNum=0;
newcolumn->next=NULL;
///////////////////the column infomation/////////////////
printf("please input the infomation the column\n");
printf("**************column name*****************\n");
printf("**************datatype********************\n");
printf("Integar------------i\n");
printf("char---------------c\n");
printf("String-------------s\n");
printf("float--------------f\n");
while(namecontrol==0)
{
printf("now the column name\n");
scanf("%s",copy);
p=searchtable->columnhead;
while(p!=NULL)
{
if(strcmp(p->cName,copy)==0)
{
printf("sorry the name has been used\n");
printf("please input another name\n");
break;
}
p=p->next;
}
if(p==NULL)
{
namecontrol=1;
strcpy(newcolumn->cName,copy);
}
getchar();
}
printf("now the datatype of the column \n");
scanf(" %c",&newcolumn->cType);
///////////////////initial value//////////////////////
newcolumn->datahead=NULL;
newcolumn->iLineNum=0;
newcolumn->next=NULL;
columntail->next=newcolumn;
searchtable->iColNum=searchtable->iColNum+1;
}
New();
}
void AddDataToTable(void)
{
struct database *searchdatabase;
struct table *searchtable;
struct column *columnhead,*p1;
// struct data* Recordset;
struct data* datahead;//,*move;
// char columnname[MAX_NAME];
char databasename[MAX_NAME];
char tablename[MAX_NAME];
int columnnumber;
int find;
int count;
char type;
find=0;
printf("******************* Add a Row ******************\n");
printf(" input the database name the column belongs to\n");
scanf("%s",databasename);
getchar();
searchdatabase=databasehead;
while(find==0)
{
while(searchdatabase!=NULL)
{
if(strcmp(searchdatabase->cName,databasename)==0)
{
find=1;
break;
}
else
searchdatabase=searchdatabase->next;
}
if(searchdatabase==NULL)
{
printf("sorry can not find the database \n ");
printf("please input again\n");
scanf("%s",databasename);
searchdatabase=databasehead;
}
}
searchtable=searchdatabase->tablehead;
find=0;
printf("input the table name the column belongs to\n ");
scanf("%s",tablename);
getchar();
while(find==0)
{
while(searchtable!=NULL)
{
if(strcmp(searchtable->cName,tablename)==0)
{
find=1;
break;
}
else
searchtable=searchtable->next;
}
if(searchtable==NULL)
{
printf("sorry can not find the table \n ");
printf("please input again\n");
scanf("%s",tablename);
searchtable=searchdatabase->tablehead;
}
}
columnnumber=searchtable->iColNum;
count=columnnumber;
columnhead=searchtable->columnhead;
p1=columnhead;
printf("%d",columnnumber);
printf(" columns exist int the table\n");
while(count>0)
{
printf("column name: ");
printf("%s ",p1->cName);
p1=p1->next;
count--;
}
/////////////////////////格式 //////////////////////////////////////
printf("\n");
count=columnnumber;
p1=columnhead;
while(count>0)
{
type=p1->cType;
printf("column type: ");
switch(type)
{
case 'i':
printf("Integar ");
break;
case 'c':
printf("Char ");
break;
case 's':
printf("String ");
break;
case 'f':
printf("Float ");
break;
}
count--;
p1=p1->next;
}
printf("\n");
datahead=InputRecord(searchtable);
/* ///////////////input data//////////////////////////
Recordset=(struct data * )malloc(LEN_DATA);
Recordset->next=NULL;
move=NULL;
count=columnnumber;
p1=columnhead;
printf("\n");
printf("you should input the data according to the type\n");
printf("now input the Recordset of a row\n");
while(count>0)
{
type=p1->cType;
switch(type)
{
case 'i':
scanf("%d",&Recordset->value);
break;
case 'c':
scanf(" %c",&Recordset->value);
break;
case 's':
scanf(" %s",Recordset->value);
break;
case 'f':
scanf("%f",&Recordset->value);
break;
}
getchar();
if(move==NULL)
{
move=Recordset;
datahead=Recordset;
}
else
{
move->next=Recordset;
move=Recordset;
}
Recordset=(struct data*)(malloc)(LEN_DATA);
count--;
p1=p1->next;
}
/**/
AddRecord(searchtable,datahead);
// Edit();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -