📄 myc.c
字号:
#include <stdio.h>
#include <math.h>
void m();
void m1();
void m_1(int matrix, int pass, float duit);
void m_2();
void m_3();
void m_1_1(int matrix, int pass, float duit);
void m_1_2(int matrix, int pass, float duit);
void m_2_1();
void m_2_1_1();
void m_2_1_2();
void m_2_1_3();
void m_2_1_4();
void m_2_1_5();
void notfound();
void deduct(int matrix, int pass, float duit, float sum);
int main(void)
{
m();
return 0;
}
void m()
{
int choice;
do {
printf("Welcome To Cafe System V1.0\n");
printf("Choice Of Menu:\n\n");
printf("\t1. Login As User\n");
printf("\t2. Login As Administrator\n");
printf("\t3. Exit");
printf("\n\n");
printf("Choice: ");
scanf("%d", &choice);
if (choice<1 || choice >3) printf("\nError! Please select the right menu\n\n");
} while (choice<1 || choice >3);
if (choice == 1) { m1(); }
if (choice == 2) { m_2(); }
if (choice == 3) { m_3(); }
}
void m1()
{
int choice,matrix,pass,matrixInput,passInput;
int i=0;
int j=999;
float duit;
FILE *USER;
int matrix_[999];
int pass_[999];
float duit_[999];
int cubaan=3;
if ( ( USER = fopen( "user.txt", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
printf("Please Insert Your Matrix NO: ");
scanf("%d", &matrixInput);
fscanf( USER, "%d%d%f", &matrix, &pass, &duit );
while ( !feof( USER ) ) {
matrix_[ i ] = matrix;
if (matrix == matrixInput) { j=i; }
pass_[ i ] = pass;
duit_[ i ] = duit;
i++;
fscanf( USER, "%d%d%f", &matrix, &pass, &duit );
}
fclose( USER );
}
if (j==999) { notfound(); }
else {
printf("Id Found. Please Insert Your Password: ");
do {
scanf("%d", &passInput);
if (passInput == pass_[j]) { printf("Access Granted!\n\n"); }
if (passInput != pass_[j]) { printf("Access Denied\a! Your Password Is Wrong! You have %d try left!\n\nPassword: ", cubaan); cubaan=cubaan-1; }
if (cubaan < 0) { exit(0); }
} while (passInput != pass_[j]);
matrix=matrix_[j];
pass=pass_[j];
duit=duit_[j];
m_1(matrix,pass,duit);
}
}
void notfound() {
printf("Id Not Found! Please Retry!\n");
m();
}
void m_1(int matrix, int pass, float duit)
{
int choice;
do {
printf("Username : %d\n", matrix);
printf("Balance : RM %.2f\n", duit);
printf("WELCOME TO OUR USER SECTION!\n");
printf("Choice Of Menu:\n\n");
printf("\t 1. Change Password\n");
printf("\t 2. Buy Items\n");
printf("\t 3. Back\n");
printf("\t 4. Exit\n");
printf("\n\n");
printf("Choice: ");
scanf("%d", &choice);
if (choice<1 || choice >4) printf("\nError! Please select the right menu\n\n");
} while (choice<1 || choice >4);
if (choice == 1) { m_1_1(matrix,pass,duit); }
if (choice == 2) { m_1_2(matrix,pass,duit); }
if (choice == 3) { m1(); }
if (choice == 4) { exit(0); }
}
void m_2()
{
int choice;
int pass;
int passF;
int cubaan=3;
FILE *ADMIN;
if ( ( ADMIN = fopen( "admin.txt", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
fscanf( ADMIN, "%d", &passF );
printf("WELCOME TO OUR ADMINISTRATOR SECTION\n");
do {
printf("Please Enter Administrator Password (NUMBER ONLY):\n\n");
printf("Password: ");
scanf("%d", &pass);
if (pass == passF) { printf("Access Granted!\n"); }
if (pass != passF) { printf("Access Denied\a! Your Password Is Wrong! You have %d try left!\n", cubaan); cubaan=cubaan-1; }
if (cubaan < 0) { exit(0); }
} while (pass != passF);
m_2_1();
fclose( ADMIN );
}
}
void m_2_1()
{
int choice;
do {
printf("WELCOME TO OUR ADMINISTRATOR SECTION\n");
printf("Choice Of Menu:\n\n");
printf("\t 1. Change Admin Password\n");
printf("\t 2. Add User\n");
printf("\t 3. Delete User\n");
printf("\t 4. Add Funds To User\n");
printf("\t 5. Display User\n");
printf("\t 6. Back\n");
printf("\t 7. Exit\n");
printf("\n\n");
printf("Choice: ");
scanf("%d", &choice);
if (choice<1 || choice >7) printf("\nError! Please select the right menu\n\n");
} while (choice<1 || choice >7);
if (choice == 1) { m_2_1_1(); }
if (choice == 2) { m_2_1_2(); }
if (choice == 3) { m_2_1_3(); }
if (choice == 4) { m_2_1_4(); }
if (choice == 5) { m_2_1_5(); }
if (choice == 6) { m(); }
if (choice == 7) { exit(0); }
}
void m_3()
{
exit(0);
}
void m_1_1(int matrix, int pass, float duit)
{
int matrixF,passF,oldpass,newpass1,newpass2,newpass;
float duitF;
int i=0;
int j;
int k=999;
FILE *USER;
int matrix_[999];
int pass_[999];
float duit_[999];
if ( ( USER = fopen( "user.txt", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
fscanf( USER, "%d%d%f", &matrixF, &passF, &duitF );
while ( !feof( USER ) ) {
if (matrixF == matrix) { k=i; matrix_[i] = matrix; pass_[i] = pass; duit_[i] = duit; }
else { matrix_[ i ] = matrixF; pass_[ i ] = passF; duit_[ i ] = duitF; }
i++;
fscanf( USER, "%d%d%f", &matrixF, &passF, &duitF );
}
fclose( USER );
}
printf("Please Insert Your Old Password (NUMBER ONLY): ");
scanf("%d", &oldpass);
if ( pass != oldpass ) { printf("Error! Wrong Password Specified!\n"); m_1(matrix,pass,duit); }
do {
printf("Please Enter Your New Password (NUMBER ONLY): ");
scanf("%d", &newpass1);
printf("Please Enter Your New Password Again (NUMBER ONLY): ");
scanf("%d", &newpass2);
if (newpass1 != newpass2) { printf("Error! Password Doesnt Matched! Please Retry!\n\n"); }
} while (newpass1 != newpass2);
newpass=newpass1;
pass_[k]=newpass;
if ( ( USER = fopen( "user.txt", "w" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
for(j=0;j<i;j++) {
fprintf( USER , "%d %d %.2f\n", matrix_[ j ], pass_[ j ], duit_[ j ] );
}
}
fclose( USER );
printf("New Password = %d\n", newpass);
printf("Password Change SUCCESSFULLY! PLEASE REMEMBER IT!!\n\n");
m_1(matrix,pass,duit);
}
void m_1_2(int matrix, int pass, float duit)
{
int check,quantity,choice;
float price,sum=0;
printf("Username : %d\n", matrix);
printf("Balance : RM %.2f\n\n", duit);
do {
printf("Price Per Item : ");
scanf("%f", &price);
printf("Quantity : ");
scanf("%d", &quantity);
sum=(float)sum+(price*quantity);
printf("\n\nEnter Value -1 If You Want To Exit Or Others To Continue: ");
scanf("%d", &check);
} while (check != -1);
printf("Total need to pay = RM %.2f\n\n", sum);
do {
printf("Are You Sure?\n");
printf("\t1. Yes\n");
printf("\t2. No\n");
printf("Choice : ");
scanf("%d", &choice);
if (choice<1 || choice>2) { printf("\nError! Please select the right menu\n\n"); }
} while (choice<1 || choice>2);
if (choice == 1) { deduct(matrix,pass,duit,sum); }
if (choice == 2) { m_1(matrix,pass,duit); }
}
void deduct(int matrix, int pass, float duit,float sum) {
int matrixF,passF;
float duitF;
int i=0;
int j;
FILE *USER;
int matrix_[999];
int pass_[999];
float duit_[999];
if (duit < sum) { printf("\n\nError! Insufficient Funds!\n\n"); m_1(matrix,pass,duit); }
else {
duit=(float)duit-sum;
printf("New Balance : RM %.2f\n", duit);
if ( ( USER = fopen( "user.txt", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
fscanf( USER, "%d%d%f", &matrixF, &passF, &duitF );
while ( !feof( USER ) ) {
if (matrixF == matrix) { matrix_[i] = matrix; pass_[i] = pass; duit_[i] = duit; }
else { matrix_[ i ] = matrixF; pass_[ i ] = passF; duit_[ i ] = duitF; }
i++;
fscanf( USER, "%d%d%f", &matrixF, &passF, &duitF );
}
fclose( USER );
}
if ( ( USER = fopen( "user.txt", "w" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
for(j=0;j<i;j++) {
fprintf( USER , "%d %d %.2f\n", matrix_[ j ], pass_[ j ], duit_[ j ] );
}
}
fclose( USER );
}
m_1(matrix,pass,duit);
}
void m_2_1_1()
{
int pass,oldpass,newpass,newpass1,newpass2;
FILE *ADMIN;
if ( ( ADMIN = fopen( "admin.txt", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
printf("Please Enter Your Old Password: ");
scanf("%d", &oldpass);
fscanf( ADMIN , "%d", &pass );
if (oldpass != pass) { printf("ERROR! Wrong Password Inserted!\n"); m_2_1(); }
do {
printf("Please Enter Your New Password (NUMBER ONLY): ");
scanf("%d", &newpass1);
printf("Please Enter Your New Password Again (NUMBER ONLY): ");
scanf("%d", &newpass2);
if (newpass1 != newpass2) { printf("Error! Password Doesnt Matched! Please Retry!\n\n"); }
} while (newpass1 != newpass2);
newpass=newpass1;
fclose( ADMIN );
}
if ( ( ADMIN = fopen( "admin.txt", "w" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
fprintf( ADMIN , "%d\n" , newpass );
fclose( ADMIN );
}
m_2_1();
}
void m_2_1_2()
{
int matrix;
int pass;
float duit;
FILE *USER;
printf("Please Insert Details Below:\n");
printf("Matrix No: ");
scanf("%d", &matrix);
printf("Password: ");
scanf("%d", &pass);
printf("Money: ");
scanf("%f", &duit);
if ( ( USER = fopen( "user.txt", "a" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
fprintf( USER, "%d %d %.2f\n", matrix, pass, duit);
printf("User Has SUCCESSFULLY ADDED!\n");
}
fclose ( USER );
m_2_1();
}
void m_2_1_3()
{
printf("\n\nComing Soon!!!\n\n");
m_2_1();
}
void m_2_1_4()
{
int matrixF,passF,matrix,pass;
float duit,duitF,dummy;
int i=0;
int j;
int k=999;
FILE *USER;
int matrix_[999];
int pass_[999];
float duit_[999];
printf("Please Insert Student`s Matrix NO: ");
scanf("%d", &matrix);
printf("Please Insert Funds: ");
scanf("%f", &duit);
if ( ( USER = fopen( "user.txt", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
fscanf( USER, "%d%d%f", &matrixF, &passF, &duitF );
while ( !feof( USER ) ) {
if (matrixF == matrix) { k=i; matrix_[i] = matrix; pass_[i] = passF; duit_[i] = duitF; }
else { matrix_[ i ] = matrixF; pass_[ i ] = passF; duit_[ i ] = duitF; }
i++;
fscanf( USER, "%d%d%f", &matrixF, &passF, &duitF );
}
fclose( USER );
}
dummy = duit_[k];
duit = duit+dummy;
duit_[k] = duit;
if ( ( USER = fopen( "user.txt", "w" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
for(j=0;j<i;j++) {
fprintf( USER, "%d %d %.2f\n", matrix_[ j ], pass_[ j ], duit_[ j ] );
}
fclose( USER );
}
m_2_1();
}
void m_2_1_5() {
int matrixF,passF;
float duitF;
int i=0;
int j;
int k=999;
FILE *USER;
int matrix_[999];
int pass_[999];
float duit_[999];
if ( ( USER = fopen( "user.txt", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
fscanf( USER, "%d%d%f", &matrixF, &passF, &duitF );
while ( !feof( USER ) ) {
matrix_[ i ] = matrixF;
pass_[ i ] = passF;
duit_[ i ] = duitF;
i++;
fscanf( USER, "%d%d%f", &matrixF, &passF, &duitF );
}
printf("\n\n\n");
printf("Displaying User:\n");
printf("No. Matrix No Password Money\n");
for(j=0;j<i;j++) {
printf( " %d %d %d RM%7.2f\n",j+1, matrix_[ j ], pass_[ j ], duit_[ j ] );
}
printf("\n\n\n");
fclose( USER );
}
m_2_1();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -