简体   繁体   English

在C ++中使用队列和堆栈对2个向量进行加减运算

[英]Addition and substraction of 2 vectors using queue and stack in C++

I have a problem with my C++ code. 我的C ++代码有问题。 I need to add and substract 2 math vectors component by component using some header files but I can also implement some other functions. 我需要使用一些头文件逐个组件添加和减去2个数学向量,但是我还可以实现其他一些功能。 The thing is that my program doesn't have any errors but it's not executing... For example, if I have vector A(3,5) and B(1,7) the results should be: A+B=(4,12) and AB=(2, -2). 问题是我的程序没有任何错误,但没有执行...例如,如果我有向量A(3,5)和B(1,7),则结果应为:A + B =(4 ,12)和AB =(2,-2)。 This is my main .cpp file: 这是我的主要.cpp文件:

#include <iostream>
#include "queque.h"
#include "stack_base.h"
using namespace std;

template<typename T>
class App {
    public:
        Stack<T> stack;
        T minEle;
        App(Stack<T> stack) {
            this->stack = stack;
        }
        T sum(){
            Stack<T> tempStack = stack;
            T sum=1;
            for(int i=0;i<stack.getTopLevel();i++){

            sum+=tempStack.peek();
            tempStack.pop();
        }
            return sum;
        }
        T substract(){
            Stack<T> tempStack = stack;
            T sum=1;
            for(int i=0;i<stack.getTopLevel();i++){

            sum-=tempStack.peek();
            tempStack.pop();
        }
            return sum;
        }
};

int main(){
  Stack<int> myStack;
    App<int> a(myStack);
    int values[7] = {5, 2, 3, 1, 4, 8, 6};
    int values1[7] = {5, 2, 3, 1, 4, 8, 6};
    for(int i=0;i<8;i++)
    myStack.push(values[i]);
    myStack.push(values1[i]);
    cout<<a.sum();
  return 0;
}

Also, here you can find the queque.h file: https://pastebin.com/yg0CdCnd and the stack_base: https://pastebin.com/P6rzQJC1 Thanks, any help would be useful! 另外,在这里您可以找到queque.h文件: https ://pastebin.com/yg0CdCnd和stack_base: https ://pastebin.com/P6rzQJC1谢谢,任何帮助都将是有用的!

You need to add parantheses to your main function otherwise it won't run. 您需要在主函数中添加括号,否则它将无法运行。

int main()
{
  Stack<int> myStack;
  App<int> a(myStack);
  int values[7] = {5, 2, 3, 1, 4, 8, 6};
  int values1[7] = {5, 2, 3, 1, 4, 8, 6};
  for(int i=0;i<8;i++)
  myStack.push(values[i]);
  myStack.push(values1[i]);
  cout<<a.sum();
  return 0;
}

I have not checked if your code is correct, but that's the reason why it will not execute. 我没有检查您的代码是否正确,但这就是为什么它不会执行的原因。

Your implementation of sum() and subtract() is flawed. 您的sum()subtract()是有缺陷的。 You shouldn't refer to getTopLevel at all. 您根本不应该引用getTopLevel It is defined in the linked header file as being the index of the top item of the stack in the internal array. 它在链接的头文件中定义为内部数组中堆栈顶部项目的索引。 This is an implementation detail of Stack and shouldn't really be part of the public interface. 这是Stack的实现细节,不应真正成为公共接口的一部分。

Instead I suggest you use Stack as it is intended - by pushing elements onto it and then popping off the top element as required. 相反,我建议您按预期使用Stack -通过元素推入 Stack ,然后根据需要弹出顶部元素。

eg 例如

    T sum()
    {
        Stack<T> tempStack = stack;
        T sum = 0;

        while (!tempStack.isEmpty())
        {
            sum += tempStack.pop();
        }

        return sum;
    }

It does not work because when you create your App object here 它不起作用,因为在此处创建App对象时

App<int> a(myStack);

You are creating a new Stack for the object a by copying your myStack . 您正在通过复制myStack为对象a创建新的Stack。 Pushing data to the variable myStack won't change the Stack<T> variable inside your object a . 将数据推送到变量myStack不会更改对象a内的Stack<T>变量。

To have the expected result, you need to change your class App so that it should have a pointer to a Stack<T> data member such as: 为了获得预期的结果,您需要更改App类,使其具有指向Stack<T>数据成员的指针,例如:

Stack<T> * stack;

After that, update your constructor to correspond your data member: 之后,更新您的构造函数以对应您的数据成员:

App(Stack<T> &stack) {
    this->stack = &stack;
}
T substract(){
    Stack<T> tempStack = *stack;
    T sum=1;
    for(int i=0; i<=stack->getTopLevel(); i++) {
        sum-=tempStack.peek();
        tempStack.pop();
    }
    return sum;
}
T sum(){
    Stack<T> tempStack = *stack;
    T sum=1;
    for(int i=0; i<=stack->getTopLevel(); i++) {
        sum+=tempStack.peek();
        tempStack.pop();
    }
    return sum;
}

That should do it. 那应该做。 Also you are defining a NMAX 10 in your stack_base.h file and NMAX 5 in queque.h file. 另外,您stack_base.hqueque.h文件中定义NMAX 10 ,并在queque.h文件中定义NMAX 5 That limits the number of the elements in your stack. 这限制了堆栈中元素的数量。 As far as your code implies queque.h is unnecessary. 据您的代码暗示queque.h是不必要的。 Remove the #include "queque.h" line and changing the line you are defining NMAX to something like #define NMAX 20 in the stack_base.h file. 删除#include "queque.h"行,并将正在定义NMAX的行更改为stack_base.h文件中的#define NMAX 20stack_base.h内容。 So that myStack could contain 20 elements. 这样myStack可以包含20个元素。 Your main function should look like this at the end: 最后,您的主要功能应如下所示:

int main(){
    Stack<int> myStack;
    App<int> a(myStack);
    int values[7] = {5, 2, 3, 1, 4, 8, 6};
    int values1[7] = {5, 2, 3, 1, 4, 8, 6};
    for(int i=0; i<8; i++) {
        myStack.push(values[i]);
        myStack.push(values1[i]);
    }
    cout<<a.sum()<<endl;
    return 0;
}

59 should be displayed which is 1 more than the summation of your elements since you initialized your sum variable to 1. 由于将sum变量初始化为1,因此应该显示59,它比元素的sum多1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM