C Language Code line number classification statistics small program (source code) __ Applet

Source: Internet
Author: User
Tags stdin
1 Requirements Analysis
1) Problem Description:Read a (up to 9) C program code file, statistics program code, comments and number of empty lines and functions, display function name and generate the corresponding log.
2 The basic requirements are as follows:
(1), the program files (such as. C or. txt, etc.) read into the source program in the order of character;
(2), input correctness check;
(3), Edge reading program, the edge of the identification of statistical code lines, comment lines and blank lines, but also to identify functions, statistics code number;
(4), allow analysis of 9 files at a maximum


2 Display Results
Note: The code Test IDE for this article is VS2013. This code to the more conventional programming habits of the written C code can be better recognized, but for some special circumstances can not identify.

1 Enter the correct number of files and path


2 Enter the correct log file generated



3 Program Structure

1) Overall structure


2 Analysis Module Structure


4 Source Code

The whole project consists of three documents: STATIST.H;SOURCE.C;MAIN.C


1) statist.h

#define SIZE_N
#define SIZE_L
#define NUM_F 9
#define TITLE "------------------------Life is a fight- ----------------------------------"

extern bool Com_flag;

void Getline (file* pfile, char* PT);
char* Clearblank (char*pt);
char* Mystrstr (char* s1, char* S2);
BOOL Isblank (char* PT);
int iscom (char* pt);
BOOL Isfunc (char* PT);
BOOL Qtycheck (int* PT);



2) source.c

