Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
viakul
/
iax0583
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
6c1963d1
authored
Nov 05, 2018
by
viakul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
d6a23ad0
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
141 additions
and
0 deletions
Lab9/12345.c
Lab9/12345.c
0 → 100644
View file @
6c1963d1
/*
* 12345.c
*
* Copyright 2018 Viktor <Viktor@DESKTOP-EJAITOO>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
isInt
(
char
*
x
,
int
y
);
int
moreInt
(
char
*
x
,
int
y
,
int
i
);
int
DeciConv
(
char
x
,
int
y
)
;
int
main
()
{
int
val
,
base
;
char
line
[
100
];
printf
(
"write the base, positive integer which is less or equal to 36
\n
"
);
scanf
(
"%d"
,
&
base
);
do
{
printf
(
"write the val for the input
\n
"
);
scanf
(
"%s"
,
line
);
val
=
isInt
(
line
,
base
);
if
(
val
){
printf
(
"please repeat
\n
"
);
}
}
while
(
val
);
printf
(
"Decimal equivalent of %s in base %d is %d
\n
"
,
x
,
y
,
DeciConv
(
x
,
y
));
printf
(
"%s"
,
line
);
return
0
;
}
int
isInt
(
char
*
x
,
int
y
){
int
i
,
bool
,
length
;
i
=
0
;
bool
=
0
;
length
=
strlen
(
x
);
while
(
i
<
length
){
bool
=
0
;
if
(
y
<
11
){
if
((
x
[
i
]
<
47
)
||
(
x
[
i
]
>
47
+
y
)){
printf
(
"%c"
,
x
[
i
]);
printf
(
" %d
\n
"
,
bool
);
bool
=
1
;
break
;
}
}
else
{
bool
=
moreInt
(
x
,
y
,
i
);
if
(
bool
==
1
){
printf
(
"%c"
,
x
[
i
]);
printf
(
" %d
\n
"
,
bool
);
break
;
}
}
printf
(
"%c"
,
x
[
i
]);
printf
(
" %d
\n
"
,
bool
);
i
++
;
}
return
(
bool
);
}
int
moreInt
(
char
*
x
,
int
y
,
int
i
){
int
bool
;
while
(
1
){
// printf("the value of the %c is %d\n",x[i],x[i]);
if
((
x
[
i
]
>
47
)
&&
(
x
[
i
]
<
58
)){
bool
=
0
;
//printf("1\n");
break
;
}
if
((
x
[
i
]
>
64
)
&&
(
x
[
i
]
<
55
+
y
)){
bool
=
0
;
//printf("2\n");
break
;
}
else
{
// printf("3\n");
bool
=
1
;
break
;
}
}
return
(
bool
);
}
int
DeciConv
(
char
x
,
int
y
)
{
int
len
=
strlen
(
x
);
int
power
=
1
;
int
num
=
0
;
int
i
;
for
(
i
=
len
-
1
;
i
>=
0
;
i
--
)
{
if
(
val
(
x
[
i
])
>=
y
)
{
printf
(
"Invalid Number"
);
return
-
1
;
}
num
+=
val
(
x
[
i
])
*
power
;
power
=
power
*
base
;
}
return
num
;
}
int
val
(
char
c
)
{
if
(
c
>=
'0'
&&
c
<=
'9'
){
return
(
int
)
c
-
'0'
;
}
else
{
return
(
int
)
c
-
'A'
+
10
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment