Skip to content

Commit 8f2dd0b

Browse files
committed
add: 20_miscellaneous 01
1 parent 7b130cc commit 8f2dd0b

File tree

3 files changed

+124
-124
lines changed

3 files changed

+124
-124
lines changed
41.7 KB
Binary file not shown.

20_miscellaneous_programs/get_only_certain_data.c.c

-124
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include <stdio.h>
2+
#include <conio.h>
3+
#include <math.h>
4+
#include <string.h>
5+
6+
double getOnlyRealNumber();
7+
double stringToDouble(char *);
8+
9+
long long int getOnlyInteger();
10+
long long int stringToInt(char *);
11+
12+
int main()
13+
{
14+
// Get Only Real Number
15+
printf("\nEnter A Real Number (Number With Decimal Point) => ");
16+
printf("\nEntered Real Number %lf", getOnlyRealNumber());
17+
18+
// Get Only Integer
19+
printf("\n\nEnter An Integer => ");
20+
printf("\nEntered Integer => %lld", getOnlyInteger());
21+
22+
getch();
23+
return 0;
24+
}
25+
26+
double getOnlyRealNumber()
27+
{
28+
char str[30];
29+
int i = 0;
30+
while (1)
31+
{
32+
char ch = getch();
33+
if (ch == ' ' || ch == 13)
34+
break;
35+
else if ((ch >= '0' && ch <= '9' || ch == '.') || ch == '-' && i == 0)
36+
{
37+
putch(ch);
38+
str[i] = ch;
39+
i++;
40+
}
41+
else if (ch == 8 && i > 0)
42+
{
43+
i--;
44+
putch('\b');
45+
putch(' ');
46+
putch('\b');
47+
}
48+
}
49+
str[i] = 0;
50+
return stringToDouble(str);
51+
}
52+
53+
double stringToDouble(char *str)
54+
{
55+
long long int integeralPart = 0, decimalPart = 0;
56+
int i = 0;
57+
if (str[0] == '-')
58+
i = 1;
59+
for (; str[i] != '.' && str[i]; i++)
60+
integeralPart = integeralPart * 10 + (str[i] - 48);
61+
62+
if (strlen(str) != i)
63+
{
64+
for (i++; str[i]; i++)
65+
decimalPart = decimalPart * 10 + (str[i] - 48);
66+
}
67+
68+
int countDigits = 0, copyDecimalPart = decimalPart;
69+
while (copyDecimalPart > 0)
70+
{
71+
countDigits++;
72+
copyDecimalPart /= 10;
73+
}
74+
double digitsAfterPoint = 0;
75+
if (countDigits)
76+
digitsAfterPoint = decimalPart / pow(10, countDigits);
77+
78+
double num = integeralPart;
79+
num += digitsAfterPoint;
80+
if (str[0] == '-')
81+
num = -num;
82+
return num;
83+
}
84+
85+
long long int getOnlyInteger()
86+
{
87+
char str[30];
88+
int i = 0;
89+
while (1)
90+
{
91+
char ch = getch();
92+
if (ch == ' ' || ch == 13)
93+
break;
94+
else if (ch >= '0' && ch <= '9' || ch == '-' && i == 0)
95+
{
96+
putch(ch);
97+
str[i] = ch;
98+
i++;
99+
}
100+
else if (ch == 8 && i > 0)
101+
{
102+
i--;
103+
putch('\b');
104+
putch(' ');
105+
putch('\b');
106+
}
107+
}
108+
str[i] = 0;
109+
return (stringToInt(str));
110+
}
111+
112+
long long int stringToInt(char *str)
113+
{
114+
long long int integeralPart = 0;
115+
int i = 0;
116+
if (str[0] == '-')
117+
i = 1;
118+
for (; str[i]; i++)
119+
integeralPart = integeralPart * 10 + (str[i] - 48);
120+
121+
if (str[0] == '-')
122+
integeralPart = -integeralPart;
123+
return integeralPart;
124+
}

0 commit comments

Comments
 (0)