C Programming Skills

Source: Internet
Author: User
Tags clear screen

Programming function set. The details are as follows:

Read Keyboard Scan code
Get machine date
Converts a floating point number to a string.
Clear Screen
Display time
Print a rectangle
Trim all spaces in the string
Substring
Read characters from a file
String Matching on the left
Returns the left string.
Write data to a file
Right string
Turn on or off the cursor
Speaker voice
Time Delay
Opening Time
Write the integer in the file
Read integers from files
Alarm
String Matching on the right
Center string
Delete a substring
Search for a specified string
Generate Space
Generate string
Trim the left space of the string
Trim spaces on the right of the string
Display a string
Define screen color
Display Prompt window display warning window
Get the file length
Window scrolling
Scan keyboard
Insert string
Check whether the disk is ready
Check whether the disk is write-protected
Modify a row of a file
Copy objects in batches
Copy an object
Create directory
Get directory
Get File Name
Add any two positive integers (<80 bits)
Multiply any two positive integers

Note: Many TC header files are omitted in this function set. You must add them when using this function. In addition, the internal functions of this function set have a call relationship, so you should be cautious when disassembling the function set.

/* A collection of programming skills */

/* Read characters from the keyboard function: When you press the normal key, return its ASCII code scan code = 0 */
Int inkey (int * Code)
{
Int m;
While (! Bioskey (1)/* can be added to the ERA code without buttons */;
* Code = bioskey (0 );
M = * Code * 255;
If (! M) M = * code> 8;
* Code = x code & 255;
Return m;
}

/* Get the machine date */
Int date (char * s, char type)
{
Char dat [30];
Int num;
Struct TM * tblock;
Time_t T;
T = time (null );
Tblock = localtime (& T );
Strcpy (DT, asctime (tblock ));
Strcpy (S ,"");
Switch (type)
{
Case 'N ':
Num= (* tblock). tm_year + 1900;
ITOA (Num, s, 10 );
Break;
Case 'y ':
Num = (* tblock). tm_mon + 1;
ITOA (Num, s, 10 );
Break;
Case 'r ':
Num = (* tblock). tm_mday;
ITOA (Num, s, 10 );
Break;
Case's ':
Strcpy (DT, asctime (tblock ));
Mid (S, DT, 12, 8 );
Break;
}
}
Return num;
}

/* Convert a floating point number to a string */
/* Parameter description data: floating point number to be converted; s: Output string; Len: converted length */
Void f_to_s (double data, char * s, int Len)
{
Int Dec, sign, I;
Char * S1, S2 [100], S3 [100];
S1 = 0;
S2 [0] = 0;
S3 [0] = 0;
S1 = fcvt (data, Len, & dec, & sign );
If (! Sign & Data> = 1)
{
Mid (S2, S1, Dec + 1,-1 );
Mid (S3, S1, 1, DEC );
Strcpy (S, "+ ");
Strcat (S, S3 );
Strcat (S ,".");
Strcat (S, S2 );
}
If (sign & FABS (data)> = 1)
{
Mid (S2, S1, Dec + 1,-1 );
Strcpy (S ,"-");
Mid (S3, S1, 1, DEC );
Strcat (S, S3 );
Strcat (S ,".");
Strcat (S, S2 );
}
If (! Sign & dec = 0)
{
Strcpy (S, "+ 0 .");
Strcat (S, S1 );
}
If (sign & dec = 0)
{
Strcpy (S, "-0 .");
Strcat (S, S1 );
}
If (! Sign & dec <0)
{
Strcpy (S, "+ 0 .");
For (I = 1; I <= FABS (DEC); I ++)
Strcat (S, "0 ");
Strcat (S, S1 );
}
If (sign & dec <0)
{
Strcpy (S, "-0 .");
For (I = 1; I <= FABS (DEC); I ++)
Strcat (S, "0 ");
Strcat (S, S1 );
}
If (strlen (s)> Len) s [Len] = 0;
}

