C++ :: How Sub String And Math Can Be Used To Make Logical Decisions
Dec 1, 2014
This is an example of how sub string and math can be used to make logical decisions without using if/elses or switches. It works because of this equation
(x + |x|)/x = 2 if x > 0 and = 2 if x < 0.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
//declaration of variables
[Code] ....
View 1 Replies
May 5, 2014
My project is binomial expansion. I want to allow for basic string input. Then, I must process the string for input into the other portions of my program. To make the binomial expansion calculation simpler I am using two separate arrays for the first and second term of the binomial expansion. The integer array structure for each term is [{numerical Coefficient of term}, {exponent of variable a}, {exponent of variable b}, {exponent of variable c}, ......{exponent of variable x}, {exponent of variable y}, exponent of variable z}].
So, if the user doesn't use a variable it's exponent's value will = 0. The reason why I set my array up like this is so it would be very simple to raise each term to the power of n, or (n-k) (Multiple variable exponents by number, raise #coefficient to the power). It would also be easy to combine the terms to get one term. terms[0] are multiplied, all other terms are added.
My issue is translating a basic mathematical string consisting of only Integers, letter, parenthesis, and '^', into the array. I find difficulty mainly due to the '^' symbol. I know how to find the array index for a given letter. termArrayIndex = (int)((char from string) - 'a' + 1)
the main things causing me issues
-The issue is knowing which numbers to multiply by the array.
E.g. (_(_____)^#(__^#__)^#____)^#
I started by finding the first ')', then finding the preceding '(' and processing that string. I would then remove said string and use recusrion to go through the whole string. This method doesn't work because there is data loss upon removal of processed string. EG (__(__)^#) =remove innermost string and parenthesis=> (__^#) != (__()^2,
-maybe enter all terms, leave parenthesis, then process exponents separately...
-I may be able to find the # of separate terms by seeing # of times a '(' comes after a ')'
This is by far the most difficult part of the problem for me. But that also makes it the most fun. I want it to be able to handle all variation of these basic mathematical expressions.
View 3 Replies
View Related
Dec 9, 2013
I have a string that contains a various number of lines which are each separated by and so what I want to do is to put each line into a node in a linked list.The relevant sections in my code are as follows:
Code:
typedef struct line *Line;
struct line {
char *text;
int lineNum;
Line next;
}
[code]....
Code:
strncpy(curr->text, text[prevPos], subLength);
With this line, I was hoping to make curr->text a string that is length subLength and begins at position prevPos in the text string. Except text[prevPos] is treated as a single character and not a string that begins at that position.
View 8 Replies
View Related
Dec 20, 2013
I wrote this program using an online compiler i am getting a lot of errors.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string birthmonth;
string birthday;
[Code] ....
View 6 Replies
View Related
Mar 9, 2013
I've received a segmentation fault in this part of code:
Code:
for (; str[i] != '