Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE HELP ME DO THIS IN C, I HAVE ALREADY MOST OF IT DONE BUT THE FIRST LINE O

ID: 3716530 • Letter: P

Question

PLEASE HELP ME DO THIS IN C, I HAVE ALREADY MOST OF IT DONE BUT THE FIRST LINE OF THE DAT FILE IS TWO CHARACTERS AND THAT RUINS MY CODE HELP ME I THINK I NEED TO CHANGE EVERYTHING TO A STRING BUT I DON'T KNOW WHAT TO DO. REFER TO THE DAT FILE AT THE BOTTOM OF THIS.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int evaluate(char c, int op1, int op2) {
switch(c) {
case '+': return op1 + op2;
case '-': return op1 - op2;
case '*': return op1 * op2;
case '/': return op1 / op2;
default: return -111111;
}
}

int kthDigit(int i, int k) {
int digit;
digit = (pow(10, k))/10;

digit = (i / digit) % 10;
return digit;
}

void separate(double x) {
char sign;
int intpart;
double fractionpart;

if(x < 0) sign = '-';
else sign = '+';

intpart = x;
fractionpart = x - intpart;

printf("Sign : %c Integer Part : %d Fraction Part : %lf ", sign, intpart, fractionpart);
}

void printTheHelpMessage() {
printf(" Command Action ");
printf("+ i j [Integer Add] Add integers i and j and print out result ");
printf("* i j [Integer Multiply] Multiply integers i and j and print out result ");
printf("- i j [Integer Subtract] Subtract integer j from i and print out result ");
printf("/ i j [Integer Divide] Divide integer i by j and print out result of integer division ");
printf("C Ch [Character Case Change] Change character Ch to uppercase and print it out ");
printf("c Ch [Character Case Change] Change character Ch to lowercase and print it out ");
printf("P i k [Print k-th Digit ] Print out the k-th digit of integer i ");
printf("R x i [Round Reals ] Round double value x to i decimal places ");
printf("S x [Separate ] Separate out the sign, integer part and fractional part of double value x ");
printf("H [Help ] Print a short synopsis of all the available commands ");
printf("Q [Quit] Quit ");
}

int main() {
FILE *Calculator;
Calculator = fopen("CommandsProj1.dat", "r");
char inital,initial2;

if(!Calculator) {
printf("Error in File Opening : Unable to open it ");
exit(0);
}

while(!feof(Calculator)) {
char operand;

fscanf(Calculator, "%c", &operand);
switch(operand) {
int op1, op2;
case '+': // arithmetic operations
case '-':
case '*':
case '/':
fscanf(Calculator, "%d %d", &op1, &op2);
int val = evaluate(operand, op1, op2);
printf("%d %c %d = %d ", op1, operand, op2, val);
break;
char c;
case 'c': // convert the char to capital letter
fscanf(Calculator, "%c", &c); // read the extraspace and ignore it
fscanf(Calculator, "%c", &c);
printf("%c => %c ", c, (c+32));
break;
case 'C':// convert the char to small letter
fscanf(Calculator, "%c", &c); // read the extraspace and ignore it
fscanf(Calculator, "%c", &c);
printf("%c => %c ", c, (c-32));
break;
int i, k;
double x;
case 'P': // find the kth digit of a number
fscanf(Calculator, "%d %d", &i, &k);
int digit = kthDigit(i, k);
printf("%d th digit of %d is %d ", k, i, digit);
break;
case 'R': // round x to i digits
fscanf(Calculator, "%lf %d", &x, &i);
double roundedX = (double)((int)(x*pow(10, i)))/pow(10, i);
printf("%lf rounded to %d digts is %lf ", x, i, roundedX);
break;
case 'S': // seperate the sign, int part and fraction in a number
fscanf(Calculator, "%lf", &x);
separate(x);
break;
case 'H': // print the help message
printTheHelpMessage();
break;
case 'Q': // exit the program execution
exit(0);
/*default:
fscanf(Calculator, "%c%c", &inital,&initial2);
printf("%c %c", inital,initial2);
break;*/
}
}
fclose(Calculator); // close the file
return 0;
}


// OUTPUT

Problem Description:

You are required to design a calculator. This calculator reads commands from data file called CommandsProj2.dat. Your program should read the commands in sequential order, process them, perform the necessary calculations, and then print out the results in a neat, readable manner, both to a file and to the screen.

Details:

The first line of the file contains two characters giving the initials of the user (such as GN). The rest of the file consists of a sequence of commands. Each line of the command file contains one command. Each command starts with a character and may have zero or more operands. You may assume that the data file has no errors. The number of commands in the file is unknown. But your processing may stop as soon as the Quit command is processed. The commands are: integer add, integer subtract, integer multiply, integer divide, change to uppercase, change to lowercase, separate, print out all digits, print out the k-th digit, round real number to desired number of decimal places, divide number into two.

Calculator Commands:

+ i j [Integer Add]

Add integers i and j and print out result

* i j [Integer Multiply]

Multiply integers i and j and print out result

- i j [Integer Subtract ]

Subtract integer j from i and print out result

/ i j [Integer Divide ]

Divide integer i by j and print out result of integer division

C Ch [Character Case Change ] Change character Ch to uppercase and print it out

c Ch [Character Case Change] Change character Ch to lowercase and print it out

P i k [Print k-th Digit ]

Print out the k-th digit of integer i

R x i [Round Reals ]

Round double value x to i decimal places

S x [Separate ]

Separate out the sign, integer part and fractional part of double value x

H [Help ]

Print a short synopsis of all the available commands

Q [Quit ]

Quit

1

Deliverables:

You must submit the C file(s) for the entire Calculator and a word document containing your engineering problem solving methodology for the entire Calculator project (Project 1 and 2 combined).

Additional Notes:

Command Notes:

There is always a space between the command character and the operands (if any).

For the division operation, the second operand will never be a 0.

For the operation that changes to uppercase, you will always be given a character that is a lowercase letter of the alphabet (never an uppercase letter of the alphabet, a digit, a punctuation, or a special character).

For the operation involving rounding a double value to the right decimal point, the output may have trailing 0's to pad it at the end. So the output for command “R 3.567 1” may be 3.600. You will not be asked to round a negative number.

Programming Notes:

Print out a short synopsis of all the commands at the start of the program in order to guide the user.

Make sure that your output prints the commands you read in from the file and provides the output in a readable format.

Make sure you separate the output of one command from the next (with a newline).

Start with 2 basic operators (Add and Quit), prepare a file with just these 2 commands and get your program tested and working for it. Then move on to other commands, one by one. If you have not gotten one of the commands to work, then make a dummy function that simply prints out a message saying “Function Z not implemented yet!”

DAT FILE:

MN
+ -3 4
+ -4 -71
- 10 2
- -41 -7
H
* 3 -4
* 2 2
/ 141 13
/ 14 -3
H
c O
C v
P 2134 3
R 3.141500000 2
R 3.141500000 3
R 0.010157 2
S -3.141500
S 31.41500
Q
H

+ i j [Integer Add]

Add integers i and j and print out result

* i j [Integer Multiply]

Multiply integers i and j and print out result

- i j [Integer Subtract ]

Subtract integer j from i and print out result

/ i j [Integer Divide ]

Divide integer i by j and print out result of integer division

Explanation / Answer

If the problem is the first line in the file which is MN, then that line
can be read and ignored before thet actual logic of the program starts.

We can ignore it by reading

char ch[2];
fscanf(Calculator,"%s",ch);

The above lines can be places above the following while loop

while(!feof(Calculator)) {

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote