Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 多项式计算器_C++_Calculator_Polynomial Math - Fatal编程技术网

C++ 多项式计算器

C++ 多项式计算器,c++,calculator,polynomial-math,C++,Calculator,Polynomial Math,我正在做一个多项式计算器,我需要一些帮助,因为我将与代码进展 现在,我只创建了polinom类,我将它表示为一个带有术语和一些函数的链表,现在只读取和打印多项式函数 以下是目前只读取多项式并打印它的主程序: #include "polinom.h" int main() { polinom P1; bool varStatus = false; char var = '\0', readStatus = '\0'; cout << "P1 = "; P1.read(readS

我正在做一个多项式计算器,我需要一些帮助,因为我将与代码进展

现在,我只创建了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,所有编译错误现在都已修复,但由于没有将输入字符正确插入列表中,read函数无法工作。我知道这对你们来说可能微不足道,但如果你们能帮我的话,那就太好了


谢谢

这些编译错误在错误消息中肯定有一个与之相关联的行号。您是否试过查看指示的行以查看缺少的内容?如果这没有帮助,请发布编译器的完整错误输出,以便我们了解错误是什么。

您的根本问题是,您编写了一堆代码,但没有对其进行逐段测试,也没有考虑它。当你作为一个初学者写作时,你应该尝试一次添加一点,并确保它能够编译。即使是高级程序员,模块化也是设计和代码编写过程中极其重要的一部分

也就是说,这里有一些关于您发布的代码的特别提示:

您的函数printable与sin一样丑陋,因此无法调试或理解。 嵌套if语句的数量表示设计缺陷。 您的if-isdigitc语句缺少一个结束括号。 在同一行上声明并初始化多个变量是一种糟糕的形式。
在read函数中缺少几个大括号

我在这里重播:

 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
编辑

我还修复了打印功能:

 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 
            } 
        } 
    } 

我在代码中发现了许多缺少的大括号和右括号。在花了几分钟的时间准备了至少10条之后,我想如果我帮助你学习钓鱼,而不是在今晚的晚餐上给你鱼,你会得到更好的服务

你的代码就像意识流一样被编写。当您构建代码时,您的思维会跳跃,思考您需要构建的其他东西,以及您刚刚编写的任何东西引入的新需求。当你想到这些事情时,你就去写它们,然后回到你原来的地方。在你意识到这一点之前,你已经写了数百行代码,到处乱写。这样做的问题是,您不可能在不丢失少量语法位的情况下继续处理这样的代码段

您应该采用更迭代的方法来编写代码。如何做到这一点需要经验,但这里有一些指导:

首先,用几个核心方法和成员变量删除一个类声明。 编写您将得到链接器错误等,但不应该得到任何语法错误,如缺少paren或分号。在继续之前,修复您发现的任何问题。 实现您刚才插入的方法/函数。编译并修复非链接器错误。 当您想到在上述步骤中出现的次要或从属需求时,请在代码中编写注释,如//TODO:ImplementBool DoTheThingint;但是现在还不要实施它们。 回到第1步,尽可能地限制你正在做的事情的范围。如果没有一个干净的编译,永远不要超越编译步骤。
重复上述步骤,直到您实现了所有内容。在此过程中,您可能会编译50次或更多次。

为什么我的问题会收到-1?因为您将代码吐到了我们的互联网上。@JonH:我也没有投反对票,但发布问题的一个重要方面是最小测试用例。一个人不应该发布自己的整个项目、作业、模块、图书馆等等。嘿,伙计们,我有个主意:别再做傻子了。人们来到这个网站寻求帮助,仅仅因为你发现一些琐碎的事情并不会让它成为一个问题。我相信你的浏览器有某种形式的后退按钮,可以让你从这个问题开始;你不必回答。与其否决投票,说些比你更神圣的话,不如试着给人们一些建议,让他们的问题更清楚、更有效。对于那些真正提供建议的人,谢谢。@GMan:我相信成为一名优秀程序员的关键部分是学会如何提出好的问题。太多时候,我们看到它不工作张贴,没有有用的细节。许多人,包括我自己,批评是为了鼓励海报提出好的问题:描述场景。描述问题。描述尝试和结果。陈述一个具体的问题,并概述成功是什么样子的。好的问题会带来更好的答案。@Vlad-请看下面的我的答案,您缺少2-3个大括号,当代码看起来像这样时,可以在结束大括号后进行注释,如果blah{//lots and lots code}//end if。。。这是可以接受的do@JonH见我的评论
对于您的回答,请感谢您的帮助,但现在它说使用一元*是非法的,而且与运营商poly.end-1@Vlad这些是额外的错误,这只会清除关于大括号的错误。@vlad您调用了poly.end,它是否存在?是的,它返回列表的末尾,请参阅@vlad-一个错误可能会抱怨一些似乎不是错误的东西。看这一行:如果*i->pow==0//如果最后一个术语的幂为0,我们只打印它,这将导致错误,因为这*在您使用它的上下文中是非法的。您的if条件re:1中也缺少a。该函数显然只是检查ASCII码以确定字符是否可打印。它基本上说c是介于*和exclusive or之间的,或者是一个空格,不在要排除的有限字符列表中。它很难看,写得很差,可以显著改进,但理解和调试都不是不可能的。我同意你其余的观点。我很高兴你记住了整个ASCII表。也许“不可能”是一个强有力的词,但当您的代码通过将字符转换为int值而变得模糊时,当您将100个逻辑测试链接在一起时,一些非常错误的东西很容易被忽略。这就是我的意思。记住ASCII值一点也不难。@john:也许不难,但是,如果c>='a'| | c为真,那么就可以记住更多有用的东西,但是考虑到'a'=65,并且'a'和'a'=97之间的差值是32,这样就可以轻松地编写一个循环来完成整个表。回答得好。它找到了问题的根源,并提出了长期解决方案。您可以从另一个答案中看到,专门修复编译错误对提问者没有帮助,因为他立即遇到另一个错误,仍然不知道该怎么办。@indiv-说它没有帮助可能是不对的,因为它确实帮助了海报。您仍然可以很容易地尝试帮助和解释一些事情,就像我在代码和注释中所做的那样。这就是为什么我们也有评论。