循环后分离

时间:2018-04-09 16:53:55

标签: c++

有输入文件包含方程式想知道方程之间的依赖关系。

将每个等式分成输入和输出两个数组"输入和输出"
在我的函数中,我比较了我的两个数组" in and out" 通过使用" find"来了解哪个等式依赖于另一个等式。

我怎样才能将依赖输入放在数组中并且独立于另一个数组而不是像我的输出?

output

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int count = 0, num = 0, num1 = 0, plu, value_is = 0, count_indd = 1;
string str, str1, str2, str3, plu1, str_ind;
string independent[100], dependent[100], out[100], in[100];

void sepa() {
  ifstream file("in.txt");  // read input file
  for (int k = 0; getline(file, str); k++)

  {
    count++;
    out[k] = str.substr(0, 1);

    if (str.find("+") != -1) {
      plu = str.find("+");
      in[k] = str.substr(2, plu + 1);
    } else {
      plu = str.find("*");
      in[k] = str.substr(2, plu + 1);
    }
  }
}

////////////////////////////////////////////////////////////dependancY
void independ(void) {
  for (int i = 0; i < count; i++) {
    str1 = out[i];
    for (int a = 0; a < count; a++) {
      str2 = in[a];
      if (str2.find(str1) != std::string::npos) {
        dependent[num] = str2;
        cout << "dep   =  " << dependent[num] << endl;
        num++;
      } else {
        independent[num1] = str2;
        cout << "indep = " << independent[num1] << "\n";
        num1++;
      }
    }
  }
}

void depend(void) {
  for (int i = 0; i < count; i++) {
    str1 = out[i];
    for (int a = 0; a < count; a++) {
      str2 = in[a];
      dependent[num1] = str2;
      cout << "dep  =  " << dependent[num1] << endl;
      num1++;
      break;
    }
  }
}

///////main

int main(int argc, char* argv[]) {
  int addnum = atoi(argv[2]);  // read number of add
  int mulnum = atoi(argv[4]);  // read number of multiplier

  sepa();
  // if(str2.find(str1) == std::string::npos)
  //{
  independ();
  //}
  /*if(str2.find(str1) != std::string::npos)
  {
      depend();
  }*/
  return 0;
}

1 个答案:

答案 0 :(得分:0)

首先让我们分解你的问题。

a=b+c代表一个等式,可进一步分为:
 * base =&gt; a
 * dependencies =&gt; bc

现在您的问题变为,从文件中读取所有方程,并检查等式的dependencies是否取决于某个基数。
如果是,请将其标记为dependent等式 如果不是,请将其标记为independent等式。

因此a=b+c是一个独立的等式,因为依赖bc不是任何等式的基础。
虽然w=a+r是一个依赖方程,但依赖a也是某个基础。

够公平吗?

因此,让我们首先创建一个equationequation存储原始line,其base及其dependencies dependencies是令牌的数组(std::vector) 它还提供print equation的机制。

struct equation
{
    equation(std::string              const & line,
             std::string              const & base,
             std::vector<std::string> const & dependencies)
        : line(line), base(base), dependencies(dependencies) {}

    void print() const {
        std::cout << "Equation => [" << line 
                  << "]; Base => [" << base 
                  << "]; Deps => [";
        for(int i = 0; i < dependencies.size(); ++i)
            std::cout << dependencies[i] << ", ";
        std::cout << "]\n";
    }

    std::string line;
    std::string base;
    std::vector<std::string> dependencies;
};

在解析每个等式时,我们需要了解它的依赖性 因此,我们需要一个将这条线分解成小标记的标记器 a=b+c基于[a, b, c]=+分为*

std::vector<std::string> tokenize(const char* str)
{
    std::vector<std::string> result;
    char plus = '+', star = '*', equals = '=';
    do {
        const char* begin = str;
        while( *str != equals
            && *str != plus
            && *str != star
            && *str          ) str++;
        result.push_back(std::string(begin, str));
    } while (0 != *str++);
    return result;
}

现在我们需要创建一个read函数 此函数的目的是读取输入文件并从中创建方程向量 我们还存储了base中找到的所有std::set<std::string> all_bases的一组。这将在以后进行比较。

std::vector<equation> read(std::set<std::string> & all_bases)
{
    std::ifstream file("in.txt");  // read input file
    // throw error if file could not be opened
    if(! file) throw std::runtime_error("Could not open file");

    std::string line;
    std::vector<equation> equations;
    while(getline(file, line))
    {
        //de-structure the line
        std::vector<std::string> tokens = tokenize(line.c_str());

        // since '=' would be the first sperator, we find base at [0]
        std::string base = tokens[0];
        all_bases.insert(base);

        // now remove the first element, all that is left is dependencies
        tokens.erase(tokens.begin());

        equations.push_back(equation(line, base, tokens));
    }
    return equations;
}

现在是时候创建一个根据标准评估依赖或独立的函数 标准如果发现任何依赖关系是基础,则等式是相关的。

bool is_dependent(equation              const & equation,
                  std::set<std::string> const & all_bases)
{
    std::vector<std::string> deps = equation.dependencies;
    for(int j = 0; j < deps.size(); ++j)
    {
        std::string item = deps[j];
        if (all_bases.find(item) != all_bases.end())
        {
            return true;
        }
    }
    return false;
}

现在,您已经将代码逻辑地分解为语义值,很容易将其组合成最终程序。 这是完整的代码,供参考 -

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <set>

struct equation
{
    equation(std::string              const & line,
             std::string              const & base,
             std::vector<std::string> const & dependencies)
        : line(line), base(base), dependencies(dependencies) {}

    void print() const {
        std::cout << "Equation => [" << line 
                  << "]; Base => [" << base 
                  << "]; Deps => [";
        for(int i = 0; i < dependencies.size(); ++i)
            std::cout << dependencies[i] << ", ";
        std::cout << "]\n";
    }

    std::string line;
    std::string base;
    std::vector<std::string> dependencies;
};

std::vector<std::string> tokenize(const char* str)
{
    std::vector<std::string> result;
    char plus = '+', star = '*', equals = '=';
    do {
        const char* begin = str;
        while( *str != equals
            && *str != plus
            && *str != star
            && *str          ) str++;
        result.push_back(std::string(begin, str));
    } while (0 != *str++);
    return result;
}

std::vector<equation> read(std::set<std::string> & all_bases)
{
    std::ifstream file("in.txt");  // read input file
    // throw error if file could not be opened
    if(! file) throw std::runtime_error("Could not open file");

    std::string line;
    std::vector<equation> equations;
    while(getline(file, line))
    {
        //de-structure the line
        std::vector<std::string> tokens = tokenize(line.c_str());

        // since '=' would be the first seperator, we find base at [0]
        std::string base = tokens[0];
        all_bases.insert(base);

        // now remove the first element, all that is left is dependencies
        tokens.erase(tokens.begin());

        equations.push_back(equation(line, base, tokens));
    }
    return equations;
}

bool is_dependent(equation              const & equation,
                  std::set<std::string> const & all_bases)
{
    std::vector<std::string> deps = equation.dependencies;
    for(int j = 0; j < deps.size(); ++j)
    {
        std::string item = deps[j];
        if (all_bases.find(item) != all_bases.end())
        {
            return true;
        }
    }
    return false;
}

void segregate(std::vector<equation> const & equations,
               std::set<std::string> const & all_bases,
               std::vector<equation>       & dependent,
               std::vector<equation>       & independent)
{
    for(int i = 0; i < equations.size(); ++i)
    {
        if (is_dependent(equations[i], all_bases))
            dependent.push_back(equations[i]); // here you can push in your desired vector
        else
            independent.push_back(equations[i]);
    }
}

int main()
{
    std::set<std::string> all_bases;
    std::vector<equation> equations = read(all_bases);

    std::vector<equation> dependent;
    std::vector<equation> independent;

    segregate(equations, all_bases, dependent, independent);

    std::cout << "Dependent Equations :\n";
    for(int i = 0; i < dependent.size(); ++i) dependent[i].print();

    std::cout << "\n\nIndependent Equations :\n";
    for(int i = 0; i < independent.size(); ++i) independent[i].print();

    return 0;
}

测试:
输入文件( in.txt

a=b+c
f=d*t
w=a+r
t=e+v
test=equi+valent*t

输出:

Dependent Equations :
Equation => [f=d*t]; Base => [f]; Deps => [d, t, ]
Equation => [w=a+r]; Base => [w]; Deps => [a, r, ]
Equation => [test=equi+valent*t]; Base => [test]; Deps => [equi, valent, t, ]

Independent Equations :
Equation => [a=b+c]; Base => [a]; Deps => [b, c, ]
Equation => [t=e+v]; Base => [t]; Deps => [e, v, ]