Loading lang_cpp_ex_0101...
int count_char(const char *txt, char c) { int count=0; for(int i=0; txt[i]!='\0'; ++i) { if(txt[i]==c) { ++count; } } return count; }
void test_count_char(char c) { printf("\n~~~~ %s() ~~~~\n", __func__); const char *msg="Here is a sentence!"; const int count=count_char(msg, c); printf("<%s> contains %d <%c>\n", msg, count, c); }
int count_multiples(const int *values, int value_count, int divisor) { int count=0; for(int i=0; i<value_count; ++i) { if((values[i]%divisor)==0) { ++count; } } return count; }
void test_count_multiples(int divisor) { printf("\n~~~~ %s() ~~~~\n", __func__); const int numbers[]={1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15}; const int number_count=(int)(sizeof(numbers)/sizeof(numbers[0])); const int count=count_multiples(numbers, number_count, divisor); printf("%d multiples of %d found\n", count, divisor); }
double collect_over(const double *values, int value_count, double limit) { double sum=0.0; for(int i=0; i<value_count; ++i) { const double delta=values[i]-limit; if(delta>0.0) { sum+=delta; } } return sum; }
void test_collect_over(double limit) { printf("\n~~~~ %s() ~~~~\n", __func__); const double samples[]={1.1, 2.2, 3.3, 5.5, 6.6, 7.7, 9.9, 10.01}; const int sample_count=(int)(sizeof(samples)/sizeof(samples[0])); const double amount=collect_over(samples, sample_count, limit); printf("%g over the limit %g\n", amount, limit); }
//----------------------------------------------------------------------------
#ifndef BASICS_REWRITE_HPP #define BASICS_REWRITE_HPP
#include <string> #include <vector>
namespace basics {
int count_char(std::string txt, char c);
int count_multiples(std::vector<int> values, int divisor);
double collect_over(std::vector<double> values, double limit);
} // namespace basics
#endif // BASICS_REWRITE_HPP
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "rewrite.hpp"
namespace basics {
int count_char(std::string txt, char c) { int count=0; for(const auto &elem: txt) { if(elem==c) { ++count; } } return count; }
int count_multiples(std::vector<int> values, int divisor) { int count=0; for(const auto &elem: values) { if((elem%divisor)==0) { ++count; } } return count; }
double collect_over(std::vector<double> values, double limit) { double sum=0.0; for(const auto &elem: values) { const double delta=elem-limit; if(delta>0.0) { sum+=delta; } } return sum; }
} // namespace basics
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "rewrite.hpp"
#include <iostream>
void test_count_char(char c) { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const std::string msg="Here is a sentence!"; const int count=basics::count_char(msg, c); std::cout << '<' << msg << "> contains " << count << " <" << c << ">\n"; }
void test_count_multiples(int divisor) { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const std::vector<int> numbers={1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15}; const int count=basics::count_multiples(numbers, divisor); std::cout << count << " multiples of " << divisor << " found\n"; }
void test_collect_over(double limit) { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; const std::vector<double> values={1.1, 2.2, 3.3, 5.5, 6.6, 7.7, 9.9, 10.01}; const double amount=basics::collect_over(values, limit); std::cout << amount << " over the limit " << limit << '\n'; }
int main() { test_count_char('a'); test_count_char('e'); test_count_multiples(3); test_count_multiples(5); test_collect_over(6.5); test_collect_over(8.0); return 0; }
//----------------------------------------------------------------------------