首页 > 解决方案 > 如何在类 inputFactory 中调用字符串解析函数

问题描述

我在打电话给我尝试过的课程时遇到了麻烦,inputFactory* i = new inputFactory(3,{2},{+},{2})但它说错了,我卡在那里我不知道如何通过char**。我是否需要在此之前加载输入,或者我可以像我尝试的那样输入它们吗

这是程序的其余部分:

INPUT_FACTORY_HPP
#define INPUT_FACTORY_HPP

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include "add.hpp"
#include "base.hpp"
#include "div.hpp"
#include "mult.hpp"
#include "sub.hpp"
#include "pow.hpp"
#include "op.hpp"

using namespace std ;

class inputFactory{        
    private:
        vector<string> inputVector ;    
        vector<Base*> _input ;
        vector<Base*> temp ;
    public:
        string parse(int n, char** input){
            for(int i = 1 ; i < n ; ++i){
                int j = 0 ;
                string temp ;
                temp.clear() ;
                while(input[i][j] != '\0'){
                    temp += input[i][j] ;
                    ++j ;
                }
                inputVector.push_back(temp) ;
            }

            if(inputVector.size() == 0) return "" ; 
            else if(inputVector.size() < 3) return "" ;
            else if((inputVector.size() - 3) % 2 != 0) return "" ;
    
            Base* a ;
            Base* b ;

            bool hasPeriod = false ;    
             bool isInt = true ;

            for(auto it : inputVector[0]){
                if(it == '.') {
                    if(hasPeriod) return "" ;
                    hasPeriod = true ;
                    continue ;
                }
                else if(!(isdigit(it))){        
                    isInt = false ;
                    break ;
                }
            }

            hasPeriod = false ;
            if(isInt == false) return "" ;
            else if(isInt == true){
                a = new Op(stod(input[1])) ;
                temp.push_back(a) ;
            }
            isInt = true ;
            for(auto it : inputVector[2]){
                if(it == '.'){
                    if(hasPeriod) return "" ;
                    hasPeriod = true ;
                    continue ;
                }
                if(!(isdigit(it))){
                    isInt = false ;
                    break ;
                }
            }

            if(isInt == false) return "" ;
            else if(isInt == true){
                b = new Op(stod(input[3])) ;
                temp.push_back(b) ;
            }

            if(input[2][0] == '*' && input[2][1] == '*' && input[2][2] == '\0') _input.push_back(new Pow(a, b)) ;
            else if(input[2][0] == '+' && input[2][1] == '\0') _input.push_back(new Add(a, b)) ;
            else if(input[2][0] == '-' && input[2][1] == '\0') _input.push_back(new Sub(a, b)) ;
            else if(input[2][0] == '/' && input[2][1] == '\0') _input.push_back(new Div(a, b)) ;
            else if(input[2][0] == '*' && input[2][1] == '\0') _input.push_back(new Mult(a,b)) ;
            else return "" ;

            string t = _input.back()->stringify() ;
            t += " = " ;
            t += to_string(_input.back()->evaluate()) ;
            if(inputVector.size() == 3) return t ;
    
            a = _input.back() ;
            b = NULL ;
            isInt = true ;
            int j = 0 ;
            int expressionsLeft = (inputVector.size() - 3) ;
            for(int i = 0 ; i < expressionsLeft ; i += 2){
                hasPeriod = false ;
                    while(input[3 + i + 2][j] != '\0'){
                    if(input[3 + i + 2][j] == '.'){
                        if(hasPeriod) return "" ;
                        j++ ;
                        hasPeriod = true ;
                        continue ;
                    }
                    else if(!(isdigit(input[3 + i + 2][j]))){
                        isInt = false ;
                    }
                    j++ ;
                }
                if(isInt == false) return "" ;
                if(isInt == true){
                    b = new Op(stod(input[3 + i + 2])) ;
                    temp.push_back(b) ;
                }
                if(input[3 + i + 1][0] == '*' && input[3 + i + 1][1] == '*' && input[3 + i + 1][2] == '\0' && isInt)
                    _input.push_back(new Pow(a, b)) ;
                            else if(input[3 + i + 1][0] == '+' && input[3 + i + 1][1] == '\0' && isInt) _input.push_back(new Add(a, b)) ;
                            else if(input[3 + i + 1][0] == '-' && input[3 + i + 1][1] == '\0' && isInt) _input.push_back(new Sub(a, b)) ;
                            else if(input[3 + i + 1][0] == '/' && input[3 + i + 1][1] == '\0' && isInt) _input.push_back(new Div(a, b)) ;
                            else if(input[3 + i + 1][0] == '*' && input[3 + i + 1][1] == '\0' && isInt) _input.push_back(new Mult(a,b)) ;
                else return "" ;
                a = _input.back() ;
                isInt = true ;
            }
            t.clear() ;    
            t = _input.back()->stringify() ;
            t += " = " ;
            t += to_string(_input.back()->evaluate()) ;
            return t ;    
        }

        ~inputFactory() { for(auto it : _input) delete it ;
                      for(auto i : temp) delete i ; }    
} ;


#endif

标签: c++mathgoogletest

解决方案


不确定这里的确切问题是什么,但你提到你想inputFactory接受测试。所以基本上你需要的是为它提供一组预期的输入(有效无效)并将实际输出与预期输出进行比较。一些用例:

TEST(inputFactoryTest, inputFactory_invlaid_entries) {
    inputFactory input_factory{};

    char args[3][6];
    strcpy(args[0],"arg_1");
    strcpy(args[1],"arg_2");
    strcpy(args[2],"arg_3");

    char *argv[] = { args[0], args[1], args[2] };

    ASSERT_EQ("", input_factory.parse(0, argv));
    ASSERT_EQ("", input_factory.parse(1, argv));
    ASSERT_EQ("", input_factory.parse(2, argv));;
}

您应该以类似的方式(作为单元测试)测试其他类(AddPow)并将inputFactoryTest其视为集成测试或使用模拟来模拟inputFactory.

附带说明:Base需要一个(默认)虚拟析构函数,并且不需要它的用户声明的构造函数。尽量将手动内存管理保持在最低限度,这意味着:避免使用原始 ptrs 并尽可能使用 unique_ptrs(例如替换Base*unique_ptr<Base>等)。尝试将代码模块化为更小、定义良好的(私有)方法,以便更容易阅读/重构。


推荐阅读