veselinsavov

Course work - 1,1,1,1

Jan 21st, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.56 KB | None | 0 0
  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. char dirname[256],fname[256],path[512];
  7. char* chooseDir() {
  8.         DIR *dir = NULL;
  9.         FILE *f;
  10.     struct dirent *pent = NULL;
  11.         while(dirname != 0){
  12.                 printf(".Choose directory and file:");
  13.                 scanf("%s",dirname);
  14.                 dir = opendir(dirname);
  15.                 printf("%s",dirname);
  16.                 printf("Choose file:\n");
  17.                
  18.                 while((pent= readdir(dir))!=NULL){
  19.                         printf("%s\n",pent->d_name);
  20.                 }
  21.                
  22.                 scanf("%s",fname);
  23.                 char wholeName[512];
  24.                 strcpy(wholeName,dirname);
  25.                 strcat(wholeName,"/");
  26.                 strcat(wholeName,fname);
  27.                 strcat(path,wholeName);
  28.                
  29.                 while((f=fopen(wholeName,"r"))==NULL){
  30.                         printf("The file does not exist!\n please choose file: ");
  31.                         scanf("%s",fname);
  32.                         strcpy(wholeName,dirname);
  33.                         strcat(wholeName,"/");
  34.                         strcat(wholeName,fname);
  35.                         strcat(path,wholeName);
  36.                 }
  37.                 closedir (dir);
  38.                 return dir;
  39.         }
  40. }
  41.  
  42. void countLines()
  43. {
  44.         int c;
  45.         int lines = 0;
  46.         FILE *selectedFile;
  47.         selectedFile = fopen(path,"r");
  48.         c = getc(selectedFile);
  49.        
  50.         while (c != EOF)              
  51.         {
  52.                 if(c == '\n') lines++;
  53.                 c = getc(selectedFile);
  54.         }
  55.        
  56.         fclose(selectedFile);
  57.         printf("\n\nRows in %s are : %d\n",fname, lines);
  58. }
  59.  
  60. void avgChRowToAllCh()
  61. {
  62.         int charsInLIne = 0,chars = 0,c;
  63.         FILE *selectedFile;
  64.         selectedFile = fopen(path,"r");
  65.         c = getc(selectedFile);        
  66.        
  67.         while (c != EOF)
  68.         {
  69.                 chars++;      
  70.                 c = getc(selectedFile);
  71.         }
  72.         fclose(selectedFile);
  73.         FILE *fp;
  74.         fp = fopen(path,"r");
  75.         c = getc(fp);
  76.         int line = 1;
  77.         printf("\n");
  78.         printf("-------------------------------------------------\n");
  79.         printf("Line       *   Chars in line       All chars\n");
  80.         printf("-------------------------------------------------\n");
  81.        
  82.         while(c != EOF){
  83.                 c = getc(fp);
  84.                 if(c == '\n'){
  85.                         printf("Line:%-5d *   Chars in line:%-5d All chars:%d  \n",line,charsInLIne,chars);
  86.                         printf("-------------------------------------------------\n");
  87.                         charsInLIne = 0;
  88.                         line ++;
  89.                 }
  90.                 charsInLIne++;
  91.                
  92.         }
  93.         fclose(fp);
  94. }
  95.  
  96. void countComments()
  97. {
  98.         int comments = 0, c;
  99.         FILE *selectedFile;
  100.         selectedFile = fopen(path,"r");
  101.         c = getc(selectedFile);
  102.         int newLine = 1;
  103.         int left = 0;
  104.         int right = 0;
  105.         while(c != EOF){
  106.                 c = fgetc(selectedFile);
  107.                 if(c == '\n'){
  108.                         newLine = 1;
  109.                 }
  110.                 if(c == '/' && newLine == 1){
  111.                         c = fgetc(selectedFile);
  112.                         if(c == '/'){
  113.                                 comments++;
  114.                                 newLine = 0;
  115.                                 }
  116.                         }
  117.         }
  118.         //
  119.         FILE *fp;
  120.         fp = fopen(path,"r");
  121.         c = getc(fp);  
  122.         while(c != EOF){
  123.                 c = fgetc(fp);
  124.                 if(c == '/'){
  125.                         c = fgetc(fp);
  126.                         if(c == '*'){
  127.                                 left++;
  128.                                 }
  129.                         }
  130.                 if(c == '*'){
  131.                         c = fgetc(fp);
  132.                         if(c == '/'){
  133.                                 right++;
  134.                                 }
  135.                         }
  136.                 }
  137.         int commentsToAdd,diff = 0;
  138.         if(left > right){
  139.                 diff = abs(left - right);
  140.                 commentsToAdd = left - diff;
  141.                 } else if(right > left){
  142.                         diff = abs(left - right);
  143.                         commentsToAdd = (right - diff);
  144.                         } else {
  145.                                 commentsToAdd = left;
  146.                                 }
  147.         comments += commentsToAdd;
  148.         printf("\n");
  149.         printf("\nAll comment in %s are:%d\n",fname, comments);
  150. }
  151. int main ()
  152. {
  153.     char menu;
  154.         do{
  155.                 printf("---Directory: %s  selected file: %s",dirname,fname);
  156.                 printf("\n1. Select a directory: \n");
  157.                 printf("2. Number of rows in program\n");
  158.                 printf("3. Average symbols in rows : all symbols \n");
  159.                 printf("4. Shows the number of all comments.\n");
  160.                 printf("5. Quit\n");
  161.                 menu = getche();
  162.                 switch(menu){
  163.                         case '1': chooseDir();break;
  164.                         case '2': countLines();break;
  165.                         case '3': avgChRowToAllCh();break;
  166.                         case '4': countComments( );break;
  167.                         case '5': printf("\n\Bye Bye !!!");break;
  168.                         default: printf("\n\nPlease enter number between 1-5");
  169.                 }
  170.         }while(menu!= '5');
  171.         return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment