69,368
社区成员
发帖
与我相关
我的任务
分享
void ToUpper (char *);
void ToLower (char *);
void Transpose (char *);
void Dummy (char *);
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int showmenu (void);
void eatline (void);
typedef void (* V_FP_CHARP) (char *);
void show (V_FP_CHARP fp, char * str);
void ToUpper (char *);
void ToLower (char *);
void Transpose (char *);
void Dummy (char *);
int main (void)
{
char line[81];
char copy[81];
// char choise;
int index;
// void (* pfun) (char *);
// V_FP_CHARP pfun;
V_FP_CHARP arpf[4] = {ToUpper, ToLower, Transpose, Dummy};
puts ("Enter a string (empty line to quit): ");
while (gets (line) != NULL && line[0] != '\0')
{
while ((index = showmenu ()) != 4)
{
// switch (index)
// {
// case 'u': pfun = arpf[0]; break;
// case 'l': pfun = arpf[1]; break;
// case 't': pfun = arpf[2]; break;
// case 'o': pfun = arpf[3]; break;
// }
index = showmenu ();
while (index >= 0 && index <= 3)
{
strcpy (copy, line);
show (arpf[index], copy);
index = showmenu ();
}
// strcpy (copy, line);
// show (pfun, copy);
}
puts ("Enter a string (Empty to quit): ");
}
puts ("Bye!");
return 0;
}
int showmenu (void)
{
char ans;
int index = 0;
puts ("Enter menu choise: ");
puts ("u)uppercase l)lowercase");
puts ("t)transposed case o)original case");
puts ("n)next string");
ans = getchar ();
ans = tolower (ans);
eatline ();
while (strchr ("ulton", ans) == NULL)
{
puts ("Please enter a u, l, t, o or n: ");
ans = tolower (getchar ());
eatline ();
}
switch (ans)
{
case 'u': index = 0; break;
case 'l': index = 1; break;
case 't': index = 2; break;
case 'o': index = 3; break;
case 'n': index = 4; break;
}
return index;
}
void eatline (void)
{
while (getchar () != '\n')
continue;
}
void show (void (* fp) (char *), char * str)
{
(*fp) (str);
puts (str);
}
void ToUpper (char * str)
{
while (*str)
{
*str = toupper (*str);
str++;
}
}
void ToLower (char * str)
{
while (*str)
{
*str = tolower (*str);
str++;
}
}
void Transpose (char * str)
{
while (*str)
{
if (islower (*str))
*str = toupper (*str);
else if (isupper (*str))
*str = tolower (*str);
str++;
}
}
void Dummy (char * str)
{
//
}
while (gets (line) != NULL && line[0] != '\0')
{
index = showmenu ();
while (index != 4)
{
// switch (index)
// {
// case 'u': pfun = arpf[0]; break;
// case 'l': pfun = arpf[1]; break;
// case 't': pfun = arpf[2]; break;
// case 'o': pfun = arpf[3]; break;
// }
// index = showmenu ();
while (index >= 0 && index <= 3)
{
strcpy (copy, line);
show (arpf[index], copy);
index = showmenu ();
}
while (gets (line) != NULL && line[0] != '\0')
{
index = showmenu ();
while (index != 4)
{
// switch (index)
// {
// case 'u': pfun = arpf[0]; break;
// case 'l': pfun = arpf[1]; break;
// case 't': pfun = arpf[2]; break;
// case 'o': pfun = arpf[3]; break;
// }
// index = showmenu ();
while (index >= 0 && index <= 3)
{
strcpy (copy, line);
show (arpf[index], copy);
index = showmenu ();
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int showmenu (void);
void eatline (void);
typedef void (* V_FP_CHARP) (char *);
void show (V_FP_CHARP fp, char * str);
void ToUpper (char *);
void ToLower (char *);
void Transpose (char *);
void Dummy (char *);
int main (void)
{
char line[81];
char copy[81];
// char choise;
int index;
// void (* pfun) (char *);
// V_FP_CHARP pfun;
V_FP_CHARP arpf[4] = {ToUpper, ToLower, Transpose, Dummy};
puts ("Enter a string (empty line to quit): ");
for(index = 0; index < 4; ++index)
arpf[index](lineget_eni);
puts ("Bye!");
return 0;
}
void ToUpper (char * str)
{
printf("yes!\n");
}
void ToLower (char * str)
{
printf("yes!\n");
}
void Transpose (char * str)
{
printf("yes!\n");
}
void Dummy (char * str)
{
printf("yes!\n");
}
输出结果正确,说明不是函数指针的事情,是其他bug
Enter a string (empty line to quit):
yes!
yes!
yes!
yes!
Bye!
Press ENTER or type command to continue
typedef void (* V_FP_CHARP) (char *);
void show (V_FP_CHARP fp, char * str);
void ToUpper (char *);
void ToLower (char *);
void Transpose (char *);
void Dummy (char *);
V_FP_CHARP arpf[4] = {ToUpper, ToLower, Transpose, Dummy};
//木有问题,我的 g++ 环境OK
typedef void (* V_FP_CHARP) (char *);
V_FP_CHARP pfun_arr[4] = {
ToUpper, ToLower, Transpose, Dummy
};
不好意思,上面写错了。应该是这样的(如上)。
typedef void (* V_FP_CHARP) (char *);
V_FP_CHARP pfun_arr[4] = {
ToUpper, ToLower (char *), Transpose (char *), Dummy (char *)
};
试试这样可以吗?
你在调用时就是用pfun_arr[index]就可以,例如
index = showmenu ();
while (index >= 0 && index <= 3)
{
strcpy (copy, line);
show (pfun_arr[index], copy);
index = showmenu ();
}