xml地图|网站地图|网站标签 [设为首页] [加入收藏]

直接崩了,当我执行完插入操作后

图片 1

图片 2

在执行六和七时总是显示文件打开错误。用的是C++6.0,请问大佬该咋改。急需。下面是代码#includestdio.h#includestdlib.h#includestring.htypedefstructLinknode{intnumber;charname[20];charsex[4];//sex性别charcla[10];intChinese;intEnglish;intMatch;inttotalpoints;structLinknode*next;}student;//*********************************voidMenu(void);voidsort(student*);//sort()对给定区间所有元素进行排序voidprint(student*);//print()打印当前窗口内容intamount=0;//取款金额//****************************************************student*initStudent()//函数功能建立一个节点,函数返回值:{student*head;head=(student*)malloc(sizeof(student));memset(head,0,sizeof(student));if(head!=NULL){head-next=NULL;returnhead;}elsereturnNULL;}//***********************************************************信息录入intcreatstudent(student*head)//creat-建立一个文件只以写的形式打开{intflag=1;student*s,*p;while(flag){s=(student*)malloc(sizeof(student));//malloc函数if(s==NULL){printf("创建失败!");return0;}aa:printf("请输入学号n");scanf("%d",s-number);if(s-number==0)break;p=head;while(p){if(s-number==p-number){printf("输入学号已存在n");gotoaa;}p=p-next;}while(s-number0){getchar();//getcher用来赋一个数字的值printf("n学号不能小于零!n");printf("n输入错误!请重新输入学号,输入0返回上一步n");scanf("%d",s-number);}printf("请输入姓名n");scanf("%s",s-name);printf("请输入性别n");scanf("%s",s-sex);printf("请输入班级n");scanf("%s",s-cla);printf("请输入语文成绩n");scanf("%d",s-Chinese);while(s-Chinese120||s-Chinese0){getchar();printf("分数错误,请重新输入范围在0到120的数字n");scanf("%d",s-Chinese);}printf("请输入英语成绩n");scanf("%d",s-English);while(s-English120||s-English0){getchar();printf("分数错误,请重新输入范围在0到120的数字n");scanf("%d",s-English);}printf("请输入数学成绩n");scanf("%d",s-Match);while(s-Match120||s-Match0){getchar();printf("分数错误,请重新输入范围在0到120的数字n");scanf("%d",s-Match);}s-totalpoints=s-Chinese+s-English+s-Match;s-next=head-next;head-next=s;amount++;}printf("创建完毕!n");printf("输入了%d个学生",amount);return0;}//*********************修改信息intModifStudent(student*head,intnumber){intflag,n;student*q;q=head;while(q!=NULLq-number!=number)q=q-next;if(q!=NULL){while(n){printf("n修改成绩科目:1.语文2.数学3.英语:");scanf("%d",flag);if(flag==1){getchar();printf("请重新输入语文成绩n");scanf("%d",q-Chinese);while(q-Chinese120||q-Chinese0){getchar();printf("分数错误,请重新输入范围在0到120的数字n");scanf("%d",q-Chinese);}}elseif(flag==2){printf("请重新输入数学成绩n");scanf("%d",q-Match);while(q-Match120||q-Match0){getchar();printf("分数错误,请重新输入范围在0到120的数字n");scanf("%d",q-Match);}}elseif(flag==3){printf("请重新输入英语成绩n");scanf("%d",q-English);while(q-English120||q-English0){getchar();printf("分数错误,请重新输入范围在0到120的数字n");scanf("%d",q-English);}}q-totalpoints=q-Chinese+q-English+q-Match;printf("n是否继续修改数据:1/0n");scanf("%d",n);}return1;}return0;}//************************************按学号查找student*findstudent(student*head,intnumber){student*q;q=head;while(q!=NULLq-number!=number)q=q-next;if(q!=NULL)returnq;elseprintf("没有该同学n");returnNULL;}//****************排序voidsort(student*head){intk;student*p=head,*q,*temp;printf("请输入排序的方式n");scanf("%d",k);if(k==0)Menu();switch(k){case1:{while(p-next!=NULL){p=p-next;q=head;while(q-next!=NULL){if((q-number)(q-next-number)){temp=(student*)malloc(sizeof(student));//(回看)temp-number=q-number;strcpy(temp-name,q-name);strcpy(temp-cla,q-cla);strcpy(temp-sex,q-sex);temp-Chinese=q-Chinese;temp-Match=q-Match;temp-English=q-English;temp-totalpoints=q-totalpoints;//后面的小空格q-number=q-next-number;strcpy(q-name,q-next-name);strcpy(q-cla,q-next-cla);strcpy(q-sex,q-next-sex);q-Chinese=q-next-Chinese;q-Match=q-next-Match;q-English=q-next-English;q-totalpoints=q-next-totalpoints;q-next-number=temp-number;strcpy(q-next-name,temp-name);strcpy(q-next-cla,temp-cla);strcpy(q-next-sex,temp-sex);q-next-Chinese=temp-Chinese;q-next-Match=temp-Match;q-next-English=temp-English;q-next-totalpoints=temp-totalpoints;free(temp);}q=q-next;}}}printf("排序成功!n");print(head);break;case2:{while(p-next!=NULL){p=p-next;q=head;while(q-next!=NULL){if((q-totalpoints)(q-next-totalpoints)){temp=(student*)malloc(sizeof(student));temp-number=q-number;strcpy(temp-name,q-name);strcpy(temp-cla,q-cla);strcpy(temp-sex,q-sex);temp-Chinese=q-Chinese;temp-Match=q-Match;temp-English=q-English;temp-totalpoints=q-totalpoints;q-number=q-next-number;strcpy(q-name,q-next-name);strcpy(q-cla,q-next-cla);strcpy(q-sex,q-next-sex);q-Chinese=q-next-Chinese;q-Match=q-next-Match;q-English=q-next-English;q-totalpoints=q-next-totalpoints;strcpy(q-next-name,temp-name);strcpy(q-next-cla,temp-cla);strcpy(q-next-sex,temp-sex);q-next-Chinese=temp-Chinese;q-next-Match=temp-Match;q-next-English=temp-English;q-next-totalpoints=temp-totalpoints;free(temp);}q=q-next;}}}printf("排序成功!n");print(head);break;default:printf("请输入正确的操作方式:n");sort(head);break;}}//*****************************输出信息voidprint(student*head){student*p;p=head;if(head-next==NULL)printf("没有学生资料!n");else{printf("输出结果为:n");printf("学号姓名班级性别语文数学英语总分n");while(p-next!=NULL){p=p-next;printf("%d,%s,%s,%s,%d,%d,%d,%dn",p-number,p-name,p-cla,p-sex,p-Chinese,p-Match,p-English,p-totalpoints);}}}//***********************删除学生信息student*delestudent(student*head,intnumber){student*p1,*p2;printf("n请输入要删除的学号,输入0退出当前界面n");scanf("%d",number);getchar();while(number!=0){if(head==NULL){printf("n没有任何学生资料n");return(head);}p1=head;while(number!=p1-numberp1-next!=NULL)//(-可能存在问题){p2=p1;p1=p1-next;}if(number==p1-number){if(p1==head)head=p1-next;elsep2-next=p1-next;printf("已经成功删除学号:%dn",number);amount=amount-1;}elseprintf("%d不存在此学生!n",number);printf("请输入继续删除的学生学号,退出请输入0:n");scanf("%d",number);getchar();}printf("现在的学生个数为:%d个!n",amount);return(head);}//************保存信息savestudent(student*p){FILE*fp;charfilepn[20];printf("请输入文件路径及文件名:n");scanf("%s",filepn);if((fp=fopen(filepn,"W+"))==NULL);{printf("不能打开文件!n");return0;}while(p!=NULL){if((fwrite(p,sizeof(student),1,fp))!=1){printf("写入失败!n");fclose(fp);}elsep=p-next;}fclose(fp);printf("文件已保存n");return0;}//调出信息student*loadstudent(){student*p,*p1,*head;charfilepn[20];FILE*fp;printf("请输入文件路径及文件名:n");scanf("%s",filepn);if((fp=fopen(filepn,"r+"))==NULL){printf("打开失败n");return0;}p=(student*)malloc(sizeof(student));head=p;while(!feof(fp)){if((fread(p,sizeof(student),1,fp))!=1);//分号else{p-next=(student*)malloc(sizeof(student));p1=p;p=p-next;}}p1-next=NULL;fclose(fp);returnhead;}//////////////菜单主界面voidMenu(){intflag,n,sure;student*head,*p;n=0;head=initStudent();begin:printf("@@@@@@@@欢迎使用@@@@@@@@n");printf("n学生管理系统n");printf("");printf("***********1:录入学生信息2:修改学生信息***********n");printf("***********3:查找学生信息4:排序学生信息***********n");printf("***********5:删除学生信息6:保存学生信息***********n");printf("***********7:导出学生信息0:退出管理系统***********n");printf("n操作方式:");scanf("%d",flag);switch(flag){case0:printf("谢谢使用学生管理系统n");exit(1);break;case1:creatstudent(head);print(head);gotobegin;case2:while(n==0){printf("请输入查找学生学号n");scanf("%d",n);}sure=ModifStudent(head,n);if(sure==1)printf("修改成功n");elseprintf("修改失败n");n=0;gotobegin;case3:while(n==0){printf("请输入查找学生学号:");;gotobegin;default:printf("请输入正确的操作方式......n");gotobegin;case5:delestudent(head,n);gotobegin;case6:savestudent(head);gotobegin;case7:head=loadstudent();print(head);gotobegin;}}voidmain(){system("colorF2");Menu();}

有大神帮忙看一下这个代码存在什么问题吗?万分感谢! 5C
一个数据结构的问题,在主函数中的‘B’情况运行到EnQueue的时候出现异常。
using namespace std;
#include
#include
#include
#include

#includeiostream#includestdio.h#includestdlib.husingnamespacestd;typedefstructQNode{intdata;QNode*next;}QNode;typedefstruct{QNode*front;QNode*rear;}linkqueue;voidinitqueue(linkqueueQ){Q.front=Q.rear=(QNode*)malloc(sizeof(QNode));if(!Q.front)exit(OVERFLOW);Q.front-next=NULL;}voidprint(){cout"请输入指令"endl;cout"e(入队)、d(出队)、、h(求队头)、l(求队长)"endl;cout"c(清空)、m(判空)、t(遍历)、x(退出)"endl;}voidenqueue(linkqueueQ){intn,i=0,m;QNode*p,*q;cout"请输入入队元素的个数"endl;cinn;cout"请输入入队的元素"endl;while(cinm){while(i!=n){q=(QNode*)malloc(sizeof(QNode));q-data=m;Q.rear-next=q;Q.rear=q;i++;}}cout"插入成功"endl;}intdequeue(linkqueueQ){if(Q.front==Q.rear){cout"空队"endl;return0;}QNode*p,*q,*r;p=Q.front;while(Q.front!=Q.rear){q=p-next;r=q;coutq-data"";p-next=q-next;free(r);}cout"出队成功"endl;return1;}voidgettop(linkqueueQ){cout"队头"(Q.front-1)-dataendl;}voidlength(linkqueueQ){cout"队长"(Q.front-Q.rear)endl;}voidclean(linkqueueQ){Q.front==Q.rear;}intempty(linkqueueQ){if(Q.front==Q.rear){cout"空队列"endl;return0;}else{cout"非空队列"endl;return1;}}inttraveral(linkqueueQ){if(Q.front==Q.rear){cout"空队"endl;return0;}QNode*q;inti;q=Q.front-next;for(i=0;iQ.front-Q.rear;i++){coutq-data"";q=q-next;}return1;}intmain(){chara;linkqueueq;initqueue(q);print();while(cinaa!='x'){switch(a){case'd':dequeue(q);print();break;case'm':empty(q);print();break;case't':traveral(q);print();break;case'h':gettop(q);print();break;case'l':length(q);print();break;case'c':clean(q);print();break;case'e':enqueue(q);print();break;}}getchar();getchar();getchar();getchar();return0;}