/* Clear the screen */
Void clsxy (INT color, int X, int y, int XL, int yl)
{
Int X1, Y1;
Union regs R;
If (x <1 | Y <1) return;
Y --;
X --;
Y1 = Y + YL-1;
X1 = x + XL-1;
If (Y1> 25 | X1> 80)
Return;
R. H. Ah = 6;/* sub-function number */
R. H. Al = 0;/* Number of rolling rows */
R. H. Ch = y;
R. H. Cl = X;
R. H. DH = Y1;
R. H. DL = x1;
R. H. bH = color * 16;
Int86 (16, & R, & R );
}

/* Display time */
Void display_time (INT color, int back_color, int y, int X)
{
Static char oldtime [9] = "";
Char newtime [9];
If (Y <1 | x <1) return;
Settextstyle (1, 0, 1 );
Data (newtime,'s ');
Back_color = 7;
If (strcmp (newtime, oldtime ))
{
Setfillstyle (1, back_color );
Setcolor (color );
Bar (535,458,635,475 );
Outtextxy (X, Y, newtime );
Strcpy (oldtime, newtime );
}
}

/* Print a rectangle */
Void print_kj (int x, int y, int wide, int high)
{
Int I;
For (I = x; I <X + wide; I + = 2)
{
Gotoxy (I, y );
Cprintf ("");
}
Gotoxy (X, Y + high );
Cprintf ("");
Gotoxy (x + wide, Y + high );
Cprintf ("");
Gotoxy (x, y );
Cprintf ("");
Gotoxy (x + wide, y );
Cprintf ("");
For (I = x + 2; I <X + wide; I + = 2)
{
Gotoxy (I, Y + high );
Cprintf ("");
}
For (I = Y + 1; I <Y + high; I ++)
{
Gotoxy (X, I );
Cprintf ("");
}
For (I = Y + 1; I <Y + high; I ++)
{
Gotoxy (x + wide, I );
Cprintf ("");
}
}

/* Cut down all spaces in the string */
Void cut_all_spc (char * s)
{
Int I, N;
Char d [10000];
N = 0;
For (I = 0; I <strlen (s); I ++)
If (s! = 32)
{
D [N] = s;
N ++;
}
D [N] = 0;
Strcpy (S, d );
}

/* Obtain the sub-string */
Void mid (char * s, char * t, int N, int m)
{
Int I, j, P;
If (n <1) n = 1;
I = strlen (s );
If (I <n) m = 0;
If (M <0) M = I;
Else M = N + m-1;
If (M> I) M = I;
P = m-n + 1;
If (p <0) p = 0;
For (I = n-1, j = 0; I <m; I ++, J ++)
T [J] = s;
T [p] = 0;
}

/* Read characters from the file */
Int read_str (char * s, file * FP)
{
Int I = 0;
If (! FP) return 0;
If (fgets (S, 10000, FP) I = 1;
S [strlen (S)-1] = 0;
Return I;
}

/* String matching on the left */
Void move_left (char * D, char * s, int N)
{
Int I, L;
L = strlen (s );
If (n> L) n = L;
For (I = 0; I <L; I ++)
* D ++ = * s ++;
* D = 0;
}

/* Take the left string */
Void left (char * D, char * s, int N)
{
Int I, L;
I = 0;
L = strlen (s );
If (n> L) n = L;
For (I = 0; I <n; I ++)
D = s;
D [N] = 0;
}

/* Write data to a file */
Void write_str (char * s, file * FP)
{
Char c = 10;
If (! FP) return;
Fputs (S, FP );
Fputc (C, FP );
}

/* Obtain the right string */
Void right (char * DEST, char * Source, int num)
{
Int I, J;
If (Num <1) num = 0;
Num = strlen (source)-num;
If (Num <0) num = 0;
For (I = 0, j = num; j <= strlen (source); I ++, J ++) DEST = source [J];
}

/* Open or close the cursor */
Void cursor (INT on2off)
{
Union regs R;
If (on2off! = OFF) on2off = 10;
R. H. Ah = 1;
R. H. Ch = 3;
R. H. Cl = on2off;
Int86 (16, & R, & R );
}

/* Speaker */
Void sound (INT frequency, int time)
{
Int I;
I = time * 50;
If (I> 30000) I = 30000;
If (I <50) I = 50;
Sound (frequency );
Mydelay (I );
Nosound ();
}

/* Time Delay */
Void mydelay (long T)
{
Time_t oldtime;
Long t0;
T0 = T/55;
If (T0 <1) t0 = 1;
Oldtime = clock ();
While (clock ()-oldtime <T0 );
}

/* Start time */
Void report_clock (void)
{
Int I;
For (I = 0; I <5; I ++ );
{
Sound );
Mydelay (1000 );
}
Sound (800,10 );
}

/* Write the integer in the file */
Void write_int (INT num, file * P)
{
Char s [20], A = 10;
If (! P) return;
ITOA (Num, s, 10 );
Fputs (S, P );
Fputc (A, P );
}

/* Read integers from the file */
Int read_int (int * num, file * P)
{
Int I;
Char s [30] = "";
If (! P) return 0;
If (fgets (S, 10000, p ))
{
I =-1;
S [strlen (S)-1] = 0;
* Num = atoi (s );
}
Else I = 0;
Return I;
}

/* Alarm */
Void warn (void)
{
Sound );
Sound (100,1 );
}

/* String matching on the right */
Void move_right (char * s, int wide)
{
Int I, L, N;
L = strlen (s );
N = wide-L;
If (n> 0)
{
For (I = L; I>-1; I --) s [I + N] = s;
For (I = 0; I <n; I ++) S = 32;
}
}

/* Center the string */
Void move_middle (char * s, int wide)
{
Int I, L, N;
L = strlen (s );
If (wide> L)
{
Wide = wide-1;
N = wide/2;
Wide = wide-N;
For (I = L; I>-1; I --) s [I + N] = s;
For (I = 0; I <n; I ++) S = 32;
For (I = 0; I <wide; I ++) s [L + N + I] = 32;
S [L + N + I] = 0;
}
}

/* Delete the substring */
Void delete_substring (char * Source, int start, int num)
{
Int I, L;
L = strlen (source );
If (Num> L-start + 1 | num =-1) num = L-start + 1;
If (start <1 | Start> 1) return;
For (I = start; I <L-num + 2; I ++)
Source [I-1] = source [I + num-1];
}

