[C] ACCP Phase I big homework

zhaozj2021-02-16  62

#include

#include

#include

Typedef struct

{

CHAR Name [31];

CHAR Add [51];

Long tel;

} consumer_det;

Void Sort (Consumer_Det * SCR, INT Y)

{

File * fp;

INT I, J, K, X;

Consumer_det temp [30];

IF ((fp = fopen ("telefon.dat", "rb")) == NULL)

{

Printf ("Cannot Open this file!");

exit (0);

}

For (i = 0;! feof (fp); i )

{

Fread (& Temp [I], SIZEOF (Consumer_Det), 1, FP);

IF (Feof (fp))

{

IF (y == 0) x = i 1;

IF (y == 1) x = I-1;

Break;

}

IF (y == 0)

IF (! STRCMP (Temp [i] .name, (* SCR) .Name) &&! strcmp (Temp [i] .add, (* SCR) .dd) && temp [i] .tel == (* SCR) .tel)

{

Printf ("/ N / TTHIS Consumer Already Existed! / N");

Return;

}

}

Fclose (fp);

IF ((fp = fopen ("telefon.dat", "wb")) == null)

{

Printf ("Cannot Open this file!");

exit (0);

}

IF (y == 0) // Insert record

{

For (k = i; strcmp (Temp [k] .name, (* SCR) .Name))> = 0 && k> = 0; K -)

{

Temp [K 1] = Temp [K];

}

Temp [K 1] = (* SCR);

}

IF (y == 1) // Delete record

{

For (k = 0; k <= i; k )

{

IF ((strcmp (Temp [k] .name, (* SCR) .Name))> = 0)

Temp [K] = Temp [K 1];

}

}

For (j = 0; J

FWRITE (& Temp [J], SIZEOF (Consumer_Det), 1, FP);

Fclose (fp);

}

Void input ()

{

INT flag = 'y';

Consumer_det DET;

While (flag == 'y' || flag == 'y')

{

SYSTEM ("CLS");

Printf ("/ t / tadd new subscriber Details);

DO

{

Printf ("/ N / NENTER SUBSCRIBER NAME (Upte 30 Chars):");

Fflush (stdin);

Scanf ("% s", DET.NAME);

IF (Strlen (DET.NAME)> 30)

Printf ("/ N / Tsubscriber Name Must Upte 30 Chars");

} while (strlen)> 30);

DO

{

Printf ("/ N / NENTER SUBSCRIBER ADDRESS (Upte 50 Chars):"); FFLUSH (stdin);

Scanf ("% s", DET.ADD);

IF (Strlen (DET.ADD)> 50)

Printf ("/ N / Tsubscriber Address Must Upte 50 Chars");

} while (strlen (DET.ADD)> 50);

DO

{

Printf ("/ N / NENTER SUBSCRIBER NUMBER:");

Fflush (stdin);

Scanf ("% ld", & det.tel;

IF (DET.TEL <4000000 || DET.TEL> 4999999)

Printf ("/ N / Tsubscriber Telephone Number Must Be Between 4000000 and 4999999);

WHILE (DET.TEL <4000000 || DET.TEL> 499999);

Sort (& DET, 0);

Printf ("/ n / ncontinue? (y / n)");

Fflush (stdin);

Scanf ("% C", & flag);

}

}

Void DisplayAll (Char SCR, INT X)

{

File * fp;

INT i, FLAG = 1, PointAdd1, PointAdd2;

Consumer_det temp;

Long Temp_tel;

CHAR TEMP_NAME [31];

IF ((fp = fopen ("telefon.dat", "rb")) == NULL)

{

Printf ("Cannot Open this file!");

exit (0);

}

SYSTEM ("CLS");

IF (SCR == 'T')

{

DO

{

Printf ("/ N / NENTER SUBSCRIBER NUMBER:");

Fflush (stdin);

Scanf ("% ld", & temp_tel);

IF (Temp_tel <4000000 || TEMP_TEL> 4999999)

Printf ("/ N / Tsubscriber Telephone Number Must Be Between 4000000 and 4999999);

WHILE (TEMP_TEL <4000000 || TEMP_TEL> 499999);

}

IF (SCR == 'N')

{

DO

{

Printf ("/ N / NENTER SUBSCRIBER NAME (Upte 30 Chars):");

Fflush (stdin);

Scanf ("% s", temp_name);

IF (Strlen (Temp_name)> 30)

Printf ("/ N / Tsubscriber Name Must Upte 30 Chars");

} while (strlen (TEMP_NAME)> 30);

}

SYSTEM ("CLS");

IF (SCR == 'a')

{

Printf ("/ N / TCONSUMER TOTOAL:");

Printf ("/ n / nNumber / T / TNAME / T / TADDRESS / T / T / TTELEPHONE / TFILEADD / N");

}

For (i = 0;! FeOf (fp); i ) {

FSeek (FP, I * Sizeof (consumer_det), 0);

Fread (& Temp, Sizeof (consumer_det), 1, fp);

PointAdd1 = FTELL (FP);

IF (PointAdd1 == POINTADD2)

Break;

IF (Temp.Tel> 4999999 || Temp.Tel <4000000)

Break;

IF (SCR == 'T')

{

IF (Temp_tel == Temp.tel)

{

Printf ("/ N / Trequested subscriber Details / N / NName:% S / NADDRESS:% S / NTELEPHONE NUMBER:% D / N", TEMP.NAME, TEMP.ADD, TEMP.TEL);

FLAG = 0;

}

}

IF (SCR == 'N')

{

IF (! strcmp (temp_name, temp.name))

{

Printf ("/ N / Trequested subscriber Details / N / NName:% S / NADDRESS:% S / NTELEPHONE NUMBER:% D / N", TEMP.NAME, TEMP.ADD, TEMP.TEL);

FLAG = 0;

IF (x == 1)

Sort (& TEMP, 1);

IF (x == 2)

{

Fflush (stdin);

GetChar ();

Sort (& TEMP, 1);

INPUT ();

}

}

}

IF (SCR == 'a')

{

Printf ("/ nRecord% -10D% -16S% -24S% -16LD% -d", i 1, temp.name, temp.add, temp.tel, ftell (fp));

FLAG = 0;

}

PointAdd2 = FTELL (FP);

}

IF (Flag)

{

Printf ("/ N / T / TTHE Record Is Not Exist!");

}

Fflush (stdin);

GetChar ();

Fclose (fp);

}

void main ()

{

INT Choice, FLAG = 1;

While (flag)

{

SYSTEM ("CLS");

printf ( "S19305 / nTELEPHONE DIRECTORY SYSTEM / n / n / t1. Add new subscriber details / n / t2. Modify existing subscriber details / n / t3. Delete existing subscriber details / n / t4. Display subscriber details based on telephone number / N / T5. Display Subscriber Details Based On Subscriber Name / N / T6. View Directory / N / T7. Quit / N / NENTER CHOICE: ");

Scanf ("% d", & choice);

IF (choice == 1)

INPUT ();

IF (choice == 2)

DisplayAll ('n', 2);

IF (choice == 3)

DisplayAll ('n', 1);

IF (choice == 4)

DisplayAll ('T', 0);

IF (choice == 5) DisplayAll ('n', 0);

IF (choice == 6)

DisplayAll ('a', 0);

IF (choice == 7)

exit (0);

IF (Choice> 7 || CHOICE <1)

{

Printf ("Error: please type.");

Fflush (stdin);

GetChar ();

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-20559.html

New Post(0)