多项式计算器
我正在做一个多项式计算器,随着代码的进展,我需要一些帮助。
现在我只制作了 polinom 类,我将其表示为带有术语和一些函数的链接列表(现在仅读取和打印多项式函数)。
这是现在仅读取多项式并打印它的主程序:
#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;
}
头文件 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
编辑
感谢 JonH,所有编译错误现在已修复,但读取函数无法用作输入字符未正确插入到列表中。我知道这对你们来说可能微不足道,但如果你们能帮助我那就太好了。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现您的代码中缺少许多花括号和右括号。在花了几分钟修复了至少 10 个这些问题之后,我认为如果我帮助你学习钓鱼,而不是给你鱼作为今晚的晚餐,你会得到更好的服务。
你的代码写得就像意识流一样。当您构建代码时,您的思维会跳来跳去,思考您需要构建的其他内容以及您刚刚编写的内容引入的新需求。当你想到这些事情时,你就去写它们,然后回到原来的地方。在不知不觉中,您已经通过跳来跳去、到处写一些位来编写了数百行代码。这样做的问题是,您不可能在不遗漏一些语法位的情况下继续处理这样的代码段。
您应该采用更具迭代性的方法来编写代码。具体如何执行此操作取决于经验,但这里有一些指导:
// TODO: Implement bool DoTheThing(int);
但先不要实现它们。重复直到你完成了所有的事情。在此过程中您可能会编译 50 次或更多次。
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:
// TODO: Implement bool DoTheThing(int);
But don't implement them yet.Repeat until you have implemented everything. You might compile 50 times or more during this process.
你的根本问题是你写了一堆代码,没有一点一点地测试它,没有考虑它。当您作为初学者编写时,应该尝试一次添加一点并确保它可以编译。即使作为高级程序员,模块化也是设计和代码编写过程中极其重要的一部分。
也就是说,这里有一些关于您发布的代码的提示:
printable
非常丑陋,因此无法调试或理解。if
语句的数量表明了设计缺陷。if (isdigit(c))
语句中缺少结束大括号。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:
printable
is ugly as sin, and therefore impossible to debug or understand.if
statements is indicative of design flaws.if (isdigit(c))
statement.这些编译错误肯定在错误消息中具有与其关联的行号。您是否尝试过查看指示的行以查看缺少的内容?如果这没有帮助,请发布编译器的完整错误输出,以便我们可以了解错误是什么。
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:
EDIT
I also fixed the print function: