C :: Calculate Value Of Math Expression Using Strings

Nov 4, 2013

Here is my objective: Write a c program that calculates the value of a mathematical expression comprised of positive numbers and the operations "+" and "-" . Specifically, first prompt the user to input an expression, read itin as a string, and then print the value of the expression. You may assume that the expression does not contain spaces, maximumsize of the expression (including digits and operators) can be 20,and that all numbers are single digit numbers.

Note that, the digits would be read in as characters; you will need to translate them to numbers (recall the ASCII table). Implementation Requirements:

Write a function called "evaluate" that takes as input a mathematical expression(as a string) and returns the value of the expression. The prototype of the function is: intevaluate(char expr[]);

Sample Output:
Input: 4+2-1+7 Output:12

Code:
int main()
{
char expr[21];
int a,ssum;
printf("Input: ");
scanf("%20s", expr);

[Code] .....

the program runs, but the output is not coming out correct.

View 11 Replies


ADVERTISEMENT

C :: Calculate Variance And Standard Deviation - MATH Error

Mar 6, 2015

The values in my file are 5,5,332,156,11,0,555,32,8,9 .... the computer when I scan and read is missing the first 5 and only showing 9 values which is messing up my mean and standard deviation. But when I have them put in order all of my numbers show up:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE 100
void bubbleSort( int * const array, size_t size);// prototype

[Code] .....

if possible, why won't the median work. I have it commented off as of right now....

View 2 Replies View Related

C :: How To Write Powers Without Using Math

Sep 22, 2014

How would one write 3^i without using .math? (i being numbers 0-9)

More specifically, this is what I have:

Code:

#include <stdio.h>
#include <stdlib.h>
int main(void) {
int x, n, answer1, first;
x = 3;
n = 10;
first = 1;

[Code]....

And this is what I want it to produce:

1 0 1.0
3 1 0.333333343
9 2 0.111111112
27 3 0.037037037
81 4 0.012345679
243 5 0.004115226
729 6 0.001371742
2187 7 0.000457247
6561 8 0.000152416
19683 9 0.000050805

I believe I am only having troubles with the first column,

View 10 Replies View Related

C++ :: Inserting Math Functions From GUI

Feb 8, 2013

I have a problem with entering math functions in my Bisection method algorithm program. I just don't know how can I make the function that I enter in my GUI app to go from the GUI to the loop and find the root.As far as I googled I only find codes that you need to pre-enter a function in the double/float.

For example:I have a function f(x) = x^3 - cos(x) - x - 3; and I want to enter that function trough the GUI i made in c++..So this is the main code.

#include "bisection.h"
#include "ui_bisection.h"
#include <QFile>
#include <QString>
#include <math.h>
#include <algorithm>
bisection::bisection(QWidget *parent) :

[code]....

View 11 Replies View Related

C++ :: Math Formulas Using Variables?

Mar 21, 2013

Working on a program that converts infix to postfix math formulas. Ive successfully converted the infix to a postfix notation but now i am having trouble solving the equation from the postfix form. Im trying to set a result equal to three variables as shown below:

result = op1 ch op2;

where op1 and op2 are numbers and ch is the operator (+-*/) depending on what the user entered into a string. The error im getting is that it expected a ";" before postfix so clearly it doesn't understand what im trying to do. how to put the answer from op1 ch op2 into result.

View 4 Replies View Related

C/C++ :: Finding The Value Of Sin(x) Without Using Math Function

Apr 14, 2013

How can i solve a problem of sin(x) without using any math.h and only using my own declared function.

#include <stdio.h>
int fact (int a) {
    int i;
    int k = 1;
    for (i=1;i<=a;i++)

[Code] ....

View 3 Replies View Related

C :: Trapezoidal Rule - Math Library

Mar 22, 2013

I am new to c programing and I had spend 2 days on a program and I can't fix the error:

Code] ....
gcc Test.c -o Test.exe
/tmp/ccZkbk0V.o: In function `f':
Test.c:(.text+0x2f): undefined reference to `sqrt'
collect2: ld returned 1 exit status

Tthe program I am working in is:

#include <stdio.h>
#include <math.h>
int f(int x){
return (sqrt(4.0 - x*x));

[Code] .....

View 2 Replies View Related

C :: Date Math Using Standard Libraries?

Dec 22, 2013

Is there any way to do date math using standard C libraries? I looked around in the time.h but didn't see what I needed.

What I need to do is be able to add a certain number of minutes to a date and have it give the current date/time. For example, add 15918765 minutes to 01/01/1980 00:00 and have it tell me 04/07/2010 4:45PM. I really don't want to write this myself or go platform-specific.

Does this exist somewhere or am I SOL?

View 13 Replies View Related

C :: Program That Solves Math Equations

Apr 30, 2013

I wrote a program that solves an equation of two numbers, but in addition to that, I want it to be able to continue to solve longer equations. Ex: ( solves 2 * 4, or 2 * 4 - 5).I want to put this part of the program's result into a variable and go from there. How do I place the result of the calculation into a variable, and where would it go?

Code:

#include <stdio.h>
#include <string.h>
int main(void) {
int a;
scanf("%d", &a);
char s[2];
scanf("%s", s);
int b;
scanf("%d", &b);
}

[code]....

View 4 Replies View Related

C++ :: Comparing Math Operator With A Variable?

Oct 14, 2013

I am trying to compare a math operator with a variable, but i keep getting this error:

postfixDriver.cpp:93: error: expected primary-expression before ')' token.

I'm sure it's probably something simple, but im just not seeing it. Here is the code snippet:

while (strlen(str) != 0) {
split (str, token);
Postfix stack;
double a;
double b;
double answer;
if (atof(token)==0.0)

[Code] .....

View 2 Replies View Related

C/C++ :: Interest Calculator Math Part

Feb 11, 2014

math part.

The equation I need to use is: payment = [(rate * (1 + rate)^number payments ) / ((1 + rate)^number of payments -1)] * loan

Also, the value of rate in the above equation is (interest/12)/100. So 12% annual interest would be 1% monthly interest.

heres my code:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

[Code].....

View 4 Replies View Related

C# :: Can't Get Math (Sine) Function To Work

May 21, 2014

I'm having trouble with getting a sine function to work. All variables are defined earlier in the same section. I have the code in a button (where I figured it would go) but I get the following error:

WindowsFormsApplication2.Math does not contain a definition for 'Sin'

For reference, I am using Microsoft Visual Studio Express 2013, and am coding a Windows Forms Application.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

[Code] ....

I've tried other functions as well (abs, sqrt, etc.) to no avail, as Math only seems to pop up with two options: Equals and ReferenceEquals.

View 2 Replies View Related

C/C++ :: Define Math Operators As Variable?

Aug 17, 2014

I have to create a program that read two numbers and the math operators +, -, * and /. After that I should print the operation required. But I tried to declare a <symbol> variable as char type, without sucess...

Here goes my script:
float num1, num2, result;
char symbol;
printf("Write two numbers:
");

[code]....

View 1 Replies View Related

C :: Math Function Using Exponential Growth Formula

Sep 3, 2013

Its a basic math function using the exponential growth formula which is correct but the computer is not outputting the right numbers.

Code:
#include <stdio.h>
#include <math.h>
int main() {
int blanketsquares, num1, num2, blanket,remainder;

[Code] .....

View 6 Replies View Related

C :: Math Quiz Program - Multiplication Function

Nov 16, 2014

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void correctResponse(void);
void incorrectResponse (void);
void multiplication( void ); // function prototype

[Code] ....

// end function multiplication

// It keeps crashing. How do I compile the responses into a grade?

View 5 Replies View Related

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 View Related

C/C++ :: CodeBlocks Random Math Quiz Generator

Nov 26, 2014

The user will be allowed to pick:

-the number of problems they want to do
-the type of problem (addition, subtraction, multiplication, division)
-the range of numbers for the problems (like 0 to 9, -100 to 100, etc.) [random numbers in the range]

Your program will then display the given number of problems and get the answer from the user. It will tell the user it they are correct or not. If they are incorrect, it will print the correct answer.

After the given number of problems are displayed, the program will give the user the final number correct and percentage correct. The user will be asked if they want to go again. If so, you will reprompt the user for all the information and run again.

I came up with code thinking I was going in the right direction but it doesn't work correctly at all. We have to use functions. My main issue is how do i call variables from another function so all of the functions work together. I repeat I don't understand parameters and calling functions. here's the code:

#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int get_user_range(int &x, int &y);
int get_random_number ();

[Code]......

View 1 Replies View Related

C/C++ :: Decoding RFID Data Hexadecimal Math

Feb 2, 2015

I currently am stuck at getting the needed outcome data from my RFID card. I got it decoded but now I need to do a few more things in order to get the final card number off the back of the card.

The cryptic value was E********B**0**E** (covered to protect card)
Decrypting it turned into 0000003048D1263B

Now I have 3 more steps to take in order to get to my wanted card number.

Quote
Step 1) Mask off the lower 20-bits (which should give me 0x1263B) I am unsure of how to go about doing that using C++.

View 2 Replies View Related

C++ :: Math Game Program - Stuck On Input File

Oct 21, 2013

I am making a math game program that simple and need to have a user input a name and then that name.txt is brought up and use variables from the text to add to their score. heres my code kinda long.

#include <iostream>
#include <cmath>
#include <cctype>
#include <string>
#include <ctime>
#include <cstdlib>

[Code] ....

View 10 Replies View Related

C/C++ :: Binomial Expansion - Simple Math String Into Array

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

Visual C++ :: Decoding RFID Data Hexadecimal Math

Feb 2, 2015

I currently am stuck at getting the needed outcome data from my RFID card. I got it decoded but now I need to do a few more things in order to get the final card number off the back of the card.

The cryptic value was E********B**0**E** (covered to protect card)

Decrypting it turned into 0000003048D1263B

Now I have 3 more steps to take in order to get to my wanted card number.

Step 1) Mask off the lower 20-bits (which should give me 0x1263B) I am unsure of how to go about doing that using C++.

Step 2) Divide by 2 to strip off the lower parity bit (which should be 0x931d). And again, I'm unsure of how to go about doing this in C++.

Step 3) Convert hexadecimal value to decimal value (which would equal my wanted card number). This should be easily done using C++ at this point - though hard to confirm that since I am on step 1).

Code:
const char* original = "0x931d";
unsigned long n = std::strtoul(original, nullptr, 16);

All of this looks to me like RegEX does to most people - complicated and not understanding why it does what it does but gives the correct output.

View 2 Replies View Related

C++ :: Math Tutor - Guess Correct Answer Of Random Numbers

Jul 11, 2014

Objective: Guess the correct answer of the random numbers

Problem: I guess the correct answer but the program keeps telling me that I'm INCORRECT.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(int argc, const char * argv[]) {
const int MIN_VALUE = 1; // Constants
const int MAX_VALUE = 200;

[Code] ....

View 2 Replies View Related

C++ :: Faster To Create New Variable Or Reuse Existing One For Math Calculation?

Feb 15, 2015

is it better to reuse an existing variable or create a new variable for the following situation:

/* Is this faster: */
glm::mat4 newViewModelMatrix = (*existingViewMatrix) * (*existingModelMatrix);
/* Or is this faster? */
glm::mat4 existingViewModelMatrix; /* declared in class header - initial value assigned in class constructor */
existingViewModelMatrix = (*existingViewMatrix) * (*existingModelMatrix);

Just wondering if it makes a difference or not.

View 1 Replies View Related

C++ :: How To Append Strings To The Front Of Other Strings

Apr 7, 2013

I am programming a translator, and I have it so that it detects words with spaces both in front of and behind them, so I did "string.append(space);" where space equals " ". That added a space to the end, but I still need a space added to the front.

View 1 Replies View Related

C/C++ :: Print More Strings (the Strings May Contain Spaces)?

Feb 12, 2014

I have a problem who must print the sentences who have lenght more than 20 characters. I dont know why, but it prints just the first words. Look what i made.

#include<stdio.h>
#include<conio.h>  
int main()

[Code]....

For instance :

Give the number of sentences : 3

First sentence : I like the website bytes.com
Second sentence : I like more the website bytes.com
Third sentence : bytes.com

After I compile the program it should print the first two sentences.

View 2 Replies View Related

C++ :: Expression Must Have Constant Value?

Oct 23, 2013

line 27 and line 88 Im having a hard time figuring it out what the error is.

#include<iostream>
#include <cmath>
#include<algorithm>

[Code]....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved