您的位置:首页 > 其它

线性表顺序存储结构实验程序 Version 2.0

2015-10-10 18:57 543 查看
增加了数据排序功能,优化了输出程序和主菜单

//*****************************************************
//线性表顺序存储结构实验程序
//2015.10.10 Version 2.0
//*****************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
#define FILE_PATH "d:\\data.txt"
typedef struct
{
int num;
char name[10];
char pos[20];
}DataType;

DataType goods[MAXSIZE];

void Inition();//初始化顺序表,置空
int Readfile();//从文件读取数据(可选)
int Inputdata();//键盘输入数据(可选)
int Length();//求表长
void Show(int length);//显示表
void Out(DataType data);//输出一个元素
void Get(int n,DataType *data);//取出第n个元素
void Insert(int n);//在第n个位置插入一个元素
void Delete(int n);//删除第n个元素
void Sort(int order);//排序,升序或降序

int main()
{
int ch = 0,len = 0;//ch:选择项,len:表长
printf_s("\tSequence List Test Program\n");
printf_s("=======================================================\n");
Inition();
printf_s("How do you Creat a list? \n");
printf_s("if you will input data from keyboard press 1 \n");
printf_s(" and read from file press 2:\n");
do{
scanf_s("%d",&ch);
if (ch == 1){len= Inputdata();break;}
else if (ch == 2){len = Readfile();break;}
else {printf_s("No such choice!choice again...\n");}
}while (ch !=1 && ch !=2);
len = Length();
Show(len);
ch = -1;
do{
printf_s("=======================================================\n");
printf_s("Select what do you do?\n");
printf_s("1 Set the sequence list empty\n");
printf_s("2 Judge if the sequence list is empty \n");
printf_s("3 Show a data of the sequence list \n");
printf_s("4 Insert a data from the sequence list \n");
printf_s("5 Delete a data from the sequence list \n");
printf_s("6 Sort the sequence list \n");
printf_s("0 Exit\n");
scanf_s("%d",&ch);
switch (ch)
{
case 1:
{
Inition();
break;
}
case 2:
{
if (Length()>0)
printf("the sequence list is not empty,it has %d data.\n",Length());
else
printf("the sequence list is empty.\n");
break;
}
case 3:
{
int n = 0;
DataType  getdata ={0,"",""};
printf("Which data would you to show? No. is ");
scanf_s("%d",&n);
Get(n,&getdata);
Out(getdata);
break;
}
case 4:
{
int n = 0;
printf("Where would you to insert a data? No. is ");
scanf_s("%d",&n);
Insert(n);
len = Length();
Show(len);
break;
}
case 5:
{
int n = 0;
printf("Which data would you to delete? No. is ");
scanf_s("%d",&n);
Delete(n);
len = Length();
Show(len);
break;
}
case 6:
{
int order = 0;
printf_s("How do you order the list? \n");
printf_s("if you will order it by ascending press 1 \n");
printf_s(" and order by descending press 2:\n");
do{
scanf_s("%d",&order);
if (order == 1 || order == 2) break;
else {printf_s("No such choice!choice again...\n");}
}while (order !=1 && order !=2);
Sort(order);
len = Length();
Show(len);
}

default:
{
printf("No such choice!choice again...\n");

4000
break;
}
}

}while(ch != 0);
return 0;
}

void Inition()
{
int i = 0;
for(i = 0;i<MAXSIZE;i++)
{
goods[i].num = 0;
strcpy_s(goods[i].name,"");
strcpy_s(goods[i].pos,"");
}

printf_s("Inition sucessful!The sequence list now is empty.\n");
printf_s("Press any key to continue.\n");
getchar();

}

int Length()
{
int i, len;
for (i = 0;i<MAXSIZE;i++)
{
if (goods[i].name[0] == '\0') break;
}
len = i;
return len;
}

void Get(int n,DataType *data)
{
if(n>Length()||n<=0) printf("No such data!\n");
else *data =  goods[n-1] ;
}

void Out(DataType data)
{
printf_s("%d\t\t",data.num);
printf_s("%s\t\t\t\t",data.name );
printf_s("%s\n",data.pos );
}

void Show(int length)
{
int i = 0;
printf_s("num\t\tname\t\t\t\tposition\n" );
printf_s("============================================================\n" );
for(i = 0;i<length;i++)
{
Out(goods[i]);
}
printf_s("OK,press any key to continue!\n");
}

int Inputdata()
{
int i = 0;
int _num ;
char  _name[10]  , _pos[20]  ;
for(i = 0;i<MAXSIZE;i++)
{
printf_s("Plesase input the num of seqlist No. %d\n",i+1);
scanf_s("%d",&_num,2);
if (_num == -1) break;
printf_s("Plesase input the name of seqlist No. %d\n",i+1);
scanf_s("%s",_name,9);
printf_s("Plesase input the position of seqlist No. %d\n",i+1);
scanf_s("%s",_pos,19);

goods[i].num = _num;
strcpy_s(goods[i].name,_name);
strcpy_s(goods[i].pos,_pos);
}
printf_s("OK,press any key to continue!\n");
getchar();
return i;
}

int Readfile()
{
int i = 0;
int _num ;
char  _name[10]  , _pos[20]  ;
FILE *fp;
if (fopen_s(&fp,FILE_PATH,"r"))
{
printf_s("File not exist or open error!\n");
return 0;
}

for(i = 0;i<MAXSIZE;i++)
{
fscanf_s(fp,"%d",&_num,2);
if (_num == -1) break;
fscanf_s(fp,"%s",_name,9);
fscanf_s(fp,"%s",_pos,19);

goods[i].num = _num;
strcpy_s(goods[i].name,_name);
strcpy_s(goods[i].pos,_pos);
}
fclose(fp);
printf_s("Read file sucessful!Press any key to continue\n");
getchar();
return i;

}

void Insert(int n)
{
int i,len=Length();
int _num ;
char  _name[10]  , _pos[20]  ;
if(n>len||n<=0) printf("No such data!\n");
else
{
printf_s("Plesase input the num of data:\n");
scanf_s("%d",&_num,2);

printf_s("Plesase input the name of of data:\n");
scanf_s("%s",_name,9);
printf_s("Plesase input the position of of data:\n");
scanf_s("%s",_pos,19);

for (i=len;i>=n;i--)
{
printf("%d",i);
goods[i].num=goods[i-1].num ;
strcpy_s(goods[i].name,goods[i-1].name);
strcpy_s(goods[i].pos,goods[i-1].pos);
}
goods[i].num = _num;
strcpy_s(goods[i].name,_name);
strcpy_s(goods[i].pos,_pos);

}
}

void Delete(int n)
{
int i,len=Length();
if(n>len||n<=0) printf("No such data!\n");
else
{
for (i=n-1;i<len-1;i++)
{
goods[i].num=goods[i+1].num ;
strcpy_s(goods[i].name,goods[i+1].name);
strcpy_s(goods[i].pos,goods[i+1].pos);
}
goods[i].num=0 ;
strcpy_s(goods[i].name,"");
strcpy_s(goods[i].pos,"");

}
}
void Sort(int order)
{
int i,j,len=Length();
DataType tmpdata={0,"",""};
if (order == 1)
{
for (i=0;i<len-1;i++)
for(j=i+1;j<len;j++)
{
if (goods[i].num > goods[j].num)
{
printf("i=%d,j=%d\n",i,j);
tmpdata = goods[i];
goods[i] = goods[j];
goods[j] = tmpdata;
}
}
}
if (order == 2)
{
for (i=0;i<len-1;i++)
for(j=i+1;j<len;j++)
{
if (goods[i].num < goods[j].num)
{
printf("i=%d,j=%d\n",i,j);
tmpdata = goods[i];
goods[i] = goods[j];
goods[j] = tmpdata;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: