Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 8266078

Browse files
authored
Add files via upload
0 parents  commit 8266078

File tree

4 files changed

+459
-0
lines changed

4 files changed

+459
-0
lines changed

Lab1.sln

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30907.101
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Lab1", "Lab1\Lab1.vcxproj", "{B58213A1-D52A-49D3-BBA2-10A24D78B0A7}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{B58213A1-D52A-49D3-BBA2-10A24D78B0A7}.Debug|x64.ActiveCfg = Debug|x64
17+
{B58213A1-D52A-49D3-BBA2-10A24D78B0A7}.Debug|x64.Build.0 = Debug|x64
18+
{B58213A1-D52A-49D3-BBA2-10A24D78B0A7}.Debug|x86.ActiveCfg = Debug|Win32
19+
{B58213A1-D52A-49D3-BBA2-10A24D78B0A7}.Debug|x86.Build.0 = Debug|Win32
20+
{B58213A1-D52A-49D3-BBA2-10A24D78B0A7}.Release|x64.ActiveCfg = Release|x64
21+
{B58213A1-D52A-49D3-BBA2-10A24D78B0A7}.Release|x64.Build.0 = Release|x64
22+
{B58213A1-D52A-49D3-BBA2-10A24D78B0A7}.Release|x86.ActiveCfg = Release|Win32
23+
{B58213A1-D52A-49D3-BBA2-10A24D78B0A7}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {113B3F89-ED05-46D6-A218-F034B8FF1F7D}
30+
EndGlobalSection
31+
EndGlobal

Lab1/Lab1.cpp

+259
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <math.h>
4+
#include <string.h>
5+
6+
#define CRT_SECURE_NO_WARNINS
7+
8+
int get_number(const char* msg);
9+
10+
int is_prime(int n);
11+
void print_first_primes(int n);
12+
void print_primes(int n);
13+
void approx_sqrt(int n, int p);
14+
void print_first_digits_of_fraction(int n, int k, int m);
15+
void print_pascal(int n);
16+
17+
void print_consecutives_sum(int n);
18+
19+
int main()
20+
{
21+
while (1)
22+
{
23+
printf("\n\n");
24+
printf(" 0 - exit\n");
25+
printf(" 1 - show primes\n");
26+
printf(" 2 - show first n primes\n");
27+
printf(" 3 - consecutive numbers sum\n");
28+
printf(" 4 - first N digits of K/M (K<M)\n");
29+
printf(" 6 - Pascal triangle\n");
30+
printf(" 7 - approx sqrt(n)\n");
31+
int action = get_number(" >>> ");
32+
if (!action) break;
33+
34+
if (action == 1)
35+
{
36+
int n = get_number("N = ");
37+
print_primes(n);
38+
}
39+
else if (action == 2)
40+
{
41+
int n = get_number("N = ");
42+
print_first_primes(n);
43+
}
44+
else if (action == 3)
45+
{
46+
int n = get_number("N = ");
47+
print_consecutives_sum(n);
48+
}
49+
else if (action == 4)
50+
{
51+
int n = get_number("N = ");
52+
int k = get_number("K = ");
53+
int m = get_number("M = ");
54+
print_first_digits_of_fraction(n, k, m);
55+
}
56+
else if (action == 6)
57+
{
58+
int n = get_number("N = ");
59+
print_pascal(n);
60+
}
61+
else if (action == 7)
62+
{
63+
int n = get_number("N = ");
64+
int p = get_number("precision = ");
65+
approx_sqrt(n, p);
66+
}
67+
}
68+
return 0;
69+
}
70+
71+
/// <summary>
72+
/// Reads int from input
73+
/// </summary>
74+
/// <param name="msg">message to display</param>
75+
/// <returns>user input integer</returns>
76+
int get_number(const char* msg)
77+
{
78+
if (msg != NULL)
79+
{
80+
printf(msg);
81+
}
82+
int result;
83+
scanf_s("%i", &result);
84+
return result;
85+
}
86+
87+
/// <summary>
88+
/// Checks if n is prime
89+
/// </summary>
90+
/// <param name="n">number to check</param>
91+
/// <returns>1 if n is prime, 0 otherwise</returns>
92+
int is_prime(int n)
93+
{
94+
if (n < 2) return 0;
95+
for (int d = 2; d * d <= n; d++)
96+
if (n % d == 0)
97+
return 0;
98+
return 1;
99+
}
100+
101+
/// <summary>
102+
/// prints primes up to n (n>0)
103+
/// </summary>
104+
/// <param name="n">upper limit</param>
105+
void print_primes(int n)
106+
{
107+
if (n <= 0)
108+
{
109+
printf("Number must be positive\n");
110+
return;
111+
}
112+
printf("Primes : ");
113+
for (int i = 2; i < n; i++)
114+
if (is_prime(i))
115+
{
116+
printf("%i ", i);
117+
}
118+
printf("\n");
119+
}
120+
121+
/// <summary>
122+
/// prints first n primes
123+
/// </summary>
124+
/// <param name="n">number of primes to print</param>
125+
void print_first_primes(int n)
126+
{
127+
if (n <= 0)
128+
{
129+
printf("N must be a non-zero positive.\n");
130+
return;
131+
}
132+
int k = 1;
133+
printf("Primes : ");
134+
for (; n > 0; k++)
135+
{
136+
if (is_prime(k))
137+
{
138+
printf("%i ", k);
139+
n--;
140+
}
141+
}
142+
}
143+
144+
/// <summary>
145+
/// Prints approximated sqrt(n) with p decimals
146+
/// </summary>
147+
/// <param name="n">sqrt argument (positive)</param>
148+
/// <param name="p">number of decimals, p=0..7 </param>
149+
void approx_sqrt(int n, int p)
150+
{
151+
if (p < 0 || p>7)
152+
{
153+
printf("Precision must be in range 0..7\n");
154+
return;
155+
}
156+
if (n < 0)
157+
{
158+
printf("Sqrt argument cannot be negative");
159+
return;
160+
}
161+
printf("%.*f\n", p, sqrt(n));
162+
}
163+
164+
165+
/// <summary>
166+
/// prints first n digits of k/m (k<m)
167+
/// </summary>
168+
/// <param name="n">number of digits</param>
169+
/// <param name="k">nominator</param>
170+
/// <param name="m">denominator ??</param>
171+
void print_first_digits_of_fraction(int n, int k, int m)
172+
{
173+
if (k >= m)
174+
{
175+
printf("Fraction value must be <1\n");
176+
return;
177+
}
178+
if (n <= 0)
179+
{
180+
printf("Number of digits must be a non-zero positive.\n");
181+
return;
182+
}
183+
printf("0.");
184+
for (int i = 0; i < n; i++)
185+
{
186+
k *= 10;
187+
while (k < m)
188+
{
189+
k *= 10;
190+
printf("0");
191+
}
192+
printf("%i", k / m);
193+
k = k % m;
194+
}
195+
}
196+
197+
/// <summary>
198+
/// prints Pascal triangle of length n
199+
/// </summary>
200+
/// <param name="n">number of lines to print</param>
201+
void print_pascal(int n)
202+
{
203+
int** mat = (int**)malloc((n + 1) * sizeof(int*));
204+
205+
for (int i = 0; i <= n; i++)
206+
mat[i] = (int*)malloc((n + 1) * sizeof(int*));
207+
208+
mat[0][0] = 1;
209+
for (int i = 1; i <= n; i++)
210+
{
211+
mat[i][0] = 1;
212+
mat[i][i] = 1;
213+
for (int j = 1; j < i; j++)
214+
mat[i][j] = mat[i - 1][j - 1] + mat[i - 1][j];
215+
}
216+
217+
for (int i = 0; i <= n; i++)
218+
{
219+
for (int j = 0; j <= i; j++)
220+
printf("%i ", mat[i][j]);
221+
printf("\n");
222+
free(mat[i]);
223+
}
224+
225+
free(mat);
226+
}
227+
228+
/// <summary>
229+
/// prints consecutive numbers that sum to n starting form k, if exists
230+
/// </summary>
231+
/// <param name="n">total sum</param>
232+
/// <param name="k">first number</param>
233+
void print_consecutives_starting_with(int n, int k)
234+
{
235+
int s = 0, cnt = 0;
236+
while (s < n)
237+
{
238+
s += k;
239+
k++;
240+
cnt++;
241+
}
242+
k -= cnt;
243+
if (s == n)
244+
{
245+
for (int i = 0; i < cnt; i++)
246+
printf("%d ", k + i);
247+
printf("\n");
248+
}
249+
}
250+
251+
/// <summary>
252+
/// prints all consecutive numbers that add up to n
253+
/// </summary>
254+
/// <param name="n">target number</param>
255+
void print_consecutives_sum(int n)
256+
{
257+
for (int i = 1; i <= n / 2; i++)
258+
print_consecutives_starting_with(n, i);
259+
}

0 commit comments

Comments
 (0)