请大家找错,找了半天没找到,是逻辑错误

Fenginf 2010-03-24 07:38:06

#include <stdio.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include <malloc.h>
//操作对象
typedef struct lendbook
{

unsigned int sno;
unsigned int bno;
struct lendbook *next;
}lendbooks;
lendbooks *head = NULL;
int n;//指示head返回
//链节创建函数
lendbooks *create(void)
{

lendbooks *s = NULL;
s = (lendbooks *)malloc(sizeof(struct lendbook));
printf("请输入学号\n");
scanf("%d",&(s->sno));
printf("请输入书号\n");
scanf("%d",&(s->bno));
s->next = NULL;
return s;

}
//插入节点
lendbooks *insert( void )
{
lendbooks *p1=head, *p2=head;
lendbooks *s=NULL;
s = create();
if( head = NULL)//判断有没有节点
{
head = s;
n =1;
return head;
}
else
if( s->sno < head->sno)//当只有一个节点且输入学号比节点中学号小时,前插
{
s->next = head;
head = s;
n = 1;
return head;
}

{
while((head->sno < s->sno)&& (p1->next))//不是上述两种情况时,后插
{
p2 = p1->next;
p1 = p2->next;
}
if (p1->next = NULL)
{
printf("插入失败");
}
s->next = p1;
p2->next = s;
return p2;
}
}
//借阅信息录入函数
lendbooks *save( void )
{
char ch;
lendbooks *p2 = NULL;

while( ch != 'q' )//输入字母q时退出
{

n = 0;
p2 = insert(); //利用插入函数构建链表
if( n = 1) //若满足条件,则p2指向链头
{
head = p2;
}
printf("是否退出?输入q退出,否则继续\n");
scanf("%c",&ch);

}
if(ch = 'q')
//最后一个节点的指针域为null
return head;
}


//删除节点
int del( void )
{
unsigned int sno;
lendbooks *p1=head;
lendbooks *p2=head;
printf("请输入你要删除的学生学号\n");
scanf("%u",&sno);
while((p1->sno != sno)&&(p1->next))
{
p2 = p1->next;
p1 = p2->next;
}
if( p1->next = NULL)
return 0;
p2->next = p1->next;
printf("你已经删除学号为%u的同学的借阅信息\n",(p1->sno));
return 0;
}
//查找节点,思考:两个函数功能相同,只是参数不同,能否何为一个函数?
int snofind(void)
{
lendbooks *p1=head;
lendbooks *p2=head;
unsigned int sno;
printf("请输入你要查找的学生的学号(\n");
scanf("%u",&sno);
while((p1->sno != sno)&&(p1->next))
{
p2 = p1->next;
p1 = p2->next;
}
if( p1->next = NULL)
{
printf("未找到");
return 0;
}
printf("你找到的同学的借阅信息如下\n");
printf("学号:%u\n",(p1->sno));
printf("借阅的书号为:%u\n",p1->bno);
return 0;
}
int bnofind( void )
{
unsigned int bno;
lendbooks *p1=head;
lendbooks *p2=head;
printf("请输查询的书号\n");
scanf("%u",&bno);
while((p1->bno!= bno)&&(p1->next))
{
p2 = p1->next;
p1 = p2->next;
}
if( p1->next = NULL)
{
printf("未找到\n");
return 0;
}
printf("你找到的同学的借阅信息如下\n");
printf("学号:%u\n",p1->sno);
printf("借阅的书号为:%u\n",p1->bno);
return 0;

}
//主函数
int main(void)
{ char ch,ch1;
printf("-------------欢迎进入超小型图书借阅情况管理系统--------------\n");
printf("请先输入图书借阅情况\n");
head = save();

while( 1 )
{
printf("请选择功能\n");
printf("1:图书借阅情况查询;\n");
printf("2:插入图书借阅情况;\n");
printf("3:删除图书借阅信息;\n");
printf("----------------当输入q时退出系统-----------------------\n");
printf("请输入你要选择的功能");
scanf("%c",&ch);
switch( ch )
{
case '1': printf("1:按学号查询;\n");
printf("2:按书号查询;\n");
printf("请输入你要选择的功能的序号;\n");
scanf("%c",&ch1);
switch( ch1 )
{
case '1': snofind();break;
case '2': bnofind();break;
default: printf("输入错误\n");break;
}

case '2': insert();break;
case '3': del();break;
case 'q': return 0;
default:printf("\n输入错误\n");break;


}
}
}

自己写了个小的图书借阅信息管理系统,不过在调试的时候在insert函数处出问题了,是逻辑错误,能正常生成exe程序,找了半天没找到,谢谢大家啦
...全文
112 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
千杯不醉-sen 2010-03-24
  • 打赏
  • 举报
回复
学习啦~~~
Tauren_Chieftan 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xiaotutu6000 的回复:]

#include <stdio.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include <malloc.h>
大哥你的头文件不要重复知道吗?malloc好像包含在stdlib里面的~~~你删除红色的就好了
[/Quote]

标准库提供的头文件有#ifndef 。#define。。。 #endif保护,不会出现重复包含的问题。
xiaotutu6000 2010-03-24
  • 打赏
  • 举报
回复
你用VC F9标记,在F5编译吧,这个自己解决好些~~
Fenginf 2010-03-24
  • 打赏
  • 举报
回复
code:blocks,不大清楚怎么弄
smallbear_2008 2010-03-24
  • 打赏
  • 举报
回复
你是用什么工具?如果是Linux的话,就用gdb单步。VC我就不熟悉了
Fenginf 2010-03-24
  • 打赏
  • 举报
回复
大家说的问题我都改过来了,能正常运行但是输入数据时会出错,是程序上的逻辑错误,望大家帮调试下,我还不大会用调试工具
xiuxianshen 2010-03-24
  • 打赏
  • 举报
回复
== 的问题,楼主的编码习惯有问题
建议看看华为编程规范
http://download.csdn.net/source/1602564
xiaotutu6000 2010-03-24
  • 打赏
  • 举报
回复
我删除就可以运行了~~~
xiaotutu6000 2010-03-24
  • 打赏
  • 举报
回复
#include <stdio.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include <malloc.h>
大哥你的头文件不要重复知道吗?malloc好像包含在stdlib里面的~~~你删除红色的就好了
昵称很不好取 2010-03-24
  • 打赏
  • 举报
回复
你大多数需要写==判断的时候,你都写成了=,我已经标出来
#include <stdio.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include <malloc.h>
//操作对象
typedef struct lendbook
{

unsigned int sno;
unsigned int bno;
struct lendbook *next;
}lendbooks;
lendbooks *head = NULL;
int n;//指示head返回
//链节创建函数
lendbooks *create(void)
{

lendbooks *s = NULL;
s = (lendbooks *)malloc(sizeof(struct lendbook));
printf("请输入学号\n");
scanf("%d",&(s->sno));
printf("请输入书号\n");
scanf("%d",&(s->bno));
s->next = NULL;
return s;

}

lendbooks *insert( void )
{
lendbooks *p1=head, *p2=head;
lendbooks *s=NULL;
s = create();
if( head == NULL)//============
{
head = s;
n =1;
return head;
}
else
if( s->sno < head->sno)
{
s->next = head;
head = s;
n = 1;
return head;
}

{
while((head->sno < s->sno)&& (p1->next))
{
p2 = p1->next;
p1 = p2->next;
}
if (p1->next == NULL)//=============
{
printf("插入失败");
}
s->next = p1;
p2->next = s;
return p2;
}
}

lendbooks *save( void )
{
char ch;
lendbooks *p2 = NULL;

while( ch != 'q' )
{

n = 0;
p2 = insert();
if( n == 1) //==============
{
head = p2;
}
printf("是否退出?输入q退出,否则继续\n");
scanf("%c",&ch);

}
if(ch == 'q')//================
//最后一个节点的指针域为null
return head;
//return 这里少了返回值,return head只是ch=='q'时候的情况,不等于的情况也需要返回一个值
}


//删除节点
int del( void )
{
unsigned int sno;
lendbooks *p1=head;
lendbooks *p2=head;
printf("请输入你要删除的学生学号\n");
scanf("%u",&sno);
while((p1->sno != sno)&&(p1->next))
{
p2 = p1->next;
p1 = p2->next;
}
if( p1->next == NULL)//=================
return 0;
p2->next = p1->next;
printf("你已经删除学号为%u的同学的借阅信息\n",(p1->sno));
return 0;
}

int snofind(void)
{
lendbooks *p1=head;
lendbooks *p2=head;
unsigned int sno;
printf("请输入你要查找的学生的学号(\n");
scanf("%u",&sno);
while((p1->sno != sno)&&(p1->next))
{
p2 = p1->next;
p1 = p2->next;
}
if( p1->next == NULL)//==============
{
printf("未找到");
return 0;
}
printf("你找到的同学的借阅信息如下\n");
printf("学号:%u\n",(p1->sno));
printf("借阅的书号为:%u\n",p1->bno);
return 0;
}
int bnofind( void )
{
unsigned int bno;
lendbooks *p1=head;
lendbooks *p2=head;
printf("请输查询的书号\n");
scanf("%u",&bno);
while((p1->bno!= bno)&&(p1->next))
{
p2 = p1->next;
p1 = p2->next;
}
if( p1->next == NULL)//===============
{
printf("未找到\n");
return 0;
}
printf("你找到的同学的借阅信息如下\n");
printf("学号:%u\n",p1->sno);
printf("借阅的书号为:%u\n",p1->bno);
return 0;

}
//主函数
int main(void)
{ char ch,ch1;
printf("-------------欢迎进入超小型图书借阅情况管理系统--------------\n");
printf("请先输入图书借阅情况\n");
head = save();

while( 1 )
{
printf("请选择功能\n");
printf("1:图书借阅情况查询;\n");
printf("2:插入图书借阅情况;\n");
printf("3:删除图书借阅信息;\n");
printf("----------------当输入q时退出系统-----------------------\n");
printf("请输入你要选择的功能");
scanf("%c",&ch);
switch( ch )
{
case '1': printf("1:按学号查询;\n");
printf("2:按书号查询;\n");
printf("请输入你要选择的功能的序号;\n");
scanf("%c",&ch1);
switch( ch1 )
{
case '1': snofind();break;
case '2': bnofind();break;
default: printf("输入错误\n");break;
}

case '2': insert();break;
case '3': del();break;
case 'q': return 0;
default:printf("\n输入错误\n");break;


}
}
}
昵称很不好取 2010-03-24
  • 打赏
  • 举报
回复
你大多数需要写==判断的时候,你都写成了=,我已经标出来
#include <stdio.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include <malloc.h>
//操作对象
typedef struct lendbook
{

unsigned int sno;
unsigned int bno;
struct lendbook *next;
}lendbooks;
lendbooks *head = NULL;
int n;//指示head返回
//链节创建函数
lendbooks *create(void)
{

lendbooks *s = NULL;
s = (lendbooks *)malloc(sizeof(struct lendbook));
printf("请输入学号\n");
scanf("%d",&(s->sno));
printf("请输入书号\n");
scanf("%d",&(s->bno));
s->next = NULL;
return s;

}

lendbooks *insert( void )
{
lendbooks *p1=head, *p2=head;
lendbooks *s=NULL;
s = create();
if( head == NULL)//============
{
head = s;
n =1;
return head;
}
else
if( s->sno < head->sno)
{
s->next = head;
head = s;
n = 1;
return head;
}

{
while((head->sno < s->sno)&& (p1->next))
{
p2 = p1->next;
p1 = p2->next;
}
if (p1->next == NULL)//=============
{
printf("插入失败");
}
s->next = p1;
p2->next = s;
return p2;
}
}

lendbooks *save( void )
{
char ch;
lendbooks *p2 = NULL;

while( ch != 'q' )
{

n = 0;
p2 = insert();
if( n == 1) //==============
{
head = p2;
}
printf("是否退出?输入q退出,否则继续\n");
scanf("%c",&ch);

}
if(ch == 'q')//================
//最后一个节点的指针域为null
return head;
//return 这里少了返回值,return head只是ch=='q'时候的情况,不等于的情况也需要返回一个值
}


//删除节点
int del( void )
{
unsigned int sno;
lendbooks *p1=head;
lendbooks *p2=head;
printf("请输入你要删除的学生学号\n");
scanf("%u",&sno);
while((p1->sno != sno)&&(p1->next))
{
p2 = p1->next;
p1 = p2->next;
}
if( p1->next == NULL)//=================
return 0;
p2->next = p1->next;
printf("你已经删除学号为%u的同学的借阅信息\n",(p1->sno));
return 0;
}

int snofind(void)
{
lendbooks *p1=head;
lendbooks *p2=head;
unsigned int sno;
printf("请输入你要查找的学生的学号(\n");
scanf("%u",&sno);
while((p1->sno != sno)&&(p1->next))
{
p2 = p1->next;
p1 = p2->next;
}
if( p1->next == NULL)//==============
{
printf("未找到");
return 0;
}
printf("你找到的同学的借阅信息如下\n");
printf("学号:%u\n",(p1->sno));
printf("借阅的书号为:%u\n",p1->bno);
return 0;
}
int bnofind( void )
{
unsigned int bno;
lendbooks *p1=head;
lendbooks *p2=head;
printf("请输查询的书号\n");
scanf("%u",&bno);
while((p1->bno!= bno)&&(p1->next))
{
p2 = p1->next;
p1 = p2->next;
}
if( p1->next == NULL)//===============
{
printf("未找到\n");
return 0;
}
printf("你找到的同学的借阅信息如下\n");
printf("学号:%u\n",p1->sno);
printf("借阅的书号为:%u\n",p1->bno);
return 0;

}
//主函数
int main(void)
{ char ch,ch1;
printf("-------------欢迎进入超小型图书借阅情况管理系统--------------\n");
printf("请先输入图书借阅情况\n");
head = save();

while( 1 )
{
printf("请选择功能\n");
printf("1:图书借阅情况查询;\n");
printf("2:插入图书借阅情况;\n");
printf("3:删除图书借阅信息;\n");
printf("----------------当输入q时退出系统-----------------------\n");
printf("请输入你要选择的功能");
scanf("%c",&ch);
switch( ch )
{
case '1': printf("1:按学号查询;\n");
printf("2:按书号查询;\n");
printf("请输入你要选择的功能的序号;\n");
scanf("%c",&ch1);
switch( ch1 )
{
case '1': snofind();break;
case '2': bnofind();break;
default: printf("输入错误\n");break;
}

case '2': insert();break;
case '3': del();break;
case 'q': return 0;
default:printf("\n输入错误\n");break;


}
}
}
Fenginf 2010-03-24
  • 打赏
  • 举报
回复
没人来看啊?我顶再顶

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