/* Find the specified string */
Int instr (int n, char * Source, char * DEST)
{
Int I, j, K1, K2, P;
Int start = 0;
If (n = 0) n = 1;
K1 = strlen (source );
K2 = strlen (DEST );
If (n <0)
{
Char s [100];
N =-N;
Mid (S, source, N, K2 );
If (strcmp (S, DEST) return 0;
Return N;
}
If (k1-n + 1 <k2) return start;
For (I = n-1; I <K1; I ++)
{
P = 0;
For (j = 0; j <k2; j ++)
If (source [I + J]! = Dest [J]) break;
Else P ++;
If (P = k2)
{
Start = I + 1;
Break;
}
}
Return start;
}

/* Generate space */
Void Space (char * s, int N)
{
Int I;
If (n <0) n = 0;
For (I = 0; I <n; I ++) * s ++ = 32;
* S = 0;
}

/* Generate a string */
Void string (int n, char * S1, char * S2)
{
Int I;
If (n <0) n = 0;
S1 [0] = 0;
For (I = 1; I <= N; I ++) strcat (S1, S2 );
}

/* Cut the left space of the string */
Void cut_left_space (char * s)
{
Int I, j, k = 0;
I = strlen (s) + 1;
For (j = 0; j <I; j ++) if (s [J]! = '') Break;
For (k = 0; j <I; k ++, J ++) s [k] = s [J];
}

/* Cut the right space of the string */
Void cut_right_space (char * s)
{
Int I, J;
I = strlen (S)-1;
For (j = I; j>-1; j --) if (s [J]! = '') Break;
S [J + 1] = 0;
}

/* Display a string */
Void display (char * s)
{
Union regs;
Int color, X, Y;
X = wherex ();
Y = wherey ();
Color = 16 * bjys + QJYS;
While (* s)
{
If (x> 80) break;
Regs. H. Ah = 9;
Regs. H. Al = * s;
Regs. H. bH = 0;
Regs. H. Bl = color;
Regs. X. Cx = 1;/* Number of times displayed, without changing the cursor position */
Int86 (16, & reg; s, & reg; S );
X ++;
If (x> 80)
{
X = 1;
Y ++;
If (Y> 25) y = 25;
}
Gotoxy (x, y );
S ++;
}
}

/* Define screen color */
Void color (INT forecolor, int backcolor)
{
If (forecolor <0 | forecolor> 15) return;
If (backcolor <0 | backcolor> 15) return;
QJYS = forecolor;
Bjys = backcolor;
}

/* Display the prompt window */
Void quit_yesno (char * S1, char * S2)
{
Char buffer [2000], JX;
Gettext (30,8, 76,16, buffer );
Textbackground (3 );
Clsxy (8, 32, 9, 30, 6 );
Clsxy (4,30, 8,30, 6 );
Color (15, 4 );
Gotoxy (35,10 );
Display (S1 );
Gotoxy (35,12 );
Display (S2 );
Gotoxy (35 + strlen (S2) + 1, 12 );
JX = getch ();
Puttext (30,8, 76,16, buffer );
If (JX = 'n' | JX = 'n') return;
Textbackground (0 );
Textcolor (15 );
Clrscr ();
Cursor (on );
Exit (0 );
}

/* Display warning window */
Void warn_message (char * S1, char * S2)
{
Char buffer [2000];
Gettext (30,8, 76,16, buffer );
Clsxy (8, 32, 9, 30, 6 );
Clsxy (4,30, 8,30, 6 );
Color (15, 4 );
Gotoxy (35,10 );
Display (S1 );
Gotoxy (35,12 );
Display (S2 );
Gotoxy (40, 13 );
Display ("press any key to continue ...");
Getch ();
Puttext (30,8, 76,16, buffer );
}

/* Get the file length */
Long getfilelength (char * file)
{
File * FP;
Int I;
If (FP = fopen (file, "rb") = NULL)
{
Clrscr ();
Printf ("can't open the file % S.", file );
Return 0;
}
I = 0;
While (! Feof (FP ))
{
I ++;
Fgetc (FP );
}
Fclose (FP );
Return (I-1 );
}

/* Window scrolling */
Void window_roll (INT Y1, int X1, int YL, int XL, int direct)
{
Union regs;
Int X2, Y2;
X1 --;
Y1 --;
X2 = X1 + XL-1;
Y2 = Y1 + YL-1;
Regs. H. Ah = 5 + direct;/* Ah = 06 h scroll up the current page */
/* Ah = 07 h down... */
Regs. H. Al = 1;
Regs. H. bH = 16 * pmbj + pmqj;
Regs. H. Ch = Y1;/* row in the upper left corner */
Regs. H. Cl = x1;/* column */
Regs. H. DH = Y2;/* row in the lower right corner */
Regs. H. DL = x2;/* column */
Int86 (16, & reg; s, & reg; S );
}

/* Scan the keyboard */
Int scan_keyboard (int * m)
{
Union inkey
{
Unsigned char ch [2];
Int II;
} Cc;
While (! Bioskey (1 ))
{;}
Cc. II = bioskey (0 );
* M = cc. ch [1];
Return cc. ch [0];
}

/* Insert a string */
Void insert_string (char * ds, char * ss, int N)
{
Char s [100];
Mid (DS, S, N,-1 );
DS [n-1] = 0;
Strcat (DS, SS );
Strcat (DS, S );
}
Void str_add_char (char * s, char ch)
{
Char S1 [2];
S1 [0] = CH;
S1 [1] = 0;
Strcat (S, S1 );
}

/* Check whether the disk is ready */
Int checkdiskreading (int n)
{
Int result;
Char buffer [512];
Result = biosdisk (4, N, 0, 0, 0, 1, buffer );
Result = Result & 0x02;
If (result! = 0x02) return 0;
Return 1;
}

/* Check whether the disk is write-protected */
Int checkdiskwriteprof (void)
{
Int result;
Char buffer [512];
Result = biosdisk (5, 0, 0, 0, 0, 1, buffer );
If (result = 0x03) return 0;
Return 1;
}

/* Modify a row of the file */
Int modifyfileline (char * filename, char * s, int N)
{
Char data [20] [80];
Int I;
File * FP;
If (FP = fopen (filename, "R") = NULL) return 0;
I = 0;
While (read_str (data, FP ))
I ++;
Fclose (FP );
Fp = fopen (filename, "W ");
Strcpy (data [n-1], S );
N = I-1;
For (I = 0; I <= N; I ++)
Write_str (data, FP );
Fclose (FP );
Return 1;
}

/* Copy objects in batches */
Int copyfile (char * sfile, char * dfile, int F2d, int barlong, int height, int X, int y)
{
Int copyfile (char * SF, char * DF );
Int makendir (char * DIR );
Char filename [200] [13], d [40], s [40], S1 [40];
Struct ffblk;
Int done, I, j, L, Len;
I = 0;
Done = findfirst (sfile, & ffblk, 0 );
If (! Done) strcpy (filename, ffblk. ff_name );
While (! Done)
{
Done = findnext (& ffblk );
If (! Done)
{
I ++;
Strcpy (filename, ffblk. ff_name );
}
}
If (F2d)
{
Copyfile (sfile, dfile );
Return 1;
}
Strcpy (S, sfile );
L = strlen (sfile );
For (j = L-1; j> = 0; j --)
If (s [J] = '//')
{
S [J + 1] = 0;
Break;
}

/* Copy an object */
Int copyfile (char * SF, char * DF)
{
File * In, * out;
Char ch;
In = 0;
Out = 0;
If (in = fopen (SF, "rb") = NULL)
Exit (0 );
If (out = fopen (DF, "WB") = NULL)
Exit (0 );
While (! Feof (in ))
{
Ch = fgetc (in );
If (ferror (in) return 0;
Fputc (CH, out );
If (ferror (out) return 0;
}
Fclose (in );
Fclose (out );
Return 1;
}

/* Create a directory */
Int makendir (char * DIR)
{
Int I, L, J;
Char s [10] [40];
J = 0;
L = strlen (DIR );
For (I = 0; I <L; I ++)
If (DIR = '//')
{
Left (s [J], Dir, I );
J ++;
}
Strcpy (s [J], DIR );
For (I = 0; I <= J; I ++)
If (access (S, 0) mkdir (s );
Return 1;
}

/* Get the directory */
Int getdir (char * dirf, char Datak [] [14])
{
Struct ffblk;
Int done;
Int I;
I = 0;
Done = findfirst (dirf, & ffblk, fa_direc );
While (! Done)
{
If (ffblk. ff_attrib = 16 | ffblk. ff_attrib = 17)
{
Strcpy (Datak, ffblk. ff_name );
Strcat (Datak ,"//");
I ++;
}
Done = findnext (& ffblk );
}
Return I;
}

/* Get the file name */
Int GetFile (char * dirf, char Datak [] [14])
{
Struct ffblk;
Int done;
Int I;
I = 0;
Done = findfirst (dirf, & ffblk, 0 );
While (! Done)
{
Strcpy (Datak, ffblk. ff_name );
Done = findnext (& ffblk );
I ++;
}
Return I;
}

/* Add any two positive integers (<80 bits )*/
/* Parameter description: NUMA and numb are adders and adders respectively. The result stores the added result */
Void add (char * NUMA, char * numb, char * result)
{
Int I, j, A, B, C, JW, Alen, blen;/* defines the variable */
Char num [81];
Char numc [81];/* define a new string with the same length as NUMA */
Char ch;
Char s [1];
Num [0] = 0;
Numc [0] = 0;
If (strlen (NUMA)> = 80) return;
If (strlen (numb)> = 80) return;
For (I = 0; I <strlen (NUMA); I ++)/* checks whether the input number is invalid */
If (! Isdigit (NUMA) return;/* If the returned result is invalid */
For (I = 0; I <strlen (numb); I ++)
If (! Isdigit (numb) return;
If (strlen (NUMA) <strlen (numb)/* put a large number in NUMA */
{
Strcpy (Num, numa );
Strcpy (NUMA, numb );
Strcpy (numb, num );
Num [0] = 0;
}
Alen = strlen (NUMA );
Blen = strlen (numb );
Strcpy (numc, numb );
For (I = 0; I <Alen; I ++)
Numc [Alen-i-1] = numc [Blen-i-1];
For (I = 0; I <Alen-blen; I ++)/* Insert n zeros in numc so that the length is equal to NUMA */
Numc = '0 ';
JW = 0;/* carry */
For (I = 0; I <Alen; I ++)
{
S [0] = NUMA [Alen-i-1];
S [1] = 0;
A = atoi (s );
S [0] = numc [Alen-i-1];
S [1] = 0;
B = atoi (s );
C = a + B + JW;
If (C> = 10)
{
JW = 1;
C = C-10;
}
Else JW = 0;
ITOA (C, S, 10 );
Num = s [0];
}
If (JW = 1) {num = '1'; num [I + 1] = 0 ;}
Else num = 0;
J = strlen (Num );
For (I = 0; I <j/2; I ++)
{
Ch = num;
Num = num [j-i-1];/* reverse num */
Num [j-i-1] = CH;
}
Strcpy (result, num );
Return;/* return the sum result */
}

/* Multiply any two positive integers */
/* Parameter description: NUMA and numb are the multiplier and the multiplier respectively, and resultm stores the product */
Void mult (char * NUMA, char * numb, char * resultm)
{
Int I, J, K, L, M, A, B, C, JW, Alen, blen, F;/* defines the variable */
Char resulta [82];
Char num [161];
Char ch;
Char s [1];
Num [0] = 0;
F = 0;
If (strlen (NUMA)> = 80) return;
If (strlen (numb)> = 80) return;
For (I = 0; I <strlen (NUMA); I ++)/* checks whether the input number is invalid */
If (! Isdigit (NUMA) return;/* If the returned result is invalid */
For (I = 0; I <strlen (numb); I ++)
If (! Isdigit (numb) return;
If (strlen (NUMA) <strlen (numb)/* put a large number in NUMA */
{
Strcpy (Num, numa );
Strcpy (NUMA, numb );
Strcpy (numb, num );
Num [0] = 0;
}
Alen = strlen (NUMA );
Blen = strlen (numb );
Strcpy (resultm, "0 ");
For (I = 0; I <blen; I ++)
{
S [0] = numb [Blen-i-1];
S [1] = 0;
C = atoi (s );
Strcpy (resulta, numa );
If (C = 0) strcpy (resulta, "0 ");
For (j = 0; j <C-1; j ++) {Add (resulta, NUMA, resulta );}
L = strlen (resulta );
For (k = 0; k <I; k ++)
{
Resulta [L + k] = '0 ';
Resulta [L + k + 1] = 0;
}
Add (resultm, resulta, resultm );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.