在C ++中生成0的getHighest函数调用导致错误

时间:2018-04-15 23:16:42

标签: c++

以下是代码。当我执行代码时,getLowest正常工作,因为它返回13,这是正确的。但是,getHighest在它应该是40时返回0。当它应该是40时,屏幕上的返回值是0.有什么我可以改变。我已经提供了4种int类型,除了getHighest函数之外,调用正在执行。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(535, 302)

        self.listWidget = QtWidgets.QListWidget(Form)
        self.listWidget.setGeometry(QtCore.QRect(50, 60, 256, 192))
        self.listWidget.setObjectName("listWidget")
        self.list = self.listWidget

        # +++
        self.currentRow  = None
        self.currentText = ''
        self.listWidget.currentRowChanged.connect(self.itemActiv)     
        self.listWidget.currentTextChanged.connect(self.itemActivText)     


        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(320, 130, 90, 33))
        self.pushButton.setObjectName("pushButton")

        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(420, 130, 90, 33))
        self.pushButton_2.setObjectName("pushButton_2")

        self.pushButton_3 = QtWidgets.QPushButton(Form)
        self.pushButton_3.setGeometry(QtCore.QRect(329, 190, 181, 61))
        self.pushButton_3.setObjectName("pushButton_3")

        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(50, 10, 431, 31))
        self.label.setObjectName("label")

        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(310, 70, 221, 33))
        self.lineEdit.setObjectName("lineEdit")
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "ekle"))
        self.pushButton.clicked.connect(self.list_add)
        self.pushButton_2.setText(_translate("Form", "sil"))
        self.pushButton_3.setText(_translate("Form", "kapat"))
        self.pushButton_3.clicked.connect(quit)
        self.pushButton_2.clicked.connect(self.f1) #refresh) #f1)
        self.label.setText(_translate("Form", "veriler ekleniyor"))

    def list_add(self):
        self.label.setText(self.lineEdit.text() + " eklendi")
        self.listWidget.addItem(self.lineEdit.text())


    def f1(self):
        self.label.setText(self.currentText + " silindi")
        it = self.listWidget.takeItem(self.currentRow)
        del it

    def itemActiv(self, item):
        self.currentRow = item

    def itemActivText(self, text):
        self.currentText = text


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

在getLowest函数中,当你执行返回指令时,它返回低,它返回值并退出它正在做的事情。在getHighest中你直接返回一个0,你实际上没有做任何事情,我会建议修复你的代码:

     int getLowest(int numArray[])
        {
            //int high = numArray[0]; in this function let´s just search for the lowest
            int numElements = sizeof( numArray ) / sizeof( numArray[0] ); //This will count automatically the number of elements in your array
            int low = numArray[0];


            for (int sub = 1; sub < numElements; sub += 1)
            {
              if (numArray[sub] < low)
              low = numArray[sub];
            }
            return low; 

        }

  int getHighest(int numArray[])
        {
            int high = numArray[0]; 
            int numElements = sizeof( numArray ) / sizeof( numArray[0] );//This will count automatically the number of elements in your array
            for (int sub = 1; sub < numElements; sub += 1)
            {
                if (numArray[sub] > high)
                high= numArray[sub];
            }
            return high; 

        }

因此,为了给他们打电话,你只需要这样做

cout << "The highest number in the array is "
        << getHighest(numbers) << "." << endl;
    cout << "The lowest number in the array is "
        << getLowest(numbers) << "." << endl;