要想使用指针访问一个容量会变的vector,安全的方法是使用使用智能指针。可以简单地把智能指针理解为管理指针的一个类,会自动帮你释放指针的内存。智能指针包含std::shared_ptr和std::unique_ptr;std::shared_ptr是可共享的智能指针,可拷贝,也可以作为返回值。std::unique_ptr独占指针,不能拷贝,但也可以作为返回值。在本篇博客中不对智能指针做过多的概述。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <iostream>
using namespace std;
struct bookinfo{
long long num;
char name [20];
float price;
};
struct Node {
struct bookinfo *tushuzhizhen;//=(bookinfo*)malloc(sizeof(bookinfo));
struct Node* next;
};
Node * creatNode(){
struct Node *Node1 =(Node*)malloc(sizeof(Node));
Node1->next=NULL;
return Node1;
}
Node * List=creatNode();
Node* ttemp=List;
Node* insert (bookinfo * tushu1,Node * headNode){
struct Node * tushu2=NULL;
tushu2=(Node*)malloc(sizeof(Node));
while ( headNode->next!=NULL){
headNode=headNode->next;
}
headNode->next=creatNode();
Node* temp1=headNode->next;
temp1->tushuzhizhen=tushu1;
temp1->tushuzhizhen->num=tushu1->num;
temp1->tushuzhizhen->price=tushu1->price;
}
void shudeshuru(bookinfo *temp){
Node* temp2=creatNode ();
temp2->tushuzhizhen=temp;
}
void savebook (const char * name, Node * book1){
FILE *fp=fopen (name,"w");
struct Node * temp1=book1->next;
while (temp1!=NULL)
{
fprintf(fp, "%s\t%.1f\t%lld\n", temp1->tushuzhizhen->name, temp1->tushuzhizhen->price, temp1->tushuzhizhen->num);
temp1=temp1->next;
}
fclose(fp);
}
void readInfoFromFile(const char* fileName, struct Node* headNode)
{
FILE* fp = fopen(fileName, "r");
if (fp == NULL)
{
//不存在就创建出来这个文件
fp = fopen(fileName, "w+");
}
struct bookinfo*tempData=(bookinfo*)malloc(sizeof(bookinfo));
// if(fscanf(fp, "%s\t%.1f\t%d\n", tempData ->name, tempData->price, tempData->num) == EOF){
// cout<<"您没有创建任何的书籍"<<endl;
// return;
//}
while (fscanf(fp, "%s\t%.1f\t%d\n", tempData ->name, tempData->price, tempData->num) != EOF)
{
insert(tempData,List);
}
fclose(fp);
}
void readbook (const char* name ,struct Node * headNode)
{
FILE *fp=fopen (name,"r");
if(fp==NULL){
fp=fopen (name,"w+");
return ;
}
struct bookinfo*tempbook=(bookinfo*)malloc(sizeof(bookinfo));
while (fscanf(fp,"%s\t%.1f\t%d\n",&tempbook->name,&tempbook->price,&tempbook->num)!=EOF)
{
insert(tempbook,List);
}
}
void print (Node * headnode1){
if(headnode1->next==NULL){
cout<<"您没有登记过任何书籍,请先登记书籍"<<endl;
return;
}
Node * headnode=headnode1->next;
printf("书名\t价格\t数量\n");
printf("%s\t%.1f\t%d\n",headnode->tushuzhizhen->name,headnode->tushuzhizhen->price,headnode->tushuzhizhen->num);
while ( headnode ->next!=NULL){
headnode=headnode ->next;
printf("%s\t%.1f\t%d\n",headnode->tushuzhizhen->name,headnode->tushuzhizhen->price,headnode->tushuzhizhen->num);
}
}
void makeMenu()
{
printf("------------------------------------\n");
printf(" xxoo图书管理系统\n");
printf("\t0.退出系统\n");
printf("\t1.登记书籍\n");
printf("\t2.浏览书籍\n");
printf("\t3.借阅书籍\n");
printf("\t4.归还书籍\n");
printf("\t5.书籍排序\n");
printf("\t6.删除书籍\n");
printf("\t7.查找书籍\n");
printf("------------------------------------\n");
printf("请输入(0~7):");
}
void xuanze (){
int choice;
cin>>choice;
bookinfo *tempbook=(bookinfo*)malloc(sizeof(bookinfo));
switch (choice){
case 0:
printf ("成功退出图书馆管理系统");
exit(1);
break;
case 1:
printf ("请输入要登记的书籍的名字,价格,编号");
scanf("%s%.1f%lld",&tempbook->name,&tempbook->price,&tempbook->num);
insert(tempbook,List);
savebook("BOOKBOOK.txt",List);//写入
printf ("您已经成功登记书籍");
break;
case 2:
printf ("书籍列表如下:------------");
print (List);
// readInfoFromFile("BOOKBOOK.txt", struct Node* headNode)
break;
}
}
int main (){
readInfoFromFile("BOOKBOOK.txt", List);
while (1){
makeMenu();
xuanze();
system("pause");
system("cls");
}
return 0;
}