结构数组模拟零件数据库

需求(<C语言程序设计-现代方法> 339页):

此程序用来维护仓库存储的零件的信息数据库. 程序围绕一个结构数组建立, 且每个结构包含以下信息: 零件的编号, 零件的名称以及某种零件的数量. 程序将支持以下操作:

1. 添加新零件编号, 名称和现有的初始数量. 如果零件已经在数据库中, 或者数据库已满, 那么程序必须显示出出错信息.

2. 给定零件编号, 显示出零件的名称和当前现有的数量. 如果零件编号不在数据库中, 那么程序必须显示出出错信息.

3. 给定零件编号, 改变现有的零件数量. 如果零件编号不在数据库中, 那么程序必须显示出错信息.

4. 显示表格列出数组库中的全部信息. 零件必须按照录入的数序显示出来

5. 终止程序的执行.

使用i(插入), s(搜索), u(更新), p(显示)和q(退出)分别表示这些操作

主程序(partdb.c):

1. 通过flag控制程序是否继续运行.

2. 不断通过prompt()提示用户输入命令, 并检查用户输入的命令, 直到用户输入一个合法的命令.

3. 根据不同的命令, 进入不同的处理分支.

4. 插入命令: 判断数据库是否满, 要插入的id是否存在决定是否继续.

5. 搜索命令: 查找并显示输入id的零件信息.

6. 更新命令: 搜索要更新数量的零件是否存在, 然后继续更新数量

7. 显示: 打印所有零件的信息到屏幕

8. 退出: 将flag置为FALSE, 退出系统

9. 其他: 认为是系统出现严重错误导致的退出.

#include <stdio.h> #include "bool.h" #include "input.h" #include "command.h" #include "part.h" int main() { char command; Bool flag = TRUE; int id; while(flag) { prompt(); while(!checkCommand(command = getCommand())) prompt(); switch(command) { case 'i': if(remainingAmount >= MAX_AMOUNT) { printf("The db is fulling./n"); break; } id = getInt("Please enter the part's id"); if(searchPart(id) < 0) { addPart(id, getString("Please enter the part's name: "), getInt("Please enter initial amount of this part: ")); } else { printf("warning: conflict id [id = %d]./n", id); } break; case 's': displayPart(getInt("Please enter the part's id: ")); break; case 'u': id = getInt("Please enter the part's id: "); if(searchPart(id) >= 0) { updatePartAmount(id, getInt("Please enter the minus of this part amount: ")); } else { printf("warning: hadn't this part [id = %d]./n", id); } break; case 'p': displayAllPart(); break; case 'q': printf("Bye-bye./n"); flag = FALSE; break; default: printf("A serious error cause the system exit./n"); flag = FALSE; break; } } return 0; }

bool.h: 定义了Bool类型及TRUE, FALSE

#ifndef BOOL_H #define BOOL_H #define TRUE 1 #define FALSE 0 typedef int Bool; #endif

command.h: 描述了与命令获取相关的函数声明

#ifndef COMMAND_H #define COMMAND_H #include "bool.h" /************************************************************************ * print the prompt information of allowed command. ************************************************************************/ void prompt(void); /************************************************************************ * get a command ************************************************************************/ char getCommand(void); /************************************************************************ * check the command. ************************************************************************/ Bool checkCommand(char command); #endif

command.c: 对命令获取相关函数的实现

#include <stdio.h> #include "bool.h" #include "command.h" char allowedCommand[] = {'i', 's', 'u', 'p', 'q'}; char allowedCommandExplain[][50] = { "add a part.", "search and display a part by id.", "update special part's amount.", "print all the part's information that in db.", "exit system." }; int commandAmount = sizeof(allowedCommand) / sizeof(allowedCommand[0]); /************************************************************************ * print the prompt information of allowed command. ************************************************************************/ void prompt(void) { printf("/n%20s|%50s/n", "command", "explain"); int i = 0; for(; i <= 70; i ++) { printf("-"); } printf("/n"); i = 0; for(; i < commandAmount; i ++) { printf("%20c|%50s/n", allowedCommand[i], allowedCommandExplain[i]); } printf("/n"); } /************************************************************************ * get a command ************************************************************************/ char getCommand(void) { char tmp; printf("Please enter a commound: "); scanf(" %c", &tmp); return tmp; } /************************************************************************ * check the command. ************************************************************************/ Bool checkCommand(char command) { int i = 0; for(; i < commandAmount; i ++) { if(command == allowedCommand[i]) return TRUE; } return FALSE; }

