|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Square root:
Using this function NG = 0.5 ( LG + n / LG ) where LG initial guess is 1.0 so the program will look like this Enter a number: 2 The square root of 2.000000 is approximately 1.4142135624 but what my program get is Enter a number: 2 The square root of 2.000000 is approximately 1.5000000000 I'm using double for all of my input and output. Calculator: ( Program 2 Write the program calculator which should implement a simple calculator. The program should allow the user to repeatedly enter expressions consisting of an operand, an operator, and another operand. It should then perform the calculation, output the result, and should wait for the next input. Your program must have a function scan_data with three output parameters, returning the operator and the two operands from the input line. Your program must also have a function calculate which should perform the calculation entered by the user. This function should have three input parameters (operator and two operands), and should return the result of the calculation. The program should ignore any incorrect input, such as a wrong operator, or an operand that is not a number. Your program should support following operators: + add - subtract * multiply / divide ^ power (accumulator to the power of operand) q quit For calculating the power of a number, your program is allowed to use the math library's pow function. The function prototype of this function is: double pow(double x, double y); The function returns the value of raising x to the power of y (that is, x ^ y). Example: ./calculator Please enter first operand, operator, and second operand, or q to quit. 1.3 + 2.5 result: 3.800000 176.12312 - 83.311 result: 92.812120 81327.12 * 23 result: 1870523.760000 738 / 3 result: 246.000000 2.25 ^ 3 result: 11.390625 q ) That's the outline but i have no idea how to make the operator. Plz help me out, thx |
| Sponsored Links |
|
|
|
|||
|
Thx for your help. But i still have probelm
here's my program /*calculate the square root of a number */ #include <stdio.h> int main(void) { double n; /* input number */ double LG; /* input */ double NG; /* output number */ /* Get number input */ printf("Enter a number: \n"); scanf("%lf", &n); while (NG-LG>= 0.0000000001){ /* input number*/ LG=1.0; /* Calculate the square root*/ NG=0.5 * (LG +n / LG); } /* Display the results */ printf("The square root of %lf is approximately %lf", n, NG); return (0); } What did i do wrong? or it can't be loop like this, PLs help me to decode a perfect program PLS! (P.S. it's blowing my headoff!!!!!!!) |
|
|||
|
Here's an example for program 2.
It'n not clean code. I had nothing better to do this afternoon, so I've made this in half an hour. Code:
#include <iostream>
#include <math.h>
using namespace std;
float calculate(float num1, float num2, char operand);
void stripInput(char* input, float& num1, float& num2, char& operand);
char* subString(const char* str, char* substr, int start, int end=-1);
int main()
{
char input[256];
do {
cin.getline(input,256);
float num1, num2;
char operand;
if(strcmp(input,"q") != 0)
{
stripInput(input, num1, num2, operand);
cout << num1 << operand << num2 << " = " << calculate(num1,num2,operand) << endl;
}
}while(strcmp(input,"q") != 0);
}
float calculate(float num1, float num2, char operand)
{
switch(operand)
{
case '+':
return num1+num2;
break;
case '-':
return num1-num2;
break;
case '*':
return num1*num2;
break;
case '/':
return num1/num2;
break;
case '^':
return (float)pow((double)num1,(double)num2);
break;
default:
cout << "Error - Invalid Operator" << endl;
break;
}
return 0;
}
void stripInput(char* input, float& num1, float& num2, char& operand)
{
char* substr;
int i;
for( i=0;
i < strlen(input) &&
(input[i] != '+' &&
input[i] != '-' &&
input[i] != '*' &&
input[i] != '/' &&
input[i] != '^');
++i);
char tmp[256];
num1 = atof(subString(input, tmp, 0, i));
num2 = atof(subString(input, tmp, i+1));
operand = input[i];
}
char* subString(const char* str, char* substr, int start, int end)
{
if(end == -1)
end=strlen(str);
int count=0;
for(int i = start; i < end; ++i)
substr[count++] = str[i];
substr[count]='\0';
return substr;
}
|
| Sponsored Links |
|
|
|
|||||
|
It doesn't seem like making a calculator would be so complicated, but I guess there are a lot of underlying principles that need to be elaborated upon, "codually".
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. |
|
|||||
|
Assigning LG = 1.0 within the while loop is guaranteed to keep you from making progress.
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||
|
Quote:
I know for sure that there are functions in C++ that do what I've made, but I don't program in ISO C++ in a wile, so I've made functions to resolv my problems. I prefered that way(I could search for documentation though). For example if I've used the String from C++ I wold smpliefied my algorithm, but I didn't know if this guy is familiared with Sring type. As for complicated code it's simple to undurstand sometimes when you are new to a language. For example: a=a==5?b:c; it's simple but I think it's a little harder to undestand at first. if(a==5) a=b; else a=c; |
|
|||||
|
Here is my version(s) of the square-root-estimation program:
-> This version only takes the input value, and then returns the resulted value back: C++ Code:
-> This version takes both the input and the user's estimation, then prints the results in more details: C++ Code:
Both versions only need to include <iostream>, using namespace std. The epsilon thing is pretty sloppy, but they should work on simple numbers. |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| Xav | ........ | 1276.19 |
| MeTh0Dz|Reb0rn | ........ | 1048.58 |
| marwex89 | ........ | 869.98 |
| John | ........ | 868.39 |
| morefood2001 | ........ | 868.04 |
| WingedPanther | ........ | 761.06 |
| Brandon W | ........ | 684.87 |
| chili5 | ........ | 294.12 |
| Steve.L | ........ | 216.18 |
| dargueta | ........ | 192.86 |
Goal: 100,000 Posts
Complete: 81%