作者mantour (朱子)
看板C_and_CPP
标题Re: [问题] 海龙公式
时间Fri Sep 18 21:28:42 2009
写个加法函式的例子给你看好了
#include<iostream>
using namespace std;
int add(int a, int b)
{
int result;
result=a+b;
return result;
}
int main()
{
int x,y,answer;
cout<<"x=";
cin>>x;
cout<<"y=";
cin>>y;
answer=add(x,y);
cout<<"x+y="<<answer<<"\n";
return 0;
}
这样应该就知道要怎麽改了吧?
※ 引述《diana76 (白凤丸)》之铭言:
: 刚学C++
: 学长出了一个作业_海龙公式
: 本人写完後发现有点问题
: 回传值的问题
: 但是不知道要怎麽解决通常不是可以在
: int main()的()中写上函数吗?
: 有点不太懂这部份
: 不知道这种问题在这边可不可以问?
: 如果不行问的话会自D
: #include<iostream>
: #include<cmath>
: using namespace std;
: double a,b,c,s,area;
: int math()
: {
: cout<<"Please input the three values for the lengths of the sides of a
: triangle."<<endl<<"a=";
: cin>>a;
: cout<<"b=";
: cin>>b;
: cout<<"c=";
: cin>>c;
: s=(a+b+c)/2;
: area=sqrt(s*(s-a)*(s-b)*(s-c));
: }
: int main(int math)
: {
: if(a+b>c&&a+c>b&&b+c>a)
: cout<<"The lengths cannot be those of the sides of a triangle."<<endl;
: else
: cout<<"The areaof the triangle is "<<area<<endl;
: }
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.213.158
1F:→ REXII:他那个int main(int math)是正确的吗 = =? 09/20 12:08
2F:→ REXII:自订函数写在写在main()之前跟之後有甚麽差别呢??想了好久 09/20 12:10
3F:→ REXII:我看有的书会先在main()之前先宣告一次,然後main()写完在写 09/20 12:14
4F:→ REXII:出完整的自订函数耶...感觉好麻烦= =; 09/20 12:14
5F:推 REXII:给大大推一下 XD 09/20 12:17
6F:推 dendrobium:写在之後 那前面要先宣告函式的原型(prototype) 09/20 16:53
7F:→ mantour:回一楼,这样不正确。main函式可以有引数但不是这样用的 09/20 17:30
8F:推 REXII:谢谢两位^^ 09/20 18:09