Question

Your program should provide a menu with the following options: 1) Author info 2) Set extension...

Your program should provide a menu with the following options:

1) Author info
2) Set extension
3) Set name
4) Run
0) Exit

When the user selects 1 print your name and student id
When the user selects 2 ask the user to enter a file extension (should allow at least 4 characters in the extensions, such as "mpeg"
When the user selects 3 ask the user to enter a file name, this should allow at least 6 character
When the user selects 4 the program should run calculations (more on this below)
When the user selects 0 the program should exit


---------------------------------
The program should keep a log file. The log file should state the menu options that are selected as well as program launch and close
---------------------------------

The log should not delete any previous logs (should have a history of all program usage across multiple runs)
When the log makes an entry you should format the infomration as follows:
"ID - Run number - log action"
where
ID is your student ID
Run number is the number of times your program has been run (this part is required for full credit, but is not worth many points, max score is 9.9 without it)
Log action would be things such as "program starter", "Author info selected", "Run Selected", and "Program exit"

for example, if your student ID is 1234567 and you started your program for the first time, the first entry would be:
1234567 - 1 - Program started

When the user chooses to run the calculations your program should do the following:
Ask the user to specify an output input file to be read.
Read from a file with the given name and process math equations in the file
Equations will be given in the file in the format of num1<op>num2 with no spaces.
Possible operators are:
+ for addition
- for subtraction
/ for division
x for multiplication

After processing the provaded equations you should write the results to the file specified using the name and extension provided earlier by the user
Or use a default of "math.out" using a fromat of name.extention, use a default name of "math" and extension of "out" if the user didn't specify anything
This would result in a file called "math.out"

The output should be given in a format of:
num1 <op> num2 = result

*** in C language.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main()
{
   const char * studentid = "12";
   const char * name = "xyz";
   char ext[6]=".out";
   char finalout[25];
   char filename[10]="math";
   char mathfile[25] ;
   int runNumber = 1;
   int choice;
   char ch;
   char num1[10];
   char num2[10];
   char opr;
   FILE *fPtr;
   int a, b, result;
   int isfirst = 1;
   int index = 0;
   fPtr = fopen("program.log", "a");
   if (fPtr != NULL)
   {
       fprintf(fPtr, "%s-%d-%s", studentid, runNumber, "Program started\n");
       while (1)
       {
           printf("\n");
           printf("1) Author info\n");
           printf("2) Set extension\n");
           printf("3) Set name\n");
           printf("4) Run\n");
           printf("0) Exit\n");
           printf("Select option : ");
           scanf("%d", &choice);
           if (choice == 1)
           {
               fprintf(fPtr, "%s-%d-%s", studentid, runNumber, "Author info selected\n");
               printf("Name-%s,ID-%s", name, studentid);
           }
           else if (choice == 2)
           {
               fprintf(fPtr, "%s-%d-%s", studentid, runNumber, "Set extension selected\n");
               printf("Enter output file name ext (.out) : ");
               scanf("%s", &ext);
           }
           else if (choice == 3)
           {
               fprintf(fPtr, "%s-%d-%s", studentid, runNumber, "Set name selected\n");
               printf("Enter output file name : ");
               scanf("%s", &filename);
           }
           else if (choice == 4)
           {
               fprintf(fPtr, "%s-%d-%s", studentid, runNumber, "Run selected\n");
               printf("Select math equation file name : ");
               scanf("%s", &mathfile);
              
               FILE * fptr1 = fopen(mathfile, "r");
               if (fptr1 == NULL)
               {
                   fprintf(fPtr, "%s-%d-%s", studentid, runNumber, "error in file\n");
                   break;
               }
               isfirst = 1;
               index = 0;
               while ((ch = fgetc(fptr1)) != EOF)
               {
                   if ((ch == '+')|| (ch == '-')|| (ch == '/')||(ch == 'X'))
                   {
                       opr = ch;
                       isfirst = 0;
                       index = 0;
                   }      
                   if (isfirst == 1)
                   {
                       num1[index] = ch;
                       index++;
                   }
                   else
                   {
                       num2[index] = ch;
                       index++;
                   }
               }
               fclose(fptr1);
               sscanf(num1, "%d", &a);
               sscanf(num2, "%d", &b);
               if (opr == '+')
               {
                   result = a + b;
               }
               else if (opr == '-')
               {
                   result = a - b;
               }
               else if (opr == '/')
               {
                   result = a / b;
               }
               else if (opr == 'X')
               {
                   result = a * b;
               }
               strcpy(finalout, filename);
               strcat(finalout, ext);
               FILE * fPtrRes = fopen(finalout, "w");
               if (fPtrRes != NULL)
               {
                   fprintf(fPtrRes, "%d%c%d=%d", a, opr, b,result);
                   fclose(fPtrRes);
               }
              
           }
           else if (choice == 0)
           {
               fprintf(fPtr, "%s-%d-%s", studentid, runNumber, "Program exit selected\n");
               break;

           }
           runNumber++;
       }
       fclose(fPtr);
   }
  

}

Add a comment
Know the answer?
Add Answer to:
Your program should provide a menu with the following options: 1) Author info 2) Set extension...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT