Nguyen Xuan Huy
Structures
(C programming)
2024
Contents
Structures................................................................................................................ 2
Quadratic Equation.................................................................................................. 2
Farey........................................................................................................................ 3
Structures
Structures (also called structs) are a way to group several related variables into one place.
Each variable in the structure is known as a member of the structure.
Unlike an array, a structure can contain many different data types (int, float, char, etc.).
Quadratic Equation
/*
Name: Quadratic Equation
Copyright: (C) 2024
Author: C Fan
Date: 22-08-24 14:59
Description:
using struct
*/
#include <stdio.h>
#include <string.h>
typedef struct {
int N; // so nghiem
float X1, X2; // nghiem
} E2Out;
E2Out E2(float a, float b, float c) {
float delta = b*b - 4*a*c;
E2Out e;
e.N = 0; e.X1 = e.X2 = 0;
if (delta < 0) return e;
float sd = sqrt(delta);
if (delta == 0) {
e.N = 1; e.X1 = e.X2 = -b/(2*a);
return e;
}
// delta > 0
e.N = 2; e.X1 = (-b+sd)/(2*a);
e.X2 = (-b-sd)/(2*a);
return e;
}
void Call(float a, float b, float c) {
printf("\n --------------------");
printf("\n %fx^2 + %fx + %f = 0 ", a, b, c);
int n;
float x1, x2;
E2Out e = E2(a, b, c);
switch(e.N) {
case 0: printf("\n No solutions ");
break;
case 1: printf("\n Double solutions %f %f ", e.X1, e.X2);
break;
case 2: printf("\n Two solutions %f %f ", e.X1, e.X2);
break;
} // switch
}
/*
x^2 + 2x +3 = 0
(x-3)^2 = x^2 -6x + 9
(x-3)(x+2) = x^2 -x -6
*/
main() {
Call(1, 2, 4);
Call(1, -6, 9);
Call(1, -1, 6);
printf("\n T h e E n d");
return 0;
}
Output
--------------------
1.000000x^2 + 2.000000x + 4.000000 = 0
No solutions
--------------------
1.000000x^2 + -6.000000x + 9.000000 = 0
Double solutions 3.000000 3.000000
--------------------
1.000000x^2 + -1.000000x + 6.000000 = 0
No solutions
T h e E n d
--------------------------------
Process exited after 0.02081 seconds with return value 0
Press any key to continue . . .
Farey
Cho số nguyên dương n, dãy Farey F(n) được tạo bởi các phân só tối giản
t
, 0 t m, 1 m n và được sắp tăng
m
Ví dụ, với n = 5 ta có dãy Farey F(5) gồm 11 phân số tối giản sau đây:
0 1 1 1 2 1 3 2 3 4 1
F ( 5 )= , , , , , , , , , ,
1 5 4 3 5 2 5 3 4 5 1
Với mỗi số nguyên dương n, hãy tạo sinh dãy F(n)?
Algorithm
Khai báo struct kiểu Frac
Phase 1.
Tạo dãy a các Frac t/m:
m = 1..n
t = 0..m-1
Rút gọn t/m
Phase 2.
Sắp tăng a
Phase 3.
Lược bỏ các Frac trùng lặp trong a
Ví dụ. n = 5
Phase 1. a = [0 1 1/2 1/3 2/3 1/4 1/2 3/4 1/5 2/5 3/5 4/5]
Phase 2. a = [0 1/5 1/4 1/3 2/5 1/2 1/2 3/5 2/3 3/4 4/5 1
Phase 3. [0 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1]
Program
/*
Name: Farey
Copyright:
Author:
Date: 08-11-24 16:28
Description:
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MN 10000
void Go() {
printf(" ? ");
char c;
scanf("%c", &c);
if (c == 10) return; // ENTER
if (c == '.') exit(0);
}
typedef struct Frac {
int num; // tu so
int den; // mau so
} Frac;
Frac a[MN];
Frac Set(int n, int d) {
Frac x;
if (d <= 0) {
printf("\n Error denominator <= 0 %d ", d);
} else { [Link] = n; [Link] = d; }
return x;
}
void P(char msg[], Frac x) {
printf("%s", msg);
printf("%d", [Link]);
if([Link] == 0 || [Link] == 1) return;
printf("/%d", [Link]);
}
void PP(char msg[], Frac a[], int d, int c) {
printf("%s", msg);
int i;
for(i = d; i <= c; ++i)
P(" ", a[i]);
}
int Gcd(int a, int b) {
return (b == 0) ? a : Gcd(b, a % b);
}
Frac Reduce(Frac x) {
int d = Gcd(abs([Link]), [Link]);
[Link] /= d;
[Link] /= d;
return x;
}
int Cmp(Frac *x, Frac *y) {
return (x->num)*(y->den) - (x->den)*(y->num);
int Diff(Frac x, Frac y) {
return (([Link])*([Link]) - ([Link])*([Link])) != 0;
}
void Farey(int n) {
a[0] = Set(0,1);
a[1] = Set(1,1);
int t, m;
int k = 1;
for(m = 2; m <= n; ++m) {
for(t = 1; t < m; ++t) {
a[++k] = Reduce(Set(t,m));
}
}
PP("\n Init: ", a, 0, k);
printf("\n Total %d", k+1);
qsort(a, k+1, sizeof(Frac), Cmp);
PP("\n Sorted: ", a, 0, k);
int i, j = 0;
for(i = 1; i <= k; ++i)
if(Diff(a[i], a[j])) a[++j] = a[i];
k = j;
PP("\n Final: ", a, 0, k);
printf("\n Total %d", k+1);
}
main() {
Farey(5);
printf("\n T h e E n d");
return 0;
}
Output
Init: 0 1 1/2 1/3 2/3 1/4 1/2 3/4 1/5 2/5 3/5 4/5
Total 12
Sorted: 0 1/5 1/4 1/3 2/5 1/2 1/2 3/5 2/3 3/4 4/5 1
Final: 0 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1
Total 11
T h e E n d
--------------------------------
Process exited after 0.07604 seconds with return value 0
Press any key to continue . . .