input.h: 描述了获取用户输入零件信息时的相关输入函数声明

#ifndef INPUT_H #define INPUT_H #define MAX_STRING_LEN 50 /************************************************************************ * get int with a prompt information. ************************************************************************/ int getInt(char *prompt); /************************************************************************ * get string with a prompt information. ************************************************************************/ char *getString(char *prompt); #endif

input.c: 描述获取用户输入相关函数的实现

#include <stdio.h> #include "input.h" char stringTmp[MAX_STRING_LEN]; /************************************************************************ * get int with a prompt information. ************************************************************************/ int getInt(char *prompt) { int tmp; printf("%s/n", prompt); scanf("%d", &tmp); return tmp; } /************************************************************************ * get string with a prompt information. ************************************************************************/ char *getString(char *prompt) { printf("%s/n", prompt); int count = 0; scanf(" %c", &stringTmp[count]); while(count < MAX_STRING_LEN && stringTmp[count] != '/n') { scanf("%c", &stringTmp[++count]); } stringTmp[count] = '/0'; return stringTmp; }

part.h:

零件的结构, 零件数据库(结构数组)定义, 零件数据库相关操作等函数的声明.

#ifndef PART_H #define PART_H #include "bool.h" #define MAX_AMOUNT 100 #define MAX_NAME_LEN 50 typedef struct { int id; char name[MAX_NAME_LEN]; int amount; } Part; Part parts[MAX_AMOUNT]; extern int remainingAmount; /************************************************************************ * add a part ************************************************************************/ Part addPart(int id, char *name, int amount); /************************************************************************ * display a part by its id ************************************************************************/ void displayPart(int id); /************************************************************************ * search a Part ************************************************************************// int searchPart(int id); /************************************************************************ * update the special part's amount ************************************************************************/ void updatePartAmount(int id, int minus); /************************************************************************ * display all the parts ************************************************************************/ void displayAllPart(void); #endif

part.c: 零件数据库相关操作函数的实现

#include <stdio.h> #include <string.h> #include "bool.h" #include "part.h" int remainingAmount = 0; /************************************************************************ * add a part ************************************************************************/ Part addPart(int id, char *name, int amount) { parts[remainingAmount].id = id; strcpy(parts[remainingAmount].name, name); parts[remainingAmount].amount = amount; return parts[remainingAmount++]; } /************************************************************************ * display a part by its id ************************************************************************/ void displayPart(int id) { int index = searchPart(id); if(index >= 0) { printf("{[id: %6d], [name: %-50s], [amount: %6d]}/n", parts[index].id, parts[index].name, parts[index].amount); } else { printf("warning: hadn't this part [id = %d]./n", id); } } /************************************************************************ * search a Part ************************************************************************// int searchPart(int id) { int i = 0; for(; i < remainingAmount; i ++) { if(parts[i].id == id) return i; } return -1; } /************************************************************************ * update the special part's remainingAmount ************************************************************************/ void updatePartAmount(int id, int minus) { int index = searchPart(id); parts[index].amount += minus; printf("information: update success. new amount is: %d./n", parts[index].amount); } /************************************************************************ * display all the parts ************************************************************************/ void displayAllPart(void) { int i = 0; for(; i < remainingAmount; i ++) { displayPart(parts[i].id); } }

makefile

partdb: partdb.o command.o input.o part.o
gcc -o partdb partdb.o command.o input.o part.o
partdb.o: partdb.c bool.h input.h command.h part.h
gcc -c partdb.c
command.o: command.c bool.h command.h
gcc -c command.c
input.o: input.c input.h
gcc -c input.c
part.o: part.c bool.h part.h
gcc -c part.c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值