📌  相关文章
📜  菜单驱动程序,用于使用C ++进行系统控制

📅  最后修改于: 2021-06-01 02:11:16             🧑  作者: Mango

先决条件: C / C++中的开关箱

问题陈述:
使用Switch-case编写菜单驱动程序来控制系统,例如关闭,重新启动,注销,手动关闭设置,中止关闭并退出。

方法:想法是在C中使用system() 。此函数用于从C++程序调用操作系统命令
在此程序中使用下面列出的一些系统命令:

  1. 关闭:要关闭系统,请使用system()函数和带有选项s的命令关闭
    system("C:\\WINDOWS\\System32\\shutdown/s")
  2. 重新启动:要重新启动系统,请使用system()函数和带有选项r的命令关闭,如下所示:
    system("C:\\WINDOWS\\System32\\shutdown/r")
  3. 注销:要注销系统,请使用system()函数和带有选项l的命令关闭,如下所示:
    system("C:\\WINDOWS\\System32\\shutdown/l")
  4. 手动关闭:要手动关闭系统,请使用system()函数和带有选项i的命令关闭
    system("C:\\WINDOWS\\System32\\shutdown/i")
  5. 中止关机:要中止关机系统,请使用system()函数和带选项a的命令shutdown
    system("C:\\WINDOWS\\System32\\shutdown/a")

下面实现上述系统函数和命令:

CPP
// Menu driven program in CPP to
// system control using system()
// function and command with option
#include 
#include 
#include 
#include 
#include 
  
void printMenu();
  
// Function to shutdown the computer
void shutDown()
{
    // Tp clears the screen
    system("cls");
    printf("\nshuttingg down..\n");
  
    // System function call to
    // shutdown system
    system("C:\\WINDOWS\\System32\\shutdown /s");
  
    // To clears the screen
    system("cls");
}
  
// Function to restart the computer
void reStart()
{
  
    // Clears the screen
    system("cls");
    printf("\nRestart in 30 seconds ...");
  
    // System function call to
    // restart system
    system("C:\\WINDOWS\\system32\\shutdown /r");
}
  
// Function to log off user
void logOff()
{
  
    // To clears the screen
    system("cls");
    printf("\n Shutting down under 30 seconds... ");
  
    // System function call to log off user
    system("C:\\WINDOWS\\system32\\shutdown /l");
}
  
// Function to open manualShutdown
// shutdown dialog box
void manualShutdown()
{
    // To clears the screen
    system("cls");
  
    // System function call to manual shutdown
    system("C:\\WINDOWS\\System32\\shutdown /i");
}
  
void abortShutdown()
{
    // To clears the screen
    system("cls");
  
    // System function call to aboart shutdown
    system("C:\\WINDOWS\\System32\\shutdown /a");
}
  
// Function to take user choices and perform
// the appropriate operation
void selectMenu()
{
    int choice;
    printf("\n Enter your choice : ");
    scanf("%d", &choice);
  
    switch (choice) {
    case 1:
        shutDown();
        break;
  
    case 2:
        reStart();
        break;
  
    case 3:
        logOff();
        break;
  
    case 4:
        manualShutdown();
        break;
  
    case 5:
        abortShutdown();
        break;
  
    case 6:
  
        printf("\n Exiting... \n\n");
        printf("Exiting in 3 seconds...\n");
        Sleep(3000);
        exit(1);
  
    default:
        printf("\ninvalid choice Try again \n");
        printMenu();
    }
}
  
// Function to print all the menus
void printMenu()
{
    // Set output color to blue
    // background and white foreground
    system("color 1F");
    printf("\n");
  
    // Create Menu
    printf("\xB2 \xB2\xB2\xB2\xB2\xB2\xB2\xB2"
           "\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"
           "\xB2\xB2\xB2\xB2\xB2 SYSTEM CONTROL \xB2"
           "\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"
           "\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"
           "\xB2\xB2 \xB2");
    printf("\n ______________________________"
           "_________________________________");
    printf("\n|\t\t\t\t\t\t\t\t|");
    printf("\n|\t\t\t\t\t\t\t\t|");
    printf("\n|\t\t\t 1. Shutdown Computer \t\t\t|");
    printf("\n|\t\t\t 2. Restart Computer \t\t\t|");
    printf("\n|\t\t\t 3. Log off \t\t\t\t|");
    printf("\n|\t\t\t 4. Manual Shutdown Settings\t\t|");
    printf("\n|\t\t\t 5. Abort Shutdown \t\t\t|");
    printf("\n|\t\t\t 6. Exit \t\t\t\t|");
    printf("\n|\t\t\t\t\t\t\t\t|");
    printf("\n|\t\t\t\t\t\t\t\t|");
    printf("\n|\t\t\t\t\t\t\t\t|");
    printf("\n|\t\t\t\t\t\t\t\t|");
    printf("\n\xB2_________________________________"
           "______________________________\xB2\n");
  
    // Function call for select options
    selectMenu();
}
  
// Driver Code
int main()
{
    // Function Call
    printMenu();
    return 0;
}


输出:
以下是上述程序的输出:

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”