📄 linkmethods.cpp
字号:
#include <iostream>
#include "SupMakt.h"
using namespace std;
/////////////////////////////////////////////////////////////
////////////// link used .. /////////////
/////////////////////////////////////////////////////////////
// Save or read objects by link
// init the link
node *init(){
Foodstuff *ch;
node *temp;
node * header=(node *)malloc(sizeof(node));
header->next=NULL;
char chi='0';
cout << "Please enter 'Q' or 'q' to exit:" << endl;
Foodstuff * goods = new Foodstuff();
// Data define
char nm[35];
double pri;
double inpri;
int num;
char con_date[11];
int ht;
char sm[20];
int total_num = 0;
double total_pri = 0.0;
double in_total_pri = 0.0;
double profi =0.0;
double profied = 0.0;
while( chi != 'q' && chi != 'Q'){
// input one data
cout << "Name(35) :";
cin >> nm;
cout << endl;
cout << "In Price(亸) :";
cin >> inpri;
cout << endl;
cout << "Sell Price(亸) :";
cin >> pri;
cout << endl;
cout << "Number :";
cin >> num;
cout << endl;
cout << "Conserve date(****/**/**) :";
cin >> con_date;
cout << endl;
cout << "Have or not(0:None, 1:Have) :";
cin >> ht;
cout << endl;
cout << "This food tasted :";
cin >> sm;
cout << endl;
int kd = goods -> num_add() - 1;
ch = new Foodstuff(kd, nm, pri, num, con_date, ht, inpri,
total_num, total_pri, in_total_pri, profi, profied, sm);
//
// ch->number_add(num); // backer amount-number update
ch->TotalPrice(num); // Compute total_price
ch->TotalProfit(num); // Compute profit
ch->TotalNumber(num); // Compute total_number
// ch->Profited(); // Compute profited
ch->Cin_total_price(); // Compute in_total_price
temp=(node *)malloc(sizeof(node));
temp->data=ch;
temp->next=header->next;
header->next=temp;
cout << "Please enter 'Q' or 'q' to exit:" << endl;
//getchar(); // ATTENTION: delete rubbish char
fflush(stdin); // clear buffer
chi = getchar();
}
return header;
}
// Add one record
node *addRec(node * header){
Foodstuff *ch;
node *temp;
Foodstuff * goods = new Foodstuff();
// Data define
char nm[35];
double pri;
double inpri;
int num;
char con_date[11];
int ht;
char sm[20];
int total_num = 0;
double total_pri = 0.0;
double in_total_pri = 0.0;
double profi =0.0;
double profied = 0.0;
// input one data
cout << "Name(35) :";
cin >> nm;
cout << endl;
cout << "In Price(亸) :";
cin >> inpri;
cout << endl;
cout << "Sell Price(亸) :";
cin >> pri;
cout << endl;
cout << "Number :";
cin >> num;
cout << endl;
cout << "Conserve date(****/**/**) :";
cin >> con_date;
cout << endl;
cout << "Have or not(0:None, 1:Have) :";
cin >> ht;
cout << endl;
cout << "This food tasted :";
cin >> sm;
cout << endl;
//int kd = goods -> num_add();
int kd = link_size(header) + 100000 + 1;
ch = new Foodstuff(kd, nm, pri, num, con_date, ht, inpri,
total_num, total_pri, in_total_pri, profi, profied, sm);
// ch->number_add(num); // backer amount-number update
ch->TotalPrice(num); // Compute total_price
ch->TotalProfit(num); // Compute profit
ch->TotalNumber(num); // Compute total_number
// ch->Profited(); // Compute profited
ch->Cin_total_price(); // Compute in_total_price
temp = (node *)malloc(sizeof(node));
temp -> data = ch;
return temp;
}
// Search the smallest data in link (kind) and return this node
node * searchSmall(node * header){
node * temp;
temp = (node *)malloc(sizeof(node));
temp = header->next;
header = header->next;
while (header != NULL) {
if (temp->data->Kind()
> header->next->data->Kind()) {
temp = header->next;
}
header = header->next;
}
return temp;
}
// Search by kindNumber and return the node
node *search(node *header,int n){
node *p = header -> next;
while( p!=NULL){
if( p->data->Kind() == n )
break;
p = p->next;
}
return p;
}
// Delete by kind number and return new link
void deleteKind(node * header, int kind){
node * temp = search(header , kind);
node * p = header;
if (temp == NULL) {
cerr << "This node is not existence!" << endl;
return;
}
while (p->next != temp) {
p = p->next;
}
p->next = temp->next;
free(temp);
cerr << "Delete operation success!" << endl;
}
// Sort a link by bubble sort
void SortLink(node * head){
node * r, //= head,
* q, //= r->next,
* p; //= q->next;
bool changed = 0;
int len = link_size(head);
if ( len < 2 ){
cerr << "You have only record, can not sort!" << endl;
return ;
}
for ( int i = len - 1 ; i > 0 ; i-- ){
r = head;
q = r->next;
p = q->next;
changed = false;
for ( int j = 0; j < i; j++ ){
if ( q->data->Kind() > p->data->Kind() ){
r->next = p;
q->next = p->next;
p->next = q;
r = p;
p = q->next;
changed = true;
}else{
r = q;
q = p;
p = p->next;
}
}
if ( ! changed ){
break;
}
}
}
/*
void SortLink(node *link) {
node * pHead, * pRear, * p, * tp;
pHead = (node *)malloc(sizeof(node));
pRear = (node *)malloc(sizeof(node));
p = (node *)malloc(sizeof(node));
tp = (node *)malloc(sizeof(node));
if (!link) return;
for (pHead=link,pRear=NULL; pHead; pHead=pHead->next) {
for (tp=pHead,p=pHead->next; p; tp=p,p=p->next)
if (pHead->data->Kind() >= p->data->Kind()){
tp->next = p->next,
p->next = pHead,
pHead = p,
p = tp;
}
if (!pRear){
link=pHead;
}else{
pRear->next=pHead;
}
pRear=pHead;
}
}*/
// Display
void display(node *header){
if(header != NULL){
header = header -> next;
cout << "Kind" << "\t" << "Name" << "\t" << "Number" << "\t" << "InPrice" <<"\t" << "Price"
<< "\t" << "Conserve Date" << "\t" << "Has It" << "\t" << "SMELL :" << endl;
while(header!=NULL){
//putchar(header->data);
header -> data -> FoodstuffShow();
header = header -> next;
}
cout << endl;
}
}
// Free memory
void freeMemory(node *header){
node *p;
if(header != NULL){
p=header->next;
free(header);
freeMemory(p);
}
}
// Link size (objects)
int link_size( node *header ){
int size = 0;
node *p = header -> next;
while(p!=NULL){
size++;
p = p -> next;
}
return size;
}
// Delete all and free memory
void del_all(node *header){
node *temp;
node *p =header;
if(p->next == NULL){
printf("\n Data is not existed.\n");
}
while(p->next != NULL){
temp= p->next;
p->next=temp->next;
free(temp);
}
if(p->next == NULL)
printf("\n Success deleted!\n");
}
// File dispose st -->
node *data_load(){
FILE *fp;
node *p;
int n;
node *header=(node *)malloc(sizeof(node));
header -> next = NULL;
if((fp = fopen( "data.dat" , "rb")) == NULL ){
printf("Data read failer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -