#include<stdio.h>
#include<string.h>
#include"malloc.h"
int a[9];
struct node{
char no[40];/*编号*/
char name[60];/*名称*/
int num;/*借阅次数*/
struct node * link;/*指向下一节点的指针struct node * 形式的指针link*/
};
typedef struct node * nodePtr;//类型定义,讲struct node * 类型 声明为 nodePtr
nodePtr construct();//创建一个表示图书馆信息的单链表
void display(nodePtr head);//利用head头指针显示图书馆现有信息
void searchMaxMin(nodePtr head);//寻找最大最小
nodePtr addNew(nodePtr head);//添加新书
void getFish();
void threeCount();
void library();
int main()
{
int choice = 1;
while(choice != 0){
printf("*****************************************************************");
printf("\n\nThis is the main program, you can choose which to execute.\n");//选择要执行的程序
printf("0.Exit 1.ThreeCount 2.GetFish 3.Library \n");
scanf("%d", &choice);
switch(choice){
case 0:
break;//选择0,退出,并且因为不符合while中的条件而不再继续进行程序
case 1:
threeCount();
break;
case 2:
getFish();
break;
case 3:
library();
break;
}
}
return 0;
}
//********************************************************************
//******************************************************************
void getFish()
{
int n,i,x,flag=1; /*flag:控制标记*/
for(n=6;flag;n++) /*采用试探的方法。令试探值n逐步加大*/
{
for(x=n,i=1&&flag;i<=5;i++)
{
if((x-1)%5==0)
x=4*(x-1)/5;
else
flag=0; /*若不能分配则置标记falg=0退出分配过程*/
}
if(flag)
break; /*若分配过程正常结束则找到结果退出试探的过程*/
else
flag=1; /*否则继续试探下一个数*/
}
printf("他们至少捕了%d条鱼\n",n); /*输出结果*/
}
//********************************************************************
//****************************************************************************
void threeCount()
{
int suit(int i,int *j) /*分解i的值,将其存入j指向的三个数组元素,若满足要求返回1*/
{
int *p1,*p2;
for(p1=j;p1<j+3;p1++)
{
*p1=i%10; /*分解整数*/
i/=10;
for(p2=a;p2<p1;p2++) /*查询分解出的数字是否已经出现过*/
if(*p1==0||*p2==*p1)
return 0; /*若重复则返回*/
}
return 1; /*否则返回1*/
}
int n,count=0;
for(n=123;n<=333;n++)/*试探可能的三位数*/
{
if(suit(n,a)&&suit(2*n,a+3)&&suit(3*n,a+6)) /*若满足题意即int suit返回1时*/
printf("No.%d\t: 第一个数:%d 第二个数:%d 第三个数:%d\n",++count,n,2*n,3*n); /*输出结果*/
}
}
//*********************************************
//**********************************************
void library(){
int choice = 0;
printf("*******************************************************\n\n\n");
printf("\t\t*欢迎使用图书借阅管理系统*\n");
printf("\n\n*******************************************************\n\n\n");
nodePtr head = construct();//head是整个图书馆链表的头指针,首先进行图书信息综合整理
nodePtr tmp = NULL;//
do{
printf("\n\n\n选择功能: 1 - 查看图书信息 2 - 搜索借阅最多最少 3 - 添加新书信息 其他 - 退出\n");
scanf("%d", &choice);
switch(choice){//系统功能选择
case 1:
display(head);
break;
case 2:
searchMaxMin(head);
break;
case 3:
tmp = addNew(head);//加到头
if(tmp == NULL){//插入地址在原信息中不存在
printf("没有此书!\n");
}
else{
printf("添加成功!\n");
head = tmp;
}
break;
default:
break;
}
}while(choice == 1 || choice == 2 || choice == 3);
}
// 图书馆控制程序
nodePtr construct(){
int n = 0;
int i = 1;
nodePtr tmpNode = NULL;//初始化插入节点为空
char tmpNo[40];//插入节点的书号
char tmpName[60];//插入节点的书名
int tmpNum = 0;//插入节点的借阅次数
nodePtr head = NULL;//初始化头指针为空
nodePtr current = NULL;
printf("请输入图书总量:\n");
scanf("%d", &n);
if(n <= 0){
printf("您输入的数值有误!\n");
}
else{
printf("请输入各图书信息:\n");
while(i <= n){
printf("请输入第 %d 种图书的编号,名称,借阅次数,以空格分隔:\n", i);
scanf("%s %s %d", tmpNo, tmpName, &tmpNum);
tmpNode = (nodePtr)malloc(sizeof(struct node));//内存分配
strcpy(tmpNode->no, tmpNo);//复制tmpNo到tmpNode
strcpy(tmpNode->name, tmpName);
tmpNode->num = tmpNum;
if(i == 1){
head = tmpNode;
current = head;
}
else{
current->link = tmpNode;
current = tmpNode;
}
i ++;
}
current->link = NULL;
return head;
}
}
// display the library book infomation
void display(nodePtr head){
nodePtr current = head;
printf("图书信息如下:\n");
for(; current != NULL; current = current->link/*指向下一个执行输出*/){
printf("%s\t %s\t 借阅了 %d 次\n", current->no, current->name, current->num);
}
}
// search the nodes which have maximum or minimum times of lending
void searchMaxMin(nodePtr head){
nodePtr current = head;
int max = head->num;
int min = head->num;
// find the first maimum and first minimum nodes
for(; current != NULL; current = current->link){
if(current->num > max){
max = current->num;
}
if(current->num < min){
min = current->num;
}
}
// find and print all maximum and minimum nodes, maybe not just one
for(current = head; current != NULL; current = current->link){
if(current->num == max){
printf("借阅次数最多的图书的编号和名称: %s\t %s\t 借阅了 %d 次\n", current->no, current->name, current->num);
}
if(current->num == min){
printf("借阅次数最少的图书的编号和名称: %s\t %s\t 借阅了 %d 次\n", current->no, current->name, current->num);
}
}
}
// add a new book
nodePtr addNew(nodePtr head){
char tmpNo[40];
char newNo[40];//为不破坏原有节点,重新定义新的结点
char newName[60];
int newNum = 0;
int hasIt = 0;
int pos = 0;
nodePtr newNode = NULL;
nodePtr current = head;
nodePtr front = head;
int i = 2;
printf("请输入需要插入的位置代表的的图书的编号:\n");
scanf("%s", tmpNo);
//search and judge whether aim exists//查询并判断目标位置是否存在
for(; current!= NULL; current = current->link){
pos ++;
if((strcmp(current->no, tmpNo)) == 0){//字符串比较,检测是否存在当前输入位置
hasIt = 1;
break;
}
}
if(hasIt == 0){
return NULL;
}
else{
printf("请输入新书的编号,名称,借阅次数,以空格相隔\n");
scanf("%s %s %d", newNo, newName, &newNum);
newNode = (nodePtr)malloc(sizeof(struct node));
strcpy(newNode->no, newNo);
strcpy(newNode->name, newName);
newNode->num = newNum;
if(pos == 1){
newNode->link = head;
head = newNode;
}
else{
for(;i != pos; i++){
front = front->link;
}
front->link = newNode;
newNode->link = current;
}
return head;
}
}
C语言程序设计-图书管理系统
最新推荐文章于 2024-09-11 12:45:41 发布