多项式计算器

时间:2010-03-09 20:31:58

标签: c++ calculator polynomial-math

我正在做一个多项式计算器,我需要一些帮助,因为我将继续使用代码。

现在我只创建了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,现在修复了所有编译错误,但由于输入字符未正确插入列表,因此读取功能无法正常工作。我知道这对你们来说可能是微不足道的,但是如果你帮助我的话会很棒。

谢谢!

4 个答案:

答案 0 :(得分:10)

我在整个代码中发现了许多缺少花括号和关闭parens的内容。在花了几分钟修理其中至少10分之后,我认为如果我帮助你学习钓鱼会更好,而不是在今晚的晚餐上给你鱼。

您的代码就像意识流一样。当您构建代码时,您的思维会跳跃,思考您需要构建的其他内容以及您刚刚撰写的任何内容引入的新需求。当你想到这些东西时,你就去写它们并回到你原来的位置。在你知道它之前,你已经编写了数百行代码,可以在这里和那里写一些代码。这样做的问题在于,你不可能继续处理这样的代码段,而不会遗漏一些语法位。

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

  1. 首先使用一些(最好是1个)核心方法和成员变量来删除一个类声明。
  2. 编译。您将收到链接器错误等,但您不应该得到任何语法错误,如丢失的parens或分号。在继续之前修复你找到的任何内容。
  3. 实现您刚刚存根的方法/功能。编译&amp;修复非链接器错误。
  4. 当您考虑上述步骤中出现的次要要求或相关要求时,请在代码中写下注释,例如// TODO: Implement bool DoTheThing(int);但不要实现它们。
  5. 循环回到第1步,尽可能保持您正在处理的范围有限且基本。如果没有干净的编译,千万不要超越编译步骤。
  6. 重复直到你实现了所有内容。在此过程中,您可以编译50次或更多次。

答案 1 :(得分:2)

你的根本问题是你在没有考虑它的情况下,在没有逐一测试的情况下编写了一堆代码。当你作为初学者写作时,你应该尝试一次添加一点并确保它编译。即使作为高级程序员,模块化也是设计和代码编写过程中非常重要的一部分。

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

  1. 你的函数printable是罪恶的,因此无法调试或理解。
  2. 嵌套if语句的数量表示设计缺陷。
  3. 您在if (isdigit(c))声明中错过了一个结束括号。
  4. 在同一行上声明(尤其是初始化)多个变量是不好的形式。

答案 2 :(得分:1)

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

答案 3 :(得分:1)

你的阅读功能中缺少一些花括号。

我在这里重做:

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