Hiện tại mình code chưa hoàn chỉnh lắm, test kết quả vẫn bị sai, nhưng không biết sữa chổ nào, mong mọi người giải quyết giúp mình, mình cảm ơn trước!!
Đây là Code mình làm:
// sophuc.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
struct sophuc{
double thuc,ao;
};
istream &operator>> (istream &is, sophuc &a){
cout<<"thuc:";
is>>a.thuc;
cout<<"ao:";
is>>a.ao;
return is;
}
int ucln(int x,int y){
x=abs(x);y=abs(y);
while(x!=y){
if(x>y)
x-=y;
else
y-=x;
}
return x;
}
sophuc operator + (sophuc x,sophuc y){
sophuc c;
c.thuc=x.thuc+y.thuc;
c.ao=x.ao+y.ao;
return c;
}sophuc operator - (sophuc x,sophuc y){
sophuc c;
c.thuc=x.thuc-y.thuc;
c.ao=x.ao-y.ao;
return c;
}
sophuc operator * (sophuc x,sophuc y){
sophuc c;
c.thuc=x.thuc*y.thuc-x.ao*y.ao;
c.ao=x.thuc*y.ao+x.ao*y.thuc;
return c;
}
sophuc operator / (sophuc x,sophuc y){
sophuc c;
double s;
s=y.thuc*y.thuc+y.ao*y.ao;
c.thuc=(x.thuc*x.ao+y.thuc*y.ao)/s;
c.ao=(x.ao*y.thuc-x.thuc*y.ao)/s;
return c;
}
ostream& operator<< (ostream &os, sophuc &a){
if(a.thuc*a.ao<0)
os<<"-";
os<<a.ao+a.thuc;
return os;
}
/* ostream& operator<< (ostream &os, sophuc &a){
cout<<"(thuc, ao) = ";
os>>a.thuc>>a.ao;
fflush(stdin);
return os;
} */
/* ostream &operator<<(ostream &o, sophuc a){
o<<a.thuc;
if(a.ao!=0){
cout<<(a.ao>0?" +j":" -j");
cout<<abs(a.ao);
}
return o;
} */
sophuc tinh(sophuc x,sophuc y,char pt){
sophuc c;
switch(pt){
case '+':
c=x+y;
break;
case '-':
c=x-y;
break;
case '*':
c=x*y;
break;
case '/':
c=x/y;
break;
}return c;
}
int _tmain(int argc, _TCHAR* argv[])
{
sophuc a,b,c;
char pt;
cout<<"Nhap so thuc: " "\n";
cin>>a;
cout<<"Nhap so ao: " "\n";
cin>>b;
cout<<"Nhap phep tinh(+,-,*,/): " "\n";
cin>>pt;
c=tinh(a,b,pt);
cout<<a<<pt<<b<<"="<<c<< "\n";
return 0;
}
- Bạn có thể cho code vào tag cho dễ nhìn đc ko ? chứ nhìn thế này đau mắt lắm
- Bài này sai báo lỗi gì ? hay ko báo lỗi nhưng lại ra kết quả sai .... Bạn trình bày mọi người mới biết đc chứ