零件库存管理系统
终于……终于……让输入变的安全了。历时3天,第一版本不到400行。
昨天的第二版本,650行左右。
今天的修改,估计快700行了。
一共10个文件,又一次体会到模块化的好处,修改起来太方便了。
整个人都要崩了。也许唯一值得高兴的是,又多了2个以后可以重用的函数。NICE。
实际上还有一个BUG,但完全不知道怎么重现,也完全不知道是怎么产生的。我就重现了一次,之后就再也不能。
悲剧。
程序代码:#include "DataType.h"
#include "DataOperation.h"
#include "FileOperation.h"
#include "Function.h"
#include "getstring.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define __SYSTEM__H__ 1
#if defined(__SYSTEM__H__)
#define CLRSCR system("cls")
#else
#define CLRSCR system("clear")
#endif
void
Number( int *Num );
int
main( int avgc, char **avgr )
{
Node *Root;
Node InNode;
int number;
int value;
char *str;
Root = NULL;
InNode.Type = Head;
InNode.Ption.HeadInformation.MaxNumber = 0;
InNode.Ption.HeadInformation.DeleteionNumber = 0;
InNode.Ption.HeadInformation.Total = 0;
if( 2 == avgc )
str = *++avgr;
else
{
printf("输入要打开的文件:");
str = getword( stdin );
}
FileRead( &Root, &InNode, str );
CLRSCR;
while( 1 )
{
FunCtion();
getint( &value );
switch( value )
{
case 1:
CLRSCR;
NewData( &Root, &InNode, str );
break;
case 2:
CLRSCR;
Number( &number );
BuyData( Root, number, str );
break;
case 3:
CLRSCR;
Number( &number );
Sell( Root, number, str );
break;
case 4:
CLRSCR;
Number( &number );
Delete( &Root, number, str );
break;
case 5:
CLRSCR;
Number( &number );
Print( Root, number, str );
break;
case 6:
CLRSCR;
PrintAll( Root );
break;
case 7:
CLRSCR;
PrintTotal( Root );
break;
case 8:
CLRSCR;
End( Root, str );
if( 2 == avgc )
free( str );
printf( "Bye!\n" );
return 0;
default:
break;
}
}
return 0;
}
void
Number( int *Num )
{
while( printf( "请输入编号:" ), 1 != scanf("%d",Num) )
{
int ch;
while( '\n' != ( ch = getchar() ) )
printf("%c ",ch);
printf("错误的输入。\n");
}
getchar();
}
程序代码:#ifndef DATATYPE_H
#define DATATYPE_H
struct Head {
int MaxNumber;//最大编号
int DeleteionNumber;//缺失的编号
double Total;//所有零件的总价
};
struct Component {
char Description[ 21 ];//描述信息
int PartNumber;//编号
int Quantity;//数量
double ComponentTotal;//该零件的总价值
};
typedef struct NODE {
union {
struct Head HeadInformation;
struct Component ComponentInformation;
}Ption;
enum { Head, Component }Type;
struct NODE *Next;
}Node;
#endif
程序代码:#ifndef FUNCTION_H #define FUNCTION_H void FunCtion( void ); void NewData( Node **Root, Node *InNode, char *str ); void BuyData( Node *Root, int Number, char *str ); void Sell( Node *Root, int Number, char *str ); void Delete( Node **Root, int Number, char *str ); void Print( Node *Root, int Number, char *str ); void PrintAll( Node *Root ); void PrintTotal( Node *Root ); void End( Node *Root,char *str ); #endif
程序代码:#include "DataType.h"
#include "DataOperation.h"
#include "FileOperation.h"
#include "Function.h"
#include "getstring.h"
#include <stdio.h>
static char *Prompt[] = { "请输入新零件的描述信息:", "请输入新零件的数量:",
"请输入新零件的单价:" };
static char *PromptBuy[] = { "请输入要增加的数量:", "请输入购入该零件的单价:" };
static char *PromptSell[] = { "请输入要减少的数量:", "请输入每个零件的出售价格:" };
int
FindNumber( Node *Root );
void
FunCtion( void )
{
printf("1.New\n2.Buy\n3.Sell\n4.Delete\n5.Print\n6.PrintAll\n7.PrintTotal\n8.end\n"
"输入你要进行操作的编号:");
}
void
NewData( Node **Root, Node *InNode, char *str )
{
double price;
Node *This;
This = *Root;
This->Ption.HeadInformation.DeleteionNumber = FindNumber( This );
printf( "%s", Prompt[ 0 ] );
getname( InNode->, 21 );
while( printf( "%s", Prompt[ 1 ] ) )
{
getint( &InNode-> );
if( 0 < InNode-> )
break;
}
while( printf( "%s", Prompt[ 2 ] ) )
{
getdouble( &price );
if( 0 < price )
break;
}
InNode-> =
InNode-> * price;
if( (*Root)->Ption.HeadInformation.DeleteionNumber )
{
InNode-> =
( *Root )->Ption.HeadInformation.DeleteionNumber;
}
else
{
InNode-> =
++(*Root)->Ption.HeadInformation.MaxNumber;
}
( *Root )->Ption.HeadInformation.Total +=
InNode-> InNode->Type = Component;
InsertNode( Root, InNode );
FileWrite( This, str );
}
void
BuyData( Node *Root, int Number, char *str )
{
int quantity;
double cost_each;
Node *This;
This = FindNode( Root, Number );
if( NULL == This )
return;
while( printf( "%s", PromptBuy[ 0 ] ) )
{
getint( &quantity );
if( 0 < quantity )
break;
}
while( printf( "%s", PromptBuy[ 1 ] ) )
{
getdouble( &cost_each );
if( 0 < cost_each )
break;
}
This-> += quantity;
This-> += quantity * cost_each;
Root->Ption.HeadInformation.Total += quantity * cost_each;
FileWrite( Root, str );
}
void
Sell( Node *Root, int Number, char *str )
{
double price_each;
double now_price_each;
int quantity;
Node *This;
This = FindNode( Root, Number );
if( NULL == This )
return;
while( printf( "%s",PromptSell[ 0 ] ) )
{
getint( &quantity ) ;
if( 0 < quantity )
break;
}
while( printf( "%s",PromptSell[ 1 ] ) )
{
getdouble( &price_each );
if( 0 < price_each )
break;
}
if( This-> - quantity < 0 )
{
printf("库存中没有足够的数量,你确定没有输入错误?\n");
return;
}
now_price_each = This-> /
This-> This-> -= now_price_each * quantity;
This-> -= quantity;
Root->Ption.HeadInformation.Total -= now_price_each * quantity;
printf("共盈利:%.2lf\n" , quantity * ( price_each - now_price_each ) );
FileWrite( Root, str );
}
void
Delete( Node **Root, int Number, char *str )
{
Node *This;
This = *Root;
DeleteNode( Root, Number );
FileWrite( This, str );
}
void
Print( Node *Root, int Number, char *str )
{
PrintNode( Root, Number );
}
void
PrintAll( Node *Root )
{
PrintList( Root );
}
void
PrintTotal( Node *Root )
{
printf("%8s %8s %10s\n","最大编号","缺失编号","总价值");
printf("%8d %8d %10.2lf\n",Root->Ption.HeadInformation.MaxNumber,
Root->Ption.HeadInformation.DeleteionNumber,
Root->Ption.HeadInformation.Total);
}
void
End( Node *Root, char *str )
{
FileWrite( Root, str );
}
int
FindNumber( Node *Root )
{
Node *Next;
int NextNum, ThisNum;
Next = Root->Next;
for( NextNum = 1; NULL != Next; Next = Next->Next )
{
ThisNum = Next->if( NextNum != ThisNum )
return NextNum;
NextNum++;
}
return 0;
}
程序代码:#ifndef FILEOPERATION_H #define FILEOPERATION_H void FileRead( Node **Root, Node *InNode, char *str ); void FileWrite( Node *Root, char *str ); #endif
程序代码:#include <stdio.h>
#include <stdlib.h>
#include "DataType.h"
#include "FileOperation.h"
#include "DataOperation.h"
void
FileRead( Node **Root, Node *InNode, char *str )
{
FILE *fp;
if( NULL == ( fp = fopen( str, "a+b" ) ) )
{
perror( str );
exit( EXIT_FAILURE );
}
fseek( fp, 0, SEEK_END);
if( 0 == ftell( fp ) )
{
InsertNode( Root, InNode );
fclose( fp );
return;
}
else
{
fseek( fp, 0, SEEK_SET );
while( 1 == fread( InNode, sizeof( Node ), 1, fp ) )
InsertNode( Root, InNode );
fclose( fp );
}
}
void
FileWrite( Node *Root, char *str )
{
FILE *fp;
Node *Current;
if( NULL == ( fp = fopen( str, "w+b" ) ) )
{
perror( str );
exit( EXIT_FAILURE );
}
for( Current = Root; NULL != Current; Current = Current->Next )
{
fwrite( Current, sizeof( Node ), 1, fp );
}
fclose( fp );
}
程序代码:#ifndef DATAOPERATION_H #define DATAOPERATION_H void InsertNode( Node **Root, Node *InNode ); Node* FindNode( Node *Root, int Number ); void PrintNode( Node *Root, int Number ); void PrintList( Node *Root ); void DeleteNode( Node **Root, int Number ); #endif
程序代码:#include "DataType.h"
#include "DataOperation.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void
InsertNode( Node **Root, Node *InNode )
{
Node *Next;
Node *NewCell;
while( NULL != ( Next = *Root ) &&
InNode-> >
Next-> )
Root = &Next->Next;
if( NULL == ( NewCell = ( Node* )malloc( sizeof( Node ) ) ) )
{
perror( "InsertNode" );
exit( EXIT_FAILURE );
}
NewCell->Next = Next;
*Root = NewCell;
if( InNode->Type == Head )
{
NewCell->Type = Head;
NewCell->Ption.HeadInformation.MaxNumber =
InNode->Ption.HeadInformation.MaxNumber;
NewCell->Ption.HeadInformation.DeleteionNumber =
InNode->Ption.HeadInformation.DeleteionNumber;
NewCell->Ption.HeadInformation.Total =
InNode->Ption.HeadInformation.Total;
}
else
{
NewCell->Type = Component;
strcpy( NewCell->,
InNode-> );
NewCell-> =
InNode-> =
InNode-> =
InNode-> }
}
Node *
FindNode( Node *Root, int Number )
{
Node *This;
This = Root->Next;
while( NULL != This && This-> != Number )
This = This->Next;
return This;
}
void
DeleteNode( Node **Root, int Number )
{
Node *This,*Next;
This = *Root;
for( Next = *Root;
NULL != Next && Next-> != Number;
Next = *Root )
Root = &Next->Next;
if( NULL == Next )
return;
else
{
This->Ption.HeadInformation.DeleteionNumber =
Next-> -=
Next->if( Next == This->Next && NULL == Next->Next )
This->Ption.HeadInformation.MaxNumber = 0;
else if( This->Ption.HeadInformation.MaxNumber ==
Next-> )
This->Ption.HeadInformation.MaxNumber -= 1;
*Root = Next->Next;
free( Next );
}
}
static void
Print( Node *Current );
void
PrintNode( Node *Root, int Number )
{
Node *This;
This = FindNode( Root, Number );
if( NULL == This )
return;
else
Print( This );
}
void
PrintList( Node *Root )
{
Node *This;
This = Root->Next;
printf("%-20s %6s %6s %10s\n","描述","编号","数量","总额");
while( NULL != This )
{
Print( This );
This = This->Next;
}
}
static void
Print( Node *Current )
{
if( !Current->Type )
{
printf( "所有零件总价值:%6.2lf\n", Current->Ption.HeadInformation.Total );
}
else
{
printf("%-20s %6d %6d %10.2lf\n",Current->,
Current->,
Current->,
Current->);
}
}
程序代码:#include <stdio.h> #ifndef getstring_h #define getstring_h char * getstring( FILE *FP ); char * getword( FILE *FP ); void getname( char *str, int size ); void getint( int *value ); void getdouble( double *value ); #endif
程序代码:#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "getstring.h"
#define INITSIZE 124
char *
getstring( FILE *FP )
{
char *Temp, *Str;
int CurrentSize, lx;
int ch;
CurrentSize = INITSIZE;
Temp = NULL;
Str = ( char * )malloc( INITSIZE * sizeof( char ) );
if( NULL != Str )
{
for( lx = 0; EOF != ( ch = getc(FP) ) && '\n' != ch; lx++ )
{
if( lx == CurrentSize - 1 )
{
CurrentSize += INITSIZE;
Temp = ( char * )realloc( Str, CurrentSize * sizeof( char ) );
if( NULL == Temp )
{
Str[ lx ] = '\0';
return Str;
}
Str = Temp;
}
Str[ lx ] = ch;
}
if( '\n' == ch )
Str[ lx++ ] = '\n';
Str[ lx ] = '\0';
if( 0 == lx )
{
free( Str );
Str = NULL;
return Str;
}
if( lx < CurrentSize )
{
Temp = ( char * )realloc( Str, ( lx + 1 ) * sizeof( char ) );
if( NULL == Temp )
return Str;
Str = Temp;
}
return Str;
}
else
return NULL;
}
char *
getword( FILE *FP )
{
char *Word, *Temp;
int ch;
int lx, CurrentSize;
CurrentSize = INITSIZE;
Temp = NULL;
Word = ( char * )malloc( CurrentSize * sizeof( char ) );
while( !isalpha( ch = getc( FP ) ) && EOF != ch )
;
if( NULL != Word )
{
for( lx = 0; EOF != ch && !isspace( ch ); lx++, ch = getc( FP ) )
{
if( lx == CurrentSize - 1 )
{
CurrentSize += INITSIZE;
Temp = ( char * )realloc( Word, CurrentSize * sizeof( char ) );
if( NULL == Temp )
{
Word[ lx ] = '\0';
return Word;
}
Word = Temp;
}
Word[ lx ] = ch;
}
if( 0 == lx )
{
free( Word );
Word = NULL;
return Word;
}
if( lx < CurrentSize )
{
Temp = ( char * )realloc( Word, ( lx + 1 ) * sizeof( char ) );
if( NULL == Temp )
{
Word[ lx ] = '\0';
return Word;
}
Word = Temp;
}
if( ispunct( Word[ lx - 1 ] ) )
{
Word[ --lx ] = '\0';
return Word;
}
Word[ lx ] = '\0';
return Word;
}
else
return NULL;
}
void
getname( char *str, int size )
{
int ch;
int i;
for(i = 0; i < size - 1 && EOF != ( ch = getchar() ) && '\n' != ch; i++ )
str[ i ] = ch;
if( '\n' == ch )
ungetc( ch, stdin );
while( getchar() != '\n' )
;
str[ i ] = '\0';
}
void
getint( int *value )
{
char *str;
str = getstring( stdin );
*value = atoi( str );
free( str );
}
void
getdouble( double *value )
{
char *str;
str = getstring( stdin );
*value = atof( str );
free( str );
}
[此贴子已经被作者于2017-3-24 08:43编辑过]