#includestdio.h#includestdlib.h#defineOK1#defineERROR0#defineMAXSIZE50//队列最大容量typedefintDataType;//队列存储元素类型typedefstructQNode{DataTypedata;structQNode*next;}QueueNode;//链队列结点类型typedefstruct{QNode*front;QNode*rear;intlength;}LinkQueue;//链队列类型LinkQueueQ;//定义一个链队列Q//链队列的初始化intInitQueue(LinkQueue*Q){Q-front=Q-rear=(QNode*)malloc(sizeof(QNode));//申请头结点if(!Q-front)returnERROR;/*Q-front=Q-rear;//尾指针也指向头结点*/Q-rear-next=NULL;Q-length=0;returnOK;}//链队列判断为空intQueueEmpty(LinkQueue*Q){if(Q-front==Q-rear)returnOK;//头尾指针相等队列为空returnERROR;}//链队列的入队运算intEnQueue(LinkQueue*Q,DataTypex){//将元素x插入链队列尾部QNode*p;p=(QNode*)malloc(sizeof(QNode));//申请新结点/*if(!p)returnERROR;*/p-data=x;p-next=NULL;Q-rear-next=p;Q-rear=p;/*Q-length++;*/returnOK;}//链队列的出队运算intDeQueue(LinkQueue*Q,DataTypee){QNode*p;if(QueueEmpty(Q))returnERROR;p=Q-front-next;e=p-data;Q-front-next=p-next;if(p==Q-rear)Q-front=Q-rear;free(p);returnOK;}//链队列的输出显示voidprintQueue(LinkQueue*Q){QNode*p;p=(QNode*)malloc(sizeof(QNode));p=Q-front-next;if(p-data==0)p=p-next;while(p!=NULL){printf("%d-",p-data);p=p-next;}printf("/n队列显示完成!");return;}intmain(){LinkQueue*Q;Q=NULL;inti,j,k,num;DataTypex,f;f=NULL;/*Q=(QNode*)malloc(sizeof(QNode));*/do{printf("nnnn");printf("ttt队列子系统n");printf("tt********************************n");printf("tt*1------进队列*n");printf("tt*2------出队列*n");printf("tt*3------队列空否*n");printf("tt*4------显示队列*n");printf("tt*5------返回*n");printf("tt********************************n");printf("tt请选择(0-5):");scanf_s("%d",k);printf("您选择的是:");getchar();switch(k){case1:InitQueue(Q);printf("n请输入要进队列的数据个数:");scanf_s("%d",x);for(i=1;i=x;i++){printf("请输入要入队的数据:");scanf_s("%d",num);EnQueue(Q,num);}break;case2:f=DeQueue(Q,f);if(f=1){printf("出队成功!");printf("出队元素为:%d",f);}elseif(f==0)printf("空队!");elseprintf("出队失败!");break;case3:j=QueueEmpty(Q);//判断队列空否if(j==1)printf("此队列为空!!!");elseprintf("此队列不空。");break;case4://显示队列printf("n此队列的数据为:");printQueue(Q);break;}}while(k!=0);}

