这是一个非常简单的矩形的程序,是前期数据结构上的一个例子。但是在VC++下不能正常的运行。
#include "stdafx.h"
#include "iostream.h"
struct Rectangle
{
float length,width;
};
void InitRectangle(Rectangle r,float length,float width);
float Circumference(Rectangle r);
float Area(Rectangle r);
void InitRectangle(Rectangle r,float len,float wid)
{
r.length=len;
r.width=wid;
}
float Circumference(Rectangle r)
{
return 2*(r.length+r.width);
}
float Area(Rectangle r)
{
return r.length*r.width;
}
void main(void)
{
float x,y;
float p,s;
Rectangle a;
cout < <"请输入一个矩形的长和宽!" < <endl;
cin>>x>>y;
InitRectangle(a,x,y);
p=Circumference(a);
s=Area(a);
cout < <endl;
cout < <"矩形的周长为" < <p < <endl;
cout < <"矩形的面积为" < <s < <endl;
}
解决方法是
#include "stdafx.h"
#include "iostream.h"
typedef struct
{
float length,width;
}Rectangle_struct;
void InitRectangle(Rectangle_struct& r,float length,float width);
float Circumference(Rectangle_struct& r);
float Area(Rectangle_struct& r);
void InitRectangle(Rectangle_struct& r,float len,float wid)
{
r.length=len;
r.width=wid;
}
float Circumference(Rectangle_struct& r)
{
return 2*(r.length+r.width);
}
float Area(Rectangle_struct& r)
{
return r.length*r.width;
}
void main(void)
{
float x,y;
float p,s;
Rectangle_struct a;
cout <<"请输入一个矩形的长和宽!"<<endl;
cin>>x>>y;
InitRectangle(a,x,y);
p=Circumference(a);
s=Area(a);
cout<<endl;
cout<<"矩形的周长为"<<p<<endl;
cout<<"矩形的面积为"<<s<<endl;
}
问题出在VC++的MFC上。