功能问题 [英] problems with function

查看:47
本文介绍了功能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,现在我开始工作这部分了,如何使用已定义的函数使它工作呢?以及我应该使用哪些项目使其成为我在较早的帖子中尝试过的myfuncion,并且您可以看到它不起作用,所以我回到了基础知识上,现在需要帮助弄清楚如何实现和创建自己的函数来做这个计算.感谢您的帮助

Ok now I got this part working how should I make this to work using a defined function? and what items should I use to make it a myfuncion I haves tried by the earlier posts and as you can see it is not working so I went back to the basic and now need help figuring out how to implement and create my own function to do this calculation. thanks for the help

#include <iostream>
#include <string>
const int SQUARE = 1;
const int TRIANGLE = 2;
const int RECTANGLE = 3;
const int CIRCLE = 4;
using namespace std;
int main()
{
    // Declare Variable
    int choice;
    int width;
    int height;
    double base;
    int area;
    int distance;
        cout << "What is the area of a square, triangle, rectangle, or circle?" ;
    // Display Choice
        cout << SQUARE << " = Square:  " << endl;
        cout << TRIANGLE << " = Triangle:  " << endl;
        cout << RECTANGLE << " = Rectangle: " << endl;
        cout << CIRCLE << " = Circle:  "  << endl;
        cout << "Make your choice? ";
        cin >> choice ;
        // Display Proper feedback based on users choice
        switch (choice)
        {
            case SQUARE:
        // Get Square information
            cout << "Enter the width of the square: " << endl;
            cin >> width;
            cin >> width;
            area = width * width;
            cout << "The area of the square is: " << area << endl;
                break;
            case TRIANGLE:
        // Get Triangle information
                cout << "Enter the base: " << endl ;
                cin >> base;
                cout << "Enter the height: " << endl ;
                cin >> height;
                area = (base * 2) * height;
                cout << "The area of a triangle is " << area << endl;
                break;
            case RECTANGLE:
        //Get the Rectangle inforamtion
                cout <<"Enter the height: " << endl;
                cin >> height;
                cout << "Enter the width: " << endl;
                cin >> width;
                area = height * width;
                cout << "The area of a rectancle is: " << area << endl;
                break;
            case CIRCLE:
        //Get the circle circumference
                cout << "Enter the distance: " << endl;
                cin >> distance;
                area = distance * 3.14;
                cout << "The circumference of a circle is: " << area << endl;
                break;
        }
system("pause");
return 0;

推荐答案

好吧,您可以创建自己的函数来计算每种形状.
相反,您还可以创建具有相同名称但具有不同签名(或参数)的函数,称为重载函数:

这是Circle的功能:

well you can create your own function for calculation of each shape.
rather you can also create functions with the same name but with different signatures(or parameters) known as Overloaded function :

here is the function for Circle :

function ComputeArea(Double distance)
{
 double area;
 area=distance*3.14;
 cout<<"Circumference of a circle is : "<<area;
}




在这里,您必须传递一个参数"distance",该参数将用于计算圆的面积.

类似矩形:




here you have to pass a parameter "distance" which will be used to compute area of circle.

Similiarly for Rectangle :

function ComputeArea(integer height,integer width)
{
integer area;
area=height*width;
cout<<"Area of rectangle is: " <<area;
}



如您所见,我创建了另一个具有相同名称但具有不同参数的函数,更重要的是具有不同类型的函数.这是重载函数的概念:

同样,您可以为三角形"和正方形"创建它.
然后在切换代码中:



As you can see, i have created another function with the same name but with different parameters and also more importantly of different type. this is the concept of overloaded function:

similiarly you can create it for Triangle and Square.
and then in the switch code :

case "CIRCLE" :
ComputeArea(2.45);



和其他人一样.

希望能帮助到你! :)



and the same for others.

Hope it helps! :)


这篇关于功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