if (num == "0")
{
if (temp)
{
return head;
}
else if (temp->next == NULL)
{
return head;
}
else
.....
这段代码你想做什么?输入0则返回head么??
我看了一下你的程序,修改了一些,,如下:// main.cpp
// ConsoleApplication
//
// Created by 廖庆飞 on 14/9/9.
// Copyright (c) 2014年 廖庆飞. All rights reserved.
//
#include<iostream>
#include<string>
using namespace std;
class book
{
public:
int num;
float price;
book *next;
};
book* creat();
book* delete_node(book*);
int delete_all(book*);
void find(book*);
bool find_num(book*,string);
book* insert(book*);
book* sort(book*);
bool check_int(string);
bool check_float(string);
int showbook(book*);
int main(int argc, const char * argv[])
{
string str;
int i;
book *head = NULL;
while (true)
{
cout << "1.重建图书2.添加图书3.删除图书4.图书排序5.图书查找6.显示图书7.屏幕清除0.程序退出" << endl;
cin >> str;
while (check_int(str))
{
cout << "你输入的格式有错误,请重新输入." << endl;;
cout << "请输入选项编号:";
cin >> str;
}
if (str == "0")
{
delete_all(head);
return 0;
}
i = atoi(str.c_str());
switch (i)
{
case 1:
if (head == NULL)
{
head = creat();
}
else
{
delete_all(head);
head = creat();
}
break;
case 2:
head = insert(head);
break;
case 3:
head = delete_node(head);
break;
case 4:
head = sort(head);
break;
case 5:
find(head);
break;
case 6:
showbook(head);
break;
case 7:
system("cls");
break;
default:
cout << "对不起,没有这个选项." << endl;
break;
}
}
return 0;
}
//--------------------create---------------------------------
book *creat()
{
string num;
string price;
book *head = NULL;
book *temp = NULL;
book *tail = head;
while(tail!=NULL && tail->next !=NULL) //找到链表的尾
{
tail = tail->next;
}
do
{
cout << "请输入书籍的编号:";
cin >> num;
while (check_int(num))
{
cout << "你输入的格式有错误,请重新输入." << endl;;
cout << "请输入书籍的编号:";
cin >> num; //输入格式错误后重新输入
}
while (find_num(head,num))
{
cout << "已存在编号,请重新输入." << endl;
cout << "请输入书籍的编号:";
cin >> num;
}
if (num == "0")
{
if(head)
{
return head;
}
cout<<"没有创建新图书"<<endl;
return NULL;
}
cout << "请输入书籍的价格:";
cin >> price;
while (check_float(price))
{
cout << "你输入的格式有错误,请重新输入." << endl;
cout << "请输入书籍的价格:";
cin >> price;
}
if (price == "0")
{
if(head)
{
return head;
}
cout<<"没有创建新图书"<<endl;
return NULL;
}
temp=new book;
temp->num = atoi(num.c_str());
temp->price = (float)atof(price.c_str());
temp->next = NULL;
if (head == NULL)
{
head=temp;
tail=head;
}
else
{
tail->next=temp;
tail=temp;
}
} while (true);
}
//---------------------------------------------check_int
//检测输入的字符是否为数字字符
//若是返回false,否则返回true
bool check_int(string str)
{
for (unsigned i = 0; i < str.length();i++)
{
if (str[i] > '9' || str[i] < '0')
{
return true;
}
}
return false;
}
///--------------------------------------------check_float
bool check_float(string str)
{
int ax = 0;//用来控制小数点的个数
for (unsigned i = 0; i < str.length(); i++)
{
if (str[i] == '.')
{
ax += 1;
}
if (ax > 1)
{
return true;
}
if (str[i] > '9' || str[i] < '.' || str[i] == '/')//由于0到.之间的ASSCII只有一个'/'所把'/'字符排除
{
return true;
}
}
return false;
}
//-----------------------------------------------showbook
int showbook(book *head)
{
if (head == NULL)
{
cout << "没有图书信息." << endl;
return 0;
}
while (head)
{
cout << "编号:\t" << head->num << "\t" << "价格:\t" << head->price << endl;
head = head->next;
}
return 0;
}
//-------------------------------------------delete_node
book *delete_node(book* head)
{
if (head == NULL)
{
cout << "没有数据." << endl;
return head;
}
string str;
cout << "请输入查找的编号:";
cin >> str;
while (check_int(str))
{
cout << "你输入的格式有错误,请重新输入." << endl;
cout << "请输入查找的编号:";
cin >> str;
}
if (str == "0")
{
return head;
}
int num = atoi(str.c_str());
book* temp = head;
book*temp_two = NULL;
if (head->num == num)
{
temp = head->next;
delete head;
head = temp;
cout << "删除完成";
return head;
}
else
{
while (head)
{
temp_two = temp->next;
if (temp_two->num == num)
{
if (temp_two->next == NULL)
{
delete temp_two;
temp->next = NULL;
return head;
}
temp->next = temp_two->next;
delete temp_two;
cout << "删除完成." << endl;
return head;
}
else
{
temp = temp_two->next;
}
}
}
cout << "没有查找到输入的相关数据." << endl;
return head;
}
//======---------------------------------------delete_all 0
int delete_all(book* head)
{
if (head == NULL)
{
cout << "没有数据." << endl;
return 0;
}
else
{
book *temp = NULL;
while (head)
{
temp = head->next;
delete head;
head = temp;
}
}
if(head == NULL)
cout << "删除成功." << endl;
else
cout<<"删除失败"<<endl;
return 0;
}
///------------------------------------------------find
void find(book* head)
{
if (head == NULL)
{
cout << "没有数据." << endl;
return;
}
string str;
cout << "请输入查找的编号:";
cin >> str;
while (check_int(str))
{
cout << "你输入的格式有错误,请重新输入." << endl;
cout << "请输入查找的编号:";
cin >> str;
}
if (str == "0")
{
return;
}
int num = atoi(str.c_str());
while (head)
{
if (head->num == num)
{
cout << "图书编号:" << head->num << endl;
cout << "图书价格:" << head->price << endl;
return;
}
else
{
head = head->next;
}
}
cout << "没有查找到相关数据." << endl;
return;
}
//=--------------------------------------------find_num
//寻找链表中是否存在输入序列
//若是返回true,否则false
bool find_num(book* head,string str)
{
if (head == NULL)
{
return false;
}
int num=atoi(str.c_str());
book *temp = head;
while (temp)
{
if (temp->num == num)
{
return true;
}
temp = temp->next;
}
return false;
}
//----------------------------------------insert 这里和create内部实现基本一样,是否考虑封装一下
book* insert(book* head)
{
string num;
string price;
book *temp=NULL;
book *tail = head;
if (head != NULL)
{
while (tail->next)
{
tail = tail->next; //寻找链表尾
}
}
do
{
cout << "请输入书籍的编号:";
cin >> num;
while (check_int(num))
{
cout << "你输入的格式有错误,请重新输入." << endl;;
cout << "请输入书籍的编号:";
cin >> num; //输入格式错误后重新输入
}
while (find_num(head,num))
{
cout << "已存在编号,请重新输入." << endl;
cout << "请输入书籍的编号:";
cin >> num;
}
if (num == "0")
{
if(head)
{
return head;
}
cout<<"没有创建新图书"<<endl;
return NULL;
}
cout << "请输入书籍的价格:";
cin >> price;
while (check_float(price))
{
cout << "你输入的格式有错误,请重新输入." << endl;
cout << "请输入书籍的价格:";
cin >> price;
}
if (price == "0")
{
if(head)
{
return head;
}
cout<<"没有创建新图书"<<endl;
return NULL;
}
temp=new book;
temp->num = atoi(num.c_str());
temp->price = (float)atof(price.c_str());
temp->next = NULL;
if (head == NULL)
{
head=temp;
tail=head;
}
else
{
tail->next=temp;
tail=temp;
}
} while (true);
}
///----------------------------------------------------------sort 4
book* sort(book* head)
{
if (head == NULL)
{
cout << "没有数据" << endl;
return NULL;
}
/*
book *p=head, *p1=NULL, *p2=NULL, *p3=NULL,*temp;
p1 = p->next;
p2 = p1->next;
if (p > p1) //这个地址有什么可排列的。。。
{
temp = p;
p = p1;
p = temp;
head = p;
if (p2 == NULL)
{
return head;
}
p = p->next;
p1 = p1->next;
p2 = p2->next;
}
do
{
if (p1 > p2)
{
p3 = p2->next;
temp = p1;
p1 = p2;
p2 = temp;
p->next = p1;
p1->next = p2;
p2->next = p3;
}
p = p->next;
p1 = p1->next;
p2 = p2->next;
} while (p3);
*/
//实现一个按序号排列 冒泡
book * tempf = head; //链表前面节点
book * tempb = head; //链表 后面节点
int temp;
while(tempb=tempf){
while(tempb){
if(tempf->num > tempb->num)
{
temp = tempb->num;
tempb->num = tempf->num;
tempf->num = temp;
}
tempb=tempb->next; //指向下一个节点
}
tempf=tempf->next;
}
cout<<"结束排列!"<<endl;
return head;
}
|