This is my first code in C++. Since I'm new to the language, I'm just looking for pointers on what can be made better. I tried to cut out unnecessary stuff, but there are some comments in there. I know Java, so if you're trying to explain anything in Java, I can understand it.
Feel free to mention anything, whether it be code style (which I'm completely unsure what the conventions are in C++), performance, etc.
The goal of the program is to take an equation, such as 5x^2+48204x=7
, and parse the 5
and 48204
into strings.
using namespace std;
void printHelp();
class Equation {
public:
string equation1;
string equation2;
int matrix [3][2];
Equation(string one, string two) {
one.erase(remove_if(one.begin(), one.end(), isspace), one.end());
two.erase(remove_if(two.begin(), two.end(), isspace), two.end());
equation1 = one;
equation2 = two;
cout << equation1 << endl;
init();
}
void init() {
size_t firstx = equation1.find_first_of("x");
size_t secondx = equation1.find_first_of("x", firstx + 1);
if (secondx == string::npos) {
cout << "Secondx == 0" << endl;
}
//cout << firstx << endl;
//cout << secondx << endl;
int startloc = firstx - 1;
while (true) {
//cout << "starting with startloc = " << startloc << endl;
if (startloc == -1) {
break;
}
char c = equation1.at(startloc);
if (c == ' ' || c == '=' || c == '+' || c == '-' || c == '*' || c == '/') {
//cout << "Found something" << endl;
break;
}
startloc--;
}
string s;
unsigned int i = startloc + 1;
//cout << i << endl;
//cout << firstx << endl;
while (i < firstx) {
//cout << "Character at " << i << endl;
//cout << equation1[i] << endl;
stringstream ss;
string temp;
ss << equation1[i];
ss >> temp;
s.append(temp);
i++;
}
cout << s << endl;
startloc = secondx - 1;
while (true) {
//cout << "starting with startloc = " << startloc << endl;
if (startloc == 0) {
cout << "Problem" << endl;
}
char c = equation1.at(startloc);
if (c == ' ' || c == '=' || c == '+' || c == '-' || c == '*' || c == '/') {
//cout << "Found something" << endl;
break;
}
startloc--;
}
s = "";
i = startloc + 1;
while (i < secondx) {
stringstream ss;
string temp;
ss << equation1[i];
ss >> s;
s.append(temp);
i++;
}
cout << s << endl;
}
};
int main(int argc, char *argv[])
{
Equation equation(argv[1], "blank");
//printHelp();
string s;
cin >> s;
return 0;
}
void printHelp()
{
cout << "Welcome!" << endl;
cout << "Stuff about commands" << endl;
}
-
3\$\begingroup\$ The main issue in this program is the program design. Most importantly, you need to use the most fundamental concept in object-oriented design: private encapsulation. C++ is no different from Java in this. Since you are a beginner programmer, you should learn object-oriented design as early as possible, this is far more important than learning all the dirty details of a particular language. I'd actually go back to Java and study OO design with that language as foundation, since Java is far cleaner (though less powerful) than C++. \$\endgroup\$Lundin– Lundin2013年01月30日 15:46:25 +00:00Commented Jan 30, 2013 at 15:46
-
1\$\begingroup\$ Comment your code. What's the purpose of the code? What's the main algorithm? What are the important lines of code and why do they exist? \$\endgroup\$Dave Jarvis– Dave Jarvis2013年01月30日 17:19:23 +00:00Commented Jan 30, 2013 at 17:19
-
\$\begingroup\$ Thanks guys. I dont think i need to go back and relearn OOP because im sure i understand it, althought this code doesnt show it. I wrote this in about 15 minutes so i didnt clean it at all. Im going to repost it once i clean it in 2ish hours. (Also, i dont understand pointers, if anyone has advice) hopefully it will look alot nicer when cleaned \$\endgroup\$Tips48– Tips482013年01月30日 18:23:45 +00:00Commented Jan 30, 2013 at 18:23
-
\$\begingroup\$ Updated the code, attempted to clean it up and use some of the changes that were suggested \$\endgroup\$Tips48– Tips482013年01月30日 21:24:31 +00:00Commented Jan 30, 2013 at 21:24
-
\$\begingroup\$ My code has been cleaned up greatly, thank you guys. \$\endgroup\$Tips48– Tips482013年01月31日 01:01:01 +00:00Commented Jan 31, 2013 at 1:01
1 Answer 1
Without changing things too much...
You need some
#include
s.Don't use
using namespace
. One of the issues is knowing where a function comes from (which one is being used) - this hides it. Using a shortstd::
gains you a lot in understandability. You can dousing std::string
which is better, but imo even that isn't worth it.You might as well put
printHelp
beforemain
rather than after, that way you don't have to declare it. And it's not used currently anyway.Matrix also isn't used.
Having an
Equation
class doesn't make sense for what you're (currently) using it for. You might as well just have a naked function.You don't allow for the possibility that
firstx
may be invalid, ie there are nox
in the equation. This may be a given, of course. And for that matter, that the x^2 term will be first!If
secondx
is invalid, skip over processing it.Multiple breaks in while loops should be avoided, for readability. This (first) loop probably should be split off into another function since it's used twice, together with the following section.
I'm a little nervous of
int startloc = firstx - 1;
, sincefirstx
is unsigned andstartloc
isn't. I'd castfirstx
to anint
first if I were using it like this.Consider using std::string::find_last_of and std::string::substr. They'll simplify the code somewhat.
Check that you don't lose a negative coefficient.
-
\$\begingroup\$ Thanks, lots of little things i didnt think of. As for printhelp() ill be using it when i start expanding the program. Matrix[][] is unused but i will be using it later. The reason i have the equation class is because ill be expanding it to do multiple things later. I just wrote some (messy) code to try to get the splitting. Your saying instead of #include <string> use std::string? O.o \$\endgroup\$Tips48– Tips482013年01月30日 11:52:59 +00:00Commented Jan 30, 2013 at 11:52
-
\$\begingroup\$ I understand putting that loop in a function since its used multiple times. My last question: is that char -> string conversion correct? I couldnt find any way to do it faster \$\endgroup\$Tips48– Tips482013年01月30日 11:57:38 +00:00Commented Jan 30, 2013 at 11:57
-
1\$\begingroup\$ You need
#include <string>
anyway. But usestd::string
rather thanusing std
andstring
. \$\endgroup\$Glenn Rogers– Glenn Rogers2013年01月30日 12:14:29 +00:00Commented Jan 30, 2013 at 12:14 -
\$\begingroup\$ re the conversion, I don't want to even look at it!! For your loops, the first loop is doing
find_last_of
, and the second is doingsubstr
. Use these instead of making your own. \$\endgroup\$Glenn Rogers– Glenn Rogers2013年01月30日 12:20:59 +00:00Commented Jan 30, 2013 at 12:20 -
\$\begingroup\$ I get how you can use substring and wish i remembered earlier, oops! But how would you use find_last_of? I dont really get how i would use it to replace the first loop. \$\endgroup\$Tips48– Tips482013年01月30日 15:04:32 +00:00Commented Jan 30, 2013 at 15:04