c++控制台程序出错,请大家帮帮忙看看

cyply 2003-10-15 09:21:18
错误信息:F:\Visual Studio Projects\c++\complex\complex.cpp(56) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
大家帮帮忙看看,或许有更好的算法,请指点指点。
源程序为:
#include"iostream.h"
class Complex//复数类定义
{ private:
double Real;//复数实部
double imag;//复数虚部
public:
void output()//返回函数
{
if(imag>=0)
cout<<a.Real<<"+"<<a.imag<<"i "<<endl;
else
cout<<a.Real<<a.imag<<"i "<<endl;
}
void getnumber(double x,double y)
{
Real=x;
imag=y;
}
Complex operator -(const Complex a)//重载一元操作符负号"-"
{
Complex b;
b.Real=-a.Real;
b.imag=-a.imag;
return b;
}
Complex operator +(const Complex a)//重载二元操作符加号"+"
{
Complex b;
b.Real=Real+a.Real;
b.imag=imag+a.imag;
return b;
}
Complex operator *(const Complex a)//重载二元操作符加号"*"
{
Complex b;
b.Real=Real*a.Real-imag*a.imag;
b.imag=Real*a.imag+imag*a.Real;
return b;
}
Complex operator /(const Complex a)//重载二元操作符加号"/"
{
Complex b;
b.Real=(Real*a.Real+imag*a.imag)/(a.Real*a.Real+a.imag*a.imag);
b.imag=(imag*a.Real-Real*a.imag)/(a.Real*a.Real+a.imag*a.imag);
return b;
}
void main()
{
Complex a,b,c;
a.getnumber(0.0,0.0);
b.getnumber(1.0,1.0);
c=a-b;
cout<<"if c=a-b 则c=";
c.output();
c=a+b;
cout<<"if c=a+b 则c=";
c.output();
c=a*b;
cout<<"if c=a*b 则c=";
c.output();
c=a/b;
cout<<"if c=a/b 则c=";
c.output();
}
...全文
60 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaolongleee 2003-10-15
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
using namespace std;

class Complex
{
public:
Complex(){}
Complex(double real, double imag)
{
m_real = real;
m_imag = imag;
}

Complex operator + (const Complex a);
Complex operator - (const Complex a);
Complex operator * (const Complex a);
Complex operator / (const Complex a);

void Output(void);

~Complex(){}
private:
double m_real;
double m_imag;
};

Complex Complex::operator +(const Complex a)
{
Complex b;

b.m_real = this->m_real + a.m_real;
b.m_imag = this->m_imag + a.m_imag;

return b;
}

Complex Complex::operator -(const Complex a)
{
Complex b;

b.m_real = this->m_real - a.m_real;
b.m_imag = this->m_imag - a.m_imag;

return b;
}

Complex Complex::operator *(const Complex a)
{
Complex b;

b.m_real = (this->m_real)*(a.m_real) - (this->m_imag)*(a.m_imag);
b.m_imag = (this->m_real)*(a.m_imag) + (this->m_imag)*(a.m_real);

return b;
}

Complex Complex::operator /(const Complex a)
{
Complex b;

double div = (a.m_real)*(a.m_real) + (a.m_imag)*(a.m_imag);
b.m_real = ((this->m_real)*(a.m_real) + (this->m_imag)*(a.m_imag)) / div;
b.m_imag = ((this->m_imag)*(a.m_real) - (this->m_real)*(a.m_imag)) / div;

return b;
}

void Complex::Output(void)
{
cout << m_real <<" + "<< m_imag << "i" << endl;
}

int main()
{
Complex a(2.1, 3.4);
Complex b(4.5, 6.7);
Complex c;

c = a / b;

c.Output();

c = a + b;
c.Output();

c = a - b;
c.Output();

c = a * b;
c.Output();


return 0;
}
loadme_litbear 2003-10-15
  • 打赏
  • 举报
回复
不要上来就OOP,C语言才是根本
cyply 2003-10-15
  • 打赏
  • 举报
回复
多谢多谢!不只有没更好的算法。
fengfeng2003 2003-10-15
  • 打赏
  • 举报
回复
#include"iostream.h"
class Complex//复数类定义
{ private:
double Real;//复数实部
double imag;//复数虚部
public:
void output()//返回函数
{
if(imag>=0)
cout<<a.Real<<"+"<<a.imag<<"i "<<endl;
else
cout<<a.Real<<a.imag<<"i "<<endl;
}
void getnumber(double x,double y)
{
Real=x;
imag=y;
}
Complex operator -(const Complex a)//重载一元操作符负号"-"
{
Complex b;
b.Real=-a.Real;
b.imag=-a.imag;
return b;
}
Complex operator +(const Complex a)//重载二元操作符加号"+"
{
Complex b;
b.Real=Real+a.Real;
b.imag=imag+a.imag;
return b;
}
Complex operator *(const Complex a)//重载二元操作符加号"*"
{
Complex b;
b.Real=Real*a.Real-imag*a.imag;
b.imag=Real*a.imag+imag*a.Real;
return b;
}
Complex operator /(const Complex a)//重载二元操作符加号"/"
{
Complex b;
b.Real=(Real*a.Real+imag*a.imag)/(a.Real*a.Real+a.imag*a.imag);
b.imag=(imag*a.Real-Real*a.imag)/(a.Real*a.Real+a.imag*a.imag);
return b;
}
}; //此处少括号,分号
void main()
{
Complex a,b,c;
a.getnumber(0.0,0.0);
b.getnumber(1.0,1.0);
c=a-b;
cout<<"if c=a-b 则c=";
c.output();
c=a+b;
cout<<"if c=a+b 则c=";
c.output();
c=a*b;
cout<<"if c=a*b 则c=";
c.output();
c=a/b;
cout<<"if c=a/b 则c=";
c.output();
}
cyply 2003-10-15
  • 打赏
  • 举报
回复
哦,忘记去掉了,
这是一开始时写的,后来改了,还有后面的const也去掉了。
xiuluo2305 2003-10-15
  • 打赏
  • 举报
回复
output 函数a是什么意思

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