Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <math.h>
- int flagOfWrongInput = 0;
- int counterOfDots = 0, positionOfDot = -1;
- int lengthOfNumber = 0;
- void getNumber(char str[], int b1);
- void calculateInOtherSystemWhole(long int part, int power);
- void calculateInOtherSystemFraction(double fraction, int power);
- void getNumber(char str[], int b1) {
- for (int i = 0; i < 13; i++) {
- str[i] = 0;
- }
- char currentChar = 0;
- int currentPosition = 0;
- scanf("%c", ¤tChar);
- while (scanf("%c", ¤tChar) && currentChar != '\n') {
- if (currentChar == '.') {
- if (counterOfDots == 1) {
- flagOfWrongInput++;
- return;
- } else {
- counterOfDots++;
- positionOfDot = currentPosition;
- }
- } else if (!isdigit(currentChar) && !((toupper(currentChar) >= 'A') && (toupper(currentChar) <= 'F'))) {
- flagOfWrongInput++;
- return;
- } else {
- if (b1 <= 10) {
- if (currentChar - '0' < b1) {
- str[currentPosition] = currentChar;
- } else {
- flagOfWrongInput++;
- return;
- }
- } else {
- if (toupper(currentChar) <= 'A' + b1 - 10) {
- str[currentPosition] = (char) toupper(currentChar);
- } else {
- flagOfWrongInput++;
- return;
- }
- }
- }
- currentPosition++;
- }
- lengthOfNumber = currentPosition;
- if (positionOfDot == 0 || positionOfDot == currentPosition - 1) {
- flagOfWrongInput++;
- }
- }
- void calculateInOtherSystemWhole(long int part, int power) {
- long int currentValue = power;
- int degree = 1;
- while (currentValue < part) {
- currentValue *= power;
- degree++;
- }
- if (currentValue != part) {
- currentValue /= power;
- degree--;
- while (degree >= 0) {
- if (power <= 10) {
- printf("%li", part / currentValue);
- } else {
- if (part / currentValue < 10) {
- printf("%li", part / currentValue);
- } else {
- printf("%c", (char)('A' + (part / currentValue) - 10));
- }
- }
- part = part - ((part / currentValue) * currentValue);
- currentValue /= power;
- degree--;
- }
- } else {
- printf("%d", 1);
- for (int i = 0; i < degree; i++) {
- printf("%d", 0);
- }
- }
- }
- void calculateInOtherSystemFraction(double fraction, int power) {
- double currentFraction = 1;
- int degree = 1;
- while (degree <= 12 && fraction != 0) {
- currentFraction = currentFraction / (double)power;
- if (fraction / currentFraction < 10) {
- printf("%d", (int)(fraction / currentFraction));
- } else {
- printf("%c", (char)('A' + (int)(fraction / currentFraction) - 10));
- }
- fraction = fraction - (double)((int)(fraction / currentFraction)) * currentFraction;
- degree++;
- }
- }
- int main() {
- int b1, b2;
- long int whole = 0;
- double fraction = 0;
- scanf("%d%d", &b1, &b2);
- if (b1 >= 2 && b1 <= 16 && b2 >= 2 && b2 <= 16) {
- char str[13];
- getNumber(str, b1);
- if (flagOfWrongInput == 0) {
- if (positionOfDot == -1) {
- for (int i = 0; i < lengthOfNumber; i++) {
- if (isdigit(str[i])) {
- whole = whole * b1 + str[i] - '0';
- } else {
- whole = whole * b1 + str[i] - 'A' + 10;
- }
- }
- if (b2 != 10) {
- calculateInOtherSystemWhole(whole, b2);
- } else {
- printf("%d", whole);
- }
- } else {
- for (int i = 0; i < positionOfDot; i++) {
- if (isdigit(str[i])) {
- whole = whole * b1 + str[i] - '0';
- } else {
- whole = whole * b1 + str[i] - 'A' + 10;
- }
- }
- int currentValue = b1;
- for (int i = positionOfDot + 1; i < 13 && str[i] != '\0'; i++) {
- if (isdigit(str[i])) {
- fraction += (double)(str[i] - '0') / currentValue;
- } else {
- fraction += (double)(str[i] - 'A' + 10) / currentValue;
- }
- currentValue *= b1;
- }
- if (b2 != 10) {
- calculateInOtherSystemWhole(whole, b2);
- if (fraction != 0) {
- printf("%c", '.');
- calculateInOtherSystemFraction(fraction, b2);
- }
- } else {
- printf("%f", whole + fraction);
- }
- }
- }
- } else {
- flagOfWrongInput++;
- }
- if (flagOfWrongInput > 0) {
- printf("Bad input\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement