zoukankan      html  css  js  c++  java
  • Linux C Programing

    #include <stdio.h> //getchar() putchar() printf() gets() puts() sprintf()
    #include <stdlib.h> //exit() rand () srand() system() free() malloc()
    
    
    //int getchoice (char *greet , char *choices[]);
    int getchoice (char *greet , char **choices);
    
    int main()
    {
        char *menu[] = {
                "a - add new record",
                "d - delete record",
                "s - show record num ",
                "q - quit",
                NULL,
        };
        int record_num = 0;
        int choice = 0;
        do
        {
            choice = getchoice("Please select an action",menu);
            printf("Your choice is %c 
    ",choice);
            switch (choice)
            {
                case 'a':
                {
                    printf("Add record over
    ");
                    record_num++;
                    break;
                }
                case 'd':
                {
                    printf("Remove a record over
    ");
                    record_num--;
                    break;
                }
                case 's':
                {
                    printf("The record num is %d 
    ",record_num);
                    break;
                }
                default:
                    break;
            }
        }
        while(choice!='q');
        exit(0);
    }
    
    int getchoice (char *greet , char **choices)
    {
        char **option;
        int chosen=0; // control choice is the member of menu list
        int selected;
        do
        {
            printf("Choice: %s 
    " ,greet);
            option=choices;
            while (*option!=NULL)
            {
                printf("%s
    ",*option);
                option++;
            }
    
            do
            {
                selected=getchar();
            }while (selected=='
    '); // solve the enter char + '
    ' problem,because will cause
            //next input enter the '
    ' directly
    
            option =choices;
            while(*option)
            {
                if(selected == *option[0])
                {
                    chosen=1;
                    break;
                }
                option++;
            }
            if (!chosen)
            {
                printf("Incorrect choice,try again
    ");
            }
    
        }
        while(!chosen);
        return selected;
    }
  • 相关阅读:
    国行ME525+(2.3.6)手机系统的备份方法
    JS定时刷新页面及跳转页面
    Webservice数据迁移
    SQL表A更新和表B同步
    C#面试题3
    移动互联
    新闻内容翻页
    教你怎么把服务器上的数据库备份到本地计算机
    asp.net性能优化
    sql2005数据库备份—sql语句
  • 原文地址:https://www.cnblogs.com/gearslogy/p/5291826.html
Copyright © 2011-2022 走看看