1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| #include<iostream> #include<cstring> #include<stdio.h> #include<stdlib.h> using namespace std; class book { public: int num; float price; book *next; }; book *head = NULL; bool check(string str) { for (int i = 0; i < (int)str.length(); i++) { if ((str[i] > '9' || str[i] < '0') && (str[i] != '.')) return false; } return true; } book* create() { book *p1, *p2; p1 = new book; head = p1; p2 = p1; cout << "请输入图书的编号,以0结束" << endl; string str; cin >> str; while (!check(str)) { cout << "输入的不是数字,请重新输入,按0返回" << endl; cin >> str; } p1->num = atoi(str.c_str()); //cin>>p1->num; if (p1->num != 0) { cout << "请输入图书的价格" << endl; cin >> str; while (!check(str)) { cout << "输入的不是数字,请重新输入,按0返回" << endl; cin >> str; } p1->price = atof(str.c_str()); //cin>>p1->price; } else { delete p1; p2 = NULL; p2->next = NULL; head = NULL; return head; } while (p1->num != 0) { p2 = p1; p1=new book; cout << "请输入图书的编号,以0结束" << endl; cin >> str; while (!check(str)) { cout << "输入的不是数字,请重新输入,按0返回" << endl; cin >> str; } p1->num = atoi(str.c_str()); //cin >> p1->num; if (p1->num != 0) { cout << "请输入图书的价格" << endl; cin >> str; while (!check(str)) { cout << "输入的不是数字,请重新输入,按0返回" << endl; cin >> str; } p1->price = atof(str.c_str()); //cin >> p1->price; } p2->next = p1; } delete p1; p2->next = NULL; return head; } void showbook(book *head) { cout<<endl; cout<<"图书信息如下:"<<endl; while(head) { cout<<"图书编号:"<<head->num<<"\t"<<"图书价格:"<<head->price<<endl; head=head->next; } } void Delete(book *head,int num) { book *l; if(head->num==num){ l=head; head=head->next; ::head=head; delete l; cout<<"操作成功"<<endl; return ; } while(head) { if(head->next==NULL) { cout<<"找不到要删除的编号。"<<endl; return ; } if(head->next->num==num) { l=head->next; head->next=l->next; delete l; cout<<"操作成功"<<endl; return ; } head=head->next; } cout<<"找不到要删除的编号。"<<endl;
} void insert(book *head,int num,float price) { book *list=new book; list->num=num; list->price=price; if(num<=head->num) { list->next=head; ::head=list; return; } book *temp=NULL; while((num>head->num)&&(head->next!=NULL)) { temp=head; head=head->next; } if(num>head->num) { head->next=list;
}else { temp->next=list; list->next=head; }
} int main() { head=create(); showbook(head); cout<<"请输入你要删除的图书编号:"; int BookNum; cin>>BookNum; Delete(head,BookNum); showbook(head); cout<<"请输入您要插入的图书编号:"; cin>>BookNum; cout<<"请输入您要插入的图书价格:"; float BookPrice; cin>>BookPrice; insert(head,BookNum,BookPrice); showbook(head); return 0; }
|