#include "stdio.h"
typedef int Elemtype;// 表的元素类型为 int
# define maxlen 800 //分配的存储空间为10
# define N 6
struct Message
{
char name[20];
Elemtype tel[20];
Elemtype num[maxlen];//存放元素序号
int len;//线性表长度
} M[maxlen],A[N],elem;
//表的创建方法
void Create_M(Message M[maxlen],Message A[N],int n)
{
int i,j=0,a;
do{
printf("请输入线性表中的元素:");
scanf("%s%s",&A[j].name,&A[j].tel);
j++;
printf("继续添加请按1,结束请按0:");
scanf("%d",&a);
}while(a);
for(i=0;i<n;i++)
{
M[i]=A[i];//给元素赋值
M.num=i+1;
}
M.len=n;//置线性表的长度
}
//元素的插入方法
void InsertList(M[maxlen],int i,Message elem)
{
if (M[1].len>maxlen)
{
printf("表已经满了!!!");
return;
}
if ((i<0)||(i>M.len))
printf("i值不合法!!");
else
{
for(int j=M.len-1;j>=1;j--)
M[j+1]=M[j];
M[i]=elem;
M.len++;
}
}
//删除算法
void DeleteList (Message M[maxlen], int i)
{
if(M.len==0)
{
printf("M表为空表!!!");
return;
}
if ((i<0)||(i>M.len))
printf("i值不合法!!");
else
{
for(int j=i+1;j>=1;j++)
M[j-1]=M[j];
M.len--;
}
}
//输出算法
void PrintList(Message M[800])
{
for(int i=0;i<M[1].len;i++)
printf("%d,%s,%s,%s",M[i].num,M[i].name,M[i].tel,M[i].len);//逐个元素输出
printf("\n");
}
void Search(Message M[maxlen],int n)
{
int i,a,k=0;
printf("请输入您要查找的元素值:");
scanf("%d",&a);
if(M.len==0)
{
printf("L表为空表!!!");
return;
}
else
{
for(i=0;i<n;i++)
if(M[i].num==a)
{
printf("%d,%s,%s,%s",M[i].num,M[i].name,M[i].tel,M[i].len);
k=0;
}
printf("\n");
while(k)
{
printf("列表中没有次项记录!");
break;
}
printf("\n");
}
}
//主函数
void main()
{
FILE *fp=fopen("E:\\Temporary Internet Files\\t.txt","w");
int a,i;
do{
printf("1。创建一个线性列表; 2。在线性表中插入元素; 3。删除线性表中元素; 4。查看列表中的元素; 5。查找列表中的元素; 6。退出\n");
printf("请选择您要进行的操作:");
scanf("%d",&a);
switch(a)
{
case 1:Create_M(M[maxlen],A,N);break;
case 2:
{
printf("请输入插入位置(指定元素的后面插入):");
scanf("%d",&i); //输入插入位置
printf("请输入插入元素值:");
scanf("%s%s",&elem.name,&elem.tel);//输入插入元素值
InsertList(M[maxlen],i,elem);//调用插入算法
}break;
case 3:
{
printf("请输入删除元素的位置:");
scanf("%d",&i);//输入删除元素的位置
DeleteList(M[maxlen],i);//调用删除算法
PrintList(M[maxlen]);//输出删除后的列表
} break;
case 4:
{
PrintList(M[maxlen]);
printf("当前线性表元素为: \n");
}break;
case 5: Search(M[maxlen],N);break;
case 6:break;
default:printf("输入错误。\n");
}
a=0;
printf("继续进行其他操作请按1,结束请按0:");
scanf("%d",&a);
}while(a);
fclose(fp);
}
这些错误好想是同一类型的 可是该怎么改呢?
--------------------Configuration: a - Win32 Debug--------------------
Compiling...
a.cpp
C:\Documents and Settings\Guest\桌面\a.cpp(28) : error C2228: left of '.num' must have class/struct/union type
C:\Documents and Settings\Guest\桌面\a.cpp(30) : error C2228: left of '.len' must have class/struct/union type
C:\Documents and Settings\Guest\桌面\a.cpp(33) : error C2062: type 'int' unexpected
C:\Documents and Settings\Guest\桌面\a.cpp(34) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Guest\桌面\a.cpp(34) : error C2447: missing function header (old-style formal list?)
C:\Documents and Settings\Guest\桌面\a.cpp(53) : error C2228: left of '.len' must have class/struct/union type
C:\Documents and Settings\Guest\桌面\a.cpp(58) : error C2228: left of '.len' must have class/struct/union type
C:\Documents and Settings\Guest\桌面\a.cpp(64) : error C2228: left of '.len' must have class/struct/union type
C:\Documents and Settings\Guest\桌面\a.cpp(79) : error C2228: left of '.len' must have class/struct/union type
C:\Documents and Settings\Guest\桌面\a.cpp(87) : error C2446: '==' : no conversion from 'int' to 'int *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\Guest\桌面\a.cpp(87) : error C2040: '==' : 'int [800]' differs in levels of indirection from 'int'
C:\Documents and Settings\Guest\桌面\a.cpp(112) : error C2664: 'Create_M' : cannot convert parameter 1 from 'struct Message' to 'struct Message []'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Documents and Settings\Guest\桌面\a.cpp(119) : error C2065: 'InsertList' : undeclared identifier
C:\Documents and Settings\Guest\桌面\a.cpp(125) : error C2664: 'DeleteList' : cannot convert parameter 1 from 'struct Message' to 'struct Message []'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Documents and Settings\Guest\桌面\a.cpp(126) : error C2664: 'PrintList' : cannot convert parameter 1 from 'struct Message' to 'struct Message []'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Documents and Settings\Guest\桌面\a.cpp(130) : error C2664: 'PrintList' : cannot convert parameter 1 from 'struct Message' to 'struct Message []'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Documents and Settings\Guest\桌面\a.cpp(133) : error C2664: 'Search' : cannot convert parameter 1 from 'struct Message' to 'struct Message []'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
a.obj - 17 error(s), 0 warning(s)