typedef int Status;

#define int Status
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define MAXSIZE 100

//链栈
typedef struct StackNode {
char data;
struct StackNode *next;
}StackNode, *LinkStackPtr;
typedef struct LinkStack {
LinkStackPtr top;
int size;
}LinkStack;

//重要操作 LinkStack
//判断是否为空栈
Status StackEmpty(LinkStack *S) {
if (S->size == 0) {
return TRUE;
}
else {
return FALSE;
}
}
//初始化一个栈
Status InitStack(LinkStack *S) {
S->top = NULL;
S->size = 0;
return OK;
}
//返回栈顶元素
Status GetTop(LinkStack *S, char * e) {//char
if (S->size == 0) {
return ERROR;
}
else {
*e = S->top->data;
}
return OK;
}
//进栈操作
Status push(LinkStack *S, char e) {
LinkStackPtr s = (LinkStackPtr)malloc(sizeof(StackNode));
s->data = e;
s->next = S->top;
S->top = s;
S->size++;
return OK;
}
//出栈操作
Status pop(LinkStack *S, char *e) {
if (StackEmpty {
return FALSE;
}
else {
*e = S->top->data;
LinkStackPtr s = S->top;
S->top = s->next;
S->size--;
free;
return OK;
}
}

//链队列
typedef struct QNode {
char data;
struct QNode *next;
}QNode, *QueuePtr;//结点结构
typedef struct {
QueuePtr front, rear;
int Size;
}LinkQueue;//队列的链表结构

//重要操作 LinkQueue
//判断是否为空队列
Status QueueEmpty(LinkQueue *Q) {
if (Q->Size == 0) {
return TRUE;
}
else {
return FALSE;
}
}
//初始化
Status InitQueue(LinkQueue *Q) {
Q->front = Q->rear = malloc(sizeof;
if (!Q->front) exit;
Q->front->next = NULL;
Q->Size = 0;
return OK;
}
//入队列
Status EnQueue(LinkQueue *Q, char e) {//链队列是带头节点的,头指针永远指向头节点
QueuePtr q = malloc(sizeof;
if exit;
q->data = e;
q->next = NULL;
Q->rear->next = q;
Q->rear = q;
Q->Size++;
return OK;
}
//出队列
Status DeQueue(LinkQueue *Q, char *e) {
if (Q->front == Q->rear) return ERROR;
*e = Q->front->data;
QueuePtr q = Q->front->next;
Q->front->next = q->next;
if (q == Q->rear) Q->rear = Q->front;
Q->Size--;
free;
return OK;
}

//talk函数用于将魔王的语言进行压栈
Status Language_Talk(LinkStack *S) {
char word[MAXSIZE];
cout << "请输入魔王的语言";
cin >> word;
int i;
int j = 0;
int left = 0, right = 0;
cout << "魔王语言为"<<word << endl;
int len = strlen - 1;
cout <<"魔王语言长度为"<< len+1 << endl;

cout << "成功输入魔王语言";while  { if (word[j] == ' left++; if (word[j] == ')') right++; j++;}if  { cout << "魔王没有说话"; return ERROR;}else { if (left != right) { cout << "魔王说了错误的话"; return ERROR; } else { for (i = len;i >= 0;i--) { push(S, word[i]); } } }return OK;}

//已经解析的语言存入数组
Status Language_Record(LinkQueue *Q, LinkQueue *Q1) {
//char *e=;
char *e = malloc(sizeof;
while (!QueueEmpty {
DeQueue;
EnQueue;
}
return OK;
}

//词汇依次出栈进行处理
//A情况
char Language_explanA(LinkStack S, LinkQueue Q) {
//char *e;
char *e = malloc(sizeof;
pop;
char E = *e;
free;
int a = 0;
char A[10] = "sae";
while (a <= 2) {
EnQueue;
a++;
}
return *e;
/*cout << A;
cout << "输出第一个元素";
cout << Q->front->data;/
//return OK;
}
//B情况
Status Language_explanB(LinkStack
S, LinkQueue *Q) {
//char *e;
char *e = (char)malloc(sizeof;
pop;
int b = 0;
char B[10] = "tsaedsae";
while {
EnQueue;
b++;
}
return 0;
}
//‘(’情况
Status Language_explanleft(LinkStack
S, LinkQueue *Q) {
//char *e;
char *e = (char)malloc(sizeof;
pop;
pop;
char sita =
e;
EnQueue;
while (S->top->data != ')') {
pop;
EnQueue;
EnQueue;
}
return 0;
}
//')'情况
Status Language_explanright(LinkStack *S, LinkQueue *Q) {
//char *e;
char *e = (char*)malloc(sizeof;
pop;
while (!QueueEmpty) {
DeQueue;
push;
}
return OK;
}

int main() {
int position = 0;
LinkStack *Link_Stack = (LinkStack *)malloc(sizeof(StackNode));
LinkQueue *Link_Queue = (LinkQueue *)malloc(sizeof;
LinkQueue *Humanwords = (LinkQueue *)malloc(sizeof;
InitStack(Link_Stack);
InitQueue(Link_Queue);
char Devilwords[MAXSIZE];

Language_Talk(Link_Stack);cout << Link_Stack->size;cout << Link_Stack->top->data << endl;while (!StackEmpty(Link_Stack)) { if (Link_Stack->top->data == 'A') { Language_explanA(Link_Stack, Link_Queue); cout << "执行了A"; cout << Link_Queue->Size << endl; cout << Link_Stack->size << endl; } if (Link_Stack->top->data == 'B') { Language_explanB(Link_Stack, Link_Queue); cout << Link_Queue->Size << endl; cout << Link_Stack->size << endl; } if (Link_Stack->top->data == ' { Language_Record(Link_Queue, Humanwords); Language_explanleft(Link_Stack, Link_Queue); } if (Link_Stack->top->data == ')') Language_explanright(Link_Stack, Link_Queue);}cout << Humanwords;system;return OK;

}

本文由必威发布于编程,转载请注明出处:直接崩了,当我执行完插入操作后