Hello I have a little problem. I have to make a single task for the school. Here the condition of the task:

Elaboration of a program with which to process the source texts of arbitrary C progams, saved as file structures. Should be created and creates and displays statistics on the file structure. The program can be implemented as a program type (menu).

1. Dayl choice of treatment and create a file (a name chosen by the user), the file must contain data from the last statistical treatment: Choice of source texts of the processed C programs, All of a list of files with the extension "*. c". The choice is to file with the file name input from the keyboard.

2. Statistical processing of the rows of source text in C program, which is designed to: Percentage of empty rows to total rows.

3. Statistical processing of the symbols in the text as follows: average number of characters in program text lines.

4. Statistical processing of the comments in the text: To determine the percentage of commented text to all text.
5. Statistical processing of identifiers: The total number of constants in the output text of C program.
6. Text Processing in C program: Insert spaces in output text - so the writing is structured, each nested block is indented to pre-selected number of intervals.
This is my code. Can you help me?

#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

char FileName[256];
// Broqchi za statistikite
int cntEmtyLine = 0, cntAll = 0, cntComent = 0, cntConst = 0;

void izborNaFile();
void stPrazniRedove();
void stIdentifikatori();
void stKomentari();
void stSimboliVteksta();
void obrabotkaNateksta();
int numCharsInFile( ifstream &in, int &numLines ); 
// funciq za 4etene na failovete v direktorita
int getdir (string dir, vector<string> &files)
{
    DIR *dp;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        files.push_back(string(dirp->d_name));
    }
    closedir(dp);
    return 0;
} //getdir()

int main(void) {
	int menu;
	
	fflush(stdin);
	printf("\n Dimitar Ivanov Hadjiev KCT, fakNo = .....");
	
	/*
	Izbor na to4ka ot menuto
	
	*/
	do {
		printf("\n\n");
		printf("Menu:\n");
		printf("1.Izbor na fail za obrabotka i sazdavane na fail:\n");
		printf("2.Statisticheska obrabotka na redovete ot izhodnia tekst na C programata, izrazqvashta se v:\n");
		printf("3.Statisticheska obrabotka na simboli v teksta, kakto sledva:\n");
		printf("4.Statisticheska obrabotka na komentarii v teksta:\n");
		printf("5.Statisticheska obrabotka na identifikatori:\n");
		printf("6.Obrabotka na teksta na C programata:\n");
		printf("7.Exit\n");
		scanf("%d", &menu);
		
		switch(menu) {
			case (1):
				izborNaFile();
				break;
			case (2);
				stPrazniRedove();
				break;
			case (3):
				stSimboliVteksta();
				break;
			case (4):
				stKomentari();
				break;
			case (5):
				stIdentifikatori();
				break;
			case (6):
				obrabotkaNateksta();
				break;
			case (7):
				default:
				break;
		}
	} while(menu != 7);

	return 0;
}

void izborNaFile() {
/*    string dir = string(".");
    vector<string> files = vector<string>();

    getdir(dir,files);

    for (unsigned int i = 0;i < files.size();i++) {
      printf(files[i]);
      printf("\n";
    }
*/	
	struct ffblk ffblk;
	int done;
	
	printf("Pokazwane na failovete s razshirenie *.c\n");
	done = findfirst("*.c", &ffblk, 0);
	
	while(!done) {
	    printf(" %s\n", ffblk.ff_name);
	    done = findnext(&ffblk);
	}
	
	printf("\n");
	printf("\n Vavedi imeto na file: ");
	gets(FileName); 
	
}//izborNaFile()

void stPrazniRedove() {
	int AllCntLine = 0; // broqch za ob]iq broi redove na faila
	float prc =0.00;
	FILE *file = fopen ( FileName, "r" );
	if ( file != NULL ){
		char line [ 128 ]; /* or other suitable maximum line size */
		
		while ( fgets ( line, sizeof line, file ) != NULL ) {/* 4etene na edin red */
			if (line == "") { // prazen red
				cntEmtyLine++; // otborqwa praznite reodve v faila
			}
			AllCntLine++;
		//	fputs ( line, stdout ); /* Pokazvane na reda */
		}
		fclose ( file );
	} else {
		printf("\n Faila ne moje da se otvori!\n");
	}
	prc = ((AllCntLine/cntEmtyLine)*100);
	printf("\n Procent na praznite redovete kam obshtiq broi redove: %f",prc);
	printf("\n");
} //stPrazniRedove()

void stIdentifikatori() {
	size_t found;
	string str;

	FILE *file = fopen ( FileName, "r" );
	if ( file != NULL ){
		char line [ 128 ]; /* or other suitable maximum line size */
		
		while ( fgets ( line, sizeof line, file ) != NULL ) {/* 4etene na edin red */
			str(line);
			found=str.find("const"); 
			  if (found!=string::npos)
			    cntConst++; // opredelq broq na kosntatnite v faila
		}
		fclose ( file );
	} else {
		printf("\n Faila ne moje da se otvori!\n");
	}
	prc = ((AllCntLine/cntConst)*100);
	printf("\n Statisticheska obrabotka na identifikatori, obsht broi na konstantite: %d",cntConst);
	printf("\n");
} //stIdentifikatori()

void stKomentari() {
	size_t found;
	string str;
	int AllCntLine = 0; // broqch za ob]iq broi redove na faila
	float prc =0.00;

	FILE *file = fopen ( FileName, "r" );
	if ( file != NULL ){
		char line [ 128 ]; /* or other suitable maximum line size */
		
		while ( fgets ( line, sizeof line, file ) != NULL ) {/* 4etene na edin red */
			str(line);
			found=str.find("//"); 
			  if (found!=string::npos){
			    cntComent++; // opredelq broq na kometarite v faila
				}
			AllCntLine++;
		}
		fclose ( file );
	} else {
		printf("\n Faila ne moje da se otvori!\n");
	}
	prc = ((AllCntLine/cntComent)*100);
	printf("\n Statisticheska obrabotka na komentarii v teksta: %f",prc);
	printf("\n");
} //stKomentari()

void stSimboliVteksta() {
	int nLines,	nChars, avgCharsPerLine;
	ifstream inFile;
	
	inFile.open(FileName);
	if( !inFile.is_open() ) {
		printf("Faila ne moje da se otvori!");
		exit (0);
	}
	nChars = numCharsInFile( inFile, nLines );
	inFile.close();
	
	avgCharsPerLine = nChars / nLines;
	
	printf("\n Sredniqt broi simvoli v programniq tekst po redove: %d",avgCharsPerLine);
	printf("\n");

} //stSimboliVteksta()

// vzeto ot http://bytes.com/topic/c/answers/733757-word-count-problem
 int numCharsInFile( ifstream &in, int &numLines ) {
    int numChars = 0; 
  
    char ch; // character holder;
  
    numLines = 0; // initialize the number of lines to zero
  
    while ( in.get(ch) ) // get the next character from the file
                         //   the function get will also get whitespace
                        //   i.e. blanks, tabs and end of line characters

    {
      if (ch != ' ' )
      {
        if(ch != '\n')
        numChars++;// increase the count of characters by one if ch is NOT '\n' AND NOT a blank space
        else
        {
       numLines++;     // increase the count of lines by one if ch IS '\n'
        }
      } 
     }
     numLines += 1; // for some reason it needs to add one and the results are correct
    return numChars; // return the number of characters in the file
  } //numCharsInFile()
  
void obrabotkaNateksta() { // this is a point 6
	// ?????
} //obrabotkaNateksta()
jephthah commented: nicely formatted C code and detailed explanation of problem +5

Recommended Answers

All 7 Replies

All this looks very exciting but you have got to understand that some of us don't understand Bulgarian (Had to Google it and I'm still not sure). Please convert the program outputs & comments in English.
You should have read the forum rules which clearly states to write "in full-sentence English".

commented: if you can't understand C, then you need to quit "helping" -1

back off Mister 666, the poster has done nothing wrong.

there is no requirement that within the body of the code comments and output text be in English.

the forum rule you cite is to discourage pidgin english and leet speak, not to enforce some anglo-centric worldview. a large percentage of users here are NOT native english speakers.

bgspace does NOT have to rewrite his/her code to conform to your sensibilities. if you cant understand C code, then you need to quit "helping"

Hi BGSPACE,

welcome to the forums. thanks for using code tags and explaining your problem.

my problem with your code, is that you've got quite a mix of different environments, you have some non-standard C, and you've also got some C++ functionality mixed in there.

the odd combination of <conio.h>, <sys/types.h> and <dirent.h> looks like you're using Borland's Turbo C. this is a terrible non-standard version of C and has been deprecated for going on 2 decades in most of the western world.

also, your use of <vector> is a C++ structure that is not C-language at all, and makes your program neither C nor C++ but some unfortunate hybrid.

Because you're using Turbo C with all it's non standard libraries, I'm afraid I can't help you. there's just no way i can even begin to compile your code on any standard C or C++ compiler. for the same reason, I suspect you will have a difficult time finding anyone else here who can help you beyond just visually inspecting your code for obvious errors.

the fact is, Borland's compilers are really crap and there are very few serious programmers who would ever use them. you will forever have difficulty as long as you continue to use it.

i strongly recommend that you toss it out and learn to use any of the following: GCC, MinGW, CodeBlocks, Bloodshed Dev C++, and/or Microsoft Visual C++. each of these are industry standard and are free, or at least have a free option.


.

Hello
I converted the source code of the English language. I program in php and mysql. I am programmed much of C and C + + and some of the things I know how it should be done in C and the other part of C + +. But I have to do this task only on C but I do not know how? For this I have done in C + +. I know that is not right to mix the two languages for programming. If someone can help me to do only the task of C.

#include <sys/types.h>
#include <dirent.h>
#include <errno.h>

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

char FileName[256];
// counters
int cntEmtyLine = 0, cntAll = 0, cntComent = 0, cntConst = 0;

void selectFile();
void statisticEmptyRows(); 
void statisticsIdentifiers(); //Statistics identifiers statisticsIdentifiers
void statisticsComents();
void statisticCharsInCode();
void textProcessing();
int numCharsInFile( ifstream &in, int &numLines ); 

int main(void) {
	int menu;
	
	fflush(stdin);
	do {
		printf("\n\n");
		printf("Menu:\n");
		printf("1. Select file for procesing or creating:\n");
		printf("2.Statistical processing of the rows of source text in C program, which is designed to:\n");
		printf("3.Statistical processing of the symbols in the text as follows:\n");
		printf("4.Statistical processing of the comments in the text:\n");
		printf("5.Statistical processing of identifiers:\n");
		printf("6.Text Processing in C program:\n");
		printf("7.Exit\n");
		scanf("%d", &menu);
		
		switch(menu) {
			case (1):
				selectFile();
				break;
			case (2);
				statisticEmptyRows();
				break;
			case (3):
				statisticCharsInCode();
				break;
			case (4):
				statisticsComents();
				break;
			case (5):
				statisticsIdentifiers();
				break;
			case (6):
				textProcessing();
				break;
			case (7):
				default:
				break;
		}
	} while(menu != 7);

	return 0;
}

void selectFile() {
	struct ffblk ffblk;
	int done;
	
	printf("File list *.c\n");
	done = findfirst("*.c", &ffblk, 0);
	
	while(!done) {
	    printf(" %s\n", ffblk.ff_name);
	    done = findnext(&ffblk);
	}
	
	printf("\n");
	printf("\n Input file Name: ");
	gets(FileName); 
	
}//selectFile()

void statisticEmptyRows() { 
	int AllCntLine = 0; // counter for total rows in file 
	float prc =0.00;
	FILE *file = fopen ( FileName, "r" );
	if ( file != NULL ){
		char line [ 128 ]; /* or other suitable maximum line size */
		
		while ( fgets ( line, sizeof line, file ) != NULL ) {/* read file line by line */
			if (line == "") { // empty row
				cntEmtyLine++; // count emty row
			}
			AllCntLine++;
		}
		fclose ( file );
	} else {
		printf("\n File can not open!\n");
	}
	prc = ((AllCntLine/cntEmtyLine)*100);
	printf("\n Percentage of empty rows to total rows: %f",prc);
	printf("\n");
} //statisticEmptyRows()

void statisticsIdentifiers() {
	size_t found;
	string str;

	FILE *file = fopen ( FileName, "r" );
	if ( file != NULL ){
		char line [ 128 ]; /* or other suitable maximum line size */
		
		while ( fgets ( line, sizeof line, file ) != NULL ) {/* read file line by line */
			str(line);
			found=str.find("const"); 
			  if (found!=string::npos)
			    cntConst++; // determines the number of constants
		}
		fclose ( file );
	} else {
		printf("\n File can not open!\n");
	}
	prc = ((AllCntLine/cntConst)*100);
	printf("\n Statistical treatment of identifiers, total number of constants: %d",cntConst);
	printf("\n");
} //statisticsIdentifiers()

void statisticsComents() {
	size_t found;
	string str;
	int AllCntLine = 0; // counter for total rows in file 
	float prc =0.00;

	FILE *file = fopen ( FileName, "r" );
	if ( file != NULL ){
		char line [ 128 ]; /* or other suitable maximum line size */
		
		while ( fgets ( line, sizeof line, file ) != NULL ) {/* read file line by line */
			str(line);
			found=str.find("//"); 
			  if (found!=string::npos){
			    cntComent++; // determines the number of coments
				}
			AllCntLine++;
		}
		fclose ( file );
	} else {
		printf("\n File can not open!\n");
	}
	prc = ((AllCntLine/cntComent)*100);
	printf("\n Statistical treatment of coments in sorce code: %f",prc);
	printf("\n");
} //statisticsComents()

void statisticCharsInCode() {
	int nLines,	nChars, avgCharsPerLine;
	ifstream inFile;
	
	inFile.open(FileName);
	if( !inFile.is_open() ) {
		printf("File can not open!");
		exit (0);
	}
	nChars = numCharsInFile( inFile, nLines );
	inFile.close();
	
	avgCharsPerLine = nChars / nLines;
	
	printf("\n The average number of characters in the program code per lines: %d",avgCharsPerLine);
	printf("\n");

} //statisticCharsInCode()

// vzeto ot http://bytes.com/topic/c/answers/733757-word-count-problem
 int numCharsInFile( ifstream &in, int &numLines ) {
    int numChars = 0; 
  
    char ch; // character holder;
  
    numLines = 0; // initialize the number of lines to zero
  
    while ( in.get(ch) ) // get the next character from the file
                         //   the function get will also get whitespace
                        //   i.e. blanks, tabs and end of line characters

    {
      if (ch != ' ' )
      {
        if(ch != '\n')
        numChars++;// increase the count of characters by one if ch is NOT '\n' AND NOT a blank space
        else
        {
       numLines++;     // increase the count of lines by one if ch IS '\n'
        }
      } 
     }
     numLines += 1; // for some reason it needs to add one and the results are correct
    return numChars; // return the number of characters in the file
  } //numCharsInFile()
  
void textProcessing() { 
	// ?????
} //textProcessing()

Hello bgspace,
I apologize for troubling you to convert the comments and outputs to English. If I can't understand a language, it means I have to improve myself or let someone else handle the situation(my bad jephthah).
It seems that you still have not changed your compiler. (I recommend code::blocks)
Before posting your new code, you should have read jephthah's post
Please attach any custom header file that you may have used in your code
Line 25 - Don't use fflush(stdin)

Hello
I converted the source code of the English language. I program in php and mysql. I am programmed much of C and C + + and some of the things I know how it should be done in C and the other part of C + +. But I have to do this task only on C but I do not know how? For this I have done in C + +. I know that is not right to mix the two languages for programming. If someone can help me to do only the task of C.

it's nice that you converted your code comments to english -- not required but perhaps more people will not be easily confused or distracted.

and some people here will point to your use of "fflush(stdin)" and "gets", which are big no-no's, but you have a more fundamental problem.

your problem remains that you have a very implementation-specific program here, full of non-standard C code due to your choice of compiler, TurboC.

as a result, i can not compile or run or debug your program. and most people who would be able to help you, will have the same issue. Turbo C is just not used by professionals in the modern technical society. Microsoft Visual C++ and GCC have free compilers and are the industry standards, and there are many high quality free development environments that are compatible with these.

there's just no reason for anyone to use Turbo C anywhere, but it seems to have a stranglehold on educational institutions in eastern europe, india, and a few asian countries. i often wonder if Borland pays your college and tech school administrators kickbacks and bribes to enforce its use.

But i digress. All i can tell you is to get rid of that crap compiler and start using one of the many free standard C compilers available.

and this is easier for me to say than for you to do, but you will need to do it if you ever hope to be able to communicate about C programming with the rest of the western world.


If you really want to do this, here's where to start:

download Code::Blocks using the MinGW compiler... http://www.codeblocks.org/downloads/5 ... select the MinGW option

launch the codeblocks application, get familiar with the interface, create a "new project" and bring in your .C file that you have.

remove the <sys/files> <dirent> and <conio> include libraries from the top.

(i'm confused by the combination of <sys/files> and <conio>... so I'm assuming you're using a Windows machine, because i just can't imagine Turbo C being used on Linux. .... please correct me if I'm wrong)

remove all calls to functions that rely on these libraries. on further inspection it doesnt appear to be too much: the 'findfirst' and 'findnext' comands on 71 and 75. you will have to rewrite this functionality, but is not too big of a deal.

you do have multiple areas where you are using C++ functions. you will have to remove these. such as 'inFile.is_open()', 'inFile.close()', 'string::npos', etc.... and you'll have to rewrite them in C.

if you get started on this, and post your progress, we can work through it. the end result will be clean, Standard-C and will even be able to be compiled and run on Turbo C, god forbid, if needed.

good luck.

.

commented: Nice response +1
commented: yup +5
commented: Nice response +6

let me know what operating system you're using. I assume Windows XP.

If you happen to be using Turbo C on Linux (unimaginable, really, but i have heard tales), this will change the way we approach the whole problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.