#include <stdio.h> #include <string.h> #include <stdbool.h> #include "statist.h" void Getline (file*
		Pfile,char* PT) {while (!feof (pfile)) {*pt = fgetc (pfile);
			if (*pt== ' \ n ')//////Read the row successfully until the newline character {*pt = ';
		Return	
	} pt++;	} *pt = ' I '; If a newline character has not been detected and the file has ended, the Terminator is added at the end} char* Clearblank (char* PT) {while (*pt = =) | |
	(*pt = = "))
	{pt++;
} return PT;
	} char* Mystrstr (char* s1, char* S2) {char*s, *SP1, *TEMP1, *TEMP2;

	int q_flag = 0;
		if (Strstr (S1, "\") = = NULL)//If no quotes {s = strstr (S1, S2) are found;
	return s;
		else//If the quotation mark {SP1 = S1 is found;
			while (*SP1) {Temp1 = STRSTR (SP1, "\");
			TEMP2 = STRSTR (SP1, S2);
				if ((Temp1!= NULL) && (Temp1 = = SP1)) {if (Q_flag = 0) Q_flag = 1;
			else Q_flag = 0;
			} if ((Temp2!= NULL) && (q_flag = 0) && (temp2 = = SP1)) return SP1;
		sp1++;
	return NULL;
	} bool Isblank (char* PT) {Clearblank (PT); if (*pt = = ' ") return 1;
return 0;

	int iscom (char* pt) {char* pt1,*pt2,*pt3;
	Clearblank (PT);
	PT1 = Mystrstr (PT, "//");
	PT2 = Mystrstr (PT, "/*");
	PT3 = Mystrstr (PT, "* *");
		if (Com_flag = = 0) {if ((pt2!=null) && (PT3 = NULL))///If only/* indicates the comment start Com_flag = 1;
		if (pt1 = = pt)//double slash at the beginning of the note return 1;
		else if (pt1-pt > 0 | | pt2-pt > 0)//has annotation symbol, but is not the Head Start return 2; else if ((pt2 = = pt) && ((Pt3!= NULL && (* (PT3 + 2) = = ' ") | |	(PT3 = = NULL))
		With/* Beginning with//ending, or starting with///*, not detected/return 1;
		else if ((pt2 = = pt) && (pt3!= NULL) && (* (PT3 + 2)!= ' ")////start with/* but not ending with/* 2;
	else if (pt1 = null | | pt2 = NULL)//no annotation sign return 0;
			else if (Com_flag = 1) {if (pt3!=null) {com_flag = 0;
		return 1;
		printf ("\nyyyy\n");	return 1;

	If the com_flag==1 is not detected and * * is an annotation}} bool Isfunc (char* PT) {char* pt1, *pt2, *PT3,*PT4,*PT5,*PT6;
	Clearblank (PT);
	PT1 = Mystrstr (PT, "(");
	PT2 = Mystrstr (PT, ";"); PT3 = Mystrstr (PT, "if");
	PT4 = Mystrstr (PT, "while");
	PT5 = Mystrstr (PT, "for");
	PT6 = Mystrstr (PT, "switch");
		if (pt1!= null&&pt2 = = NULL&AMP;&AMP;PT3 = = Null&&pt4 = Null&&pt5 = NULL&AMP;&AMP;PT6 = NULL)
	return true;
return false;
	BOOL Qtycheck (int* PT)//only allows input of a 1 to 9 number {char temp[3];
	char c;
	Fflush (stdin);
	Gets (temp);
		if (temp[0]> ' 0 ' &&temp[0] <= ' 9 ' &&temp[1] = = ' C ') *pt = (int) (Temp[0]-48);
			else {fflush (stdin);
			printf ("Please re-enter the file quantity (0<n<10):");
		return 0;
return 1;
 }




3) Main.c

#include <stdio.h> #include <string.h> #include <stdbool.h> #include "statist.h" #include <

Stdlib.h>//system () header file #include <malloc.h> bool Com_flag;
	int main (void) {file* in,*log;
	Char con;
	Char temp[size_l + 1];
	Char Name[size_n];
	char* pt;
	char* name_t;

	int* result;
	int n = 0;
		struct fil{int cod;
		int blk;
		int com;
	int FCT;
	}fil = {0, 0, 0, 0};
	Com_flag = 0;
		do{printf ("%s", title);
		printf ("\n\n\nplease Enter the file quantity (0<n<10):"); while (!
		Qtycheck (&n));
		name_t = (char*) malloc (n*sizeof (name));
		result = (int*) malloc (n * 4 * sizeof (int));
		Log = fopen ("Log.txt", "w");

		fprintf (log, "%s\n\n\n", title);
			for (int i = 0; i < n; i++) {printf ("Please enter file \"%d\ "Name:", i + 1);
			scanf ("%s", name_t + I*size_n);
			in = fopen (name_t + i*size_n, "R");
				while (!in) {fflush (stdin);
				printf ("Please re-enter file \"%d\ "Name:", i + 1);
				scanf ("%s", name_t + I*size_n); in = fopen (name_t + I*size_n, "R");
		} fclose (in);
			for (int i = 0; i < n; i++) {in = fopen (name_t + i*size_n, "R");
				while (!feof (in)) {getline (in, temp);
				PT = Clearblank (temp);
					if (Isblank (pt) = = 1 && com_flag = 0) {fil.blk++;
				Continue
					else if (iscom (pt) = = 1) {fil.com++;
				Continue
				else if (iscom (pt) = = 2 && isfunc (pt) = = 0) fil.com++;
					else if (iscom (pt) = = 2 && isfunc (pt) = = 1) {fil.com++;
					fil.fct++;
					fil.cod++;
					fprintf (log, "%s%s\n", name_t + i*size_n, temp);
				Continue
					else if (Isfunc (pt) = = 1) {fil.fct++;
				fprintf (log, "%s%s\n", name_t + i*size_n, temp);
			} fil.cod++;
			* (Result + 4 * i) = fil.blk;
			* (Result + 4 * i + 1) = Fil.cod;
			* (Result + 4 * i + 2) = fil.com;
			* (Result + 4 * i + 3) = FIL.FCT;
			fil.blk = 0;
			fil.cod = 0;
			fil.com = 0;
			FIL.FCT = 0;
			Com_flag = 0;
			Fclose (in); If(FIL.FCT) fprintf (log, "\n\n\n");
		} fprintf (log, "%s", "\n\n\n==========================================================");
		printf ("==========================================================");	printf ("\nfile No.
		Blank Code commen function\n ");	fprintf (log, "\n%s\n", "File No.")
		Blank Code commen Function "); for (int i = 0; i < n; i++) {printf ('%d%d%d%d%d\n ', i + 1, * (Result + 4 * i), * (Result + 4 * i + 1), * (Resul
			T + 4 * i + 2), * (Result + 4 * i + 3));  fprintf (log, "%d%d%d%d\n", i + 1, * (Result + 4 * i), * (Result + 4 * i + 1), * (Result + 4 * i + 2), * (Result + 4 * I
		+ 3));
		} fclose (log);
		Free (name_t);
		Free (result);
		printf ("\n\n\npress ' Y ' to continue\n");
		Fflush (stdin);
		con = GetChar ();
	printf ("\n\n\n"); while (con== ' Y ') | |

	con== ' y ');
	System ("pause");
return 0; }


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.