diff --git a/base/CMakeLists.txt b/base/CMakeLists.txt index 7d1aca0..4460b71 100644 --- a/base/CMakeLists.txt +++ b/base/CMakeLists.txt @@ -92,4 +92,29 @@ add_executable(list stl/list.cpp) add_executable(setops stl/setops.cpp) add_executable(multimap stl/multimap.cpp) add_executable(functor stl/functor.cpp) -add_executable(funadap stl/funadap.cpp) \ No newline at end of file +add_executable(funadap stl/funadap.cpp) +add_executable(strgst1 stl/strgst1.cpp) +add_executable(listrmv stl/listrmv.cpp) +add_executable(usealgo stl/usealgo.cpp) +add_executable(valvect stl/valvect.cpp) +add_executable(vslice stl/vslice.cpp) +add_executable(ilist stl/ilist.cpp) +add_executable(write io/write.cpp) +add_executable(defaults io/defaults.cpp) +add_executable(manip io/manip.cpp) +add_executable(width io/width.cpp) +add_executable(fill io/fill.cpp) +add_executable(precise io/precise.cpp) +add_executable(showpt io/showpt.cpp) +add_executable(setf io/setf.cpp) +add_executable(iomanip io/iomanip.cpp) +add_executable(check_it io/check_it.cpp) +add_executable(cinexcp io/cinexcp.cpp) +add_executable(get_gun io/get_gun.cpp) +add_executable(peeker io/peeker.cpp) +add_executable(truncate io/truncate.cpp) +add_executable(filleio io/filleio.cpp) +add_executable(count io/count.cpp) +add_executable(append io/append.cpp) +add_executable(strout io/strout.cpp) +add_executable(strin io/strin.cpp) \ No newline at end of file diff --git a/base/stl/ilist.cpp b/base/stl/ilist.cpp index 14b668d..3e57c6c 100644 --- a/base/stl/ilist.cpp +++ b/base/stl/ilist.cpp @@ -3,12 +3,41 @@ // #include -#include "ilist.h" +#include using namespace std; -int main(){ - +inline double sum(initializer_list il); + +inline double average(const initializer_list &ril); + +int main() { + + cout << "List 1: sum = " << sum({2, 3, 4}) << ", ave = " << average({2, 3, 4}) << endl; + initializer_list dl = {1.1, 2.2, 3.3, 4.4, 5.5}; + cout << "List 2: sum = " << sum(dl) << ", ave = " << average(dl) << endl; + dl = {16.0, 25.0, 36.0, 40.0, 64.0}; + cout << "List 3: sum = " << sum(dl) << ", ave = " << average(dl) << endl; return 0; } +inline double sum(initializer_list il) { + double tot = 0; + for (auto p = il.begin(); p != il.end(); p++) { + tot += *p; + } + return tot; +} + +inline double average(const initializer_list &ril) { + double tot = 0; + int n = ril.size(); + double ave = 0.0; + if (n > 0) { + for (auto p = ril.begin(); p != ril.end(); p++) { + tot += *p; + } + ave = tot / n; + } + return ave; +} \ No newline at end of file diff --git a/base/stl/listrmv.cpp b/base/stl/listrmv.cpp index 3c75e86..3ce7c6d 100644 --- a/base/stl/listrmv.cpp +++ b/base/stl/listrmv.cpp @@ -3,12 +3,43 @@ // #include -#include "listrmv.h" +#include +#include using namespace std; -int main(){ - +void Show(int); + +const int LIM = 10; + +int main() { + + int ar[LIM]{ + 4, 5, 4, 2, 2, 3, 4, 8, 1, 4 + }; + list la(ar, ar + LIM); + list lb(la); + cout << "Original list contents:\n\t"; + for_each(la.begin(), la.end(), Show); + cout << endl; + la.remove(4); + cout << "After using the remove() methond:\n"; + cout << "la:\t"; + for_each(la.begin(), la.end(), Show); + cout << endl; + auto last = remove(lb.begin(), lb.end(), 4); + cout << "After using the remove() function:\n"; + cout << "lb:\t"; + for_each(lb.begin(), lb.end(), Show); + cout << endl; + lb.erase(last, lb.end()); + cout << "After using the earse() methond:\n"; + cout << "lb:\t"; + for_each(lb.begin(), lb.end(), Show); + cout << endl; return 0; } +void Show(int v) { + cout << v << ' '; +} \ No newline at end of file diff --git a/base/stl/strgst1.cpp b/base/stl/strgst1.cpp index 6437b6a..c79086f 100644 --- a/base/stl/strgst1.cpp +++ b/base/stl/strgst1.cpp @@ -10,6 +10,16 @@ using namespace std; int main() { - + string letters; + cout << "Enter the letter grouping (quit to quit): "; + while (cin >> letters && letters != "quit") { + cout << "Permutations of " << letters << endl; + sort(letters.begin(), letters.end()); + cout << letters << endl; + while (next_permutation(letters.begin(), letters.end())) + cout << letters << endl; + cout << "Enter next sequence (quit to quit): "; + } + cout << "Done.\n"; return 0; } diff --git a/base/stl/usealgo.cpp b/base/stl/usealgo.cpp index 78427b1..0fa253a 100644 --- a/base/stl/usealgo.cpp +++ b/base/stl/usealgo.cpp @@ -3,12 +3,60 @@ // #include -#include "usealgo.h" +#include +#include +#include +#include +#include +#include +#include using namespace std; -int main(){ - +char toLower(char ch) { + return tolower(ch); +} + +string &ToLower(string &st); + +void display(const string &s); + +int main() { + + vector words; + cout << "Enter words(enter quit to quit) :\n"; + string input; + while (cin >> input && input != "quit") + words.push_back(input); + + cout << "You entered the following words:\n"; + for_each(words.begin(), words.end(), display); + cout << endl; + + set wordset; + transform(words.begin(), words.end(), insert_iterator >(wordset, wordset.begin()), ToLower); + cout << "\nAlphabetic list of words:\n"; + for_each(wordset.begin(), wordset.end(), display); + cout << endl; + + map wordmap; + set::iterator si; + for (si = wordset.begin(); si != wordset.end(); si++) { + wordmap[*si] = count(words.begin(), words.end(), *si); + } + + cout << "\nWord frequency:\n"; + for (si = wordset.begin(); si != wordset.end(); si++) { + cout << *si << " : " << wordmap[*si] << endl; + } return 0; } +string &ToLower(string &st) { + transform(st.begin(), st.end(), st.begin(), toLower); + return st; +} + +void display(const string &s) { + cout << s << " "; +} \ No newline at end of file diff --git a/base/stl/valvect.cpp b/base/stl/valvect.cpp index 91fe784..1254d0d 100644 --- a/base/stl/valvect.cpp +++ b/base/stl/valvect.cpp @@ -3,12 +3,37 @@ // #include -#include "valvect.h" - +#include +#include +#include using namespace std; -int main(){ - +int main() { + vector data; + double temp; + + cout<<"Enter numbers (<=0 to quit):\n"; + while (cin>>temp && temp >0) + data.push_back(temp); + sort(data.begin(), data.end()); + int size =data.size(); + valarray numbers(size); + for (int i = 0; i < size; ++i) { + numbers[i] = data[i]; + } + valarray sq_rts(size); + sq_rts = sqrt(numbers); + valarray result(size); + result = numbers + 2.0 * sq_rts; + cout.setf(ios_base::fixed); + cout.precision(4); + for (int i = 0; i < size; ++i) { + cout.width(8); + cout< -#include "vslice.h" +#include +#include using namespace std; -int main(){ - +const int SIZE = 12; +typedef valarray vint; + +void show(const vint &v, int cols); + +int main() { + + vint valint(SIZE); + for (int i = 0; i < SIZE; ++i) { + valint[i] = rand() % 10; + } + cout << "Original array:\n"; + show(valint, 3); + vint vcol(valint[slice(1, 4, 3)]); + cout << "Second column:\n"; + show(vcol, 1); + vint vrow(valint[slice(3, 3, 1)]); + cout << "Second row:\n"; + show(vrow, 3); + valint[slice(2, 4, 3)] = 10; + cout << "Set last column to 10:\n"; + show(valint, 3); + cout << "Set first column to sum of next two:\n"; + valint[slice(0, 4, 3)] = vint(valint[slice(1, 4, 3)]) + vint(valint[slice(2, 4, 3)]); + show(valint, 3); return 0; } +void show(const vint &v, int cols) { + int lim = v.size(); + for (int i = 0; i < lim; ++i) { + cout.width(3); + cout << v[i]; + if (i % cols == cols - 1) + cout << endl; + else + cout << ' '; + } + if (lim % cols != 0) + cout << endl; +} \ No newline at end of file