Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
4
Добавлен:
26.05.2014
Размер:
1.45 Кб
Скачать
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>

#include "convert.h"

#define MAXLINE 100

const char *program_name;

void print_usage(FILE* stream, int exit_code){
	fprintf(stream, "Usage: %s options \n", program_name);
	fprintf(stream," -h --help		Display this usage information.\n");
	fprintf(stream," -n --number		Source number.\n");
	fprintf(stream," -s --srcbase		Source number base.\n");
	fprintf(stream," -d --destbase		Destination number base.\n");
	exit(exit_code);
}

int main(int argc, char *argv[]){
	int next_option;
	
	int src;
	int dest;
	char *str = (char *)malloc(sizeof(char) * MAXLINE);
	
	const char* const short_options = "hn:s:d:";
	
	const struct option long_options[] = {
		{"help",	0,NULL,'h'},
		{"number",	1,NULL,'n'},
		{"srcbase",	1,NULL,'s'},
		{"destbase",	1,NULL,'d'},
		{NULL,		0,NULL, 0}
	};
	
	program_name = argv[0];
	
	if(argc != 7) print_usage(stdout,1);
		
		do{
		next_option = getopt_long(argc, argv, short_options, long_options, NULL);
		
		switch(next_option){
			case 'h':
				print_usage(stdout, 0);
				break;
			case 'n':
				strcpy(str,optarg);
				break;
			case 's':
				src = atoi(optarg);
				break;
			case 'd':
				dest = atoi(optarg);
				break;
			case '?':
				print_usage(stderr,1);
				break;
			case -1:
				break;
			default:
				abort();
				break;
		}
	}
	while(next_option != -1);
	
	convert(str,src,dest);
	fprintf(stdout,"%s\n",str);
		
	free(str);
		
	return 0;
}

Соседние файлы в папке lab1