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

时间:2014-10-20 13:30:29

标签: c++

我执行此代码时会让用户选择一个选项。一旦用户输入选项,程序将被清除并执行另一个程序。以下是样本。在底部是选项1的另一个程序

#include <iostream>
using namespace std;
int main()
{
    int a;
    cout<<"Please choose an option below: \n";
    cout<<"1. Area of Shapes\n";
    cout<<"2. Cost of your items\n";
    cout<<"3. Flood Control\n";
    cout<<"4. Fibonacci Numbers\n";
    cout<<"5. Addition Table\n";
    cout<<"6. Exit\n";
    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;
}

2 个答案:

答案 0 :(得分:1)

如果您 想要将当前程序替换为其他程序,请查看exec系列调用。

答案 1 :(得分:0)

  1. 把每个&#34;程序&#34;在它自己的文件中(或不是,但将它们分开是一个好主意)。
  2. 将每个&#34;程序&#34; main重命名为有意义的内容,例如&#34;区域&#34;。
  3. 在标题中声明该功能。
  4. 在&#34;控制器&#34;中加入标题程序
  5. 调用相应的&#34;程序功能&#34;来自&#34;控制器&#34;基于您阅读的输入。