C programming HELP ME!!!! SAVE ME!!!!

  • Egg
  • Born
  • Born
  • Egg
  • Posts: 1

Post 3+ Months Ago

Hi all,

I am currently an engineering student and need assistence in C programming. I'm trying to churn out the assignment pasted below.......

LOOK! A helpful code browser.
When learning a new language, almost every program you write has something to teach you. It is helpful to keep a directory of programs you have written, well commented to highlight significant points. But how to quickly find the exact piece of code you want? In this assignment you will write an application to browse and print programs based on special comments in their source code.

The input
LOOK! comments.
A LOOK! comment is text of one of the following forms embedded inside a normal C comment:



<LOOK! "topic" "particulars">
<LOOK! "topic" "particulars" n>


i.e. each LOOK! comment is contained in a pair of angle brackets, and starts with the fixed string LOOK!. Inside the brackets are two strings, each enclosed in double quotes. There might be a positive integer just before the closing bracket. Here is a C program with LOOK! comments:

Code: [ Select ]
#include <stdio.h>

//Thirty days has september, april, june, and november.
int
is_thirty_day_month(int month)
{
 return (month==4 || month==6 || month==9 || month==11);
}


int
is_leapyear(int year)
{
 if (year % 400 == 0) // <LOOK! "mod" "true if year divisible by 400">
  return 1;
 else if (year % 100 == 0)
  return 0;
 else if (year % 4 == 0)
  return 1;
 else
  return 0;
}

// replace the date in (day, month, year) with the date of the
// following day. <LOOK! "pointers" "the function needs to return 3
// numbers, so we pass it the addresses of three variables in which to
// put the output">
void
tomorrow(int *day, int *month, int *year)
{
 int month_carry=0, year_carry=0;

 // <LOOK! "boolean operators" "|| is OR and && is AND. AND has
 // higher precedence."4>
 if (*day==31 ||
   (*day==30 && is_thirty_day_month(*month)) ||
   (*day==29 && (*month==2) && is_leapyear(*year)) ||
   (*day==28 && (*month==2) && !is_leapyear(*year))) {
  *day = 1; //<LOOK! "pointers" "put the value 1 in the location
       //referenced by 'day'">
  month_carry = 1;
 }
 else
  *day = *day+1;

 if (month_carry) {
  if (*month==12) {
   *month = 1;
   year_carry = 1;
  }
  else
   *month = *month + 1;
 }

 if (year_carry)
  *year = *year + 1;
}


//returns true iff the first date is strictly before the second date
int
is_in_order(int day1, int month1, int year1,
      int day2, int month2, int year2)
{
 if (year1 < year2)
  return 1;
 else if (year1 > year2)
  return 0;

 if (month1 < month2)
  return 1;
 else if (month1 > month2)
  return 0;

 return day1 <= day2;
}


int
main(int argc, char *argv[])
{

 int day0, month0, year0, day1, month1, year1;
 int day_count;

 if (argc != 7) {
  printf("Enter two dates: 'between d1 m1 y1 d2 m2 y2'\n");
  return 0;
 }

 day0  = atoi(argv[1]); //<LOOK! "atoi" "converts a string to an int">
 month0 = atoi(argv[2]);
 year0 = atoi(argv[3]);

 day1  = atoi(argv[4]);
 month1 = atoi(argv[5]);
 year1 = atoi(argv[6]);

 if (! is_in_order(day0, month0, year0, day1, month1, year1) ) {
  printf("The first date cannot be later than the second one.\n");
  return 0;
 }

 day_count = 0;
 while( is_in_order(day0, month0, year0, day1, month1, year1) ) {
  day_count++;
  tomorrow(&day1, &month1, &year1); //<LOOK! "pointers" "passing a
                   //function the addressess of
                   //three variables, so it can
                   //change them">
 }

 printf("There are %d days in the range\n", day_count);
 return 0;
}
  1. #include <stdio.h>
  2. //Thirty days has september, april, june, and november.
  3. int
  4. is_thirty_day_month(int month)
  5. {
  6.  return (month==4 || month==6 || month==9 || month==11);
  7. }
  8. int
  9. is_leapyear(int year)
  10. {
  11.  if (year % 400 == 0) // <LOOK! "mod" "true if year divisible by 400">
  12.   return 1;
  13.  else if (year % 100 == 0)
  14.   return 0;
  15.  else if (year % 4 == 0)
  16.   return 1;
  17.  else
  18.   return 0;
  19. }
  20. // replace the date in (day, month, year) with the date of the
  21. // following day. <LOOK! "pointers" "the function needs to return 3
  22. // numbers, so we pass it the addresses of three variables in which to
  23. // put the output">
  24. void
  25. tomorrow(int *day, int *month, int *year)
  26. {
  27.  int month_carry=0, year_carry=0;
  28.  // <LOOK! "boolean operators" "|| is OR and && is AND. AND has
  29.  // higher precedence."4>
  30.  if (*day==31 ||
  31.    (*day==30 && is_thirty_day_month(*month)) ||
  32.    (*day==29 && (*month==2) && is_leapyear(*year)) ||
  33.    (*day==28 && (*month==2) && !is_leapyear(*year))) {
  34.   *day = 1; //<LOOK! "pointers" "put the value 1 in the location
  35.        //referenced by 'day'">
  36.   month_carry = 1;
  37.  }
  38.  else
  39.   *day = *day+1;
  40.  if (month_carry) {
  41.   if (*month==12) {
  42.    *month = 1;
  43.    year_carry = 1;
  44.   }
  45.   else
  46.    *month = *month + 1;
  47.  }
  48.  if (year_carry)
  49.   *year = *year + 1;
  50. }
  51. //returns true iff the first date is strictly before the second date
  52. int
  53. is_in_order(int day1, int month1, int year1,
  54.       int day2, int month2, int year2)
  55. {
  56.  if (year1 < year2)
  57.   return 1;
  58.  else if (year1 > year2)
  59.   return 0;
  60.  if (month1 < month2)
  61.   return 1;
  62.  else if (month1 > month2)
  63.   return 0;
  64.  return day1 <= day2;
  65. }
  66. int
  67. main(int argc, char *argv[])
  68. {
  69.  int day0, month0, year0, day1, month1, year1;
  70.  int day_count;
  71.  if (argc != 7) {
  72.   printf("Enter two dates: 'between d1 m1 y1 d2 m2 y2'\n");
  73.   return 0;
  74.  }
  75.  day0  = atoi(argv[1]); //<LOOK! "atoi" "converts a string to an int">
  76.  month0 = atoi(argv[2]);
  77.  year0 = atoi(argv[3]);
  78.  day1  = atoi(argv[4]);
  79.  month1 = atoi(argv[5]);
  80.  year1 = atoi(argv[6]);
  81.  if (! is_in_order(day0, month0, year0, day1, month1, year1) ) {
  82.   printf("The first date cannot be later than the second one.\n");
  83.   return 0;
  84.  }
  85.  day_count = 0;
  86.  while( is_in_order(day0, month0, year0, day1, month1, year1) ) {
  87.   day_count++;
  88.   tomorrow(&day1, &month1, &year1); //<LOOK! "pointers" "passing a
  89.                    //function the addressess of
  90.                    //three variables, so it can
  91.                    //change them">
  92.  }
  93.  printf("There are %d days in the range\n", day_count);
  94.  return 0;
  95. }

The first string in a look comment is called the topic string. It provides a classifiction for the comment. It is expected that many LOOK! comments in different files will have the same topic string.

The second string in a look comment is called the particulars string. It describes the specific instance being commented.

Look comments can occur in four situations. In each situation, they serve to indicate a relevant section of code: Position of comment containing LOOK! Relevant section of code
The first comment at the top of the file, preceeding any preprocessor directives. In this position, the LOOK! refers to the entire file.
Immediately preceeding a function definition In this section the LOOK! refers to the following function.
Within the body of a function, at the end of a line of code In this position, the LOOK! refers to the single line at the end of which the comment starts.
Within the body of a function, on a line with no preceeding code If a LOOK! comment is in this position it will contain a number n before the closing bracket. The LOOK! refers to the n lines immediately following the comment.


You may assume that neither of the two strings in a LOOK! comment will contain the < or > or double quote characters.

Your program will be called with two or three command line arguments:


look directory action topic

The directory is the name of a directory, the action is a single character specifying what your program is to do. It will be either 'i', or 'p'.

If the action is 'i' your program is to scan all files with a ".h" or ".c" extension in the specified directory (and its subdirectories) and print an index of all relevant features marked with a LOOK! comment. In this case there will be no third command line argument. The format of the index is described below.

If the action is 'p' there will be a third command line argument. Your program is to print all relevant sections of code that are marked with a LOOK! comment whose topic string matches the third command line argument. The format of the output is described below.

The directory in the first argument will be expressed relative to the directory containing the executable. For example, to produce an index of all files in the same directory as the executable, the command line will be



./look . i

If we have the directory tree:


Code: [ Select ]
dirA
|
+--look
|
+--dirB
   |
   +--dirC
  1. dirA
  2. |
  3. +--look
  4. |
  5. +--dirB
  6.    |
  7.    +--dirC


Then to produce an index of all files in directory C, the command line will be:

./look dirB/dirC i

You can assume that the current directory "." will always be the directory containing the executable.

You can assume that the code will be formatted according to the convention that the only time a brace { or } occurs as the first character of a line is to start or end a function.

You can assume that all comments are the "//" variety, i.e. no comments delimited by /* and */ will occur.

The output
The index
Your program is to print the index to standard output. The index is to have the following format: it is to be a sequence of sections, one for each different topic string occuring in files in the directory. The sections are to be ordered alphabetically by topic string. Within each section there is to be one line for each occurence of a LOOK! comment with the given topic string. Each line should contain the following information:

The name of the file containing the comment. The filename should be expressed relative to the current directory (i.e. the directory containing the executable).
A pair of numbers specifying the start line and end line of the region the comment refers to. (The first line of a file is line number 1).
The particulars string of the comment. Only print as much of the particulars string as will make the entire line exactly 80 characters long (including the "\n" at the end). So long particulars strings will have to be truncated.
There must be a single whitespace between each entry on an index line.

The overall format of the index is:



topic-string
file line0 line1 particulars
file line0 line1 partiulars
...
file line0 line1 particulars
-one blank line-
topic-string
file line0 line1 particulars
...

Within each section, the files can be listed in any order.

The printout
When your program prints out the relevant sections of files, it should do so according to the following format:

Print the topic string. Above and below the topic string, print a line of "=" characters of the same length as the topic string. (So the topic string and its border takes up three lines.).
For each file containing a LOOK! with the given topic, print the pariculars string of the comment, starting on a new line, then print a blank line, then print the relevant lines of the file. In the printout of the file, erase the LOOK! comments (i.e. overwrite them with whitespace so that the file does not change in length).
After the end of each file, print a line containing 80 characters of "-".

You can print the files in any order.

Examples
Suppose that the following two files are the only ".c" or ".h" files in the directory containing the look program:

dayrange.c
stack.c
Then running ./look . i sould produce this output, and running ./look . p pointers should produce this output.


I just need some help....

OK I know how to open a file and print it out thats basically all.
Can someone tell me how to flick thru a directory and all subdirectories aswell?

Also how do you go through a file and search for a particular string.

Thanks ALL
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • phpSelectah
  • Student
  • Student
  • User avatar
  • Posts: 97

Post 3+ Months Ago

Are you using Gnu c or a Microsoft version ?

Try:
scandir (gnu) :
http://www.gnu.org/software/libc/manual ... ntent.html

and you should be using some sort of REGEX library to do your pattern matching:
http://www.gnu.org/directory/regex.html

( you can do this assignment in a short time with perl or php)

cheers :D

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 62 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.