課程設計:学生のコンピューター管理システム
, 。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<io.h>
#define N 100
/*
** ,
**
*/
struct student // ,
{
char time[ 20 ]; //
char name[ 200 ]; //
char num[ 200 ]; //
char computer[ 5 ]; //
char fee[ 200 ]; // , 20
char sex; //
} student[ N ];
/*
** ,
*/
int menu();
int initial_password();
int login( int k );
int change_password();
int browse();
int add();
int search();
int search_byname( int z );
int search_bynum( int z );
int modify();
int save( int n );
int load();
int input( int i );
int ensure_input( int i, int n );
char modify_data( int i, int n );
int print_head();
int print_record( int i );
int after_print();
int syscom();
int sort();
int date();
int del();
int
main()
{
int k; //
/*
**
*/
printf( "
" );
printf( "
" );
printf( "
" );
printf( "
" );
printf( " ---------------------------------------------------------------" );
printf( "
" );
printf( " | ***-------------------*** ***--------------------*** |" );
printf( "
" );
printf( " ---------------------------------------------------------------" );
printf( "
" );
printf( "
" );
printf( "
" );
printf( " ---------------------------------------------------------------" );
printf( "
" );
printf( " | ***-------------------*** ***--------------------*** |" );
printf( "
" );
printf( " ---------------------------------------------------------------" );
printf( "
" );
printf( "
" );
printf( "
" );
printf( " ---------------------------------------------------------------" );
printf( "
" );
printf( " | ***-------------- -------------*** |" );
printf( "
" );
printf( " ---------------------------------------------------------------" );
printf( "
" );
printf( "
" );
printf( "
" );
printf( "
" );
/*
**
*/
system( "pause" );
system( "cls" );
if( ( access( "password.dat", 0 ) ) == -1 )
{
k = initial_password();
}
else
k = 1;
login( k );
return 0;
}
int
menu() //
{
int n; //
int w = 1; //
system( "cls" );
getchar();
printf( "
" );
printf( "\t\t|***************** *********************|
" );
printf( "\t\t|***************** 1. ***********|
" );
printf( "\t\t|***************** 2. ***********|
" );
printf( "\t\t|***************** 3. ***********|
" );
printf( "\t\t|***************** 4. ***********|
" );
printf( "\t\t|***************** 5. ***********|
" );
printf( "\t\t|**********************************************|
" );
do
{
printf( "
\t (1~5):[ ]\b\b" );
scanf( "%d", &n );
if( n < 1 || n > 5 )
{
if( ( w % 2 ) == 0 )
{
system( "cls" );
getchar();
printf( "
" );
printf( "\t\t|***************** *********************|
" );
printf( "\t\t|***************** 1. ***********|
" );
printf( "\t\t|***************** 2. ***********|
" );
printf( "\t\t|***************** 3. ***********|
" );
printf( "\t\t|***************** 4. ***********|
" );
printf( "\t\t|***************** 5. ***********|
" );
printf( "\t\t|**********************************************|
" );
}
w++;
printf( "
\t , 1~5 。
" );
}
else
w = 0;
} while( w >= 1 );
switch( n )
{
case 1: change_password();break;
case 2: syscom();break;
case 3: search();break;
case 4: date();break;
case 5: printf( "
" );exit( 0 );
}
return 0;
}
int
initial_password() // , !!!
{
char key_word[ 200 ];
FILE *key;
int w;
if( ( key = fopen( "password.dat", "wb" ) ) == NULL )
{
printf( " !
" );
printf( " 。
" );
exit( 0 );
}
//
printf( " , ( 15 )
" );
printf( ":" );
scanf( "%s", key_word );
w = strlen( key_word );
if( w >= 15 )
key_word[ 15 ] = '\0';
if( fwrite( &key_word, sizeof( key_word ), 1, key ) != 1 )
{
printf( " !
" );
printf( " 。
" );
exit( 0 );
}
fclose( key );
return 1;
}
int
login( int k ) //
{
int w = 0; //
int w1 = 0; //
char temp_key[ 200 ];
char key_word[ 200 ];
FILE *key;
fflush( stdin );
if( k == 1 )
{
system( "cls" );
printf( " ---------------------------------------------------------------" );
printf( "
" );
printf( " | ***------ ( , )-----*** |" );
printf( "
" );
printf( " ---------------------------------------------------------------" );
printf( "
" );
printf( " :" );
if( ( key = fopen( "password.dat", "rb" ) ) == NULL )
{
printf( " !
" );
printf( " 。
" );
exit( 0 );
}
fread( &key_word, sizeof( key_word ), 1, key );
do
{
scanf( "%s", &temp_key );
if( ( strcmp( temp_key, key_word ) ) == 0 )
{
menu();
exit( 0 );
}
else
{
if( w1 > 2 )
{
printf( "
, 。
" );
system( "pause" );
exit( 0 );
}
printf( "
, 。
:" );
w = 1;
w1++;
}
} while( w == 1 );
}
else
{
printf( " ,
" );
exit( 0 );
}
fclose( key );
return 0;
}
int
change_password() //
{
FILE *key;
char temp_key[ 200 ]; //
char cmp_key[ 200 ]; //
int w = 0; //
do
{
if( w != 0 )
{
printf( "
\t !!!
" );
}
printf( "
\t :" );
scanf( "%s", temp_key );
printf( "\t :" );
scanf( "%s", cmp_key );
} while( ( w = strcmp( temp_key, cmp_key ) ) != 0 );
if( ( key = fopen( "password.dat", "wb" ) ) == NULL )
{
printf( " !
" );
printf( " 。
" );
exit( 0 );
}
if( fwrite( &cmp_key, sizeof( cmp_key ), 1, key ) != 1 )
{
printf( " !
" );
printf( " 。
" );
exit( 0 );
}
else
printf( "
\t !!!" );
fclose( key );
printf( "
" );
system( "pause" );
menu();
return 0;
}
int
syscom() //
{
int n; //
int w = 1; //
system( "cls" );
getchar();
printf( "
" );
printf( "\t\t|***************** *****************|
" );
printf( "\t\t|***************** 1. *******|
" );
printf( "\t\t|***************** 2. *******|
" );
printf( "\t\t|***************** 3. *******|
" );
printf( "\t\t|***************** 4. *******|
" );
printf( "\t\t|***************** 5. *******|
" );
printf( "\t\t|***************** 6. *******|
" );
printf( "\t\t|***************** 7. *******|
" );
printf( "\t\t|************************************************|
" );
do
{
printf( "
\t (1~7):[ ]\b\b" );
scanf( "%d", &n );
if( n < 1 || n > 7 )
{
if( ( w % 2 ) == 0 )
{
system( "cls" );
getchar();
printf( "
" );
printf( "\t\t|***************** *****************|
" );
printf( "\t\t|***************** 1. *******|
" );
printf( "\t\t|***************** 2. *******|
" );
printf( "\t\t|***************** 3. *******|
" );
printf( "\t\t|***************** 4. *******|
" );
printf( "\t\t|***************** 5. *******|
" );
printf( "\t\t|***************** 6. *******|
" );
printf( "\t\t|***************** 7. *******|
" );
printf( "\t\t|************************************************|
" );
}
w++;
printf( "
\t , 1~7 。
" );
}
else
w = 0;
} while( w >= 1 );
switch( n )
{
case 1: add();break;
case 2: modify();break;
case 3: del();break;
case 4: browse();break;
case 5: sort();break;
case 6: menu();break;
case 7: exit( 0 );
}
return 0;
}
int
search() //
{
int z = -1; //
int n; //
int m; //
int i; //
n = load();
if( n == 0 )
{
system( "cls" );
printf( " !!!" );
printf( "
" );
system( "pause" );
menu();
}
system( "cls" );
printf( " ?( , )
" );
printf( "1. \t2. \t3. : [ ]\b\b" );
scanf( "%d", &m );
if( m == 1 )
i = search_bynum( z );
else if( m == 2 )
i = search_byname( z );
else if( m == 3 )
menu();
else
{
printf( "
!
" );
printf( "
" );
system( "pause" );
menu();
}
printf( "
:
");
print_head();
print_record( i );
printf( "
" );
system( "pause" );
menu();
return 0;
}
int
search_byname( int z )
{
int w0; //
int w = -1; //
int i; //
int n; //
struct student temp;
n = load();
printf( "
:" );
scanf( "%s", temp.name );
for( i = 0; i < n; i++ )
if( !( w0 = strcmp( temp.name, student[ i ].name ) ) )
{
w = 1;
break;
}
if( w == 1 )
return i;
else if( w == -1 )
{
if( z == -1 )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
menu();
}
else if ( z == 1 )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
syscom();
}
}
return 0;
}
int
search_bynum( int z )
{
int w0; //
int w = -1; //
int i; //
int n; //
int flag = 0; //
int m = 0; //
struct student temp;
n = load();
do
{
printf( "
:" );
scanf( "%s", temp.num );
for( i = 0; temp.num[ i ] != '\0'; i++ )
if( temp.num[ i ] > '9' )
{
printf( "
, 0~9 。
!
" );
flag = 1;
m++;
break;
}
else
flag = 0;
if( m > 2 )
{
if( z == -1 )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
menu();
}
else if ( z == 1 )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
syscom();
}
}
} while( flag );
for( i = 0; i < n; i++ )
if( !( w0 = strcmp( temp.num, student[ i ].num ) ) )
{
w = 1;
break;
}
if( w == 1 )
return i;
else if( w == -1 )
{
if( z == -1 )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
menu();
}
else if ( z == 1 )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
syscom();
}
}
return 0;
}
int
date()
{
system( "cls" );
printf( " :" );
system( "echo %date:~0,14%" );
printf( " :");
system( "echo %time:~0,5%" );
printf( "
" );
system( "pause" );
menu();
return 0;
}
int
add() //
{
int i; //
int n; //
int m; //
int k; //
FILE *stu;
n = load();
printf( "\t (0~%d)?( 0 , ^,^)
\t:", N - 1 - n );
scanf( "%d", &m );
while( m < 0 || m > ( N - 1 - n ) )
{
printf( "
\t !
\t !
\t (0~%d)?
\t:", N - 1 - n );
getchar();
scanf( "%d", &m );
}
k = m + n;
for( i = n; i < k; i++ )
{
system( "cls" );
printf( " %d
", i - n + 1 );
input( i );
}
/*
**
*/
if( ( stu = fopen( "student_ID.dat", "a" ) ) == NULL )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
syscom();
}
for( i = n; i < k; i++ )
if( fwrite( &student[ i ], sizeof( struct student ), 1, stu ) != 1 )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
syscom();
}
fclose( stu );
after_print();
return 0;
}
int
modify() //
{
int z = 1; //
char w; //
int i; // ,
int j; //
int n; //
struct student temp;
n = load();
if( n == 0 )
{
system( "cls" );
printf( " !!!" );
printf( "
" );
system( "pause" );
syscom();
}
print_head();
for( j = 0; j < n; j++ )
{
if( ( j != 0 ) && ( j % 10 == 0 ) )
{
printf( "
( )
" );
system( "pause" );
system( "cls" );
}
print_record( j );
}
printf( "
?
" );
i = search_bynum( z );
w = modify_data( i, n );
if( w == '1' )
{
save( n );
after_print();
}
else if( w == '3' )
{
printf( "
" );
system( "pause" );
syscom();
}
return 0;
}
char
modify_data( int i, int n ) //
{
int m; //
int j = 0; //
int flag = 0; //
char w; //
int a; //
int k; //
do // !!!???
{
system( "cls" );
printf( " : 1. 2. 3. 4. 5.
" );
printf( " : [ ]\b\b" );
scanf( "%d", &m );
while( m < 1 || m > 5 )
{
getchar();
printf( "
! !
");
printf( " : 1. 2. 3. 4. 5.
" );
printf( " : [ ]\b\b" );
scanf( "%d", &m );
}
switch( m )
{
case 1:
do
{
printf( "
:" );
scanf( "%s", student[ i ].num );
for( a = 0; student[ i ].num[ a ] != '\0'; a++ )
if( student[ i ].num[ a ] > '9' )
{
printf( "
, 0~9 。
!
" );
flag = 1;
j++;
break;
}
else
flag = 0;
if( flag != 1 )
for( k = 0; k < n; k++ )
if( k != i )
if( strcmp( student[ k ].num, student[ i ].num ) == 0 )
{
printf( "
。
!
" );
flag = 1;
}
if( j > 2 )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
syscom();
}
} while( flag );
break;
case 2:
printf( "
:" );
scanf( "%s", student[ i ].name );
break;
case 3:
getchar();
do
{
printf( "
( m, f):" );
scanf( "%c", &student[ i ].sex );
if( student[ i ].sex != 'm' && student[ i ].sex != 'f' )
{
getchar();
printf( "
, m, f
!
" );
flag = 1;
}
else
flag = 0;
} while( flag );
break;
case 4:
do
{
printf( "
:" );
scanf( "%s", student[ i ].fee );
for( a = 0; student[ i ].fee[ a ] != '\0'; a++ )
if( student[ i ].fee[ a ] > '9' )
{
printf( "
, 0~9 。
!
" );
flag = 1;
j++;
break;
}
else
flag = 0;
if( j > 2 )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
syscom();
}
} while( flag );
break;
case 5:
printf( "
" );
system( "pause" );
syscom();
break;
}
print_head();
print_record( i );
printf( "
?
" );
printf( "1. 2. , 3. : [ ]\b\b" );
getchar();
scanf( "%c", &w );
while( w < '1' || w > '3' )
{
getchar();
printf( "
! !
");
printf( "
?
" );
printf( "1. 2. , 3. : [ ]\b\b" );
scanf( "%c", &w );
if( isspace( w ) )
getchar();
}
} while( w == '2' );
return w;
}
int
del() //
{
int z = 1; //
int j; //
int m; //
char w; //
int n; //
int i = -1; //
n = load();
if( n == 0 )
{
system( "cls" );
printf( " !!!" );
printf( "
" );
system( "pause" );
syscom();
}
print_head();
for( j = 0; j < n; j++ )
{
if( ( j != 0 ) && ( j % 10 == 0 ) )
{
printf( "
( )
" );
system( "pause" );
system( "cls" );
}
print_record( j );
}
printf( "
?
" );
i = search_bynum( z );
printf( "
?
" );
printf( "1. \t2. : [ ]\b\b" );
getchar();
scanf( "%c", &w );
while( w < '1' || w > '2' )
{
getchar();
printf( "
! !
");
printf( "
?
" );
printf( "1. \t2. : [ ]\b\b" );
scanf( "%c", &w );
}
if( w == '1' )
{
print_head();
print_record( i );
for( j = i; j < n; j++ )
{
strcpy( student[ j ].name, student[ j + 1 ].name );
strcpy( student[ j ].num, student[ j + 1 ].num );
student[ j ].sex = student[ j + 1 ].sex;
}
save( n - 1 );
}
else if( w == '2' )
syscom();
else
{
printf( "
!
" );
printf( "
" );
system( "pause" );
syscom();
}
system( "pause" );
after_print();
return 0;
}
int
browse() //
{
int i; //
int n; //
n = load();
print_head();
for( i = 0; i < n; i++ )
{
if( ( i != 0 ) && ( i % 10 == 0 ) )
{
system( "pause" );
system( "cls" );
}
print_record( i );
}
printf( " %d 。
", n );
printf( "
。
" );
system( "pause" );
syscom();
return 0;
}
int
sort() // ( )
{
int i, j; //
int n; //
char w; //
char tmp[ 200 ];
char temp;
n = load();
system( "cls" );
if( n == 0 )
{
printf( " !!!" );
printf( "
" );
system( "pause" );
syscom();
}
printf( " ?
" );
printf( "1. \t2. \t3. : [ ]\b\b" );
getchar();
scanf( "%c", &w );
while( w < '1' || w > '3' )
{
getchar();
printf( "
! !
");
printf( " ?
" );
printf( "1. \t2. \t3. : [ ]\b\b" );
scanf( "%c", &w );
}
if( w == '1' )
{
for( i = 0; i < n - 1; i++ )
for( j = 0; j < n - 1 - i; j++ )
if( atoi( student[ j ].num ) > atoi( student[ j + 1 ].num ) )
{
strcpy( tmp, student[ j + 1 ].num );
strcpy( student[ j + 1 ].num, student[ j ].num );
strcpy( student[ j ].num, tmp );
strcpy( tmp, student[ j + 1 ].name );
strcpy( student[ j + 1 ].name, student[ j ].name );
strcpy( student[ j ].name, tmp );
strcpy( tmp, student[ j + 1 ].fee );
strcpy( student[ j + 1 ].fee, student[ j ].fee );
strcpy( student[ j ].fee, tmp );
temp = student[ j + 1 ].sex;
student[ j + 1 ].sex = student[ j ].sex;
student[ j ].sex = temp;
}
}
else if( w == '2' )
{
for( i = 0; i < n - 1; i++ )
for( j = 0; j < n - 1 - i; j++ )
if( atoi( student[ j ].fee ) > atoi( student[ j + 1 ].fee ) )
{
strcpy( tmp, student[ j + 1 ].num );
strcpy( student[ j + 1 ].num, student[ j ].num );
strcpy( student[ j ].num, tmp );
strcpy( tmp, student[ j + 1 ].name );
strcpy( student[ j + 1 ].name, student[ j ].name );
strcpy( student[ j ].name, tmp );
strcpy( tmp, student[ j + 1 ].fee );
strcpy( student[ j + 1 ].fee, student[ j ].fee );
strcpy( student[ j ].fee, tmp );
temp = student[ j + 1 ].sex;
student[ j + 1 ].sex = student[ j ].sex;
student[ j ].sex = temp;
}
}
else if( w == '3' )
syscom();
else
{
printf( "
!
" );
printf( "
" );
system( "pause" );
syscom();
}
save( n );
after_print();
return 0;
}
int
save( int n ) //
{
FILE *stu; //
int i; //
if( ( stu = fopen( "student_ID.dat", "w" ) ) == NULL )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
menu();
}
for( i = 0; i < n; i++ )
if( fwrite( &student[ i ], sizeof( struct student ), 1, stu ) != 1 )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
menu();
}
fclose( stu );
return 0;
}
int
load() //
{
FILE *stu; //
int i; //
if( ( stu = fopen( "student_ID.dat", "r" ) ) == NULL )
return 0; //
for( i = 0; !feof( stu ); i++ )
fread( &student[ i ], sizeof( struct student ), 1, stu );
fclose( stu );
return ( i - 1 );
}
int
input( int i ) //
{
int w; //
strcpy( student[ i ].time, "2011-7-14" );
strcpy( student[ i ].computer, "007" );
strcpy( student[ i ].fee, "20" );
do
{
printf( " :" );
getchar();
scanf( "%s", student[ i ].name );
printf( "
( m, f):" );
getchar();
scanf( "%c", &student[ i ].sex );
printf( "
( :4100121):" );
getchar();
scanf( "%s", student[ i ].num );
} while( ( w = ensure_input( i, i ) ) );
return 0;
}
int
ensure_input( int i, int n ) // , ,
{
int j, k; //
int flag = 0; //
if( student[ i ].sex != 'm' && student[ i ].sex != 'f' )
{
printf( "
, m, f
!
" );
flag = 1;
}
for( j = 0; student[ i ].num[ j ] != '\0'; j++ )
if( student[ i ].num[ j ] > '9' )
{
printf( "
, 0~9 。
!
" );
flag = 1;
break;
}
if( flag != 1 )
for( k = 0; k < n; k++ )
if( k != i )
if( strcmp( student[ k ].num, student[ i ].num ) == 0 )
{
printf( "
。
!
" );
flag = 1;
return flag;
}
return flag;
}
int
print_head() //
{
system( "cls" );
printf( "
" );
return 0;
}
int
print_record( int i ) //
{
printf( "%-7s %-15s %c %-9s %-3s %s
", student[ i ].num, student[ i ].name, student[ i ].sex, student[ i ].time, student[ i ].computer, student[ i ].fee );
return 0;
}
int
after_print() //
{
char w; //
int flag = 0; //
system( "cls" );
printf( "------------------------
" );
printf( "| !!! |
" );
printf( "| ? |
" );
printf( "------------------------" );
printf( "
1. \t2. \t3. : [ ]\b\b" );
getchar();
scanf( "%c", &w );
while( w < '1' || w > '3' )
{
printf( "
!
" );
printf( "
" );
system( "pause" );
system( "cls" );
printf( "------------------------
" );
printf( "| !!! |
" );
printf( "| ? |
" );
printf( "------------------------" );
printf( "
1. \t2. \t3. : [ ]\b\b" );
getchar();
scanf( "%c", &w ); // !
if( iscntrl( w ) )
getchar();
}
if( w =='1' )
browse();
else if( w == '2' )
menu();
else if( w == '3' )
syscom();
return 0;
}