I'm doing a polynomial calculator and i'll need some help as i'll progress with the code.
For now I made only the polinom class which i represented it as a linked list with terms and some functions(only read and print the polynomial functions for now).
Here's the main program which for now only read a polynomial and prints it:
#include "polinom.h"
int main()
{
polinom P1;
bool varStatus = false;
char var = '\0', readStatus = '\0';
cout << "P1 = ";
P1.read(readStatus, var, varStatus); // i don't need readStatus yet as i haven't implemented the reset and quit functions
cout << "\n\nP = ";
P1.print(var);
getch();
return 0;
}
And the header file polinom.h:
#ifndef _polinom_h
#define _polinom_h
#include <iostream>
#include <list>
#include <cstdlib>
#include <cctype>
#include <cstdio>
#include <conio.h>
using namespace std;
class polinom
{
class term
{
public:
int coef;
int pow;
term()
{
coef = 1;
pow = 0;
}
};
list<term> poly;
list<term>::iterator i;
public:
bool printable(char c)
{
return (
((int(c) > 42 && int(c) < 123) || isspace(c)) && int(c) != 44 && int(c) != 46 && int(c) != 47 &&
int(c) != 58 && int(c) != 59 &&
int(c) != 60 && int(c) != 61 && int(c) != 62 && int(c) != 63 && int(c) != 64 && int(c) != 65 &&
int(c) != 91 && int(c) != 92 && int(c) != 93 && int(c) != 95 && int(c) != 96
);
}
void read(char &readStatus, char &var, bool &varStatus)
{
term t; // term variable to push it into the list of terms
char c, lc, sign; // c = current char, lc = lastchar and sign the '+' or '-' sign before a coefficient
int coef, pow; //variables to pass the coef and power to term t
bool coefRead = false, powRead = false; //reading status of coef and power
while (c != '\r') { //we read characters until carriage return
c = getch(); // get the new imputed char
if (tolower(c) == 'r' || tolower(c) == 'q') { //if the user inputed r or q we reset the input or quit the program
readStatus = c; //pass current char value to readStatus so the program will know what to do next
return; //aborting the reading process
}
else
{
if (printable(c)) cout << c; //print on screen only the correct characters
if (!coefRead && !powRead) //we set term coef to the inputed value
{
if (isdigit(c)) {
if (isdigit(lc)) coef = coef * 10 + int(c); //if the last char was also a digit we multiply the last value of coef by 10 and add current char
else {
if (sign == '-') coef = -(int(c));//if the current coef has '-' before we set coef to it's negative value
else coef = int(c); //this means a new term's coef is read
}
if (!isdigit(c) && isdigit(lc)) coefRead = true; //if the last char was a digit and we reached the var name we stop reading the coefficient
}
else if (coefRead && !powRead) //after coefficient is read we get the term's varname and power
{
if (isdigit(c)) { // just like in the case with coefficient we read the power until the current char is not a digit
if (isdigit(lc)) pow = pow * 10 + int(c);
else pow = int(c);
}
else if (isalpha(c) && isdigit(lc) && !varStatus) { //if the last char was a digit and the current not we reached the var name
var = c; //also even though the variable is inputed more than once we save it only once
varStatus = true; //we mark the var name as read
}
else {
if (isdigit(lc)) powRead = true;
}
}
else {
if (c == '+' || c == '-') { // if a sign was inputed it means a new term is coming and we push the current term to the list and reset
t.coef = coef; // coefRead and powRead so we can read another term
t.pow = pow;
poly.push_back(t);
sign = c;
coefRead = false;
powRead = false;
}
}
lc = c; // we save the last character
}
}
}
void print(char var)
{
for ( i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them
if (i == poly.end() - 1) { // if we reached the last term
if (*(i->pow == 0) //if the last term's power is 0 we print only it's coefficient
cout << *(i->coef);
else
cout << *(i->coef) << var << "^" << *(i->pow); //otherwise we print both
}
else {
if (*(i->coef > 0) //if the coef value is positive
cout << *(i->coef) << var << "^" << *(i->pow) << " + "; //we also add the '+' sign
else
cout << *(i->coef) << var << "^" << *(i->pow) << " - "; // otherwise we add '-' sign
}
}
}
};
#endif
EDIT
All compile errors fixed now thanks to JonH, but the read function is not working as the input characters aren't correctly inserted into the list. I know it may be trivial for you guys, but it would be great if you help me out.
Thanks!
I found MANY missing curly braces and closing parens all throughout your code. After having spent several minutes fixing at least 10 of these, I thought you would be better served if I helped you to learn to fish, rather than giving you fish for tonight's dinner.
Your code is written like a stream of consciousness. As you are building your code your mind jumps around, thinking of other things you need to build and new requirements introduced by whatever you just wrote. When you think of these things, you go write them and come back to where you were. Before you know it, you have written hundreds of lines of code by jumping around, writing bits here and there. The problem with this is that you can't possibly keep juggling sections of code like this without missing little syntax bits along the way.
You should take a more iterative approach to writing code. How exactly you do this will come with experience, but here's some guidance:
Start by stubbing out a class declaration with a few (preferably 1) core methods and member variables.
Compile. You'll get linker errors and the like, but you shouldn't get any syntax errors like missing parens or semicolons. Fix any you do find before moving on.
Implement the methods/functions you just stubbed. Compile & fix non-linker errors.
As you think of minor or dependant requirements that came up during the above steps, write comments in your code, like // TODO: Implement bool DoTheThing(int); But don't implement them yet.
Loop back to step 1, keeping the scope of what you're working on as limited and fundamental as possible. Never move beyond a compilation step without a clean compile.
Repeat until you have implemented everything. You might compile 50 times or more during this process.
Your fundamental problem is that you wrote a bunch of code down without testing it piece by piece, without thinking about it. When you write as a beginner, you should try adding one little bit at a time and making sure it compiles. Even as an advanced programmer, modularization is an extremely important part of the design and code-writing process.
That said, here are a few tips about your posted code in particular:
Your function printable is ugly as sin, and therefore impossible to debug or understand.
The number of nested if statements is indicative of design flaws.
You're missing an end brace on your if (isdigit(c)) statement.
Declaring (and especially initializing) multiple variables on the same line is bad form.
Those compile errors surely has a line number associated to them in the error message. Have you tried looking at the line indicated to see what is missing? If that does not help, please post the complete error output from the compiler so that we can se what the error is.
You are missing a few curly braces in your read function.
I redid it here:
void read(char &readStatus, char &var, bool &varStatus)
{
term t; // term variable to push it into the list of terms
char c, lc, sign; // c = current char, lc = lastchar and sign the '+' or '-' sign before a coefficient
int coef, pow; //variables to pass the coef and power to term t
bool coefRead = false, powRead = false; //reading status of coef and power
while (c != '\r') { //we read characters until carriage return
c = getch(); // get the new imputed char
if (tolower(c) == 'r' || tolower(c) == 'q')
{ //if the user inputed r or q we reset the input or quit the program
readStatus = c; //pass current char value to readStatus so the program will know what to do next
return; //aborting the reading process
}
else
{
if (printable(c))
cout << c; //print on screen only the correct characters
if (!coefRead && !powRead) //we set term coef to the inputed value
{
if (isdigit(c))
{
if (isdigit(lc))
coef = coef * 10 + int(c); //if the last char was also a digit we multiply the last value of coef by 10 and add current char
else
{
if (sign == '-')
coef = -(int(c));//if the current coef has '-' before we set coef to it's negative value
else
coef = int(c); //this means a new term's coef is read
} //end else
}//end if isdigit(c)
if (!isdigit(c) && isdigit(lc))
coefRead = true; //if the last char was a digit and we reached the var name we stop reading the coefficient
} //end if
else if (coefRead && !powRead) //after coefficient is read we get the term's varname and power
{
if (isdigit(c))
{ // just like in the case with coefficient we read the power until the current char is not a digit
if (isdigit(lc))
pow = pow * 10 + int(c);
else
pow = int(c);
}
else if (isalpha(c) && isdigit(lc) && !varStatus)
{ //if the last char was a digit and the current not we reached the var name
var = c; //also even though the variable is inputed more than once we save it only once
varStatus = true; //we mark the var name as read
}
else
{
if (isdigit(lc))
powRead = true;
}
} //end else if
else
{
if (c == '+' || c == '-')
{ // if a sign was inputed it means a new term is coming and we push the current term to the list and reset
t.coef = coef; // coefRead and powRead so we can read another term
t.pow = pow;
poly.push_back(t);
sign = c;
coefRead = false;
powRead = false;
}
}
lc = c; // we save the last character
} //end else
} //end while
} //end function
EDIT
I also fixed the print function:
void print(char var)
{
for ( i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them
if (i == poly.end()) { // if we reached the last term
if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient
cout << i->coef;
else
cout << i->coef << var << "^" << i->pow; //otherwise we print both
}
else {
if (i->coef > 0) //if the coef value is positive
cout << i->coef << var << "^" << i->pow << " + "; //we also add the '+' sign
else
cout << i->coef << var << "^" << i->pow << " - "; // otherwise we add '-' sign
}
}
}
Related
Here is the code:
void Reader::read(short& in) {
char* str = new char[6];
char* strbeg = str;
cin.getline(str, 6);
in = 0;
int value = 0;
short sign = 1;
if (*str == '+' || *str == '-') {
if (*str == '-' ) sign = -1;
str++;
}
while (isdigit(*str)) {
value *= 10;
value += (int) (*str - '0');
str++;
if (value > 32767) {
cout.write("Error, value can't fit short. Try again.\n", 41);
delete[] strbeg;
read(in);
return;
}
}
if (sign == -1) { value *= -1; }
in = (short) value;
delete[] strbeg;
return;
}
What happens is that if I type 999999999, it calls itself but on fourth line it's not gonna ask for input again. Debugger couldn't give much info as it is more language-specific question. Thank you in advance. Have a nice day!
Yes, the goal is to parse input as short. I know about losing 1 from min negative, wip :)
=== edit ===
I've tried goto... No, same thing. So it's not about visible variables or addresses, I guess.
=== edit ===
I can't use operator >> as it is forbidden by the task.
999999999 will cause an overflow, thus failbit is set for cin. Then your program reach read(in), then the cin.getline(). Here, beacause of failbit, cin will not ask any input again.
If you tried to figure out why in my code cin do ask for more input, you might find out all this by yourself.
I write you an example.
#include <iostream>
#include <climits>
using namespace std;
int main() {
char str[6];
short x = 0;
bool flag = false;
while (flag == false) {
cin.getline(str, 6);
flag = cin.good();
if (flag) { // if read successfully
char *p = str;
if (*p=='-') // special case for the first character
++p;
while (*p && *p>='0' && *p<='9')
++p;
if (*p) // there is a non digit non '\0' character
flag = false;
}
if (flag == false) {
cout << "An error occurred, try try again." << endl;
if (!cin.eof()) {
cin.unget(); // put back the possibly read '\n'
cin.ignore(INT_MAX, '\n');
}
cin.clear();
} else {
// str is now ready for parsing
// TODO: do your parsing work here
// for exemple x = atoi(str);
}
}
std::cout << x << std::endl;
return 0;
}
As we have discussed, you don't need new.
Check whether the string read is clean before parsing. If you mix checking and parsing, things will be complicated.
And you don't need recursion.
Read characters from stream by istream::getline seems to be the only option we have here. And when an error occurred, this function really doesn't tell us much, we have to deal with overflow and other problem separately.
Sorry if this is REALLY trivial, I'm just sick at being stuck at step one and need to continue and I have no help anywhere else. I can not for the life of me get this function to cooperate with me. Here's the source:
#include <iostream>
using namespace std;
void ReadDials(){
};
void ToDigit(char *x){
if (*x = 'a'){*x = '54';}
else if (*x = 'A'){*x = 2;}
else if (*x = 'b'){*x = 3;}
else(*x = 4);
};
int main()
{
char one;
char two;
char three;
cin >> one;
ToDigit(&one);
cout << "one is: " << one << endl;
system("PAUSE");
}
What I've tried: alternating between the actual number 2 and the ascii pointer of 2 which I believe is '32' I've used == to try, I've done everything I can possibly think of and I know I'm over thinking it. The point is to have that function convert user input into numbers for a phone dialer.
Problems ranging from numbers not syncing with if statements, and seeing a ':D' face on the console. It's making me very annoyed.
If I need to make myself more clear, I'll be more than happy too.
Thanks in advance for any assistance.
In if (*x = 'a'), the = is an assignment. You want == for comparison. Also, '54' isn't a character value.
void ToDigit(char *x){
if (*x == 'a') { *x = '54'; } /* what is this supposed to do? */
else if (*x == 'A') {*x = 2; }
else if (*x == 'b') {*x = 3; }
else { *x = 4 };
};
It looks like you are trying to use x as an input and output. Let's step back a minute. Let's instead write a function that takes a single char and returns an integer. This would be the canonical way of taking a char and getting a number from it.
int ToDigit(char x){
if (x == 'a') { return 54; }
if (x == 'A') { return 2; }
if (x == 'b') { return 3; }
return 4 ;
};
To use this form of the function, you'd assign the return value to a variable of type int in this case.
char my_input;
int mapped_number;
std::cin >> my_input;
mapped_number = ToDigit(my_input);
std::cout << my_input << " maps to " << mapped_number << ".\n";
novice coder here and I've been asking around how I should go about creating a two stack algorithm for calculating simple expressions (Dijkstra's Two Stack Algorithm) in C++. A quick refresher for anybody that need it:
Two Stack Algorithm:
Value - Push onto value stack
Operator - Push onto operator stack
Left Parenthesis - Ignore
Right Parenthesis - Pop two values from value stack and one value from operator stack and push the result
It appears that using istringstream, which was recommended to me, should allow me to separate the user inputted expression into basic, doubles, and non-doubles. This should allow me to populate my vals and ops stack respectively, however upon debugging, I realized that my vals stack ended up empty at the end (causing a segmentation fault)
I've got no idea what I'm doing wrong, and any help would be appreciated! Keep in mind I am relatively new to coding and my syntax is probably horrible, therefore any type of criticism is welcome.
For reference an input of:
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
Should output:
101
Thus far my code looks like this:
stack<string> ops;
stack<double> vals;
string input;
getline(cin, input);
istringstream scanner(input);
while(true){
double num;
scanner >> num;
if(scanner.fail() && scanner.eof()) break;
else if(!scanner.fail()) vals.push(num);
else{
scanner.clear();
string token;
scanner >> token;
if(token == "(") ;
else if(token == "+") ops.push(token);
else if(token == "*") ops.push(token);
/*Add more operations here (Log, sin, cos...)*/
else if(token == ")"){
string op = ops.top();
ops.pop();
if(op == "+"){
double a, b;
a = vals.top();
vals.pop();
b = vals.top();
vals.pop();
vals.push(a+b);
}
else if(op == "*"){
double a, b;
a = vals.top();
vals.pop();
b = vals.top();
vals.pop();
vals.push(a*b);
}
/*Add more operations here*/
}
}
return vals.top();
}
Thank you for your help!
Turns out the problem was with this:
scanner >> num;
if (scanner.fail() && scanner.eof()) break;
else if (!scanner.fail()) vals.push(num);
Changing it to the following fixed the problem:
if (scanner >> num) vals.push(num);
if (scanner.fail() && scanner.eof()) break;
else {
// ...
}
And putting the return statement below the loop also helped.
Live example
Not a straightforward answer to what you asked, but I reworked your example with out the use of stringstreams, which might be confusing to you. You can use the following code as an alternative:
#include <iostream>
#include <string>
#include <stack>
#include <ctype.h>
double parseExpression(std::string const &expr)
{
std::stack<char> ops;
std::stack<double> vals;
std::string num;
for (auto c : expr) {
if (c == '+' || c == '*') {
if(!num.empty()) vals.push(std::stod(num));
num.clear();
ops.push(c);
} else if (c == ')') {
if (!num.empty()) vals.push(std::stod(num));
num.clear();
char op = ops.top();
ops.pop();
switch (op) {
case '+': {
double tmp = vals.top();
vals.pop();
tmp += vals.top();
vals.pop();
vals.push(tmp);
} break;
case '*': {
double tmp = vals.top();
vals.pop();
tmp *= vals.top();
vals.pop();
vals.push(tmp);
} break;
};
num.clear();
} else if(isdigit(c) || c == '.') {
num.push_back(c);
} else if (isspace(c)) {
if (!num.empty()) vals.push(std::stod(num));
num.clear();
} else if(c != '(') {
throw std::runtime_error("Unknown character in expression!");
}
}
return vals.top();
}
int main()
{
std::string expr("( 1.00 + ( ( 2.000000 + 3.00 ) * ( 4.00 * 5.00 ) ) )");
std::cout << expr << " = " << parseExpression(expr) << std::endl;
return 0;
}
HTH
I can't get the right output.. please help me..
and it should return false when I put number as the first character for the name
like this ,
Enter the name of the first rectangle: rec 1a
Invalid input. Type 'rec' following by the name or 'stop' if done.
Try again! Enter the name of the first rectangle: rec a
Enter a's bottom left x and y coords: 9 3
Enter a's length and height: 2 8
i am only allow to use these 3, not anything else..
#include <iostream>
#include <string>
#include <vector>
and my code is
bool promptread_rec(const string & prompt, const string & invalid, const string & usedname, string & input, vector<Rectangle> & list)
{
cout << prompt;
getline(cin, input);
if (input == "stop")
{
return true;
}
else if (input.substr(0,4) != "rec ")
{
cout << invalid << endl;
return false;
}
else if (input[4] == '0' || input [4] == '1' || ......)
{
cout << invalid << endl;
return false;
}
else if (list.size() > 0)
{
for (int i = 0; i < list.size(); i++)
{
if (input == list[i].getName())
{
cout << usedname;
return false;
}
}
return true;
}
else
{
return true;
}
}
is there a faster way to do it?? need to avoid all numbers e.g. 0,1,2,3,...,9
From the header cctype, you may use the function isalpha(c) on the first character of the string, like:
string a = "a1234";
assert(isalpha(a.at(0)) == true);
a = "1234";
assert(isalpha(a.at(0)) == true);
Just remember to access a.at(0) only if the string is not empty or else it will throw a std::out_of_range exception
References:
http://www.cplusplus.com/reference/cctype/isalpha/
http://www.cplusplus.com/reference/cassert/assert/
http://www.cplusplus.com/reference/string/string/at/
Since you cannot use any other headers you have to implement your own functions, which is actually simple enough for ASCII characters:
bool IsLetter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool IsDigit(char c) {
return c >= '0' && c <= '9';
}
Using these two helper functions you can write a function to test if a name is valid:
bool IsValidName(const std::string &name);
I leave this for you to implement.
Hate coming back for help after getting help on the same program just a few days ago, but I am really struggling to finish this program. In short, I need to create a postfix notation calculator (RPN) with a linked list stack, that allows me to do expressions such as 5 5 5 + + (=15). I've managed to get the main calculation part down now, but I am struggling with handling two of the errors. One of which is "too many operators," and the other, "too many operands." Currently working on "too many operators" and I feel like I am close but can't quite get there.
If the user enters 5 5 + + on the first entry, it catches it and says "too many operands." However, if something is already in the stack from a previous calculation, and they then type the same expression 5 5 + +, it is not saying that the stack is empty, and is instead outputting an answer with the previous number being used. If anyone can see where I am going wrong here, and also point me in a direction for figuring out the other error "too many operators" (ex: 5 5 5 +) that'd be greatly appreciated. Thanks again in advance.
(After messing with it more, it seems the more calculations I do, the more operators actually need to be put in place to be considered empty. I'm guessing I need to popVal somewhere before each expression but not sure where to put it, as I've tried many places and it's not working)
#include<iomanip>
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
class SLLNode
{
double data;
SLLNode *top;
SLLNode *ptr;
public:
SLLNode()
{
top = NULL;
ptr = NULL;
}
bool isEmpty()
{
return top == 0;
}
void pushVal(double val)
{
SLLNode *next = new SLLNode;
next -> data = val;
next -> ptr = top;
top = next;
}
double popVal()
{
if (isEmpty())
{
cout << "Error: Too many operators" << endl;
}
else
{
SLLNode *next = top -> ptr;
double ret = top -> data;
delete top;
top = next;
return ret;
}
}
void print()
{
cout << top -> data << endl;
}
};
bool isOperator(const string& input)
{
string ops[] = {"+", "-", "*", "/"};
for(int i = 0; i < 4; i++)
{
if(input == ops[i])
{
return true;
}
}
return false;
}
void performOp(const string& input, SLLNode& stack)
{
double fVal, sVal;
int errorCheck = 0;
sVal = stack.popVal();
fVal = stack.popVal();
if(input == "+")
{
stack.pushVal(fVal + sVal);
}
else if(input == "-")
{
stack.pushVal(fVal - sVal);
}
else if(input == "*")
{
stack.pushVal(fVal * sVal);
}
else if(input == "/" && sVal != 0)
{
stack.pushVal(fVal / sVal);
}
if(input == "/" && sVal == 0)
{
cout << "Error: Division by zero" << endl;
errorCheck = 1;
}
if(errorCheck == 0)
{
stack.print();
}
}
int main()
{
cout << "::::::::::::::::RPN CALCULATOR:::::::::::::::::" << endl;
cout << "::TYPE IN A POSTFIX EXPRESSION OR 'q' TO QUIT::" << endl;
cout << ":::::::::::::::::::::::::::::::::::::::::::::::" << endl << endl;
string input;
SLLNode stack;
while(true)
{
cin >> input;
double num;
if(istringstream(input) >> num)
{
stack.pushVal(num);
}
else if (isOperator(input))
{
performOp(input, stack);
}
else if (input == "q")
{
return 0;
}
}
}
The basic idea is to:
Read one line (std::getline);
Process this line (std::stringstream);
Output the answer or any errors;
Clean the stack (or destroy it and create a new one on step 2);
Go to 1 and repeat.
What you are missing is the first step. If you get everything directly from stdin, you will treat the newline as a simple whitespace.