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

增加了数据排序功能,优化了输出程序和主菜单

//*****************************************************
//线性表顺序存储结构实验程序
//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");
                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;
                }
            }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值