templist.h
这是代码:
#ifndef TEMPLIST_H_
#define TEMPLIST_H_
#include <iostream>
using namespace std;
template <class T>
class TemperatureList{
public:
T* list;
long size;
long MAX_SIZE;
TemperatureList(int n);
void add_temperature(T temperature);
bool full() const;
long get_size() const;
T get_temperature(long position) const;
void output(ostream &outs) const;
T get_last() const;
void detele_last();
};
#endif
:
templist.cpp
#include "templist.h"
template <class T>
TemperatureList<T>::TemperatureList(int n){
list = new T[n];
MAX_SIZE = n;
size = 0;
}
template <class T>
void TemperatureList<T>::add_temperature(T temperature){
if (full() == false){
list[size] = temperature;
size += 1;
}
}
template <class T>
bool TemperatureList<T>::full() const{
if (size == MAX_SIZE) return true;
return false;
}
template <class T>
long TemperatureList<T>::get_size() const{
return size;
}
template <class T>
T TemperatureList<T>::get_temperature(long position) const{
if (position >= size || position < 0) return 0.0;
return list[position];
}
template <class T>
void TemperatureList<T>::output(ostream &outs) const{
for (long i = 0; i < size; i++)
outs << list[i] << endl;
}
template <class T>
T TemperatureList<T>::get_last() const{
if (size == 0) return 0.0;
return list[size-1];
}
template <class T>
void TemperatureList<T>::detele_last(){
if (size > 0);
size -= 1;
}
:
main.cpp
#include "templist.h"
int main(){
TemperatureList<double> temp(50);
temp.add_temperature(4.0);
temp.add_temperature(7.0);
temp.add_temperature(8.0);
temp.add_temperature(9.6);
cout << "Last element is : " << temp.get_last() << endl;
return 0;
}
:
Function MYVLOOKUP(pValue As String, pWorkRng As Range, pIndex As Long)
'the "Function" word define that is that, a Function not s SubRutine, then name is MYVLOOKUP
'and need some parameters: pValue As String, pWorkRng As cell/Range, pIndex As Long
'and String is a string of characters, A range is just that and a Lond stores values from -2,147,483,648 to 2,147,483,647
'Dim declares variables and allocates storage space.
Dim rng As Range
Dim xResult As String
'Here you have two variables rng and xResult
xResult = "" 'here you set the xResulta var as empty var
'also you can do this: xResult = Empty is exactly the same
'Loops!
'You read this this way
'For each rng (that is a range that is a cells inside a couple of cells/bigger range)inside pWorkRng you will do this
'then if inside the pWorkRng the is 100 cells, the loop will run 100 time (if is no errors)
For Each rng In pWorkRng
If rng = pValue Then 'would be better |rng.value = pValue| for better reading
'if the value of rng (one cell from pWorkRng) is equal to the value of pValue then do this:
xResult = xResult & " * " & rng.Offset(0, pIndex - 1) 'here would be better |rng.Offset(0, pIndex - 1).value|
'store inside xResult this = the value of xResult with this string " * " and the value of
'rng with an columns offset of pIndex -1 (not in rows)
'note that the & (ampersand) is the operator to concatenate strings, then if you have two strings
'x = "abc"
'y = "def"
'and you do this:
'x = x & y
'inside x you will get "abcdef", because when you asign the value of a variable, you will erase any other
'previous value and write the new values
'rng.offset(0,pIndex-1) says:
'rng.offset(row,columns)
'rng.offset(same row as rng | 0, col)
'Imagine rng address is G5, then rng.offset(2,1) address will be H7 because you say, send me the address of the cells
'plus 2 rows and plus 1 column, then if you send a negative number this way rng.offset(-2,1) you will get H3,
'then that is why in rng.Offset(0, pIndex - 1) you will get the same row and the column indicated (inside the var pIndex)
'less one.
'And because this happends inside a loo, that runs n times, do the validation (the if statement)
'you will store values concatenated with the previous valus inside xResult
End If
Next 'this word say: go to the next cell, then when is finish just continue
MYVLOOKUP = xResult
'here you send the string of inside xResult to the function, that is, you will see that string in your cell
End Function