《C语言程序设计》实验报告
指导教师 纪良浩 学院 通信与信息工程 专业 通信类
班级 0101022 学号 2010210722 姓名 曾小兵 实验室 S319
实验题目:
实 验 一:用选择法对10个整数进行由大到小的排序
实 验 二:判断闰年
实 验 三:求1到100的和
实 验 四:求π的近似值
实验要求:
1.掌握用C语言解决实际问题的步骤;
2.掌握C语言的语法规则及算法的表示;
3.通过上机实验,加深对《计算机程序设计基础》课程中基本概念的理解;
4.提高对计算机程序的编写和应用能力;
实验内容及步骤:
1.问题的提出与分析;
2.建立计算模型;
3.算法的确定与表示;
4.编写程序代码;
5.调试程序;
6.整理结果(实验结果及分析)。
具体实验步骤
实验一:实验步骤
1.问题的提出与分析
用选择法对10个整数进行由大到小的排序。
2.确定数学模型
a>b>c>d>e>f>j>h>i>k
3.算法
4.程序编码
#include<stdio.h>
void main()
{void sort(int x[ ],int n);
int*p,i,a[10];
p=a;
for(i=0;i<10;i++)
scanf("%d",p++);
p=a;
sort(p,10);
for(p=a,i=0;i<10;i++)
{printf("%d",* p);p++;}
}
void sort(int x[],int n)
{int i,j,k,t;
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(x[j]>x[k]) k=j;
if(k!=i)
{t=x[i];x[i]=x[k];x[k]=t;}
}
}
5.调试程序
Enter data
a[1]=10↙
a[2]=25↙
a[3]=45↙
a[4]=66↙
a[5]=88↙
a[6]=110↙
a[7]=119↙
a[8]=114↙
a[9]=86↙
a[10]=886
10 25 45 66 88 110 119 114 86 886
The sorted numbers:
886 119 114 110 88 86 66 45 25 10
6.整理结果
通过验算结果正确。
实验二:实验步骤
1.问题的提出与分析
随意给出一年,判断其是否为闰年。
2.确定数学模型
闰年必为4的倍数且不是100的倍数,或是400的倍数。
3.程序编码
#include<stdio.h>
void main()
{
int year;
scanf("%d",&year);
if(year/4==0&&year/100!=0)
printf("%d is a leap year.\n",year);
else if(year/400==0)
printf("%d is a leap year.\n",year);
else printf("%d is not a leap year.\n",year);
}
5.调试程序
经过上机调试,运行情况如下:
Enter
① 2000
2000 is a leap year.
② 2010
2010 is not a leap year.
6.整理结果
通过验算结果正确。
实验三:实验步骤
1. 问题的提出与分析
从1一直加到100.
2. 确定数学模型
3. 算法
4.程序编码
#include<stdio.h>
void main()
{
int i,sum=0;
for(i=1;i<=100;i++)sum+=i;
printf("%d\n",sum);
}
5.调试程序
经过上机调试,运行情况如下:
5050
6.整理结果
通过验算结果正确。
实验四:实验步骤
1.问题的提出与分析
怎样求无限不循环小数π的近似值。
2.确定数学模型
用公式:π/4≈1-1/3+1/5-1/7+…
3. 算法:
4.程序编码
#include<stdio.h>
#include<math.h>
void main()
{
int s;
float n,t,pi;
t=1;pi=0;n=1.0;s=1;
while(fabs(t)>1e-6)
{
pi=pi+t;
n=n+2;
s=-s;
t=s/n;
}
pi=pi*4;
printf("pi=%10.6f\n",pi);
}
5.调试程序
经过上机调试,运行情况如下:
Pi=3.141593
6.整理结果:
通过验算结果正确。
心得体会:
通过该实验,我对分支(if,if``else,if```else if```)和循环结构(for,while)的几种用法更加熟练,并掌握了函数的定义与调用,参数说明以及返回值使用。并对C程序设计有了总体的认识。
第二篇:c语言 图书管理系统 上机实验报告
成都信息工程大学计算机系
课程实验报告
一【上机实验目的】
设计并实现该课程设计的目的主要在于:
1) 对一定规模的综合软件编程有一定的经历与认识。在做的过程中,你会发现,提前的规划即分析与设计重要过编程过程,否则会走很多弯。
2) 综合并结合现实应用使用C语言的知识。这个设计会用到C语言这门课的全部知识,其中以文件、数组与链表为主,书中提到的主要及重点算法都会使用到。
3) 不仅涉及编程,还涉及到功能分析、模块规划等方面的知识,这些知识在后续课程学习时,会经常使用。这些知识在学《软件工程》这门课时,会系统讲解。希望有了这次的经历,你能更好的体会《软件工程》这门课的意义,并能学好和用好其中的知识 。
4) 每项功能的实现,一般有多种方法,这里都强调使用时空效率最高的方法,此次实现只是让你有一个初步的认识,在下学期学习了《数据结构》后,你就知道为什么让你这样做,你也对《数据结构》这门课的作用及意义有所了解。
二【实验环境】
PC机每人1台
三【上机实验内容】
编写图书管理系统,具有以下基本功能
(1) 各种基本数据的录入。如:图书资料基本信息录入等。
(2) 各种基本数据的修改。即:允许对以及录入的数据重新进行编辑、修改。
(3) 各种基本数据的插入。如:在图书采购信息中插入一条新信息等。
(4) 各种基本数据的删除。如:假设某本书遗失且馆藏数为0,删除该书的相关信息等。
(5) 基于各种数据的查询。如:书名中含有“计算机”的所有书籍、全部借出的所有图书等。
(6) 基于各种基本数据的统计计算。
四【上机调试程序流程图】(注:可打印)
五【上机调试中出现的错误信息、错误原因及解决办法】
读写错误:fopen的时候改成用二进制读写.目前不知道为什么不用二进制不能读写.
六【上机调试后的源程序及还存在的问题】
#include <stido.h>
#include <dos.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define Key_UP 72
#define Key_DOWN 80
#define Key_ENTER 28
#define Key_N 49
#define Key_Y 21
#define LEN sizeof(struct linklist)
typedef struct book1 /*图书基本信息结构体*/
{
char name[20];
char writer[30];
char type[20];
char num[20];
char time[20];
char press[20];
char edition[20];
char ISBN[40];
float price;
int count;
int lendnum;
}bbasic;
/*
图书资料基本信息:中图法分类号、图书编号、书名、作者(要考虑多个作者情况)、出版社、出版日期、ISBN、版次、定价、馆藏数、借阅数等。
*/
typedef struct book2 /*图书采购信息结构体*/
{
char name[20];
char writer[20];
char time[15];
int num;
float price;
float money;
char bill [30];
}bpurchase;
/*
图书采购信息:书名、作者、采购日期、采购数量、采购单价、采购金额、发票号码、图书编号等。
*/
typedef struct book3 /*图书借阅信息结构体*/
{
/***************需要输入的信息*********/
char name[20];
char person[20];
char company[20];
char num[20]; /*借书证号*/
char btime[15];
/**************运算可得信息************/
char rtime[15];
char fine[20];
}blend;
/*
图书借阅信息:借阅人、借阅人所在单位、借书证号、所借书名、借阅日期、归还日期、逾期罚款等。借阅期限为一个月,逾期1天,罚款1角。
*/
struct linklist
{
bbasic binfo;
struct linklist *next;
};
int key();
void menu();
void _window();
void words();
void box(int,int,int,int);
int _choose(int bot, int top);
void play(int);
int readsum();
void add();
void addbook();
void addbuy();
void addborrow();
void manage();
struct linklist* create();
void modify();
void delete();
void insert();
void search();
void searchname();
void searchwriter();
void orderw(bbasic binfo[100]);
void orderw(bbasic binfo[100]);
int halfn(int sum,bbasic binfo[100],char *find);
int halfw(int sum,bbasic binfo[100],char *find);
void sta();
void blist();
void bmoney();
int main(void)
{
menu();
}
void menu()
{
int y;
_window(); /*显示窗口*/
words(); /*显示菜单文字*/
y = choose(7,19); /*光标*/
play(y); /*根据键盘值操作*/
}
void _window() /*窗口初始化*/
{
textbackground(BLUE);
textcolor(WHITE);
clrscr();
box(1,1,24,80);
}
void words() /*显示菜单文字*/
{
textcolor(WHITE);
gotoxy(30,7);
textbackground(RED);
cprintf("Add Book info");
textbackground(BLUE);
gotoxy(30,10);
cprintf("Manage Book");
gotoxy(30,13);
cprintf("Search Book");
gotoxy(30,16);
cprintf("Book Statistic");
gotoxy(30,19);
cprintf("Exit");
gotoxy(35,22);
textcolor(LIGHTGRAY);
textbackground(BLUE);
cprintf(" Main Menu ");
gotoxy(25, 2);
textcolor(RED);
highvideo();
cprintf("The Library Managemnt Program");
normvideo();
}
void box(int x,int y, int high, int width) /*画方框*/
{
int i;
gotoxy(x,y);
putchar(0xda);
for (i = 1 ; i < width - 1; i++)
{
putchar(0xc4);
}
putchar(0xbf);
gotoxy(x, y + high - 3);
putchar(0xc0);
gotoxy(width, y + high - 3);
putchar(0xd9);
for (i = 1; i < high - 1; i++)
{
gotoxy(x,y+i);
putchar(0xb3);
gotoxy(x + width -1, y+i);
putchar(0xb3);
}
gotoxy(x, y + high - 1);
putchar(0xc0);
for (i = 1 ; i < width - 1; i++)
{
putchar(0xc4);
}
gotoxy(x + 1, y + high - 3);
for (i = 1; i < width - 1; i++)
{
putchar(0xc4);
}
gotoxy(x + width-1 ,y + high -1);
putchar(0xd9);
}
int key() /*读键盘*/
{
union REGS rg;
rg.h.ah = 0;
int86(0x16, &rg, &rg);
return rg.h.ah;
}
int choose(int bot,int top) /*根据Y的值选择操作*/
{
int ky,y = 7;
gotoxy(30,bot);
do
{
ky = key();
switch(ky)
{
case Key_UP:
{
if (y > bot)
{
upbar(y);
y = y - 3;
}
};break;
case Key_DOWN:
{
if (y < top)
{
downbar(y);
y = y + 3;
}
};break;
}
}
while(ky != Key_ENTER);
return y;
}
upbar(int y) /*光标上移*/
{
int i;
typedef struct texel_struct
{
unsigned char ch;
unsigned char attr;
}texel;
texel t;
for(i=30;i<=48;i++)
{
gettext(i,y,i,y,&t);
t.attr=0x1f;
puttext(i,y,i,y,&t);
gettext(i,y-3,i,y-3,&t);
t.attr=0x4f;
puttext(i,y-3,i,y-3,&t);
};
gotoxy(30,y-3);
return;
}
downbar(int y) /*光标下移*/
{
int i;
typedef struct texel_struct
{
unsigned char ch;
unsigned char attr;
}texel;
texel t;
for(i=30;i<=48;i++)
{
gettext(i,y,i,y,&t);
t.attr=0x1f;
puttext(i,y,i,y,&t);
gettext(i,y+3,i,y+3,&t);
t.attr=0x4f;
puttext(i,y+3,i,y+3,&t);
};
gotoxy(30,y+3);
return;
}
void play(int y) /*根据y的值选择操作*/
{
switch(y)
{
case 7:add();
break;
case 10:manage();
break;
case 13:search();
break;
case 16:sta();
break;
case 19:exit(1);
default:printf("Error");exit(0);
}
}
void add() /*显示 "选择增加那种类型的信息" 的窗口*/
{
int y;
_window();
gotoxy(30,7);
textbackground(RED);
cprintf("Add Basic info");
textbackground(BLUE);
gotoxy(30,10);
cprintf("Add Purchase info");
gotoxy(30,13);
cprintf("Add Borrow info");
gotoxy(30,16);
cprintf("Back");
gotoxy(25, 2);
textcolor(RED);
highvideo();
cprintf("The Library Managemnt Program");
normvideo();
gotoxy(35,22);
textcolor(LIGHTGRAY);
textbackground(BLUE);
cprintf(" Add Book ");
textbackground(BLUE);
textcolor(WHITE);
y=choose(7,16);
switch(y) /*选择增加信息类型的函数*/
{
case 7 :addbook();break; /*增加图书基本信息*/
case 10:addbuy();break; /*增加图书购买信息*/
case 13:addborrow();break; /*增加图书借阅信息*/
case 16:menu();break;
default:printf("Error");break;
}
}
void addbook() /*增加图书基本信息*/
{
int ky, sum = 0;
FILE *fp;
bbasic binfo;
textbackground(BLUE);
textcolor(WHITE);
clrscr();
if((fp = fopen("e:\\sum.dat","rb")) != NULL) /*读取图书基本信息数量sum*/
{
fread(&sum,2,1,fp);
}
fclose(fp);
if ((fp = fopen("e:\\addbook.dat","ab")) == NULL)
{
printf("cannot write the addbook.dat");
}
do
{
clrscr();
gotoxy(2,2);
cprintf("Please input the classfication of chinese library classification:");
scanf("%s",&binfo.type);
getchar();
gotoxy(2,4);
cprintf("Please input the book's number:");
scanf("%s",&binfo.num);
getchar();
gotoxy(2,6);
cprintf("Please input the book's name:");
scanf("%s",&binfo.name);
getchar();
gotoxy(2,8);
cprintf("Please input the book's writer:");
scanf("%s",&binfo.writer);
getchar();
gotoxy(2,10);
cprintf("Please input the the book's press:");
scanf("%s",&binfo.press);
getchar();
gotoxy(2,12);
cprintf("Please input the book's edition:");
scanf("%s",&binfo.edition);
getchar();
gotoxy(2,14);
cprintf("Please input the book's publish time:");
scanf("%s",&binfo.time);
getchar();
gotoxy(2,16);
cprintf("Please input the price:");
scanf("%f",&binfo.price);
getchar();
gotoxy(2,18);
cprintf("Please input the ISBN of the book:");
scanf("%s",&binfo.ISBN);
getchar();
gotoxy(2,20);
cprintf("Please input the count of books:");
scanf("%d",&binfo.count);
getchar();
gotoxy(2,22);
cprintf("Please input the lend number of the book:");
scanf("%d",&binfo.lendnum);
getchar();
gotoxy(10,25);
cprintf("Press N(n) to finish add book or Press any key to add more book.");
ky = key();
sum++;
if(fwrite(&binfo,sizeof(bbasic),1,fp) != 1)
{
clrscr();
printf("cannot save the data.");
}
}while(ky != Key_N);
fclose(fp);
if ((fp = fopen("e:\\sum.dat","wb")) == NULL)
{
printf("cannot save the sum of the book.");
getch();
exit(0);
}
if(fwrite(&sum,2,1,fp) != 1)
{
printf("fail to write the sum.dat.");
getch();
exit(0);
};
fclose(fp);
menu();
}
/* 中图法分类号、(要考虑多个作者情况)、出版社、出版日期、ISBN、版次、定价、馆藏数、借阅数等。 */
void addbuy() /*增加图书购买信息*/
{
int ky;
FILE *fp;
bpurchase pinfo;
textbackground(BLUE);
textcolor(WHITE);
clrscr();
if((fp = fopen("e:\\addbuy.dat","ab")) == NULL)
{
printf("cannot open the addbuy data.");
exit(0);
}
do
{
clrscr();
gotoxy(2,2);
cprintf("Please input the book's name:");
scanf("%s",&pinfo.name);
gotoxy(2,4);
cprintf("Please input the book's writer:");
scanf("%s",&pinfo.writer);
gotoxy(2,6);
cprintf("Please input the book's buy time:");
scanf("%s",&pinfo.writer);
gotoxy(2,8);
cprintf("Please input the number of the book buy:");
scanf("%d",&pinfo.num);
gotoxy(2,10);
cprintf("Please input the price:");
scanf("%f",&pinfo.price);
gotoxy(2,12);
cprintf("Please input the real money of buy the book:");
scanf("%f", &pinfo.money);
gotoxy(2,14);
cprintf("Please input the bill number:");
scanf("%s", &pinfo.bill);
gotoxy(10,20);
cprintf("Press N(n) to finish add book or Press any key to add more book.");
ky = key();
if(fwrite(&pinfo,sizeof(pinfo),1,fp) != 1)
{
clrscr();
printf("cannot save the data.");
}
}while(ky != Key_N);
fclose(fp);
menu();
}
void addborrow() /*增加图书借阅信息*/
{
int ky;
FILE *fp;
blend linfo;
textbackground(BLUE);
textcolor(WHITE);
clrscr();
if((fp = fopen("e:\\addborrow.dat","ab")) == NULL)
{
printf("cannot open the addborrow data.");
exit(0);
}
do
{
clrscr();
gotoxy(2,2);
cprintf("Please input the lend book's name:");
scanf("%s",&linfo.name);
gotoxy(2,4);
cprintf("Please input the borrower's name:");
scanf("%s",&linfo.person);
gotoxy(2,6);
cprintf("Please input the company of the borrower:");
scanf("%s",&linfo.company);
gotoxy(2,8);
cprintf("Please input the borrower's card:");
scanf("%s", &linfo.num);
gotoxy(2,10);
cprintf("Please input the borrow time(ex.2010/01/26):");
scanf("%s", &linfo.btime);
gotoxy(2,12);
cprintf("Please input the return time(ex.2010/05/08):");
scanf("%s", &linfo.rtime);
gotoxy(10,20);
cprintf("Press N(n) to finish add book or Press any key to add more book.");
ky = key();
if(fwrite(&linfo,sizeof(linfo),1,fp) != 1)
{
clrscr();
printf("cannot save the data.");
}
}while(ky != Key_N);
fclose(fp);
menu();
}
struct linklist* create() /*创建链表*/
{
int i, sum;
struct linklist *head, *p1, *p2;
FILE *fp;
sum = readsum();
p1 = (struct linklist*)malloc(LEN);
head = p2 = p1;
if ((fp = fopen("e:\\addbook.dat","rb")) == NULL)
{
printf("cannot open the file.");
}
for (i = 0; i < sum; i++)
{
p2 = p1;
p1 = (struct linklist*)malloc(LEN);
p2 -> next = p1;
if(fread(&(p2 -> binfo),sizeof(bbasic),1,fp) != 1)
{
printf("cannot save the data.");
}
}
p2 -> next = NULL;
fclose(fp);
return head;
}
void modify()
{
struct linklist* p,*head;
char find[30];
int i,ky,sum;
FILE *fp;
p= head = create();
sum = readsum();
clrscr();
printf("Plese input the name of the book you want to modify:");
scanf("%s",find);
for (i = 0;i<sum;i++)
{
if(strcmp(find,p -> binfo.name) == 0)
{
gotoxy(2,2);
cprintf("Please input the classfication of chinese library classification:");
scanf("%s",&(p -> binfo.type));
getchar();
gotoxy(2,4);
cprintf("Please input the book's number:");
scanf("%s",&(p -> binfo.num));
getchar();
gotoxy(2,6);
cprintf("Please input the book's name:");
scanf("%s",&(p -> binfo.name));
getchar();
gotoxy(2,8);
cprintf("Please input the book's writer:");
scanf("%s",&(p -> binfo.writer));
getchar();
gotoxy(2,10);
cprintf("Please input the the book's press:");
scanf("%s",&(p -> binfo.press));
getchar();
gotoxy(2,12);
cprintf("Please input the book's edition:");
scanf("%s",&(p -> binfo.edition));
getchar();
gotoxy(2,14);
cprintf("Please input the book's publish time:");
scanf("%s",&(p -> binfo.time));
getchar();
gotoxy(2,16);
cprintf("Please input the price:");
scanf("%f",&(p -> binfo.price));
getchar();
gotoxy(2,18);
cprintf("Please input the ISBN of the book:");
scanf("%s",&(p -> binfo.ISBN));
getchar();
gotoxy(2,20);
cprintf("Please input the count of books:");
scanf("%d",&(p -> binfo.count));
getchar();
gotoxy(2,22);
cprintf("Please input the lend number of the book:");
scanf("%d",&(p -> binfo.lendnum));
getchar();
printf("Press Y to continue or Press anykey to back to menu.");
ky = key();
if(ky == Key_Y)
{
p = head;
i = -1;
clrscr();
printf("Plese input the name of the book you want to modify:");
scanf("%s",find);
}
else
{
p = head;
if ((fp = fopen("e:\\addbook.dat","wb")) == NULL)
{
printf("cannot open addbook.dat");
}
for (i = 0;i <sum ;i++)
{
if(fwrite(&(p -> binfo),sizeof(bbasic),1,fp) != 1)
{
clrscr();
printf("cannot save the data.");
}
p = p -> next;
}
fclose(fp);
menu();
}
}
p = p -> next;
}
clrscr();
printf("cannot find the book.");
getch();
}
void delete()
{
struct linklist* p1,*p2,*head;
char find[30];
int i,ky,sum,flag = 0;
FILE *fp;
p1 = p2 = head = create();
sum = readsum();
clrscr();
printf("Plese input the name of the book you want to Delete:");
scanf("%s",find);
if(strcmp(find,head -> binfo.name) == 0)
{
head = head ->next;
sum--;
flag = 1;
}
else
{
for (i = 0;i< sum ;i++)
{
p1 = p1 -> next;
if(strcmp(find,p1 -> binfo.name) == 0)
{
p1 = p1 -> next;
p2 -> next = p1;
sum --;
flag = 1;
break;
}
else
{
p2 = p2 -> next;
}
}
}
if (flag != 1)
{
printf("cannot find the book.");
getch();
menu();
}
p1 = head;
if ((fp = fopen("e:\\addbook.dat","wb")) == NULL)
{
printf("cannot open addbook.dat");
}
for (i = 0;i <sum ;i++)
{
if(fwrite(&(p1 -> binfo),sizeof(bbasic),1,fp) != 1)
{
clrscr();
printf("cannot save the data.");
}
p1 = p1 -> next;
}
fclose(fp);
if((fp = fopen("e:\\sum.dat","wb")) == NULL)
{
printf("cannot open sum.dat");
}
if(fwrite(&sum,2,1,fp) != 1)
{
printf("fail to write the sum.dat.");
getch();
exit(0);
}
fclose(fp);
printf("succeed to delete the book.press anykey to get back to the mainmenu.");
getch();
}
void insert()
{
struct linklist* p1,*p2, *head, *temp;
char find[10];
int i,ky,sum,flag = 0;
FILE *fp;
clrscr();
sum = readsum();
p1 = p2 = head = create();
temp = (struct linklist*)malloc(LEN);
printf("The number you want to insert behind:");
scanf("%s",find);
for(i = 0;i< sum; i++)
{
p1 = p1->next;
if(strcmp(find,p2 -> binfo.num) == 0)
{
p2 -> next = temp;
temp -> next =p1;
sum ++;
flag = 1;
break;
}
else
{
p2 = p2->next;
}
}
if (flag != 1)
{
printf("cannot find the book.");
getch();
menu();
}
clrscr();
gotoxy(2,2);
cprintf("Please input the classfication of chinese library classification:");
scanf("%s",&(temp -> binfo.type));
getchar();
gotoxy(2,4);
cprintf("Please input the book's number:");
scanf("%s",&(temp -> binfo.num));
getchar();
gotoxy(2,6);
cprintf("Please input the book's name:");
scanf("%s",&(temp -> binfo.name));
getchar();
gotoxy(2,8);
cprintf("Please input the book's writer:");
scanf("%s",&(temp -> binfo.writer));
getchar();
gotoxy(2,10);
cprintf("Please input the the book's press:");
scanf("%s",&(temp -> binfo.press));
getchar();
gotoxy(2,12);
cprintf("Please input the book's edition:");
scanf("%s",&(temp -> binfo.edition));
getchar();
gotoxy(2,14);
cprintf("Please input the book's publish time:");
scanf("%s",&(temp -> binfo.time));
getchar();
gotoxy(2,16);
cprintf("Please input the price:");
scanf("%f",&(temp -> binfo.price));
getchar();
gotoxy(2,18);
cprintf("Please input the ISBN of the book:");
scanf("%s",&(temp -> binfo.ISBN));
getchar();
gotoxy(2,20);
cprintf("Please input the count of books:");
scanf("%d",&(temp -> binfo.count));
getchar();
gotoxy(2,22);
cprintf("Please input the lend number of the book:");
scanf("%d",&(temp -> binfo.lendnum));
getchar();
gotoxy(10,25);
cprintf("Press Y to continue or Press anykey to back to menu.");
ky = key();
if(ky == Key_Y)
{
insert();
}
else
{
p1 = head;
if ((fp = fopen("e:\\addbook.dat","wb")) == NULL)
{
printf("cannot open addbook.dat");
}
for (i = 0;i <sum ;i++)
{
if(fwrite(&(p1 -> binfo),sizeof(bbasic),1,fp) != 1)
{
clrscr();
printf("cannot save the data.");
}
p1 = p1 -> next;
}
fclose(fp);
if((fp = fopen("e:\\sum.dat","wb")) == NULL)
{
printf("cannot open sum.dat");
}
if(fwrite(&sum,2,1,fp) != 1)
{
printf("fail to write the sum.dat.");
getch();
exit(0);
}
fclose(fp);
menu();
}
}
void manage() /* 图书管理 */
{
int y;
_window();
gotoxy(30,7);
textbackground(RED);
cprintf("Modify Book");
textbackground(BLUE);
gotoxy(30,10);
cprintf("Delete Book");
gotoxy(30,13);
cprintf("Insert Book");
gotoxy(30,16);
cprintf("Back");
gotoxy(25, 2);
textcolor(RED);
highvideo();
cprintf("The Library Managemnt Program");
normvideo();
gotoxy(35,22);
textcolor(LIGHTGRAY);
textbackground(BLUE);
cprintf(" Manage Book ");
textbackground(BLUE);
textcolor(WHITE);
y=choose(7,16);
switch(y)
{
case 7 :modify();menu();break;
case 10:delete();menu();break;
case 13:insert();menu();break;
case 16:menu();
default:printf("Error");break;
}
}
void search()
{
int y;
_window();
gotoxy(30,7);
textbackground(RED);
cprintf("Search by Name");
textbackground(BLUE);
gotoxy(30,10);
cprintf("Search by Writer");
gotoxy(30,13);
cprintf("Back");
gotoxy(25, 2);
textcolor(RED);
highvideo();
cprintf("The Library Managemnt Program");
normvideo();
gotoxy(35,22);
textcolor(LIGHTGRAY);
textbackground(BLUE);
cprintf(" Search Book ");
textbackground(BLUE);
textcolor(WHITE);
y=choose(7,13);
switch(y)
{
case 7 :searchname();break;
case 10:searchwriter();break;
case 13:menu();
default:printf("Error");break;
}
}
int readsum()
{
FILE *fp;
int sum;
if((fp = fopen("e:\\sum.dat","rb")) != NULL) /*读取图书基本信息数量sum*/
{
fread(&sum,2,1,fp);
}
else
{
clrscr();
printf("cannot open the sum.dat");
getch();
exit(0);
}
fclose(fp);
return sum;
}
void orderw(bbasic binfo[100])
{
FILE *fp;
int sum,i ,j;
bbasic temp;
sum = readsum(); /*读取图书基本信息数量sum*/
if((fp = fopen("e:\\addbook.dat","rb")) ==NULL) /*读取文件中所存的图书基本信息*/
{
printf("cannot open the addbook.dat");
getch();
exit(0);
}
for (i = 0;i < sum ; i++)
{
if (fread(&binfo[i],sizeof(temp),1,fp) != 1)
{
printf("cannot read the addbook.dat.");
getch();
exit(0);
}
}
fclose(fp);
for (j = 1; j < sum; j++) /* 插入排序 */
{
temp = binfo[j];
i = j - 1;
while (i >= 0 && (strcmp(binfo[i].writer, temp.writer) > 0))
{
binfo[i + 1] = binfo[i];
i --;
}
binfo[i + 1] = temp;
}
}
void ordern(bbasic binfo[100])
{
FILE *fp;
int sum,i ,j;
bbasic temp;
sum = readsum(); /*读取图书基本信息数量sum*/
if((fp = fopen("e:\\addbook.dat","rb")) ==NULL) /*读取文件中所存的图书基本信息*/
{
printf("cannot open the addbook.dat");
getch();
exit(0);
}
for (i = 0;i < sum ; i++)
{
if (fread(&binfo[i],sizeof(temp),1,fp) != 1)
{
printf("cannot read the addbook.dat.");
getch();
exit(0);
}
}
fclose(fp);
for (j = 1; j < sum; j++) /* 插入排序 */
{
temp = binfo[j];
i = j - 1;
while (i >= 0 && (strcmp(binfo[i].name, temp.name) > 0))
{
binfo[i + 1] = binfo[i];
i --;
}
binfo[i + 1] = temp;
}
}
int halfn(int sum,bbasic binfo[100],char *find) /*二分法查找书名,返回所需数组标号*/
{
int start = 0,mid;
int end = sum - 1;
while(start < end)
{
mid = (start + end)/2;
if (strcmp(find,binfo[mid].name) > 0)
{
start = mid + 1;
}
else if (strcmp(find,binfo[mid].name) < 0)
{
end = mid - 1;
}
else return mid;
}
if (strcmp(find,binfo[start].name) == 0)
{
return start;
}
else
{
return -1;
}
}
int halfw(int sum,bbasic binfo[100],char *find) /*二分法查找作者,返回所需数组标号*/
{
int start = 0,mid;
int end = sum - 1;
while(start < end)
{
mid = (start + end)/2;
if (strcmp(find,binfo[mid].writer) > 0)
{
start = mid + 1;
}
else if (strcmp(find,binfo[mid].writer) < 0)
{
end = mid - 1;
}
else return mid;
}
if (strcmp(find,binfo[start].writer) == 0)
{
return start;
}
else
{
return -1;
}
}
void searchname()
{
FILE *fp;
int sum, obj,ky;
long i;
char find[30];
bbasic binfo[100];
textbackground(BLUE);
textcolor(WHITE);
clrscr();
sum = readsum(); /*读取图书基本信息数量sum*/
if((fp = fopen("e:\\addbook.dat","rb")) ==NULL) /*读取文件中所存的图书基本信息*/
{
printf("cannot open the addbook.dat");
getch();
exit(0);
}
for (i = 0;i < sum ; i++)
{
if (fread(&binfo[i],sizeof(bbasic),1,fp) != 1)
{
printf("cannot read the addbook.dat.");
getch();
exit(0);
}
} /*读取文件中所存的图书基本信息*/
fclose(fp);
ordern(binfo); /*对图书基本信息数组排序*/
gotoxy(1,2);
cprintf("Please input the book name you search for:");
scanf("%s",find);
getchar();
obj = halfn(sum,binfo,find); /*二分法找到所查找书数组标号*/
if(obj != -1)
{
printf("\nthe book's name is:%s\n", binfo[obj].name);
printf("the book's writer is:%s\n", binfo[obj].writer);
printf("the book's cclc is:%s\n", binfo[obj].type);
printf("the book's num is:%s\n", binfo[obj].num);
printf("the book's publish time is:%s\n", binfo[obj].time);
printf("the book's press is:%s\n", binfo[obj].press);
printf("the book's edition is:%s\n", binfo[obj].edition);
printf("the book's ISBN is:%s\n", binfo[obj].ISBN);
printf("the book's price is:%f\n", binfo[obj].price);
printf("the book's count is:%d\n", binfo[obj].count);
printf("the book's cclc is:%d\n", binfo[obj].lendnum);
}
else
{
printf("\ncannot find the book");
}
printf("\n\n\n press Y to continue,or press any key to back to menu.");
ky = key();
if (ky == Key_Y)
{
searchname();
}
else
{
menu();
}
}
void searchwriter()
{
FILE *fp;
int sum, obj, ky;
long i;
char find[30];
bbasic binfo[100];
textbackground(BLUE);
textcolor(WHITE);
clrscr();
sum = readsum(); /*读取图书基本信息数量sum*/
if((fp = fopen("e:\\addbook.dat","rb")) ==NULL) /*读取文件中所存的图书基本信息*/
{
printf("cannot open the addbook.dat");
getch();
exit(0);
}
for (i = 0;i < sum ; i++)
{
if (fread(&binfo[i],sizeof(bbasic),1,fp) != 1)
{
printf("cannot read the addbook.dat.");
getch();
exit(0);
}
} /*读取文件中所存的图书基本信息*/
fclose(fp);
orderw(binfo); /*对图书基本信息数组排序*/
gotoxy(1,2);
cprintf("Please input the writer you search for:");
scanf("%s",find);
getchar();
obj = halfw(sum,binfo,find); /*二分法找到所查找书数组标号*/
if(obj != -1)
{
printf("\nthe book's name is:%s\n", binfo[obj].name);
printf("the book's writer is:%s\n", binfo[obj].writer);
printf("the book's cclc is:%s\n", binfo[obj].type);
printf("the book's num is:%s\n", binfo[obj].num);
printf("the book's publish time is:%s\n", binfo[obj].time);
printf("the book's press is:%s\n", binfo[obj].press);
printf("the book's edition is:%s\n", binfo[obj].edition);
printf("the book's ISBN is:%s\n", binfo[obj].ISBN);
printf("the book's price is:%f\n", binfo[obj].price);
printf("the book's count is:%d\n", binfo[obj].count);
printf("the book's cclc is:%d\n", binfo[obj].lendnum);
}
else
{
printf("\ncannot find the book");
}
printf("\n\n\n press Y to continue or press anykey to back to menu.");
ky = key();
if (ky == Key_Y)
{
searchwriter();
}
else
{
menu();
}
}
void sta()
{
int y;
_window();
gotoxy(35,22);
textcolor(LIGHTGRAY);
textbackground(BLUE);
cprintf(" statistic ");
gotoxy(25, 2);
textcolor(RED);
highvideo();
cprintf("The Library Managemnt Program");
normvideo();
gotoxy(30,7);
textbackground(RED);
textcolor(WHITE);
cprintf("Book List");
textbackground(BLUE);
gotoxy(30,10);
cprintf("Book Money");
gotoxy(30,13);
cprintf("Back");
y=choose(7,13);
switch(y)
{
case 7 :blist();menu();break;
case 10:bmoney();menu();break;
case 13:menu();break;
default:printf("Error");break;
}
}
void blist()
{
FILE *fp;
int sum;
long i;
int j;
bbasic binfo[100];
_window();
gotoxy(35,22);
textcolor(LIGHTGRAY);
textbackground(BLUE);
cprintf(" Book List ");
textcolor(WHITE);
sum = readsum(); /*读取图书基本信息数量sum*/
gotoxy(25,2);
cprintf("The Total Book number is %d",sum);
if((fp = fopen("e:\\addbook.dat","rb")) ==NULL) /*读取文件中所存的图书基本信息*/
{
printf("cannot open the addbook.dat");
getch();
exit(0);
}
for (i = 0,j = 0;i < sum ; i++)
{
if (fread(&binfo[i],sizeof(bbasic),1,fp) != 1)
{
printf("cannot read the addbook.dat.");
getch();
exit(0);
}
gotoxy(2,4 + j);
cprintf("Book Number:%s",binfo[i].num);
gotoxy(25,4 + j);
cprintf("Book Name:%s", binfo[i].name);
gotoxy(48,4 + j);
cprintf("Book Writer:%s", binfo[i].writer);
j = j + 4;
if (wherey() > 19)
{
gotoxy(10,23);
cprintf("press anykey to clear screen and continue display book list.");
getch();
clrscr();
j = 0;
_window();
gotoxy(35,22);
textcolor(LIGHTGRAY);
textbackground(BLUE);
cprintf(" book list ");
textcolor(WHITE);
gotoxy(25,2);
cprintf("The Total Book number is %d",sum);
}
}
fclose(fp);
gotoxy(20,23);
cprintf("That's All.Press Anykey To Back To Menu.");
getch();
}
void bmoney()
{
FILE *fp;
int sum;
long i;
float rmb = 0;
bbasic binfo[100];
_window();
gotoxy(35,22);
textcolor(LIGHTGRAY);
textbackground(BLUE);
cprintf(" Money List ");
textcolor(WHITE);
sum = readsum(); /*读取图书基本信息数量sum*/
if((fp = fopen("e:\\addbook.dat","rb")) ==NULL) /*读取文件中所存的图书基本信息*/
{
printf("cannot open the addbook.dat");
getch();
exit(0);
}
for (i = 0;i < sum ; i++)
{
if (fread(&binfo[i],sizeof(bbasic),1,fp) != 1)
{
printf("cannot read the addbook.dat.");
getch();
exit(0);
}
rmb = rmb + binfo[i].price;
}
fclose(fp);
gotoxy(2,3);
cprintf("The Total Money is:%.2f",rmb);
gotoxy(2,5);
cprintf("The Average Cost of Every Book is:%.2f",rmb/sum);
gotoxy(25,23);
cprintf("Press Anykey To Back To Menu.");
getch();
}
七【上机实验中的其他它问题及心得】
做这次图书管理系统收获很大,第一次不是只单纯的解题,第一次将C语言运用,学以致用的感觉很好。与解题相比有趣很多。
这次做图书管理系统,功能方面,链表的创建,插入,修改,以及二分法这些都已经很熟了,唯一的问题就是把它们结合在一起,这并不算很难。所以刚开始花时间比较多的地方主要是在界面和文件的应用那里。
做界面的时候,因为《c程序设计(第三版)谭浩强》里面没有讲,我专门去买了C语言高级编程及实例,我是从开头开始看的,前面看了第一章内存管理,不是很懂,又去图书馆借《汇编语言基础》来看,看了似乎懂了一些,马马虎虎看过去,花了点时间。第二章就是文本屏幕界面设计,看了以后,勉强算是把界面做出来了,然后慢慢加了一些光标,边框,变换了一下颜色,使界面美观了一些。
文件的应用的时候,在文件读写那里老是想用fseek去使fread的指针跳到下一条,卡住了很久。后来李老师说不应该用,改了以后确实能行。之前我存的时候用了fseek,读的时候也用了,就刚好没有问题。但应该存的文件就不连续,空了很多吧。下次要注意。
做功能的时候,基本上没有遇到什么困难,做的还算顺利。
这次做图书管理系统,让我思考了很多关于设计一个真正的程序的问题。比如数据结构的选择,时间复杂度的考虑,优化的算法,结构化程序的设计,用宏定义使程序简单明了等等。在做的过程中,我也发现很多细节的东西其实自己还没有完全弄懂,那天听了周立功的讲座后更是觉得如此。学好C语言还有很长的路要走,这是一个漫长而有趣的过程。路漫漫其修远兮,吾将上下而求索。