Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

got a calculator code but it is not working and i dont understand why. Enter an operator ( + , - , * , /

got a calculator code but it is not working and i dont understand why.
Enter an operator (+,-,*,/, sin, cos, tan, log, sqrt, pow): log
Enter a number: 0.00 l 0.00=-729.19
didnt even enter a number it just happenning like that. Or doing like
Enter an operator (+,-,*,/, sin, cos, tan, log, sqrt, pow): sin
Enter a number: 0.00 s 0.00=0.00
cant write a number.
#include
#include
// Function declarations
double add(double num1, double num2);
double subtract(double num1, double num2);
double multiply(double num1, double num2);
double divide(double num1, double num2);
double sine(double angle);
double cosine(double angle);
double tangent(double angle);
double logarithm(double num);
double squareRoot(double num);
double power(double base, double exponent);
int main(){
char op;
double num1, num2, result;
printf("Enter an operator (+,-,*,/, sin, cos, tan, log, sqrt, pow): ");
scanf("%c", &op);
if (op !='s' && op !='c' && op !='t' && op !='l' && op !='q'){
// For binary operations, prompt for two numbers
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
} else {
// For unary operations, prompt for one number
printf("Enter a number: ");
scanf("%lf", &num1);
num2=0; // Set num2 to 0 for unary operations
}
switch (op){
case '+':
result = add(num1, num2);
break;
case '-':
result = subtract(num1, num2);
break;
case '*':
result = multiply(num1, num2);
break;
case '/':
if (num2==0){
printf("Error: Division by zero
");
return 1; // Exit with an error code
} else {
result = divide(num1, num2);
}
break;
case 's':
result = sine(num1);
break;
case 'c':
result = cosine(num1);
break;
case 't':
result = tangent(num1);
break;
case 'l':
if (num1>0){
result = logarithm(num1);
} else {
printf("Error: Logarithm undefined for non-positive numbers
");
return 1;
}
break;
case 'q':
if (num1>=0){
result = squareRoot(num1);
} else {
printf("Error: Square root undefined for negative numbers
");
return 1;
}
break;
case 'p':
result = power(num1, num2);
break;
default:
printf("Invalid operator
");
return 1;
}
printf("%.2lf %c %.2lf =%.2lf
", num1, op, num2, result);
return 0;
}
// Function definitions
double add(double num1, double num2){
return num1+ num2;
}
double subtract(double num1, double num2){
return num1- num2;
}
double multiply(double num1, double num2){
return num1* num2;
}
double divide(double num1, double num2){
return num1/ num2;
}
double sine(double angle){
return sin(angle * M_PI /180.0);
}
double cosine(double angle){
return cos(angle * M_PI /180.0);
}
double tangent(double angle){
return tan(angle * M_PI /180.0);
}
double logarithm(double num){
return log(num);
}
double squareRoot(double num){
return sqrt(num);
}
double power(double base, double exponent){
return pow(base, exponent);
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions

Question

Describe the selection process.

Answered: 1 week ago

Question

Describe performance management.

Answered: 1 week ago

Question

Explain the importance of preliminary screening.

Answered: 1 week ago