问题:
下面是所有的源代码,那位大神可以给我点拨点拨:
include <ctime>
include <cstdio>
include <iostream>
include <string>
include <cstdlib>
include <cstring>
using namespace std;
typedef int dataType;
string account, password;
string currentTime()
{
string str="";
char ch[20];
time_t tt = time(NULL);
struct tm* t = localtime(&tt);
sprintf(ch, "%d%s", t->tm_year + 1900, "-");
sprintf(ch, "%02d%s", t->tm_mon + 2, "-");
sprintf(ch, "%02d", t->tm_mday);
str += ch;
return str;
}
typedef struct bookLinkNode
{
dataType nowNumber, totalNumber;
string isbn,title, author;
struct bookLinkNode* next;
}bookList;
bookList* head = (bookList)malloc(sizeof(bookList));
bookList last;
//bookList* last = (bookList*)malloc(sizeof(bookList));
typedef struct registerLinkNode
{
string id, returnDate,isbn2;
struct registerLinkNode* next;
}registerList;
registerList* head2 = (registerList)malloc(sizeof(registerList));
registerList last2;
//registerList* last2 = (registerList*)malloc(sizeof(registerList));
void initBookList()
{
head->next = NULL;
bookList* s = (bookList*)malloc(sizeof(bookList));
last = head;
s->isbn = "1";
s->title = "老人与海";
s->author = "【美】欧内斯特·海明威";
s->nowNumber = 3;
s->totalNumber = 3;
s->next = NULL;
last->next = s;
last = s;
s = (bookList*)malloc(sizeof(bookList));
s->isbn = "2";
s->title = "毛泽东传";
s->author = "【美】罗斯·特里尔";
s->nowNumber = 2;
s->totalNumber = 2;
s->next = NULL;
last->next = s;
last = s;
s = (bookList*)malloc(sizeof(bookList));
s->isbn = "3";
s->title = "社会心理学";
s->author = "【美】戴维·迈尔斯";
s->nowNumber = 4;
s->totalNumber = 4;
s->next = NULL;
last->next = s;
last = s;
s = (bookList*)malloc(sizeof(bookList));
s->isbn = "4";
s->title = "极简天文学";
s->author = "【英】科林·斯图尔特";
s->nowNumber = 2;
s->totalNumber = 2;
s->next = NULL;
last->next = s;
last = s;
}
void initRegisterList()
{
head2->next = NULL;
last2 = head2;
}
bookList* bookExist(string str)
{
bookList* s = head;
while (s != NULL)
{
if (s->next->isbn == str)
return s->next;
else
s = s->next;
}
return NULL;
}
void inBook()
{
bookList* s = (bookList)malloc(sizeof(bookList));
cin >> s->isbn >> s->title >> s->author >> s->nowNumber >> s->totalNumber;
bookList s2 =bookExist(s->isbn);
if (s2 != NULL)
s2->totalNumber+=s->totalNumber;
else
{
s-> next = NULL;
last->next = s;
last = s;
}
}
int outBook(string str)
{
bookList* s = head;
s = s->next;
while (s != NULL)
{
if (s->title == str)
{
if (s->nowNumber > 0)
{
registerList* s2 = (registerList*)malloc(sizeof(registerList));
if (s2)
{
s2->id = account;
s2->returnDate = currentTime();
s2->isbn2 = s->isbn;
s2->next = NULL;
last2->next = s2;
last2 = s2;
s->nowNumber--;
cout << "借书成功,最迟还书日期为:" << s2->returnDate << '\n';
return 1;
}
}
else
{
cout << "这本书已经没有了,借不了" << '\n';
return 0;
}
}
s = s->next;
}
cout << "馆藏暂时还没有这本书";
return 0;
}
int returnBook(string str)
{
registerList* s = head2;
bookList* s2 = head;
s = s->next;
s2 = s2->next;
while (s != NULL)
{
if (s->id == str)
{
while (s2 != NULL)
{
if (s2->isbn == s->isbn2)
s2->nowNumber++;
s2 = s2->next;
}
free(s);
cout << "还书成功" << '\n';
return 1;
}
s = s->next;
}
cout << "你还没有借过书,无需还书" << "\n";
return 0;
}
int main()
{
initBookList();
initRegisterList();
bookList* s=head;
cout << "图书列表如下:" << '\n'
<< "书号 "
<< "书名 "
<< "作者 "
<< "现存量 "
<< "库存量" << '\n';
s = s->next;
for (int i = 0; i < 4; i++)
{
cout << s->isbn << " "
<< s->title << " "
<< s->author << " "
<< s->nowNumber << " "
<< s->totalNumber << "\n";
s = s->next;
}
cout << "请输入你的账号登录系统" <<'\n'<< "账号:";
cin >> account;
cout <<'\n'<<"登录成功"<< '\n';
cout << "菜单如下:"
<< "\n" << "1.我要存书"
<< "\n" << "2.我要借书"
<< "\n" << "3.我要还书"
<< "\n" << "请输入序号选择你要的服务:";
int n;
cin >> n;
while (true)
{
switch (n)
{
case 1:
{
cout << "请输入所存书的信息,以空格分隔";
inBook();
break;
}
case 2:
{
string bookName;
cout << "请输入书名借书:";
cin >> bookName;
outBook(bookName);
break;
}
case 3:
{
returnBook(account);
break;
}
default :
{
cout << "输入错误,请重新输入";
}
}
}
}
回答1:
出现的错误具体是什么呢?你卡在哪里了,你发现的问题是什么?