用c++在同一个程序中求长方形的面积和周长?

如题所述

第1个回答  2010-03-28
#include<iostream>
using namespace std;

main()
{
int lenth;
int width;
cout<<"长:"<<lenth<<endl;
cout<<"宽:"<<width<<endl;
cout<<"面积"<<lenth*width<<endl;
cout<<"周长"<<(lenth+width)*2<<endl;
return 0;
}

你要是想一个函数返回两个值,那返回一个数组
第2个回答  推荐于2017-09-23
第二个人答的不错,不过比较复杂!
#include"iostream"
using namespace std;
void main()
{double a,b,s,l;
cout<<"分别输入长 宽";
cin>>a>>b;
l=2*(a+b);
s=a*b;
cout<<"周长为:"<<l<<"面积为"<<s<<endl;}

参考资料:如果您的回答是从其他地方引用,请表明出处

本回答被提问者采纳
第3个回答  2010-03-29
#include<iostream>
using namespace std;
int main()
{
double width;
double lenght;
double area;
double circ;

cout << "边长:";
while(!(cin >> lenght))
{
cin.clear();
while(cin.get()!='\n')
continue;
cout << "输入数据类型错误\n";
cout << "请重新输入\n" << "边长:";
}

cout << "宽:";
while(!(cin >> width))
{
cin.clear();
while(cin.get()!='\n')
continue;
cout << "输入数据类型错误\n";
cout << "请重新输入\n" << "宽:";
}

area = lenght * width;
circ = 2*(lenght+width);
cout << "面积:" << area;
cout << "周长" <<circ;

return 0;
}
相似回答