// 2022 12 11 C语言大学教程 11.12 库存管理清单 课后作业
//尚待完成:罗列清单列表
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct stockRecord {
int recordNum;
char toolName[ 15 ];
int amount;
double cost;
};
/// ///
int getChoice();
void updateRecord( FILE *fPtr );
void newRecord( FILE *fPtr );
void deleteRecord( FILE *fPtr );
void textFile( FILE *readPtr );//用文本文档存储信息
void textPrint();
int getUpdateChoice();
int main( void )
{
FILE *cfPtr;
int choice;
char ch_next ='0';
char print_string[100] = {0};
if ( ( cfPtr = fopen( "hardware.dat", "rb+" ) ) == NULL ) {
printf( "File could not be opened.\n" );
}
else {
printf("__________________________________________________\n");
printf("记录号码\t工具名称\t数量\t成本\n");
printf("——————————————————————————————————————————————————\n");
textPrint();
while ( ( choice = getChoice() ) != 5 ) {
switch ( choice ) {
case 1:
updateRecord( cfPtr );
break;
case 2:
newRecord( cfPtr );
break;
case 3:
deleteRecord( cfPtr );
break;
case 4:
textFile(cfPtr);
break;
default:
printf( "Incorrect choice\n" );
break;
}
}
fclose( cfPtr );
}
return 0;
}
void textPrint()
{
FILE *writePtr;
char a='0';
struct stockRecord aRecord = { 0, "", 0, 0.0 };
if ( ( writePtr = fopen( "hardware.txt", "r" ) ) == NULL ) {
printf( "File could not be opened.\n" );
}
else {
//读取
while((a=fgetc(writePtr))!=EOF){
fscanf(writePtr,"%c",&a);
printf("%c",a);
}
fclose( writePtr );
}
}
void textFile( FILE *readPtr )
{
FILE *writePtr;
struct stockRecord aRecord = { 0, "", 0, 0.0 };
if ( ( writePtr = fopen( "hardware.txt", "w" ) ) == NULL ) {
printf( "File could not be opened.\n" );
}
else {
rewind( readPtr );
fprintf( writePtr, "%-6s%-16s%-11s%10s\n",
"recordNum", "Tool Name", "Amount","Cost" );
while ( !feof( readPtr ) ) {
fread( &aRecord, sizeof( struct stockRecord ), 1, readPtr );
if ( aRecord.recordNum != 0 ) {
fprintf( writePtr, "%-6d%-16s%-11d%10.2f\n",
aRecord.recordNum, aRecord.toolName,
aRecord.amount, aRecord.cost );
}
}
fclose( writePtr );
}
}
void updateRecord( FILE *fPtr )
{
int record = 0;
double transaction = 0;
char new_tool_name[15] = "";
int choice = 0;
int new_amount = 0;
struct stockRecord new_record = { 0, "",0, 0.0 };
printf( "Enter a record to update ( 1 - 100 ): " );
scanf( "%d", &record );
fseek( fPtr, ( record - 1 ) * sizeof( struct stockRecord ),
SEEK_SET );
fread( &new_record, sizeof( struct stockRecord ), 1, fPtr );
if ( new_record.recordNum == 0 ||new_record.recordNum>100) {
printf( "Record #%d has no information.\n", record );
}
else {
printf( "%-6d%-16s%-11d%10.2f\n\n",
new_record.recordNum, new_record.toolName,
new_record.amount, new_record.cost );
choice = getUpdateChoice();//获得需修改的项目
switch ( choice ) {
case 1:
printf( "Enter the new tool name: " );
scanf( "%15s", new_tool_name );
strcpy(new_record.toolName,new_tool_name);
break;
case 2:
printf( "Enter the new amount: " );//更新剩余库存数量
scanf("%d",&new_amount);
new_record.amount = new_amount;
break;
case 3:
printf( "Enter the new cost: " );
scanf( "%lf", &transaction );
new_record.cost = transaction;
break;
default:
printf( "Incorrect choice\n" );
break;
}
printf( "%-6d%-16s%-11d%10.2f\n",
new_record.recordNum, new_record.toolName,
new_record.amount, new_record.cost );
fseek( fPtr, ( record - 1 ) * sizeof( struct stockRecord ),
SEEK_SET );
fwrite( &new_record, sizeof( struct stockRecord ), 1, fPtr );
}
}
void deleteRecord( FILE *fPtr )
{
struct stockRecord client;
struct stockRecord blankClient = { 0, "",0, 0 };
int tool_num;
printf( "Enter record number to delete ( 1 - 100 ): " );
scanf( "%d", &tool_num );
fseek( fPtr, ( tool_num - 1 ) * sizeof( struct stockRecord ),
SEEK_SET );
fread( &client, sizeof( struct stockRecord ), 1, fPtr );
if ( client.recordNum == 0 ) {
printf( "Record %d does not exist.\n", tool_num );
}
else {
fseek( fPtr, ( tool_num - 1 ) * sizeof( struct stockRecord ),
SEEK_SET );
fwrite( &blankClient,
sizeof( struct stockRecord ), 1, fPtr );
}
}
void newRecord( FILE *fPtr )
{
struct stockRecord client = { 0, "",0, 0.0 };
int tool_num;
printf( "Enter new number (1 - 100): " );
scanf( "%d", &tool_num );
fseek( fPtr, ( tool_num - 1 ) * sizeof( struct stockRecord ),
SEEK_SET );
fread( &client, sizeof( struct stockRecord ), 1, fPtr );
if ( client.recordNum != 0 ) {
printf( "Stock #%d already contains this information.\n",client.recordNum );
}
else {
printf( "Enter tool name, amount, cost\n? " );
scanf( "%s%d%lf", &client.toolName, &client.amount,
&client.cost );
client.recordNum = tool_num;
fseek( fPtr, ( client.recordNum - 1 ) * sizeof( struct stockRecord ), SEEK_SET );
fwrite( &client,
sizeof( struct stockRecord ), 1, fPtr );
}
}
int getChoice()
{
int menuChoice;
printf( "\nEnter your choice\n"
"1 - update a record\n"
"2 - add a new record\n"
"3 - delete a record\n"
"4 - store a formatted text file\n"
"5 - end program\n? " );
scanf( "%d", &menuChoice );
return menuChoice;
}
int getUpdateChoice()
{
int updateChoice;
printf(
"1 - update the tool name \n"
"2 - update the amount\n"
"3 - update the cost \n? " );
scanf( "%d", &updateChoice );
return updateChoice;
}
【C语言大学教程】 第八版 11.12 库存管理清单 课后作业
最新推荐文章于 2024-01-27 11:54:06 发布