Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

There's a few issues here with c++ basics:

using namespace std

using namespace std;

This is usually a code smell, generally speaking the only time you would really want to do this is when you are making some small throwaway program for testing a concept or making an example (or similar). In that case the reduction in typing time actually has a positive ROI. But you don't want to do it in any production code because this pollutes the global namespace which is something you should try to avoid, any benefit from time saved typing is immediately wiped out the first time your program breaks because a name conflict. See this StackOverflow question this StackOverflow question for more info on that.

If the main purpose for doing this is to cut down typing std:: then you can selectively bring in just the names you need by doing:

using std::cout;
using std::cin;

an so on. This cuts down on typing without the downside of polluting the global namespace.

Function declarations

Currently you have this line:

double addition(double num1, double num2), subtraction(double num1, double num2), division(double num1, double num2), 
 multiplication(double num1, double num2), exponential(double num1, double num2), num1, num2, total;

First of all, this line is way too many characters wide to be readable. But more importantly you are declaring some functions and some variables on the same line. This is very bad for readability. Some people only have one declaration per line, but at the very least don't have more than one type of thing being declared on the same line.

The idiomatic c++ way to do function declarations is at the top of the file or in header files. Instead put the function declarations above main one per line:

double addition(double num1, double num2);
double subtraction(double num1, double num2);
double division(double num1, double num2);
double multiplication(double num1, double num2);
double exponential(double num1, double num2);

This way it's much clearer to the readers of your code which functions you are forward declaring.

Unnecessary variables

In each of the functions that computes the value of an expression you have the same structure:

double addition(double num1, double num2)
{
 double total;
 total = num1 + num2;
 return (total);
}

This can be simplified by just doing:

double addition(double num1, double num2)
{
 return num1 + num2;
}

The reduction in the number of lines of code is a win for readability.

Improving the features of the program

In order to compute the expressions such as 5+5^2-10 with correct operator precedence you will need to implement a parser. When I last did something like this I implemented a recursive descent parser. Be warned that this can be difficult to implement.

I would recommend reading: http://en.wikipedia.org/wiki/Recursive_descent_parser

And looking into the Boost Spirit parser library.

There's a few issues here with c++ basics:

using namespace std

using namespace std;

This is usually a code smell, generally speaking the only time you would really want to do this is when you are making some small throwaway program for testing a concept or making an example (or similar). In that case the reduction in typing time actually has a positive ROI. But you don't want to do it in any production code because this pollutes the global namespace which is something you should try to avoid, any benefit from time saved typing is immediately wiped out the first time your program breaks because a name conflict. See this StackOverflow question for more info on that.

If the main purpose for doing this is to cut down typing std:: then you can selectively bring in just the names you need by doing:

using std::cout;
using std::cin;

an so on. This cuts down on typing without the downside of polluting the global namespace.

Function declarations

Currently you have this line:

double addition(double num1, double num2), subtraction(double num1, double num2), division(double num1, double num2), 
 multiplication(double num1, double num2), exponential(double num1, double num2), num1, num2, total;

First of all, this line is way too many characters wide to be readable. But more importantly you are declaring some functions and some variables on the same line. This is very bad for readability. Some people only have one declaration per line, but at the very least don't have more than one type of thing being declared on the same line.

The idiomatic c++ way to do function declarations is at the top of the file or in header files. Instead put the function declarations above main one per line:

double addition(double num1, double num2);
double subtraction(double num1, double num2);
double division(double num1, double num2);
double multiplication(double num1, double num2);
double exponential(double num1, double num2);

This way it's much clearer to the readers of your code which functions you are forward declaring.

Unnecessary variables

In each of the functions that computes the value of an expression you have the same structure:

double addition(double num1, double num2)
{
 double total;
 total = num1 + num2;
 return (total);
}

This can be simplified by just doing:

double addition(double num1, double num2)
{
 return num1 + num2;
}

The reduction in the number of lines of code is a win for readability.

Improving the features of the program

In order to compute the expressions such as 5+5^2-10 with correct operator precedence you will need to implement a parser. When I last did something like this I implemented a recursive descent parser. Be warned that this can be difficult to implement.

I would recommend reading: http://en.wikipedia.org/wiki/Recursive_descent_parser

And looking into the Boost Spirit parser library.

There's a few issues here with c++ basics:

using namespace std

using namespace std;

This is usually a code smell, generally speaking the only time you would really want to do this is when you are making some small throwaway program for testing a concept or making an example (or similar). In that case the reduction in typing time actually has a positive ROI. But you don't want to do it in any production code because this pollutes the global namespace which is something you should try to avoid, any benefit from time saved typing is immediately wiped out the first time your program breaks because a name conflict. See this StackOverflow question for more info on that.

If the main purpose for doing this is to cut down typing std:: then you can selectively bring in just the names you need by doing:

using std::cout;
using std::cin;

an so on. This cuts down on typing without the downside of polluting the global namespace.

Function declarations

Currently you have this line:

double addition(double num1, double num2), subtraction(double num1, double num2), division(double num1, double num2), 
 multiplication(double num1, double num2), exponential(double num1, double num2), num1, num2, total;

First of all, this line is way too many characters wide to be readable. But more importantly you are declaring some functions and some variables on the same line. This is very bad for readability. Some people only have one declaration per line, but at the very least don't have more than one type of thing being declared on the same line.

The idiomatic c++ way to do function declarations is at the top of the file or in header files. Instead put the function declarations above main one per line:

double addition(double num1, double num2);
double subtraction(double num1, double num2);
double division(double num1, double num2);
double multiplication(double num1, double num2);
double exponential(double num1, double num2);

This way it's much clearer to the readers of your code which functions you are forward declaring.

Unnecessary variables

In each of the functions that computes the value of an expression you have the same structure:

double addition(double num1, double num2)
{
 double total;
 total = num1 + num2;
 return (total);
}

This can be simplified by just doing:

double addition(double num1, double num2)
{
 return num1 + num2;
}

The reduction in the number of lines of code is a win for readability.

Improving the features of the program

In order to compute the expressions such as 5+5^2-10 with correct operator precedence you will need to implement a parser. When I last did something like this I implemented a recursive descent parser. Be warned that this can be difficult to implement.

I would recommend reading: http://en.wikipedia.org/wiki/Recursive_descent_parser

And looking into the Boost Spirit parser library.

Fixed missing return type on functions
Source Link
shuttle87
  • 2k
  • 14
  • 19

There's a few issues here with c++ basics:

using namespace std

using namespace std;

This is usually a code smell, generally speaking the only time you would really want to do this is when you are making some small throwaway program for testing a concept or making an example (or similar). In that case the reduction in typing time actually has a positive ROI. But you don't want to do it in any production code because this pollutes the global namespace which is something you should try to avoid, any benefit from time saved typing is immediately wiped out the first time your program breaks because a name conflict. See this StackOverflow question for more info on that.

If the main purpose for doing this is to cut down typing std:: then you can selectively bring in just the names you need by doing:

using std::cout;
using std::cin;

an so on. This cuts down on typing without the downside of polluting the global namespace.

Function declarations

Currently you have this line:

double addition(double num1, double num2), subtraction(double num1, double num2), division(double num1, double num2), 
 multiplication(double num1, double num2), exponential(double num1, double num2), num1, num2, total;

First of all, this line is way too many characters wide to be readable. But more importantly you are declaring some functions and some variables on the same line. This is very bad for readability. Some people only have one declaration per line, but at the very least don't have more than one type of thing being declared on the same line.

The idiomatic c++ way to do function declarations is at the top of the file or in header files. Instead put the function declarations above main one per line:

double addition(double num1, double num2);
double subtraction(double num1, double num2);
double division(double num1, double num2);
double multiplication(double num1, double num2);
double exponential(double num1, double num2);

This way it's much clearer to the readers of your code which functions you are forward declaring.

Unnecessary variables

In each of the functions that computes the value of an expression you have the same structure:

double addition(double num1, double num2)
{
 double total;
 total = num1 + num2;
 return (total);
}

This can be simplified by just doing:

double addition(double num1, double num2)
{
 return num1 + num2;
}

The reduction in the number of lines of code is a win for readability.

Improving the features of the program

In order to compute the expressions such as 5+5^2-10 with correct operator precedence you will need to implement a parser. When I last did something like this I implemented a recursive descent parser. Be warned that this can be difficult to implement.

I would recommend reading: http://en.wikipedia.org/wiki/Recursive_descent_parser

And looking into the Boost Spirit parser library.

There's a few issues here with c++ basics:

using namespace std

using namespace std;

This is usually a code smell, generally speaking the only time you would really want to do this is when you are making some small throwaway program for testing a concept or making an example (or similar). In that case the reduction in typing time actually has a positive ROI. But you don't want to do it in any production code because this pollutes the global namespace which is something you should try to avoid, any benefit from time saved typing is immediately wiped out the first time your program breaks because a name conflict. See this StackOverflow question for more info on that.

If the main purpose for doing this is to cut down typing std:: then you can selectively bring in just the names you need by doing:

using std::cout;
using std::cin;

an so on. This cuts down on typing without the downside of polluting the global namespace.

Function declarations

Currently you have this line:

double addition(double num1, double num2), subtraction(double num1, double num2), division(double num1, double num2), 
 multiplication(double num1, double num2), exponential(double num1, double num2), num1, num2, total;

First of all, this line is way too many characters wide to be readable. But more importantly you are declaring some functions and some variables on the same line. This is very bad for readability. Some people only have one declaration per line, but at the very least don't have more than one type of thing being declared on the same line.

The idiomatic c++ way to do function declarations is at the top of the file or in header files. Instead put the function declarations above main one per line:

double addition(double num1, double num2);
double subtraction(double num1, double num2);
division(double num1, double num2);
multiplication(double num1, double num2);
exponential(double num1, double num2);

This way it's much clearer to the readers of your code which functions you are forward declaring.

Unnecessary variables

In each of the functions that computes the value of an expression you have the same structure:

double addition(double num1, double num2)
{
 double total;
 total = num1 + num2;
 return (total);
}

This can be simplified by just doing:

double addition(double num1, double num2)
{
 return num1 + num2;
}

The reduction in the number of lines of code is a win for readability.

Improving the features of the program

In order to compute the expressions such as 5+5^2-10 with correct operator precedence you will need to implement a parser. When I last did something like this I implemented a recursive descent parser. Be warned that this can be difficult to implement.

I would recommend reading: http://en.wikipedia.org/wiki/Recursive_descent_parser

And looking into the Boost Spirit parser library.

There's a few issues here with c++ basics:

using namespace std

using namespace std;

This is usually a code smell, generally speaking the only time you would really want to do this is when you are making some small throwaway program for testing a concept or making an example (or similar). In that case the reduction in typing time actually has a positive ROI. But you don't want to do it in any production code because this pollutes the global namespace which is something you should try to avoid, any benefit from time saved typing is immediately wiped out the first time your program breaks because a name conflict. See this StackOverflow question for more info on that.

If the main purpose for doing this is to cut down typing std:: then you can selectively bring in just the names you need by doing:

using std::cout;
using std::cin;

an so on. This cuts down on typing without the downside of polluting the global namespace.

Function declarations

Currently you have this line:

double addition(double num1, double num2), subtraction(double num1, double num2), division(double num1, double num2), 
 multiplication(double num1, double num2), exponential(double num1, double num2), num1, num2, total;

First of all, this line is way too many characters wide to be readable. But more importantly you are declaring some functions and some variables on the same line. This is very bad for readability. Some people only have one declaration per line, but at the very least don't have more than one type of thing being declared on the same line.

The idiomatic c++ way to do function declarations is at the top of the file or in header files. Instead put the function declarations above main one per line:

double addition(double num1, double num2);
double subtraction(double num1, double num2);
double division(double num1, double num2);
double multiplication(double num1, double num2);
double exponential(double num1, double num2);

This way it's much clearer to the readers of your code which functions you are forward declaring.

Unnecessary variables

In each of the functions that computes the value of an expression you have the same structure:

double addition(double num1, double num2)
{
 double total;
 total = num1 + num2;
 return (total);
}

This can be simplified by just doing:

double addition(double num1, double num2)
{
 return num1 + num2;
}

The reduction in the number of lines of code is a win for readability.

Improving the features of the program

In order to compute the expressions such as 5+5^2-10 with correct operator precedence you will need to implement a parser. When I last did something like this I implemented a recursive descent parser. Be warned that this can be difficult to implement.

I would recommend reading: http://en.wikipedia.org/wiki/Recursive_descent_parser

And looking into the Boost Spirit parser library.

Source Link
shuttle87
  • 2k
  • 14
  • 19

There's a few issues here with c++ basics:

using namespace std

using namespace std;

This is usually a code smell, generally speaking the only time you would really want to do this is when you are making some small throwaway program for testing a concept or making an example (or similar). In that case the reduction in typing time actually has a positive ROI. But you don't want to do it in any production code because this pollutes the global namespace which is something you should try to avoid, any benefit from time saved typing is immediately wiped out the first time your program breaks because a name conflict. See this StackOverflow question for more info on that.

If the main purpose for doing this is to cut down typing std:: then you can selectively bring in just the names you need by doing:

using std::cout;
using std::cin;

an so on. This cuts down on typing without the downside of polluting the global namespace.

Function declarations

Currently you have this line:

double addition(double num1, double num2), subtraction(double num1, double num2), division(double num1, double num2), 
 multiplication(double num1, double num2), exponential(double num1, double num2), num1, num2, total;

First of all, this line is way too many characters wide to be readable. But more importantly you are declaring some functions and some variables on the same line. This is very bad for readability. Some people only have one declaration per line, but at the very least don't have more than one type of thing being declared on the same line.

The idiomatic c++ way to do function declarations is at the top of the file or in header files. Instead put the function declarations above main one per line:

double addition(double num1, double num2);
double subtraction(double num1, double num2);
division(double num1, double num2);
multiplication(double num1, double num2);
exponential(double num1, double num2);

This way it's much clearer to the readers of your code which functions you are forward declaring.

Unnecessary variables

In each of the functions that computes the value of an expression you have the same structure:

double addition(double num1, double num2)
{
 double total;
 total = num1 + num2;
 return (total);
}

This can be simplified by just doing:

double addition(double num1, double num2)
{
 return num1 + num2;
}

The reduction in the number of lines of code is a win for readability.

Improving the features of the program

In order to compute the expressions such as 5+5^2-10 with correct operator precedence you will need to implement a parser. When I last did something like this I implemented a recursive descent parser. Be warned that this can be difficult to implement.

I would recommend reading: http://en.wikipedia.org/wiki/Recursive_descent_parser

And looking into the Boost Spirit parser library.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /