马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
就是第一部插入元素的时候就出问题了,只能插两个元素,我想要插三个。然后他不让输了以后也不会进行接下来的操作,就直接跳出这个程序了,这要怎么解决啊
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10000
#define N 3
typedef struct
{
char No[40]; //书的编号
char name[50]; //书的名字
float price; //书的价格
} Book;
typedef struct
{
Book *elem;
int lenght;
} SqList;
void InitList(SqList &L);
void GetElem(SqList L, int i, Book &e);
int LocateElem(SqList L, Book e);
void ListInsert(SqList &L, int i, Book e);
void ListDelete(SqList &L, int i);
int main()
{
SqList p1;
Book e[N];
Book e1, e2;
int p;
InitList(p1);
printf("向顺序表加入%d个元素:\n书的编号 书的价格 书的名字\n", N);
//添加顺序表的元素
for (int i = 0; i < N; i++)
{
scanf("%s%f%s", e[i].No, &e[i].price, e[i].name);
ListInsert(p1, i + 1, e[i]);
}
//输出一次;
for (int i = 0; i < N; i++)
printf("%s :%s\n价格:%.2f\n", p1.elem[i].No, p1.elem[i].name, p1.elem[i].price);
printf("要查找第几书本?\n");
scanf("%d", &p);
GetElem(p1, p, e1);
printf("%s :%s\n价格:%.2f\n", e1.No, e1.name, e1.price);
scanf("%s %s %f", e2.No, e2.name, &e2.price);
printf("这本书是第%d本书\n", LocateElem(p1, e2));
scanf("%d", &p);
ListDelete(p1, p);
for (int i = 0; i < N - 1; i++)
printf("%s :%s\n价格:%.2f\n", p1.elem[i].No, p1.elem[i].name, p1.elem[i].price);
return 0;
}
//顺序表的初始化,构造一个空的顺序表
void InitList(SqList &L)
{
//为顺序表分配一个大小为MAXSIZE的数组空间
L.elem = new Book[MAXSIZE];
if (!L.elem)
exit(0); //存储空间分配失败退出
L.lenght = 0; //空表长度为0
}
//顺序列表的取值,通过指定位置序号i
void GetElem(SqList L, int i, Book &e)
{
//判断定位的i是否为合理的。不合理就输出ERROR
//合理就输出通过e返回的第i个数据的传值
if (i < 1 || i > L.lenght)
{
printf("ERROR");
}
else
{
e = L.elem[i - 1];
}
}
//顺序表的查找
int LocateElem(SqList L, Book e)
{
for (int i = 0; i < L.lenght; i++)
{
if (L.elem[i].name == e.name && L.elem[i].No == e.No && L.elem[i].price == e.price)
return i + 1;
}
return 0;
}
//顺序表的插入
void ListInsert(SqList &L, int i, Book e)
{
if (i < 1 || i > L.lenght + 1)
printf("enter i was ERROR");
if (L.lenght == MAXSIZE)
printf("out of sequence table range");
for (int j = L.lenght - 1; j >= 0; j--)
{
if (i != j + 1)
{
L.elem[j + 1] = L.elem[j];
L.elem[j] = L.elem[j - 1];
}
else
{
L.elem[j] = e;
break;
}
}
L.lenght++;
}
//顺序表的删除
void ListDelete(SqList &L, int i)
{
if (i < 1 || i > L.lenght + 1)
printf("enter i was ERROR");
if (L.lenght == MAXSIZE)
printf("out of sequence table range");
for (int j = i - 1; j < L.lenght; j++)
{
L.elem[j] = L.elem[j + 1];
}
}
|