first comm in cpp

This commit is contained in:
lda committed 2024-12-05 21:33:39 +07:00
1 parent 5968efc884
commit 9e742f0d79
22 files changed
+154 -1379

No files matched your search

-47
View File
@@ -1,47 +0,0 @@
#include <stdio.h>
#include <string.h>
typedef struct
{
int real;
int imag;
} ldaComplex;
void print(ldaComplex *lda)
{
printf("%d + %di", lda->real, lda->imag);
}
// this is the best of my ability in c: returning string is a Fucking Mess
// i would love to do this in rust
ldaComplex add(ldaComplex a, ldaComplex b)
{
return (ldaComplex){a.real + b.real, a.imag + b.imag};
}
ldaComplex multiply(ldaComplex a, ldaComplex b)
{
return (ldaComplex){a.real * b.real - a.imag * b.imag,
a.real * b.imag + a.imag * b.real};
}
int main(int argc, char const *argv[])
{
ldaComplex lda = {1, 2};
ldaComplex lda2 = {5, 2};
ldaComplex addRes = add(lda, lda2);
ldaComplex multiRes = multiply(lda, lda2);
printf("(");
print(&lda);
printf(") + (");
print(&lda2);
printf(") = ");
print(&addRes);
printf("\n(");
print(&lda);
printf(") * (");
print(&lda2);
printf(") = ");
print(&multiRes);
return 0;
}
// ass
+57
View File
@@ -0,0 +1,57 @@
#include <iostream>
#include <string>
typedef struct
{
int real;
int imag;
} ldaComplex;
void print(const ldaComplex *lda)
{
std::printf("%d + %di", lda->real, lda->imag);
}
std::string to_string(const ldaComplex *lda)
{
return std::to_string(lda->real) + " + " + std::to_string(lda->imag) + "i";
}
/**
* @brief Adds two complex numbers.
*
* @param[in] a The first complex number.
* @param[in] b The second complex number.
*
* @return The sum of a and b.
*/
ldaComplex add(const ldaComplex a, const ldaComplex b)
{
return ldaComplex{a.real + b.real, a.imag + b.imag};
}
ldaComplex multiply(const ldaComplex a, const ldaComplex b)
{
return ldaComplex{a.real * b.real - a.imag * b.imag,
a.real * b.imag + a.imag * b.real};
}
ldaComplex operator+(const ldaComplex a, const ldaComplex b)
{
return add(a, b);
}
ldaComplex operator*(const ldaComplex a, const ldaComplex b)
{
return multiply(a, b);
}
int main(int argc, char const *argv[])
{
ldaComplex lda = {1, 2};
ldaComplex lda2 = {5, 2};
ldaComplex addRes = add(lda, lda2);
ldaComplex addResadd = lda + lda2;
ldaComplex multiRes = multiply(lda, lda2);
ldaComplex multiResmul = lda * lda2;
std::cout << to_string(&lda) + " + " + to_string(&lda2) + " = " + to_string(&addRes) << " (" << to_string(&addResadd) << ")" << std::endl;
std::cout << to_string(&lda) + " * " + to_string(&lda2) + " = " + to_string(&multiRes) << " (" << to_string(&multiResmul) << ")" << std::endl;
return 0;
}
// ass
+2 -2
View File
@@ -1,15 +1,15 @@
#include <iostream>
void findMax(int *max, int a)
{
if (a > *max)
*max = a;
};
#include <stdio.h>
int main()
{
int c = 176;
int f = 10;
findMax(&f, c);
printf("%d, %d", c, f);
std::printf("%d, %d", c, f);
return 0;
}
+3 -2
View File
@@ -1,4 +1,4 @@
#include <stdio.h>
#include <iostream>
int log10intint(int n)
{
@@ -12,7 +12,8 @@ int log10intint(int n)
if (n - a < 0)
return i;
}
};
return ~(1 << 31);
}
int ten_to_the(int i)
{
if (i < 0)
+2 -4
View File
@@ -1,7 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <iostream>
#include "sphenic.hpp"
bool sphenic(int i)
{
if (i < 30)
+1
View File
@@ -0,0 +1 @@
bool sphenic(int i);