www.pudn.com > database_edwardversion.rar > edit.c
#include#include #include #include "io.h" #include "database.h" /*/////////////////////////////////////////////////////////////////// the functions below are used to realize the operation of editing a table InputRecord: ShowRecord: ShowAllRecord: FindRecord: AddRecord: ModifyRecord: DeleteRecord: SortRecord: Author: Edward Lee ///////////////////////////////////////////////////////////////////**/ /*--------------------------------------- Input a record ---------------------------------------*/ struct data* InputRecord(struct table *table) { int i; struct data *precord,*pdat; struct column *pcol=table->columnhead; // malloc the record pdat = (struct data *)malloc(LEN_DATA); precord=pdat; for(i=1;i iColNum;i++) { pdat->next=(struct data *)malloc(LEN_DATA); pdat=pdat->next; } pdat->next=NULL; printf("Now input the new data or the row\n"); printf("You should input the data according to the type\n"); // input the record pdat=precord; while(pcol!=NULL) { switch(pcol->cType) { case 'i': scanf("%d",&pdat->value.iValue); break; case 'c': scanf(" %c",&pdat->value.cValue); break; case 's': scanf(" %s",pdat->value.strValue); break; case 'f': scanf("%f",&pdat->value.fValue); break; } pcol=pcol->next; pdat=pdat->next; } return precord; } /*--------------------------------------- Show a record on screen ---------------------------------------*/ void ShowRecord(struct table *table,int index) { int i; struct data *p; struct column *pcol; pcol = table->columnhead; if(index<=0) { printf("Error! Wrong index\n"); return; } printf("%20d",index); while(pcol!=NULL) { p = pcol->datahead; for(i=0;i next; switch(pcol->cType) { case 'c': printf("\t%c",p->value.cValue); break; case 's': printf("\t%20s",p->value.strValue); break; case 'i': printf("\t%d",p->value.iValue); break; case 'f': printf("\t%f",p->value.fValue); break; default : printf("Error : data type not definite\n"); return; } pcol = pcol->next; } printf("\n"); } /*--------------------------------------- Show all the record of a table on screen ---------------------------------------*/ void ShowAllRecord(struct table *table) { int i; struct column *pcol; pcol=table->columnhead; if(pcol==NULL) { printf("No column found!\n"); return; } printf("\t\tINDEX\t"); while(pcol!=NULL) { printf("%s\t",pcol->cName); pcol=pcol->next; } printf("\n"); for(i=1;i<=table->columnhead->iLineNum;i++) ShowRecord(table,i); } /*--------------------------------------- Add a record to an existed table ---------------------------------------*/ // // Notice : // The record is called as a chained list // Providing it has been input by the user void AddRecord(struct table *table,struct data *record) { struct data *prec,*p,*ptemp; struct column *pcol; pcol = table->columnhead; prec = record; // If columns are empty if(pcol->datahead == NULL) { while(pcol!=NULL) { pcol->datahead = prec; // Add it pcol = pcol->next; // Next column ptemp = prec; prec = prec->next; // Next data ptemp->next = NULL; } } else { while(pcol!=NULL) { p=pcol->datahead; while(p->next!=NULL) // Move to the last p=p->next; p->next = prec; // Add it to the last pcol = pcol->next; // Next column ptemp = prec; prec = prec->next; // Next data ptemp->next = NULL; } } // Update the number of records pcol=table->columnhead; while(pcol!=NULL) { pcol->iLineNum++; pcol = pcol->next; } } /*--------------------------------------- Delete a record of an existed table ---------------------------------------*/ void DeleteRecord(struct table *table,struct column *column,struct data *data) { int index=0; int i; struct data *p; struct column *pcol; pcol = table->columnhead; // pdat = column->datahead; // Nothing to delete or delete nothing if(data==NULL||column->datahead==NULL) { printf("The table is empty or the pointer to the data is NULL\n"); return; } // Mark the position of the record index=FindRecord(table,column,data); // Delete the record if(index==0) // If the first record : index = 0 while(pcol!=NULL) { pcol->datahead = pcol->datahead->next; pcol = pcol->next; } else { while(pcol!=NULL) { p = pcol->datahead; for(i=0;i next; // if(data->next==NULL) // If the last record : index = [1,iLine-1] // p->next = NULL; // else // If the middle record : index = iLine p->next = (p->next)->next; pcol = pcol->next; } } // Update the number of records pcol=table->columnhead; while(pcol!=NULL) { pcol->iLineNum--; pcol = pcol->next; } } /*--------------------------------------- Find a record of an existed table ---------------------------------------*/ int FindRecord(struct table *table,struct column *column,struct data *data) { int index=0; struct data *pdat; pdat = column->datahead; // Nothing to find or find nothing if(data==NULL||pdat==NULL) { printf("The table is empty or the pointer to the data is NULL\n"); return -1; } // Mark the position of the record switch(column->cType) { case 'i': while(pdat->value.iValue!=data->value.iValue) { pdat = pdat->next; index++; // Record[1,MAX] NO.n(1<=n<=MAX) : index = n-1 if(pdat==NULL) // No such a record { printf("No such a record in the table\n"); return -1; } } break; case 'f': while(pdat->value.fValue!=data->value.fValue) { pdat = pdat->next; index++; // Record[1,MAX] NO.n(1<=n<=MAX) : index = n-1 if(pdat==NULL) // No such a record { printf("No such a record in the table\n"); return -1; } } break; case 'c': while(pdat->value.cValue!=data->value.cValue) { pdat = pdat->next; index++; // Record[1,MAX] NO.n(1<=n<=MAX) : index = n-1 if(pdat==NULL) // No such a record { printf("No such a record in the table\n"); return -1; } } break; case 's': while( strcmp(pdat->value.strValue,data->value.strValue)!=0 ) { pdat = pdat->next; index++; // Record[1,MAX] NO.n(1<=n<=MAX) : index = n-1 if(pdat==NULL) // No such a record { printf("No such a record in the table\n"); return -1; } } } // Show the record on screen // ShowRecord(table,index+1);///////// ////////// //////// ////////// return index; // return the index to the record } /*--------------------------------------- Modify a record of an existed table ---------------------------------------*/ void ModifyRecord(struct table *table,struct column *column,struct data *data) { // delete the record, add a new record DeleteRecord(table,column,data); // AddDataToTable(); AddRecord(table, InputRecord(table)); } ///////////////////////////////////////////////////////////////////// // SortRecord Functions : // Sort records in a table // Notice : // as for SortRecord(), if ascent=1, order in ascend way, // if 0, in descend order void CopyIndexToIndex(struct column *column,struct dataindex *pdst, struct dataindex *psrc) {// copy data in dsrc to the one in dst switch(column->cType) { case 'c': pdst->value.cValue=psrc->value.cValue; break; case 'i': pdst->value.iValue=psrc->value.iValue; break; case 'f': pdst->value.fValue=psrc->value.fValue; break; case 's': strcpy(pdst->value.strValue,psrc->value.strValue); break; default: printf("Error : data type not definite\n"); exit(1); } pdst->index=psrc->index; } void CopyIndexToData(struct column *column,struct data *pdst, struct dataindex *psrc) {// copy data in dsrc to the one in dst switch(column->cType) { case 'c': pdst->value.cValue=psrc->value.cValue; break; case 'i': pdst->value.iValue=psrc->value.iValue; break; case 'f': pdst->value.fValue=psrc->value.fValue; break; case 's': strcpy(pdst->value.strValue,psrc->value.strValue); break; default: printf("Error : data type not definite\n"); exit(1); } } void CopyDataToIndex(struct column *column,struct dataindex *pdst, struct data *psrc) {// copy data in dsrc to the one in dst switch(column->cType) { case 'c': pdst->value.cValue=psrc->value.cValue; break; case 'i': pdst->value.iValue=psrc->value.iValue; break; case 'f': pdst->value.fValue=psrc->value.fValue; break; case 's': strcpy(pdst->value.strValue,psrc->value.strValue); break; default: printf("Error : data type not definite\n"); exit(1); } } int CompareData(struct column *column,struct dataindex *p1, struct dataindex *p2) { switch(column->cType) { case 'c': if(p1->value.cValue==p2->value.cValue)return 0; else if(p1->value.cValue>p2->value.cValue)return 1; else return -1; break; case 'i': if(p1->value.iValue==p2->value.iValue)return 0; else if(p1->value.iValue>p2->value.iValue)return 1; else return -1; break; case 'f': if(p1->value.fValue==p2->value.fValue)return 0; else if(p1->value.fValue>p2->value.fValue)return 1; else return -1; break; case 's': if( strcmp(p1->value.strValue,p2->value.strValue)==0 )return 0; else if( strcmp(p1->value.strValue,p2->value.strValue)>0 )return 1; else return -1; break; default: printf("Error : data type not definite\n"); exit(1); } } void ExchangeIndex(struct column *column,struct dataindex *p1, struct dataindex *p2) { struct dataindex *ptemp; ptemp=(struct dataindex *)malloc(LEN_DATA); ptemp->next=NULL; CopyIndexToIndex(column,ptemp,p1); CopyIndexToIndex(column,p1,p2); CopyIndexToIndex(column,p2,ptemp); } void SortColumn(struct column *column,struct dataindex *indexhead,int ascend) { struct dataindex *pin1,*pin2; for(pin1=indexhead;pin1!=NULL;pin1=pin1->next) for(pin2=pin1->next;pin2!=NULL;pin2=pin2->next) { if(ascend==1) { if(CompareData(column,pin1,pin2)>0) ExchangeIndex(column,pin1,pin2); } else { if(CompareData(column,pin1,pin2)<0) ExchangeIndex(column,pin1,pin2); } } } void ExportData(struct column *column,struct dataindex *indexhead) { struct data *pda=column->datahead; struct dataindex *pin=indexhead; // export data to the dataindex linklist for(;pda!=NULL;pin=pin->next,pda=pda->next) { CopyDataToIndex(column,pin,pda); } } void ImportData(struct column *column,struct dataindex *indexhead) { int i; struct data *pda=column->datahead; struct dataindex *pin=indexhead,*p; // copy from beginning to the end for(;pin!=NULL;pin=pin->next,pda=pda->next) { for(p=indexhead,i=1;i index;p=p->next,i++); CopyIndexToData(column,pda,p); } } void SortRecord(struct table *table,struct column *column,int ascend) { int i; struct column *pcol = table->columnhead; struct data *pda= column->datahead; struct dataindex *indexhead,*pin; // create the linklist and initialize the dataindex linklist pin = (struct dataindex *)malloc(LEN_DATA+4); indexhead=pin; indexhead->index=1; for(i=0;i iLineNum-1;i++) { pin->next = (struct dataindex *)malloc(LEN_DATA+4); pin->next->index=i+2; pin = pin->next; // Next data } pin->next=NULL; ExportData(column,indexhead); SortColumn(column,indexhead,ascend); // sort each column while(pcol!=NULL) { // if(pcol!=column) { // copy column data to index linklist ExportData(pcol,indexhead); // copy data in index linklist to column ImportData(pcol,indexhead); } pcol = pcol->next; // move to next column } } ///////////////////////////////////////////////////////////////////// // SortRecord Interface Function // Ascend or descend void SortTable(void) { // add code here char databasename[MAX_NAME]; char tablename[MAX_NAME]; char columnname[MAX_NAME]; int ascend=0; struct database *pdb; struct table *ptab; struct column *pcol; ShowDatabaseInfo(); printf("Database Name:"); scanf(" %s",databasename); pdb=GetDatabase(databasename); if(pdb==NULL)return; ShowTableInfo(pdb); printf("Table Name:"); scanf(" %s",tablename); ptab=GetTable(pdb,tablename); if(ptab==NULL)return; ShowColumnInfo(pdb,ptab); printf("According to which Column:"); scanf(" %s",columnname); pcol=GetColumn(ptab,columnname); if(pcol==NULL)return; printf("Ascend[1] of descend[0]:[ ]\b\b"); scanf(" %d",&ascend); // default is 0 if(ascend!=1&&ascend!=0)ascend=0; SortRecord(databasehead->tablehead,databasehead->tablehead->columnhead,ascend); printf("Order Sucess!\n"); }