软件技术基础上机实验报告
姓名:肖燕平上机实验 一 Ex1_1 #include #define maxnum 20
typedef struct list_type { int data[maxnum]; int length; }list_type;
void error(int a) { switch(a) {
case 0:
printf("\nthe length of the data is too long\n");break;
case 1:
printf("\nthe place is wrong\n");break; } }
void creatlist( list_type *table)
//创建链表
{ int i; int k; table->length=0; printf("\nplease input the numbers of the data\n"); for(i=0;i
scanf("%d",&k);
if(k==-1)
//输入-1则结束
break;
学号:2011019090028
}
} table->data[i]=k; table->length++;
while(k!=-1) //如果数据输入过长,则报错且重新输入数据
{ error(0); while(k!=-1) //防止接下来的程序变量得到错误的数据
{
scanf("%d",&k); } creatlist(table); } void showlist(list_type *table) //显示数据
{ int i; while(table->length
printf("NO DATA");
creatlist(table);
}
for(i=0;ilength;i++) {
printf("%d
",table->data[i]); } printf("\nthe length of the data is %d\n",table->length);
}
void insertlist(list_type *table,int pla,int num)//插入一个数
{ int i;
while(platable->length)//如果插入的位置不符合条件,则重新输入
{
error(1);
printf("\nplease input the place of the insert number again\n");
scanf("%d",&pla);
}
table->length=table->length-1;
pla=pla-1;
for(i=table->length;i>pla-1;i--)
{
table->data[i+1]=table->data[i];
}
table->data[pla]=num;
table->length=table->length+2;
}
void delete_list(list_type *table,int place)
//删除一个数
{ int i; while(place>table->length-1||place
error(1);
printf("\nplease input the place of the delete number again\n");
scanf("%d",&place);
} for(i=place-1;ilength-1;i++) {
table->data[i]=table->data[i+1]; } table->length--; }
void main() { int inse_place,inse_num; int del_place;
list_type table;
creatlist(&table); //创建顺序表
showlist(&table); //显示顺序表
printf("\nplease input the insert place and number\n"); scanf("%d%d",&inse_place,&inse_num);
insertlist(&table,inse_place,inse_num);
//插入一个数
printf("\nthe new list is\n");
showlist(&table);//显示插入数后的顺序表
printf("\nplease input the delete place\n"); scanf("%d",&del_place);
//删除一个数 delete_list(&table,del_place); printf("\nthe new list is\n"); showlist(&table);
}
输出数据:
1,不考虑边界情况
2,考虑边界
问题及解决方法
问题1:遗漏了某个变量而直接用
注意:对于变量,要那个变量则再来定义,定义后再用。
问题2:输入数据量大于19时,其余数字会赋给后程序的scanf。 解决方法:利用另一变量k值,若k不等于-1则不允许程序往下走。
问题3:在print“NO DATA”后,程序依然往下走,不能重新录入数据。
解决方法:再调用输入数据函数,并且惯用while.
问题还有很多,但忘了些。。。。。。
心得体会:
1,在使用变量时,将要用哪个马上定义再使用。
2,error函数尽量写在所有函数之前。方便任一函数调用。 3,对于printf函数,多用换行。
4,用scanf函数时,若输入数的量大于某一scanf的需求量,则要运用一函数消耗掉多余量,避免影响下面的函数,赋错值。
5,对于一些不符合要求的录入值,要习惯于重新调用输入函数或调用自我重新录入。
6,虽然自我的思考很重要,但还是要和同学讨论和交流算法。 7,对于取值检验时,不能只取一种类型的数值或只随机取值,要注意取边界值和范围之外的值。
8,对于删除许多数时(例如把负数全部删除),要避免一次又一次的调用删除函数,防止出错。用x,y。Y首先比x前一位,若想
9,编程时,不要一来就写代码,一定要明确自己的步骤,在纸上写下来,用框图画好再写程序。
10,在有的方法总走不通时回头看是不是自己把问题复杂化了。