菜单的使用方法

来源:互联网 发布:网络推广简历 编辑:程序博客网 时间:2024/05/29 19:37

来源于C Primer Plus(第五版)第416页

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 ();
 }

 

void eatline (void)
{
 while (getchar () != '/n')
  continue;
}

 

 

感觉整个程序结构较好,特记下:

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

char showmenu (void);
void eatline (void);
void show (void (*p) (char *));
void ToUpper (char *);
void ToLower (char *);
void Transpose (char *);
void Dummy (char *);

int main (void)
{
 char line[81];
 char copy[81];
 char choice;
 void (*pfun)(char *);

 puts ("Enter a string (empty line to quit): ");
 while (gets (line) != NULL && line[0] != '/0')
 {
  while ((choice = showmenu ()) != 'n')
  {
   switch (choice)
   {
    case 'u':
     pfun = ToUpper; break;
    case 'l':
     pfun = ToLower; break;
    case 't':
     pfun = Transpose; break;
    case 'o':
     pfun = Dummy; break;
    default:
     break;
   }
   strcpy (copy, line);
   show (pfun, copy);
  }
  puts ("Enter a string (empty line to quit): ");
 }
 puts ("Bye!");
 return 0;
}

char showmenu (void)
{
 char ans;
 puts ("Enter menu choice: ");
 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 ();
 }
 return ans;
}

void eatline (void)
{
 while (getchar () != '/n')
  continue;
}

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)
{
 //不改变字符串
}

void show (void (*fp)(char *), char *str)
{
 (*fp)(str);
 puts (str);
}

原创粉丝点击