用C语言编程
问题描述:
实验设备信息包括:设备编号,设备种类(如:微机、打印机、扫描仪等等),设备名称,设备价格,设备购入日期,是否报废,报废日期等。
主要功能:
[1]完成对设备信息的录入函数
[2]完成修改某个设备信息的函数
[3]根据给定信息对设备进行分类统计
[4]设备的破损耗费和遗损处理(删除某个指定设备)
[5]设备的查询
[解决办法]
# include <stdio.h># include <stdlib.h># include <string.h>int count=0;//全局变量,书的个数void read_file(struct book *p, char* filename); //读入文件void display(struct book *p); //显示文件内容void change(struct book *p); //修改文件内容void reduce(struct book *p); //减少文件内容void add(struct book *p); //添加文件内容void write_file(struct book *p, char*); //写入文件#define MAXTITL 40#define MAXAUTL 40#define MAXBKS 100 struct book{ char title[MAXTITL]; //40 char author[MAXAUTL]; //40 float value;};int main(void){ struct book library[MAXBKS]; //10 char command[10]; read_file(library,"book.dat"); //读入文件 printf("选择功能:\n"); printf("d: 显示所有设备 c: 修改设备\n"); printf("r: 删除设备 a: 添加设备\n"); printf("其他: 保存和退出:\n"); while( 1 ) { gets(command); switch( command[0] ) { case 'd': display(library); break; //显示文件内容 case 'c': change(library); break; //修改文件内容 case 'r': reduce(library); break; //减少文件内容 case 'a': add(library); break; //添加文件内容 default : write_file(library,"book.dat"); { printf("操作结束.\n"); return 0; } } printf("选择: d/c/r/a/other:"); }}void read_file(struct book *p, char* filename){ FILE * pbooks; if ((pbooks = fopen(filename, "a+b")) == NULL)//a+ 可以创造一个文件,且不清空 { printf("Can't open %s file\n", filename); exit(1); } rewind(pbooks); while (count < MAXBKS && fread(p+count, sizeof(struct book), 1, pbooks) == 1) count++; fclose(pbooks); printf("读取 %s 成功! 有 %d 台设备.\n", filename, count);}void display(struct book *p){ int i; if ( count == 0 ) { printf("没有设备.\n"); } printf("有 %d 台设备:\n",count); for(i=0; i<count; i++) printf("设备种类: %s\n设备名称: %s\n设备价格: %.2f\n",p[i].title, p[i].author, p[i].value);}void change(struct book *p){ int i; char title[MAXTITL]; printf("输入你要修改设备的名字:"); gets(title); for(i=0; i<count; i++) if ( strcmp(p[i].title, title) == 0 ) { puts("请添加一台新的设备种类."); gets(p[i].title); puts("输入设备名称."); gets(p[i].author); puts("输入设备价格."); scanf("%f", &p[i].value); getchar(); printf("这台设备以修改!\n"); return; } printf("错误!这台设备是不在列表中\n"); return;}void reduce(struct book *p){ int i; char title[MAXTITL]; if(count == 0) { printf("错误!设备列表为空!"); return; } printf("输入你要删除设备的名字:"); gets(title); for(i=0; i<count; i++) if ( strcmp(p[i].title, title) == 0 ) { p[i] = p[count-1]; strcpy(p[count-1].title, ""); strcpy(p[count-1].author, ""); p[count-1].value=0; count--; printf("%s 以从设备的列表中删除\n", title); return; } printf("错误!%s设备不在列表中\n", title); return;}void add(struct book *p){ if (count == MAXBKS) { fputs("错误!这个 book.dat 文件已满.", stderr); return; } puts("请添加新的设备种类."); puts("请按 [enter] 键停止."); while (count < MAXBKS && gets(p[count].title) != NULL && p[count].title[0] != '\0') { puts("输入设备名称."); gets(p[count].author); puts("输入设备价格."); scanf("%f", &p[count++].value); getchar(); if (count < MAXBKS) puts("输入下一台设备."); else puts("这个设备列表单已满."); }}void write_file(struct book *p, char *str){ FILE * pbooks; pbooks = fopen(str, "wb"); //将文件以可写模式打开,目的是清空文件内容 pbooks = fopen(str, "r+b"); fwrite(p, sizeof (struct book), count, pbooks); fclose(pbooks);}
[解决办法]
LZ自定义一个结构体来弄吧!挺容易的,如果不知道这部分知识就先看看书再来写,不懂再上来问。
[解决办法]
这个。。你把谭浩强C语言书后面的那个管理系统改改就是你这个了。。无非是学生管理系统的变体。