#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
int data[100];
int len=0;
void MyGUI()
{
printf("************** Table Operations********************\n\n");
printf(" 1. Input the datas from keyboard \n");
printf(" 2. Insert a data into the table \n");
printf(" 3. Delete a data from the table \n");
printf(" 4. Find a data in the table \n");
printf(" 5. Diaplay the table \n");
printf(" 6. Count the length of the table \n");
printf(" 7. Exit \n\n");
printf(" Please input the index accord to your choice\n");
}
void createTable()
{
printf("\nPlease input the data. You can input 100 data in the process of create table.");
printf("\nThe input format is that 12 8 3 72 9 60 45 -1. every data is isolated by ");
printf("\nspace and -1 stands for data end. input key 'Enter' to accomplishment\n");
int temp;
scanf("%d",&temp);
int i=0;
while(temp!=-1&&i<100)
{
data[i]=temp;
i++;
scanf("%d",&temp);
}
len=i;
printf("\nInput any key to continue\n");
getch();
}
void insertDataToTable()
{
if(len==100)
{
printf("\nThe tbale is full. Can't insert the data into the table.\n");
printf("\nInput any key to continue\n");
getch();
return ;
}
printf("\nInput the data location you want to insert.");
printf("\nThe location range from 1 to %d.\n",len+1);
int index;
scanf("%d",&index);
if(index>len+1&&index<1)
{
printf("\nThe location don't range from 1 to %d.\n",len+1);
printf("\nInput any key to continue\n");
getch();
return ;
}
printf("\nInput the data you want to insert.\n");
int tempdata;
scanf("%d",&tempdata);
for(int i=len;i>=index;i--)
data[i]=data[i-1];
data[i]=tempdata;
len=len+1;
printf("\nInput any key to continue\n");
getch();
}
void deleteDatafromTable()
{
if(len==0)
{
printf("\nThe tbale is empty. Can't delete the data from the table.\n");
printf("\nInput any key to continue\n");
getch();
return ;
}
printf("\nInput the data location you want to delete.");
printf("\nThe location range from 1 to %d.\n",len);
int index;
scanf("%d",&index);
if(index>len&&index<1)
{
printf("\nThe location don't range from 1 to %d.\n",len);
printf("\nInput any key to continue\n");
getch();
return ;
}
for(int i=index;i<len;i++)
data[i-1]=data[i];
len=len-1;
printf("\nInput any key to continue\n");
getch();
}
void findDataInTable()
{
if(len==0)
{
printf("\nThe tbale is empty. Can't delete the data from the table.\n");
printf("\nInput any key to continue\n");
getch();
return ;
}
printf("\nInput the data you want to find.\n");
int tempdata;
scanf("%d",&tempdata);
for(int i=0;i<len;i++)
if(data[i]==tempdata)
break;
if(i<len)
printf("\nThe data Location which you want to find is %d",i+1);
else
printf("\nThe data you want to find don't exist in the table");
printf("\nInput any key to continue\n");
getch();
}
void displayTable()
{
if(len==0)
{
printf("\nThe tbale is empty\n");
return ;
}
printf("\nThe datas of the table are:\n");
for(int i=0;i<len;i++)
printf("%d ",data[i]);
printf("\nInput any key to continue\n");
getch();
}
void getlengthOfTable()
{
printf("\nThe lenght of the table is %d",len);
printf("\nInput any key to continue\n");
getch();
}
void main()
{
int choice;
bool exit=true;
while(exit)
{
system("cls");
MyGUI();
choice=getch();
if(choice>48&&choice<56)
{
switch(choice)
{
case 49: createTable();break;
case 50: insertDataToTable();break;
case 51: deleteDatafromTable();break;
case 52: findDataInTable();break;
case 53: displayTable();break;
case 54: getlengthOfTable();break;
case 55: exit=false;break;
}
}
}
}
评论0
最新资源