Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yuth committed Sep 24, 2024
1 parent d2c831e commit 683fb7a
Show file tree
Hide file tree
Showing 61 changed files with 602 additions and 292 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
*.out
*.app

bin/
build/
.vscode/
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<img align="right" src="./doc/cover.png" width="300">

# Lab C++
This Package is create to collect some of my cpp code that I learn. [For Mom!]
This Package is create to collect some of my cpp code that I have learnt. [For Mom!]

### Reference [List](./doc/)
There are detail of the package, read more in the references list link above.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions cmake/complex/src/tutorial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "math_lib.h"
#include <cmath>
#include <cstdlib>
#include <iostream>

int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " number" << std::endl;
return 1;
}
double inputValue = atof(argv[1]);
std::cout << "The square root of " << inputValue << " is " << mysqrt(inputValue) << std::endl;
return 0;
}
9 changes: 0 additions & 9 deletions cmake/diffdrive_kinematic/CMakeLists.txt

This file was deleted.

27 changes: 0 additions & 27 deletions cmake/diffdrive_kinematic/kinematic_normal.cpp

This file was deleted.

File renamed without changes.
5 changes: 5 additions & 0 deletions cmake/minimal/math_lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "math_lib.h"

double mysqrt(double value) {
return sqrt(value);
}
File renamed without changes.
File renamed without changes.
6 changes: 0 additions & 6 deletions cmake/minimal_1/CMakeLists.txt

This file was deleted.

10 changes: 0 additions & 10 deletions cmake/minimal_1/tutorial.cpp

This file was deleted.

6 changes: 0 additions & 6 deletions cmake/minimal_2/math_lib.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions cmake/minimal_3/src/tutorial.cpp

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions cppsyntax/function_array_pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <stdio.h>
#include <string>

int main() {

int arry[5] = {10, 20, 30, 40, 50};
int *arry2[5];

for (int i = 0; i < 5; ++i) {
arry2[i] = &arry[i];
}

for (int i = 0; i < 5; ++i) {
std::cout << *arry2[i] << std::endl;
}

return 0;
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main() {
int arry[5] = {10, 20, 30, 40, 50};
func1(arry);

std::cout << *(arry+1) << std::endl;
std::cout << *(arry + 1) << std::endl;

return 0;
}
34 changes: 34 additions & 0 deletions cppsyntax/function_pass_by_reference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <stdio.h>

// return output by function paramter - done by using reference

void function_reference(int &x, int &y) {
int z = x;
x = y;
y = z;
}

int first = 31;
int secnd = 55;

void cal_int(int a, int b, int &c) {
c = a + b;
};

int main() {

std::cout << first << std::endl;
std::cout << secnd << std::endl;
function_reference(first, secnd);
std::cout << first << std::endl;
std::cout << secnd << std::endl;

int a = 10;
int b = 20;
int c{};
cal_int(a, b, c);
std::cout << c << std::endl;

return 0;
}
28 changes: 28 additions & 0 deletions cppsyntax/function_pointer_argument.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
using namespace std;

double getAverage(int *arr, int size) {
int sum = 0;
double avg;

for (int i = 0; i < size; ++i) {
sum += arr[i];
}
avg = double(sum) / size;

return avg;
}

int main() {
// an int array with 5 elements.
int balance[5] = {1000, 2, 3, 17, 50};
double avg;

// pass pointer to the array as an argument.
avg = getAverage(balance, 5);

// output the returned value
cout << "Average value is: " << avg << endl;

return 0;
}
File renamed without changes.
16 changes: 16 additions & 0 deletions cppsyntax/h_compare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "h_compare.h"

double find_max(double a, double b) {
if (a > b) {
return a;
} else {
return b;
};
}
double find_min(double a, double b) {
if (a < b) {
return a;
} else {
return b;
};
}
6 changes: 6 additions & 0 deletions cppsyntax/h_compare.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

double find_max(double a, double b);
double find_min(double a, double b);

// define #pragma once to prevent duplicate include.
7 changes: 7 additions & 0 deletions cppsyntax/h_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "h_compare.h"
#include <iostream>

int main() {
std::cout << " C is " << find_max(575, 2.255) << std::endl;
return 0;
}
33 changes: 33 additions & 0 deletions cppsyntax/pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <stdio.h>

// &variable = to get the address of the variable
// *address = to get the value inside of address

std::string name = "Johnyuth"; // the original string variable "name" that has the value of "Johnyuth"
std::string *neam = &name; // a pointer of type string "neam" that used to store the memory address of the reference variable "name"

int main() {
// create original variable
std::cout << "Original = " << name << std::endl;

// print out the memory address of name
std::cout << "Adrs of Original = " << &name << std::endl;

// create a pointer to the original variable and call the pointer 'neam' and print the 'neam' out
std::cout << "Pointer address of Original = " << neam << std::endl;

// dereference : to get the value back that the memory of that address has. this operation can be done
// with asterisk symbol (*). example : *pointer_variable
std::cout << "Deference back for the value of Pointer = " << *neam << std::endl;

// change the value of the pointer
*neam = "GOD YUTH";

// print out the new pointer value
std::cout << "Changed value of the pointer = " << *neam << std::endl;

std::cout << "Changed value of the Original = " << name << std::endl;

return 0;
}
19 changes: 19 additions & 0 deletions cppsyntax/pointer_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
using namespace std;

int main() {
// an int array with 5 elements.
int balance[5] = {1000, 2, 3, 17, 50};

int *ptr = &balance[0] + 1; // the address can use addition and substract operation
cout << *ptr << endl;

// In array
// the array address is the first element of the array address
// all the code below print the address of the array all the same
cout << balance << endl;
cout << &balance << endl;
cout << &balance[0] << endl;

return 0;
}
9 changes: 4 additions & 5 deletions simple/print.cpp → cppsyntax/print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include <stdio.h>

int main() {
std::cout << "Hello \rMr. Nobby-"; // print hello to screen
std::cout << "\n"; // print newline
std::cout << 5; // can directly print number
std::cout << "Hello \rMr. Nobby-"; // print hello to screen
std::cout << "\n"; // print newline
std::cout << 5; // can directly print number
std::cout << "\n";
std::cout << "5"; // can directly print number
std::cout << "5"; // can directly print number
std::cout << "\n";
std::cout << "\r";

Expand All @@ -30,6 +30,5 @@ int main() {

printf("Pointer Address: %p\n", pPi);


return 0;
}
File renamed without changes.
14 changes: 14 additions & 0 deletions cppsyntax/template.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <cstdio>
#include <iostream>

template <typename T>
void find_val(T a, T b, T &res) {
res = a + b * a + b;
}

int main(int argc, char const *argv[]) {
int res;
find_val(14, 12, res);
printf("the result is : %i", res);
return 0;
}
28 changes: 28 additions & 0 deletions cppsyntax/this.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class T {
int x;

void foo() {
x = 6; // same as this->x = 6;
this->x = 5; // explicit use of this->
}

void foo() const {
// x = 7; // Error: *this is constant
}

void foo(int x) // parameter x shadows the member with the same name
{
this->x = x; // unqualified x refers to the parameter
// 'this->' required for disambiguation
}

int y;
T(int x) : x(x), // uses parameter x to initialize member x
y(this->x) // uses member x to initialize member y
{}

T &operator=(const T &b) {
x = b.x;
return *this; // many overloaded operators return *this
}
};
3 changes: 0 additions & 3 deletions function/function_return_tuple.cpp → cppsyntax/tuple.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include <iostream>
#include <tuple>

char hero;
std::string res1;
std::string res2;
std::string name;

// return multiple value from function in tuple form
// std::tuple<datatype, datatype, datatype> name of function(datatype input)
// code
// return std::make_tuple(value1, value2 , value3), value have to have the exact same datatype as define above
std::tuple<std::string, std::string, std::string> Return_responce(char hero) {
if (hero == 'T' || hero == 't') {
Expand Down
Binary file removed doc/cover.png
Binary file not shown.
Loading

0 comments on commit 683fb7a

Please sign in to comment.