当选择一个选项时,如何执行另一个c++程序

How to execute a another c++ program when an option is chosen?

本文关键字:执行 何执行 另一个 程序 c++ 选择 一个 选项      更新时间:2023-10-16

我有这段代码,当执行时,它会让用户选择一个选项。一旦用户输入一个选项,程序将被清除并执行另一个程序。下面是示例。底部是选项1 的另一个程序

#include <iostream>
using namespace std;
int main()
{
    int a;
    cout<<"Please choose an option below: n";
    cout<<"1. Area of Shapesn";
    cout<<"2. Cost of your itemsn";
    cout<<"3. Flood Controln";
    cout<<"4. Fibonacci Numbersn";
    cout<<"5. Addition Tablen";
    cout<<"6. Exitn";
    cin>> a;
system("pause");
return 0;    
}

以下是选项1:的程序

#include <iostream>
using namespace std;
float circle (float a)
{
      float z;
      z = 3.141593*(a*a);
      return (z);
}
float square (float b)
{
      float y;
      y = b * b;
      return (y);
}
float rectangle (float c, float d)
{
      float x;
      x = c * d;
      return (x);
}
float triangle (float e, float f)
{
      float w;
      w = (e * f) / 2;
      return (w);
}
void exit ()
{
     cout << "THANK YOU! GOODBYE!" <<endl;
}            
int main()
{
      float n;
      float l;
      float m;
      float radius;
      float side;
      float length;
      float width;
      float base;
      float height;
      do
      {
            cout << "1 => Area of Circle" <<endl;
            cout << "2 => Area of Square" <<endl;
            cout << "3 => Area of Rectangle" <<endl;
            cout << "4 => Area of Triangle" <<endl;
            cout << "0 => Exit" <<endl;
            cout << "Please enter number of your choice: ";
            cin >> n;
            {
                if (n==0)
                {
                         exit ();
                         system("pause");
                         return 0;
                }
                else if (n==1)
                {
                     cout << "Enter radius of the circle: ";
                     cin >> radius;
                     l = circle (radius);
                     cout << "Area of the circle is: " <<l <<endl;
                }
                else if (n==2)
                {
                     cout << "Enter side of the square: ";
                     cin >> side;
                     cout << "Area of the square is: " <<square (side) <<endl;
                }
                else if (n==3)
                {
                     cout << "Enter length of the rectangle: ";
                     cin >> length;
                     cout << "Enter width of the rectangle: ";
                     cin >> width;
                     m = rectangle (length, width);
                     cout << "Area of the rectangle is: " <<m <<endl;
                }
                else if (n==4)
                {
                     cout << "Enter base of the triangle: ";
                     cin >> base;
                     cout << "Enter height of the triangle: ";
                     cin >> height;
                     cout << "Area of the triangle is: " <<triangle (base, height) <<endl;
                }
                else
                cout << "Invalid number. Please enter a valid number below" <<endl;
                }
            }
            while (n!=0);
            cout <<endl <<endl;
            system("pause");
            return 0;
}

如果您真的想用另一个程序替换当前程序,请查看系统调用的exec系列。

  1. 将每个"程序"放在自己的文件中(或者不放,但最好将它们分开)
  2. 将每个"程序"的main重命名为有意义的内容,如"区域"
  3. 在标头中声明该函数
  4. 在您的"控制器"程序中包含该标头
  5. 根据您读取的输入,从"控制器"调用相应的"程序函数"