https://www.cise.ufl.edu/~sahni/dsaac
1 array count
#include <iostream>
#include <string>
using namespace std;
template <typename T, size_t N>
size_t get_count(T (&arr)[N]) {
return sizeof(arr)/sizeof(T);
}
void print(int (&arr)[10]) {
for (auto i:arr) {
cout << i << endl;
}
}
int main(int argc, char **argv) {
int arr[5] = {1,2,3,4,5};
cout << get_count(arr) << endl;
return 0;
}
2 array_fill
#include <iostream>
#include <string>
using namespace std;
template <typename T, size_t N,typename Value>
void array_fill(T (&arr)[N], Value val) {
for (auto &i : arr) {
i = val;
}
}
int main() {
int arr[5] = {1,2,3,4,5};
int value = 10;
array_fill(arr, value);
for (auto i : arr) {
cout << i << endl;
}
}
3 inner_product
#include <iostream>
#include <string>
using namespace std;
template <typename T, size_t N>
T inner_product(T (&arr1)[N], T (&arr2)[N]) {
T sum{0};
for (size_t i = 0; i < N; ++i) {
sum += arr1[i] * arr2[i];
}
return sum;
}
int main() {
int a[3] = {1,2,3};
int b[3] = {4,5,6};
cout << inner_product(a,b) << endl;
}
4 iota
#include <iostream>
#include <string>
using namespace std;
template <typename T, size_t N, typename Value>
void iota(T (&arr)[N], Value val) {
for (size_t i = 0; i < N; ++i) {
arr[i] = val + i;
}
}
int main() {
int arr[5] = {1,2,3,4,5};
int value = 9;
iota(arr, value);
for (auto i:arr) {
cout << i << endl;
}
}
5 is_sorted
#include <iostream>
#include <string>
using namespace std;
template <typename T, size_t N>
bool is_sorted(T (&arr)[N]) {
bool increase_sorted = true;
bool decrease_sorted = true;
for (size_t i = 0; i < N-1; ++i) {
if (arr[i] > arr[i+1]) {
increase_sorted = false;
} else {
decrease_sorted = false;
}
}
if (increase_sorted || decrease_sorted) {
return true;
} else {
return false;
}
}
int main() {
int a[5] = {1,2,3,4,5};
int b[5] = {3,2,5,4,1};
int c[5] = {5,4,3,2,1};
cout << "a is sorted: " << is_sorted(a) << endl;
cout << "b is sorted: " << is_sorted(b) << endl;
cout << "c is sorted: " << is_sorted(c) << endl;
}
6 mismatch
#include <iostream>
#include <string>
using namespace std;
template <typename T, size_t N>
int mismatch(T (&arr1)[N], T (&arr2)[N]) {
for (size_t i = 0; i < N; ++i) {
if (arr1[i] != arr2[i]) {
return i;
}
}
return -1;
}
int main() {
int a[5] = {1,2,3,4,5};
int b[5] = {1,2,5,4,5};
int c[5] = {1,2,3,4,5};
cout << mismatch(a,b) << endl;
cout << mismatch(a,c) << endl;
}
7 try catch
#include <iostream>
#include <string>
#include <exception>
using namespace std;
template <typename T>
T abc(T a, T b, T c) {
if (a < 0 && b < 0 && c < 0) {
throw 1;
} else if (a == 0 && b == 0 && c == 0) {
throw 0;
}
return a + b * c;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
try {
cout << abc(a,b,c) << endl;
} catch (int e) {
if (e == 0) {
cout << "All parameters are 0" << endl;
} else if (e == 1) {
cout << "All parameters < 0" << endl;
}
}
}
- 创建2D数组
create2dArray.h
#pragma once
#include <stddef.h>
#include <exception>
#include <iostream>
using namespace std;
template <typename T>
bool make2dArray(T **&ptr, size_t row, size_t col) {
try {
ptr = new T* [row];
for (size_t i = 0; i < row; ++i) {
ptr[i] = new T[col];
}
return true;
} catch (bad_alloc e) {
cout << e.what() << endl;
return false;
}
}
template <typename T>
void print(T **arr, size_t row, size_t col) {
for (size_t i = 0; i < row; ++i) {
std::cout << "Print an row: " << std::endl;
for (size_t j = 0; j < col; ++j) {
std::cout << "Print a col: " << std::endl;
std::cout << arr[i][j] << " " << std::endl;
}
std::cout << std::endl;
}
}
template <typename T>
void delete2dArray(T **ptr, size_t row) {
for (size_t i = 0; i < row; ++i) {
delete [] ptr[i];
}
delete [] ptr;
ptr = nullptr;
}
第三个参数是数组而不是colum
create2dArray.h
#pragma once
#include <stddef.h>
#include <iostream>
#include <exception>
#include <memory>
template <typename T>
bool make2dArray(T **&ptr, size_t row, size_t arr[]) {
ptr = new T * [row];
for (size_t i = 0; i < row; ++i) {
ptr[i] = new T[arr[i]];
}
return true;
}
template <typename T>
void print(T **ptr, size_t row, size_t arr[]) {
for (size_t i = 0; i < row; ++i) {
std::cout << "Print a row" << std::endl;
for (size_t j = 0; j < arr[i]; ++j) {
std::cout << ptr[i][j] << " ";
}
std::cout << std::endl;
}
}
template <typename T>
void deleteArray(T **ptr, size_t row) {
for (size_t i = 0; i < row; ++i) {
delete [] ptr[i];
ptr[i] = nullptr;
}
delete [] ptr;
ptr = nullptr;
}
main.cpp
#include "make2dArray.h"
using namespace std;
int main() {
int **ptr = nullptr;
size_t row;
cout << "Enter the row and col" << endl;
cin >> row;
size_t arr[row];
int temp;
for (int i = 0; i < row; ++i) {
cout << "Enter the col" << endl;
cin >> temp;
arr[i] = temp;
}
if (make2dArray(ptr, row, arr)) {
for (size_t i = 0; i < row; ++i) {
for (size_t j = 0; j < arr[i]; ++j) {
ptr[i][j] = i;
}
}
print(ptr, row, arr);
deleteArray(ptr, row);
}
}
- Change 1D array size
new1Darray.h
#pragma once
#include <stddef.h>
#include <exception>
#include <iostream>
#include <cstring>
using namespace std;
template <typename T>
bool new1Darray(T *oldArr, T **newArr, size_t oldSize, size_t newSize) {
try {
*newArr = new T [newSize];
// memcpy(*newArr, oldArr, min(oldSize, newSize));
for (size_t i = 0; i < min(oldSize, newSize); ++i) {
(*newArr)[i] = oldArr[i];
}
delete [] oldArr;
return true;
} catch (bad_alloc e) {
cout << e.what() << endl;
return false;
}
}
template <typename T>
void print(T *arr, size_t size) {
for (size_t i = 0; i < size; ++i) {
cout << arr[i] << endl;
}
}
main.cpp
#include "new1Darray.h"
int main() {
cout << "Enter the old array size" << endl;
size_t oldSize;
cin >> oldSize;
int * oldArr = new int[oldSize];
for (size_t i = 0; i < oldSize; ++i) {
oldArr[i] = i;
}
cout << "Print the old array" << endl;
print(oldArr, oldSize);
cout << "Enter the new size" << endl;
size_t newSize;
cin >> newSize;
int *newArr = nullptr;
if (new1Darray(oldArr, &newArr, oldSize, newSize)) {
cout << "Print the new array" << endl;
print(newArr, newSize);
}
return 0;
}
拷贝时,本想使用memcpy
,但是拷贝过后,新的值全为0,看起来memcpy
只能用在char
类型数组拷贝上?
文中习题currency:
currency.h
#pragma once
#include <iostream>
enum signType{plus, minus};
class currency {
friend std::ostream &operator<<(std::ostream &os, currency &c);
public:
currency() = default;
currency(signType symble, unsigned long dollar, unsigned int cent)
: sign(symble), dollars(dollar), cents(cent) {}
~currency() {}
void setValue(signType sign, unsigned long, unsigned int);
void setValue(double);
signType getSign() const;
unsigned long getDollars() const;
unsigned int getCents() const;
currency operator+(const currency &rhs) const;
private:
signType sign;
unsigned long dollars;
unsigned int cents;
};
currency.cpp
#include "currency.h"
void currency::setValue(signType sign, unsigned long dollar, unsigned int cent) {
if (cent > 99) {
throw "Cents should be < 100";
}
sign = sign;
dollars = dollar;
cents = cent;
}
void currency::setValue(double val) {
if (val < 0) {
sign = minus;
val = -val;
} else {
sign = plus;
}
dollars = (unsigned long) val;
cents = (unsigned int)((val - dollars + 0.001) * 100);
}
signType currency::getSign() const {
return sign;
}
unsigned long currency::getDollars() const {
return dollars;
}
unsigned int currency::getCents() const {
return cents;
}
currency currency::operator+(const currency &rhs) const {
signType tmpSign;
double m1 = dollars + cents/100.00;
double m2 = rhs.dollars + rhs.cents/100.00;
if (sign == minus) {m1 = -m1;}
if (rhs.sign == minus) {m2 = -m2;}
if (m1 >= m2) {
tmpSign = plus;
} else {
tmpSign = minus;
}
currency tmp(tmpSign, 0, 0);
tmp.setValue(m1 - m2);
return tmp;
}
std::ostream &operator<<(std::ostream &os, currency &c) {
if (c.getSign() == minus) {std::cout << "-";}
std::cout << " $" << c.getDollars() << ".";
if (c.getCents() < 10) {std::cout << "0";}
std::cout << c.getCents() << std::endl;
return os;
}
main.cpp
#include "currency.h"
using namespace std;
int main() {
currency g, h(signType::plus, 3, 50), i, j;
g.setValue(signType::minus, 2, 25);
i.setValue(-6.25);
j = h + g;
cout << j;
}
全排列
#pragma once
#include <stddef.h>
#include <iostream>
#include <string>
#include <vector>
template <typename T>
std::vector<T> removeElement(std::vector<T> v, size_t index) {
v.erase(std::begin(v) + index);
return v;
}
template <typename T>
void combine(T element, std::vector<std::vector<T>> &arr) {
if (arr.empty()) {arr.push_back(std::vector<T>{element});}
for (auto &i:arr) {
i.insert(i.begin(), element);
}
}
template <typename T>
void print(const std::vector<std::vector<T>> &v) {
std::cout << "List the combination: " << std::endl;
for (const auto &i:v) {
std::cout << "List the element: " << std::endl;
for (const auto &j:i) {
std::cout << j << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
template <typename T>
void combination(std::vector<T> &input, std::vector<std::vector<T>> &output) {
if (input.size() == 1) {
output.push_back(input);
return;
}
for (size_t i = 0; i < input.size(); ++i) {
std::vector<T> removedVec = removeElement(input, i);
std::vector<std::vector<T>> tmpVec;
combination(removedVec, tmpVec);
combine(input[i], tmpVec);
for (const auto &i:tmpVec) {
output.push_back(i);
}
}
}
main.cpp
#include "combination.h"
#include <chrono>
using namespace std;
int main() {
vector<vector<int>> output;
int temp;
vector<int> t;
cout << "Please enter the elements" << endl;
while (cin >> temp) {
t.push_back(temp);
}
auto start = chrono::high_resolution_clock::now();
combination(t, output);
auto end = chrono::high_resolution_clock::now();
chrono::duration<double, milli> time = end - start;
print(output);
cout << "Time consuming: " << time.count() << endl;
}
阶乘非递归算法
#pragma once
template <typename T>
T jiecheng(T target) {
T val = 1;
while (target >= 1) {
val *= target;
--target;
}
return val;
}
斐波纳锲数列:
fab.h
#pragma once
#include <iostream>
#include <string>
template <typename T>
T fab(T n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fab(n - 1) + fab(n - 2);
}
template <typename T>
class fabObj {
public:
T operator()(T n) {
if (n == 0) return 0;
if (n == 1) return 1;
T val = fab(n - 1) + fab(n - 2);
std::cout << "The number is: " << n;
std::cout << ", the value is: " << val << std::endl;
return val;
}
};
main.cpp
#include <vector>
#include <algorithm>
#include "fab.h"
using namespace std;
int main() {
fabObj<int> f;
vector<int> v{1,2,3,4,5,6,7,8,9};
for_each(v.begin(), v.end(), f);
}
The number is: 2, the value is: 1
The number is: 3, the value is: 2
The number is: 4, the value is: 3
The number is: 5, the value is: 5
The number is: 6, the value is: 8
The number is: 7, the value is: 13
The number is: 8, the value is: 21
The number is: 9, the value is: 34