سؤال

I have to write a program in C that reads and parses different command-line arguments but I have no idea where to start. All I have write now is the usage:

usage:
binary OPTION SIZE NUMBER
OPTION:
-b NUMBER is binary and output will be in decimal.
-d NUMBER is decimal and output will be in binary.
SIZE:
-8 input is an unsigned 8-bit integer.
-16 input is an unsigned 16-bit integer.
-32 input is an unsigned 32-bit integer.
-64 input is an unsigned 64-bit integer.
NUMBER:
number to be converted.

Other than this, I am not sure how to get user input and go about with the conversions. Any help would be great!

هل كانت مفيدة؟

المحلول

You can take the following code-snippet and work it on from there, but please note:

  1. I do not see any need to make use of the SIZE argument. If this argument is essential for your exercise (or any other reason behind this question), then you'll need to think how you want to use it.

  2. I do not perform any assertion on the input number, so the code below assumes a legal decimal input number when OPTION = -d and a legal binary input number when OPTION = -d.

  3. There is probably more than one way to implement it, and the code below is merely an example.


#include <stdio.h>
#include <string.h>

unsigned long long str_to_ull(char* str,int base)
{
    int i;
    unsigned long long ull = 0;
    for (i=0; str[i] != 0; i++)
    {
        ull *= base;
        ull += str[i]-'0';
    }
    return ull;
}

void print_ull(unsigned long long ull,int base)
{
    if (ull/base > 0)
        print_ull(ull/base,base);
    printf("%d",ull%base);
}

int main(int argc,char* argv[])
{
    char* OPTION;
    char* SIZE  ;
    char* NUMBER;

    unsigned long long number = 0;

    if (argc < 4)
    {
        printf("Missing input arguments\n");
        return -1;
    }

    OPTION = argv[1];
    SIZE   = argv[2];
    NUMBER = argv[3];

    if (strcmp(OPTION,"-b") == 0)
    {
        number = str_to_ull(NUMBER,2);
        print_ull(number,10);
        return 0;
    }

    if (strcmp(OPTION,"-d") == 0)
    {
        number = str_to_ull(NUMBER,10);
        print_ull(number,2);
        return 0;
    }

    printf("Invalid input arguments\n");
    return -1;
}

نصائح أخرى

Here is simple example code to enter number from command-line and convert that decimal-integer to binary.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    //Variable declarations.
    int i;  //Holds input decimal number.
    int t;  //Holds temp mod i%2
    int c = 0;  //Holds the bit count.
    char b[2] = "";  //Hold the temp binary bit string to be append.
    char bin[64] = "";  //Holds the final binary string

    //Check if the syntax is proper.
    if (argc != 2)
    {
        printf("\nSYNTAX: binary <decimal>\n");
        return 1;
    }

    //Convert the input string parameret into decimal.
    i = atoi(argv[1]);

    //reset the flags.
    t = 0;

    while (i != 0)
    { 
        t = i % 2;
        i = i / 2;
        //convert int decimal (base 10) to string.
        itoa(t, b, 10);
        //Concat to main binary string.
        strcat(bin, b);
        c++;
    } 
    //reverse the string.
    strrev(bin);
    printf("\nBinary: %d(10) = %s(2)\n",i, bin);
    return 0;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top