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()