関数ポインタとコマンドライン引数の組み合わせのプログラムについて
Posted: 2012年1月31日(火) 09:00
下のプログラムを下の条件でコマンドライン引数を用いて書き換えたいのですが、なかなかうまくいきません。回答お願いします
<条件>
コマンドラインの第 1 引数は、-a, -d, -l, -u のいずれかで、入力必須とする。
コマンドラインの第 2 引数は、チェック対象となる 1 つの文字とする。
コマンドライン引数が適切でない場合、および、エラーがある場合は、使い方を表示する。
<もとのコード>
#include <stdio.h>
#include<ctype.h>
char *isalpha_p(void) {
return "an alphabet";
}
char *isdigit_p(void) {
return "a digit";
}
char *islower_p(void) {
return "lowercase";
}
char *isupper_p(void) {
return "uppercase";
}
int main(void)
{
int (*p[])(int c) = {isalpha, isdigit, islower, isupper};
char *(*q[])(void)={isalpha_p, isdigit_p, islower_p, isupper_p};
int i;
char c;
printf("0: isalpha, 1: isdigit, 2: islower, 3: isuppper ? ");
scanf("%d", &i);
if((i>=0)&&(i<=3)){
printf("char = ");
scanf(" %c", &c);
if(p(c)) {
printf(" %c is %s\n", c, q());
}else{
printf(" %c is NOT %s\n", c, q());
}
}
return 0;
}
<条件>
コマンドラインの第 1 引数は、-a, -d, -l, -u のいずれかで、入力必須とする。
コマンドラインの第 2 引数は、チェック対象となる 1 つの文字とする。
コマンドライン引数が適切でない場合、および、エラーがある場合は、使い方を表示する。
<もとのコード>
#include <stdio.h>
#include<ctype.h>
char *isalpha_p(void) {
return "an alphabet";
}
char *isdigit_p(void) {
return "a digit";
}
char *islower_p(void) {
return "lowercase";
}
char *isupper_p(void) {
return "uppercase";
}
int main(void)
{
int (*p[])(int c) = {isalpha, isdigit, islower, isupper};
char *(*q[])(void)={isalpha_p, isdigit_p, islower_p, isupper_p};
int i;
char c;
printf("0: isalpha, 1: isdigit, 2: islower, 3: isuppper ? ");
scanf("%d", &i);
if((i>=0)&&(i<=3)){
printf("char = ");
scanf(" %c", &c);
if(p(c)) {
printf(" %c is %s\n", c, q());
}else{
printf(" %c is NOT %s\n", c, q());
}
}
return 0;
}