我在Windows窗体中创建了项目Calculator
。另外,我在其他cpp文件logic.cpp
中有我的计算器逻辑。
问题:我无法在logic.cpp
中添加<msclr\marshal_cppstd.h>
和Calculator.h
文件。
此文件在此文件中没有其他包含时,我可以包含logic.cpp
。
这是代码: 的 Calculator.h
#pragma once
#include "E:\november\CalculatorForm\CalculatorForm\logic.cpp"
#include <msclr\marshal_cppstd.h>
namespace CalculatorForm {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
...
}
logic.cpp
using namespace std;
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool isMultiplicationOrDevision(char c) {
if (c == '*' || c == '/')return true;
return false;
}
bool isSumOrSubtraction(char c) {
if (c == '+' || c == '-')return true;
return false;
}
double doOp(char c, double a, double b) {
switch (c)
{
case '*':
return a*b;
case '/':
if (!b) {
return 0;
}
return a / b;
case '+':
return a + b;
case '-':
return a - b;
default:
return 0;
}
}
class Expression {
private:
vector<double> values;
vector<char> operators;
public:
Expression(string expr) {
string t;
for each (char c in expr)
{
if (c == '+' || c == '-' || c == '/' || c == '*') {
values.push_back(stod(t));
t.clear();
operators.push_back(c);
}
else {
t += c;
}
}
values.push_back(stod(t));
}
Expression(vector<double> values, vector<char> operators) {
this->values = values;
this->operators = operators;
}
double getResult() {
auto i = find_if(operators.begin(), operators.end(), isMultiplicationOrDevision);
while (i != operators.end()) {
doOperation(i);
i = find_if(operators.begin(), operators.end(), isMultiplicationOrDevision);
}
i = find_if(operators.begin(), operators.end(), isSumOrSubtraction);
while (i != operators.end()) {
doOperation(i);
i = find_if(operators.begin(), operators.end(), isSumOrSubtraction);
}
return values[0];
}
private:
void doOperation(vector<char>::iterator i) {
int index = distance(operators.begin(), i);
double t = doOp(operators[index], values[index], values[index + 1]);
operators.erase(i);
values.erase(values.begin() + index);
values[index] = t;
}
};
string checkForBrackets(string s) {
for (auto i = s.begin(); i < s.end(); i++) {
string::iterator start;
if (*i == '(') {
int dist = distance(s.begin(), i);
string t(i, s.end());
t.erase(t.begin());
string temp(s.begin(), i);
s = temp + checkForBrackets(t);
i = s.begin();
advance(i, dist);
}
if (*i == ')') {
i = s.erase(i);
string t(s.begin(), i);
Expression e(t);
double res = e.getResult();
s.erase(s.begin(), i);
s = to_string(res) + s;
return s;
}
}
return s;
}
string doCalculating(string t) {
t = checkForBrackets(t);
Expression a(t);
t = to_string(a.getResult());
return t;
}
答案 0 :(得分:0)
在.cpp文件中包含.h文件并添加包含防护以防止重新声明变量和函数