system("cls.exe");//没有错
unicorn-h.spaces. ◇◆ sava-scratch.spaces.
#include "stdio.h"
#define pr printf
#define SIZE sizeof(struct student)
#define ppp "%10ld%-10s%-4s\n",st1->num,st1->name,st1->sex /*这些尽量不要用*/
/*函数前最好写上函数返回值类型,新学习编程一定要注意编写格式!*/
/*我已经帮你进行了程序格式整理,以及函数返回值等修改*/
/*在DEV-CPP(GCC) Win-TC(TC) 下编译通过,你运行一下看看还有没有问题*/
/*最好用32位编译器GCC(DEV-CPP),否则用TC编译后会出现错误!*/
struct student {
long num;
char name;
char sex;
struct student *next;
} *head;
/*去掉,无用 struct student ; */
char filename[20];
/*****************开始界面********************/
void in_print()
{
pr("************************* welcome ********************************\n");
pr(" author: senyee(great man) heihei!!\n");
pr(" order: 'd':delect. 'a':add. 's':search.\n");
pr(" 'x':show. 'q':quit. 'c':cls. 'b':save.\n");
pr("************************* welcome ********************************\n");
}
/********************退出*************************/
void quit()
{
pr("************************ thanks to used %c **********************\n",1);
pr(" press anly key to end......\n");
pr("************************ thanks to used %c **********************\n",1);
}
/*******************显示***************/
void show() {
struct student *st1,*p;
p=head;
st1=p;
if(p==NULL)printf("null file!\n");
do {
pr(ppp);
p=st1;st1=p->next;
} while(st1);
}
/***************查询***********************/
void search() {
struct student *st1,*p;
int r=0,n;
printf("please enter the student number:");
scanf("%d",&n);
p=head;st1=p;
do {
if(st1->num==n) {
pr(ppp);
r=1;break;
}
} while(st1);
if(r==0) pr("error! not found!\n");
}
/****************删除*******************/
void del() {
struct student *st1,*p;
int r=0,n;
p=head; /*程序要分行写,虽然不是必须的,但根据软件工程学以及易读性等看,最好一行一句*/
st1=p;
printf("please enter the student number:");
scanf("%d",&n);
do {
if(st1->num==n) {
if(st1==head)
head=st1->next;
else
p->next=st1->next;
r=1;
break;
}
p=st1;
st1=p->next;
} while(st1);
if(r==0) pr("error! not found!\n");
}
/*****************添加*******************/
void add()
{
int r=1;
struct student stu,*st1,*p;
p=head;st1=p->next;
scanf("%ld,%s,%s",stu.num,stu.name,stu.sex);
if(stu.num<=head->num)
{
head=&stu;
head->next=p;
return;
}
p=st1;
do
{
if(st1->num>=stu.num&&st1->num<=(st1->next)->num)
{stu.next=st1->next;st1->next=&stu;r=0;break; }
p=st1;
st1=p->next;
}
while(p);
if(r) {st1->next=&stu;stu.next=0;}
}
/********************确认******************/
int ackkw()
{
char str;
pr("do you want to exit? y/n\n");
scanf("%c",&str);
if(str=='y'||str=='Y')
return(1);
else
return(0);
}
/*******************保存修改******************/
void save() {
FILE *fp;
struct student *st1,*st2;
char s;
pr("covered the old file? y/n\n");
scanf("%c",&s);
if(s=='Y'||s=='y')
scanf("%s",filename);
fp=fopen("filename","wb");
st1=head;
st2=st1;
do {
fwrite(st1,SIZE,1,fp);
st1=st2->next;
st2=st1;
} while(st1);
}
/*************打开并显示************************/
void open() {
FILE *fp;
int i=0;
char s;
struct student stud,*st1,*st2;
pr("enter the filename:");
gets(filename);
if((fp=fopen(filename,"r"))==NULL) { /* 用GCC(DEV-CPP)编译就没有问题,我在Win-TC下也遇到这个问题 */
pr("no have the file\n"); /*打开文件*/
pr("do you want build a new file? y/n");
s=getchar();
getchar();
if(s=='Y'||s=='y') {
fp=fopen(filename,"w");
pr("add data now? y/n\n");
s=getchar();
getchar();
if(s=='Y'||s=='y') add();
}
return;
}
while(!feof(fp)) {
fread(&st1,SIZE,1,fp); /*读取文件*/
if(i==0) head=st1;
else st2->next=st1;
st2=st1;i++;
st2->next=0;
pr(ppp);
}
fclose(fp);
pr("you %d ge.\n",i+1);
getchar();
}
/******************主函数******************************/
int main(void) {
char order='x';
int num;
in_print();
/*这里的几句去掉,因为与open()函数中的重复,引起使用者的误会*/
open();
while(order!='q') {
pr("please enter your order:\n");
scanf("%c",&order); /*%s是字符串格式表达,%c才是字符*/
switch(order) {
case 'A':
case 'a':add();break;
case 'D':
case 'd':del();break;
case 'S':
case 's':search();break;
case 'X':
case 'x':show();break;
case 'b':
case 'B':if(ackkw()) save();break;
case 'Q':order+=32;
case 'q':if(ackkw())quit();
else order=0;break;
case 'c':
case 'C':in_print();break;
default:printf("no have this order\n");
}
}
getchar();
return 0;
}
这个月我要期末考试,很忙,所以本月可能不常上
还有错误:错误1:
if(s=='Y'||s=='y') {
fp=fopen(filename,"w");
pr("add data now? y/n\n");
getchar(); /*注意这里*/
s=getchar();
if(s=='Y'||s=='y') add();
}
错误2:
void add()
{
int r=1;
struct student stu,*st1,*p;
p=head;st1=p->next;
scanf("%ld%s%s",stu.num,stu.name,stu.sex);/*没有逗号*/
if(stu.num<=head->num)
{
head=&stu;
head->next=p;
return;
}