This commit is contained in:
2024-12-05 15:46:08 +08:00
parent 08f6f4b0f4
commit 4ae4cf8554
26 changed files with 1385 additions and 1381 deletions
+63 -63
View File
@@ -1,63 +1,63 @@
//LinkList.cpp
#include <iostream>
#include<iomanip>
#include"LinkList.h"
using namespace std;
bool isEmpty(bookList *l) {
if ((*l)->next == NULL) {
return true;
}
return false;
}
void create(bookList &l) {
bookNode *input = new bookNode;
input->next = NULL;
bookNode *temp = l; // 从头节点开始
bool flag = true;
while (flag) {
cin >> input->id >> input->name >> input->price;
if (input->id == "0" && input->name == "0" && input->price == 0) {
flag = false;
delete input;
} else {
if (temp == l) {
l->next = input;
} else {
temp->next = input;
}
temp = input;
input = new bookNode;
input->next = NULL;
}
}
}
void display(bookList *l) {
if (isEmpty(l)) {
printf("List is empty\n");
} else {
bookNode *temp = (*l)->next;
while (temp != NULL) {
cout << temp->id << " " << temp->name << " " << fixed << setprecision(2) << temp->price << endl;
temp = temp->next;
}
}
}
int getLength(bookList *l) {
if (isEmpty(l)) {
return 0;
}
bookNode *temp = (*l)->next;
int length = 0;
while (temp != NULL) {
length++;
temp = temp->next;
}
return length;
}
//LinkList.cpp
#include <iostream>
#include<iomanip>
#include"LinkList.h"
using namespace std;
bool isEmpty(bookList *l) {
if ((*l)->next == NULL) {
return true;
}
return false;
}
void create(bookList &l) {
bookNode *input = new bookNode;
input->next = NULL;
bookNode *temp = l; // 从头节点开始
bool flag = true;
while (flag) {
cin >> input->id >> input->name >> input->price;
if (input->id == "0" && input->name == "0" && input->price == 0) {
flag = false;
delete input;
} else {
if (temp == l) {
l->next = input;
} else {
temp->next = input;
}
temp = input;
input = new bookNode;
input->next = NULL;
}
}
}
void display(bookList *l) {
if (isEmpty(l)) {
printf("List is empty\n");
} else {
bookNode *temp = (*l)->next;
while (temp != NULL) {
cout << temp->id << " " << temp->name << " " << fixed << setprecision(2) << temp->price << endl;
temp = temp->next;
}
}
}
int getLength(bookList *l) {
if (isEmpty(l)) {
return 0;
}
bookNode *temp = (*l)->next;
int length = 0;
while (temp != NULL) {
length++;
temp = temp->next;
}
return length;
}
+21 -21
View File
@@ -1,21 +1,21 @@
//LinkList.h
#ifndef LINKLIST_H
#define LINKLIST_H
#include<string>
using namespace std;
typedef struct bookNode {
string id;
string name;
double price;
bookNode *next;
} book_node, *bookList;
bool isEmpty(bookList);
void create(bookList &l);
void display(bookList *l);
int getLength(bookList *l);
#endif //LINKLIST_H
//LinkList.h
#ifndef LINKLIST_H
#define LINKLIST_H
#include<string>
using namespace std;
typedef struct bookNode {
string id;
string name;
double price;
bookNode *next;
} book_node, *bookList;
bool isEmpty(bookList);
void create(bookList &l);
void display(bookList *l);
int getLength(bookList *l);
#endif //LINKLIST_H
+14 -14
View File
@@ -1,14 +1,14 @@
//test1.cpp
#include <iostream>
#include "LinkList.h"
using namespace std;
int main() {
bookList books = new bookNode;
books->next = NULL;
create(books);
cout << getLength(&books) << endl;
display(&books);
return 0;
}
//test1.cpp
#include <iostream>
#include "LinkList.h"
using namespace std;
int main() {
bookList books = new bookNode;
books->next = NULL;
create(books);
cout << getLength(&books) << endl;
display(&books);
return 0;
}
+47 -47
View File
@@ -1,47 +1,47 @@
//test2.cpp
#include <iostream>
#include"LinkList.h" //LinkList类见第一题
using namespace std;
void swapNode(bookNode &n1, bookNode &n2) {
string tempId = n1.id;
n1.id = n2.id;
n2.id = tempId;
string tempName = n1.name;
n1.name = n2.name;
n2.name = tempName;
double tempPrice = n1.price;
n1.price = n2.price;
n2.price = tempPrice;
}
void bubble_sort(bookList &l) {
int n = getLength(&l);
bool swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
bookNode *temp = l->next;
for (int j = 0; j < n - i - 1; j++) {
bookNode *temp1 = temp->next;
if (temp != NULL && temp1 != NULL && temp->price < temp1->price) {
swapNode(*temp, *temp1);
swapped = true;
}
temp = temp1;
}
if (!swapped) {
break;
}
}
}
int main() {
bookList books = new bookNode;
books->next = NULL;
create(books);
bubble_sort(books);
display(&books);
return 0;
}
//test2.cpp
#include <iostream>
#include"LinkList.h" //LinkList类见第一题
using namespace std;
void swapNode(bookNode &n1, bookNode &n2) {
string tempId = n1.id;
n1.id = n2.id;
n2.id = tempId;
string tempName = n1.name;
n1.name = n2.name;
n2.name = tempName;
double tempPrice = n1.price;
n1.price = n2.price;
n2.price = tempPrice;
}
void bubble_sort(bookList &l) {
int n = getLength(&l);
bool swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
bookNode *temp = l->next;
for (int j = 0; j < n - i - 1; j++) {
bookNode *temp1 = temp->next;
if (temp != NULL && temp1 != NULL && temp->price < temp1->price) {
swapNode(*temp, *temp1);
swapped = true;
}
temp = temp1;
}
if (!swapped) {
break;
}
}
}
int main() {
bookList books = new bookNode;
books->next = NULL;
create(books);
bubble_sort(books);
display(&books);
return 0;
}
+21 -21
View File
@@ -1,21 +1,21 @@
//test3.cpp
#include <iostream>
#include"LinkList.h" //LinkList类见实验一
using namespace std;
void alter_to_110_percent(bookList &l) {
bookNode *temp = l->next;
while (temp != NULL) {
temp->price *= 1.1;
temp = temp->next;
}
}
int main() {
bookList books = new bookNode;
books->next = NULL;
create(books);
alter_to_110_percent(books);
display(&books);
return 0;
}
//test3.cpp
#include <iostream>
#include"LinkList.h" //LinkList类见实验一
using namespace std;
void alter_to_110_percent(bookList &l) {
bookNode *temp = l->next;
while (temp != NULL) {
temp->price *= 1.1;
temp = temp->next;
}
}
int main() {
bookList books = new bookNode;
books->next = NULL;
create(books);
alter_to_110_percent(books);
display(&books);
return 0;
}
+57 -57
View File
@@ -1,57 +1,57 @@
//test4.cpp
#include <iomanip>
#include<iostream>
#include"LinkList.h" //LinkList类见实验一
using namespace std;
void create_with_count(bookList &l, int count) {
bookNode *input = new bookNode;
input->next = NULL;
bookNode *temp = l;
for (int i = 0; i < count; i++) {
cin >> input->id >> input->name >> input->price;
if (temp == l) {
l->next = input;
} else {
temp->next = input;
}
temp = input;
input = new bookNode;
input->next = NULL;
}
}
double find_max_price(bookList *l) {
bookNode *temp = (*l)->next;
double max = 0;
while (temp != NULL) {
if (temp->price > max) {
max = temp->price;
}
temp = temp->next;
}
return max;
}
void display_max_prices(bookList *l, double max_price) {
bookNode *temp = *l;
while (temp != NULL) {
if (temp->price == max_price) {
cout << temp->id << " " << temp->name << " " << fixed << setprecision(2) << temp->price << endl;
}
temp = temp->next;
}
}
int main() {
int count;
bookList books = new bookNode;
double max_price;
books->next = NULL;
cin >> count;
create_with_count(books, count);
max_price = find_max_price(&books);
cout << max_price << endl;
display_max_prices(&books, max_price);
return 0;
}
//test4.cpp
#include <iomanip>
#include<iostream>
#include"LinkList.h" //LinkList类见实验一
using namespace std;
void create_with_count(bookList &l, int count) {
bookNode *input = new bookNode;
input->next = NULL;
bookNode *temp = l;
for (int i = 0; i < count; i++) {
cin >> input->id >> input->name >> input->price;
if (temp == l) {
l->next = input;
} else {
temp->next = input;
}
temp = input;
input = new bookNode;
input->next = NULL;
}
}
double find_max_price(bookList *l) {
bookNode *temp = (*l)->next;
double max = 0;
while (temp != NULL) {
if (temp->price > max) {
max = temp->price;
}
temp = temp->next;
}
return max;
}
void display_max_prices(bookList *l, double max_price) {
bookNode *temp = *l;
while (temp != NULL) {
if (temp->price == max_price) {
cout << temp->id << " " << temp->name << " " << fixed << setprecision(2) << temp->price << endl;
}
temp = temp->next;
}
}
int main() {
int count;
bookList books = new bookNode;
double max_price;
books->next = NULL;
cin >> count;
create_with_count(books, count);
max_price = find_max_price(&books);
cout << max_price << endl;
display_max_prices(&books, max_price);
return 0;
}