如何使用c ++ dinamic内存显示和使用浮点数进行计算

时间:2015-11-22 02:55:06

标签: c++

我怎样才能说出最大值5.3而不仅仅是5  使其与其他值和方法一起使用??

   //main class
            #include<iostream>
            #include "FloatList.h"
            using namespace std;

            int main(){

                int size;
                float element;
                cout<<"Enter the maximum number of values to store in the array: ";
                cin>>size;
                FloatList f1(size);
                for(int e=0;e<size;e++){
                    cout<<"Enter the value you want to store in the position number "<<e+1<<" :";
                    cin>>element;
                    f1.setElement(e,element);
                }
                cout<<endl;
                cout<<"The elements in the array are the following: "<<endl;
                for(int i=0;i<size;i++){
                    cout<<f1.getElement(i)<<endl;
                }
                cout<<"The maximum value in the array is: "<<f1.calculateMaxValue()<<endl;
                cout<<"The minimum value in the array is: "<<f1.calculateMinValue()<<endl;
                cout<<"The average value in the array is: "<<f1.calculateAverage()<<endl;
                cout<<"This is now a copy of the original array:"<<endl;

                return 0;
            }

            //FloatList.h

            #ifndef FLOATLIST_H_
            #define FLOATLIST_H_
            #include<iostream>
            #include <cstdlib>
            using namespace std;

            class FloatList {
            private:
                int size;
                int numElements;
                float *list;
                bool isValid(int);
            public:
                FloatList(int=1);
                FloatList(FloatList &);
                void setElement(int, int); // Sets an element to a value.
                float getElement(int); // Returns an element.
                ~FloatList();
                void setNumElements(int numElements);
                void setSize(int size);
                int getNumElements();
                int getSize();
                float calculateMaxValue();
                float calculateMinValue();
                float calculateAverage();

            };

            #endif /* FLOATLIST_H_ */

            //FloatList.cpp
            #include "FloatList.h"

            FloatList::FloatList(int tempSize):size(tempSize){
                list = new float[size];
                    numElements = size;
                    for (int i = 0; i < size; i++){
                        list[i] = 0;
                    }

            }

            void FloatList::setNumElements(int numElements) {
                this->numElements = numElements;
            }

            FloatList::FloatList(FloatList& temp) {
                 size=temp.getSize();
                list = new float[size];
                        numElements = size;
                        for (int i = 0; i < size; i++){
                            list[i] = temp.getElement(i);
                        }
            }

            int FloatList::getNumElements() {
                return numElements;
            }

            int FloatList::getSize()  {
                return size;
            }

            void FloatList::setSize(int size) {
                this->size = size;
            }

            FloatList::~FloatList() {
                delete [] list;
            }

            bool FloatList::isValid(int element)
            {
                bool status;
                if (element < 0 || element >= numElements)
                    status = false;
                else
                    status = true;
                return status;
            }
            void FloatList::setElement(int element, int value)
            {
                if (isValid(element))
                    list[element] = value;
                else
                {
                    cout << "Error: Invalid subscript\n";
                    exit(EXIT_FAILURE);
                }
            }
            float FloatList::getElement(int element)
            {
                if (isValid(element))
                    return list[element];
                else
                {
                    cout << "Error: Invalid subscript\n";
                    exit(EXIT_FAILURE);
                }
            }

            float FloatList::calculateMaxValue() {
                float maxValue=list[0];
                for(int i=1;i<size;i++){
                    if(list[i]>maxValue){
                        maxValue=list[i];
                    }
                }
                return maxValue;
            }

            float FloatList::calculateMinValue() {
                float minValue=list[0];
                    for(int i=1;i<size;i++){
                        if(list[i]<minValue){
                            minValue=list[i];
                        }
                    }
                    return minValue;
            }

            float FloatList::calculateAverage() {
                float average;
                for(int i=0;i<size;i++){
                            average+=list[i];
                        }
                        return average/size;

            }

        //output:
        Enter the maximum number of values to store in the array: 3
        Enter the value you want to store in the position number 1 :1.2
        Enter the value you want to store in the position number 2 :5.3
        Enter the value you want to store in the position number 3 :4.5

        The elements in the array are the following: 
        1
        5
        4
        The maximum value in the array is: 5
        The minimum value in the array is: 1
        The average value in the array is: 3.33333
        This is now a copy of the original array:
        1
        5
        4

我怎样才能说出最大值5.3而不仅仅是5 使其与其他值和方法一起使用??`

0 个答案:

没有答案