mirror of
https://github.com/hairrrrr/C-CrashCourse.git
synced 2026-05-11 19:17:27 +08:00
4-17
This commit is contained in:
194
code/practise/16 结构&联合&枚举/01 维护零件数据库/inventory.c
Normal file
194
code/practise/16 结构&联合&枚举/01 维护零件数据库/inventory.c
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
#define _CRT_SECURE_NO_WARNINGS 1
|
||||||
|
|
||||||
|
#include<stdio.h>
|
||||||
|
#include"readline.h"
|
||||||
|
|
||||||
|
#define NAME_LEN 20
|
||||||
|
#define MAX_PARTS 100
|
||||||
|
|
||||||
|
struct part {
|
||||||
|
int number;
|
||||||
|
char name[NAME_LEN + 1];
|
||||||
|
int on_hand;
|
||||||
|
}inventory[MAX_PARTS];
|
||||||
|
|
||||||
|
int num_parts = 0; //number of parts current stored
|
||||||
|
|
||||||
|
void menu();
|
||||||
|
int find_part(int number);
|
||||||
|
void insert();
|
||||||
|
void search();
|
||||||
|
void update();
|
||||||
|
void print();
|
||||||
|
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
|
||||||
|
char code = 'a';
|
||||||
|
|
||||||
|
menu();
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
printf("Enter operation code: ");
|
||||||
|
scanf(" %c", &code);
|
||||||
|
while (getchar() != '\n') // ships to end of line
|
||||||
|
;
|
||||||
|
switch (code) {
|
||||||
|
case 'i': insert(); break;
|
||||||
|
case 's': search(); break;
|
||||||
|
case 'u': update(); break;
|
||||||
|
case 'p': print(); break;
|
||||||
|
case 'q': return 0;
|
||||||
|
default: printf("Illegal code.\n"); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void menu() {
|
||||||
|
|
||||||
|
printf(" ==================================\n");
|
||||||
|
printf(" * *\n");
|
||||||
|
printf(" * i: insert *\n");
|
||||||
|
printf(" * s: search *\n");
|
||||||
|
printf(" * u: undate *\n");
|
||||||
|
printf(" * p: print *\n");
|
||||||
|
printf(" * q: quit *\n");
|
||||||
|
printf(" * *\n");
|
||||||
|
printf(" ==================================\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**********************************************************
|
||||||
|
*
|
||||||
|
* find_part: Looks up a part number in the inventory
|
||||||
|
* array.Returns the array index if the part
|
||||||
|
* number is found;otherwise,return -1
|
||||||
|
*
|
||||||
|
***********************************************************/
|
||||||
|
int find_part(int number) {
|
||||||
|
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < num_parts; i++) {
|
||||||
|
if (inventory[i].number == number)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**********************************************************
|
||||||
|
*
|
||||||
|
* insert: Inserts the part into the database.Prints
|
||||||
|
* an error message and returns prematurely
|
||||||
|
* if the part already exists or the database
|
||||||
|
* is full.
|
||||||
|
*
|
||||||
|
***********************************************************/
|
||||||
|
void insert() {
|
||||||
|
|
||||||
|
int part_number;
|
||||||
|
|
||||||
|
if (num_parts == MAX_PARTS) {
|
||||||
|
printf("Database is full; can't add more parts.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Enter part number: ");
|
||||||
|
scanf("%d", &part_number);
|
||||||
|
|
||||||
|
if (find_part(part_number) >= 0) {
|
||||||
|
printf("Part already exists.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
inventory[num_parts].number = part_number;
|
||||||
|
printf("Enter part name: ");
|
||||||
|
read_line(inventory[num_parts].name, NAME_LEN);
|
||||||
|
printf("Enter quantity on hand: ");
|
||||||
|
scanf("%d", &inventory[num_parts].on_hand);
|
||||||
|
num_parts++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************************
|
||||||
|
*
|
||||||
|
* search: Look up a part by the number user enters.
|
||||||
|
* If the part exists, prints the name and quantity
|
||||||
|
* on hand;if not, print an error message.
|
||||||
|
*
|
||||||
|
************************************************************/
|
||||||
|
void search() {
|
||||||
|
|
||||||
|
int index, number;
|
||||||
|
|
||||||
|
printf("Enter part number: ");
|
||||||
|
scanf("%d", &number);
|
||||||
|
|
||||||
|
index = find_part(number);
|
||||||
|
|
||||||
|
if (index == -1) {
|
||||||
|
printf("Part not found.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Part name: %s\n", inventory[index].name);
|
||||||
|
printf("Quantity on hand: %d\n", inventory[index].on_hand);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************************
|
||||||
|
*
|
||||||
|
* update: Prompts user to enter a number.
|
||||||
|
* Print an error message if the part doesn't exist;
|
||||||
|
* otherwise,prompts the user to enter change in
|
||||||
|
* quantity on hand and updates the database.
|
||||||
|
*
|
||||||
|
************************************************************/
|
||||||
|
|
||||||
|
void update() {
|
||||||
|
|
||||||
|
int number, index, change;
|
||||||
|
|
||||||
|
printf("Enter part number: ");
|
||||||
|
scanf("%d", &number);
|
||||||
|
|
||||||
|
index = find_part(number);
|
||||||
|
|
||||||
|
if (index == -1) {
|
||||||
|
printf("Part not found.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Enter change in quantity on hand(- means minus): ");
|
||||||
|
scanf("%d", &change);
|
||||||
|
inventory[index].on_hand += change;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************************
|
||||||
|
*
|
||||||
|
* print: Print a listing of all parts in the database,
|
||||||
|
* showing the part number,part name and quantity
|
||||||
|
* on hand.Parts are printed in the order in which
|
||||||
|
* they were entered into the database.
|
||||||
|
*
|
||||||
|
************************************************************/
|
||||||
|
|
||||||
|
void print() {
|
||||||
|
|
||||||
|
int i;
|
||||||
|
|
||||||
|
printf("Part Number Part Name Quantity on Hand\n");
|
||||||
|
for (i = 0; i < num_parts; i++) {
|
||||||
|
printf("%6d%20s%15d\n", inventory[i].number, inventory[i].name, inventory[i].on_hand);
|
||||||
|
}
|
||||||
|
}
|
||||||
24
code/practise/16 结构&联合&枚举/01 维护零件数据库/readline.c
Normal file
24
code/practise/16 结构&联合&枚举/01 维护零件数据库/readline.c
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#define _CRT_SECURE_NO_WARNINGS 1
|
||||||
|
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<ctype.h>
|
||||||
|
#include"readline.h"
|
||||||
|
|
||||||
|
int read_line(char str[], int n) {
|
||||||
|
|
||||||
|
int ch, i = 0;
|
||||||
|
|
||||||
|
while (isspace(ch = getchar()))
|
||||||
|
;
|
||||||
|
|
||||||
|
while (ch != '\n' && ch != EOF) {
|
||||||
|
if (i < n)
|
||||||
|
str[i++] = ch;
|
||||||
|
|
||||||
|
ch = getchar();
|
||||||
|
}
|
||||||
|
str[i] = '\0';
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
16
code/practise/16 结构&联合&枚举/01 维护零件数据库/readline.h
Normal file
16
code/practise/16 结构&联合&枚举/01 维护零件数据库/readline.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef READLINE_H
|
||||||
|
#define READLINE_H
|
||||||
|
|
||||||
|
/***********************************************************
|
||||||
|
*
|
||||||
|
* read_line: Skips leading white-space characters, then
|
||||||
|
* reads the remainder of the input line and
|
||||||
|
* stores it in str. Truncates the line if its
|
||||||
|
* length exceeds n. Return the number of
|
||||||
|
* characters stores.
|
||||||
|
*
|
||||||
|
***********************************************************/
|
||||||
|
|
||||||
|
int read_line(char str[], int n);
|
||||||
|
|
||||||
|
#endif
|
||||||
51
code/practise/16 结构&联合&枚举/01 维护零件数据库/readme.md
Normal file
51
code/practise/16 结构&联合&枚举/01 维护零件数据库/readme.md
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#### 程序:维护零件数据库
|
||||||
|
|
||||||
|
此程序用来维护仓库存储的零件信息的数据库。程序围绕一个结构数组构建,且每个结构包含以下信息:零件编号,名称和数量。程序将支持下列操作:
|
||||||
|
|
||||||
|
- **添加新零件信息**。如果零件已经存在,或数据库已满,显示出错信息。
|
||||||
|
- **给定零件编号,显示零件的名称,数量信息**。如果零件编号不存在,那么给出出错信息。
|
||||||
|
- **给定零件编号,改变零件的数量**。如果零件编号不存在,给出出错消息。
|
||||||
|
- **显示列出数据库中的全部信息**。零件必须按照录入顺序显示。
|
||||||
|
- **终止程序的执行**
|
||||||
|
|
||||||
|
使用:
|
||||||
|
|
||||||
|
- `i`:插入
|
||||||
|
- `s`:搜索
|
||||||
|
- `u`:更新
|
||||||
|
- `p`:显示
|
||||||
|
- `q`:退出
|
||||||
|
|
||||||
|
分表表示这种操作,与程序得到会话如下:
|
||||||
|
|
||||||
|
```c
|
||||||
|
Enter operation code: i
|
||||||
|
Enter part number: 833
|
||||||
|
Enter part name: Disk Drive
|
||||||
|
Enter quantity on hand: 90
|
||||||
|
Enter operation code: i
|
||||||
|
Enter part number: 788
|
||||||
|
Enter part name: USB 3.0
|
||||||
|
Enter quantity on hand: 67
|
||||||
|
Enter operation code: s
|
||||||
|
Enter part number: 832
|
||||||
|
Part not found.
|
||||||
|
Enter operation code: 833
|
||||||
|
Illegal code.
|
||||||
|
Enter operation code: s
|
||||||
|
Enter part number: 833
|
||||||
|
Part name: Disk Drive
|
||||||
|
Quantity on hand: 90
|
||||||
|
Enter operation code: u
|
||||||
|
Enter part number: 788
|
||||||
|
Enter change in quantity on hand(- means minus): 3
|
||||||
|
Enter operation code: p
|
||||||
|
Part Number Part Name Quantity on Hand
|
||||||
|
833 Disk Drive 90
|
||||||
|
788 USB 3.0 70
|
||||||
|
Enter operation code: q
|
||||||
|
```
|
||||||
|
|
||||||
|
注意:菜单可以没有
|
||||||
|
|
||||||
|
因为 readline 函数和这个程序的主干没有太大关系,我们用单独的头文件和源文件包含它。
|
||||||
Reference in New Issue
Block a user