Commit 6c1963d1 by viakul

Upload New File

parent d6a23ad0
Showing with 141 additions and 0 deletions
/*
* 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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment