0
誰能幫我找出錯誤? 如何解決它們? 錯誤4錯誤C2264:'PhanSo :: Cong':函數定義或聲明中的錯誤;功能不叫如何修復錯誤C2679:二進制'>>':沒有找到運算符,它需要
錯誤2錯誤C2679:二進制 '>>':沒有操作員發現它採用類型 '爲const char [3]'
#include <conio.h>
#include <iomanip>
#include <iostream>
using namespace std;
class PhanSo
{
private:
int xtu;
int xmau;
public:
void nhap();
void xuat();
PhanSo Cong(const PhanSo &);
PhanSo Tru(const PhanSo &);
PhanSo Nhan(const PhanSo &);
PhanSo Chia(const PhanSo &);
};
void PhanSo::nhap()
{
cout<<"Nhap tu so: ";
cin>>"%d">>xtu;
cout<<"Nhap mau so: ";
cin>>"%d">>xmau;
}
void PhanSo::xuat()
{
cout<<"\n %d/%d"<<xtu<<xmau;
}
int UCLN(int A, int B)
{
int a = abs(A);
int b = abs(B);
while(a != b)
{
if(a > b)
A = a - b;
else
B = b - a;
}
return A;
}
PhanSo PhanSo::Cong(const PhanSo &x)
{
PhanSo tmp;
tmp.xtu = xtu*x.xmau + xmau*x.xtu;
tmp.xmau = xmau*x.xmau;
int u = UCLN(tmp.xtu,tmp.xmau);
tmp.xtu /=u;
tmp.xmau /=u;
return tmp;
}
PhanSo PhanSo::Cong(const PhanSo &x)
{
PhanSo tmp;
tmp.xtu = xtu*x.xmau - xmau*x.xtu;
tmp.xmau = xmau*x.xmau;
int u = UCLN(tmp.xtu,tmp.xmau);
tmp.xtu /=u;
tmp.xmau /=u;
return tmp;
}
PhanSo PhanSo::Nhan(const PhanSo &x)
{
PhanSo tmp;
tmp.xtu = xtu*x.xtu;
tmp.xmau = xmau*x.xmau;
int u = UCLN(tmp.xtu,tmp.xmau);
tmp.xtu /=u;
tmp.xmau /=u;
return tmp;
}
PhanSo PhanSo::Chia(const PhanSo &x)
{
PhanSo tmp;
tmp.xtu = xtu*x.xmau;
tmp.xmau = xmau*x.xtu;
int u = UCLN(tmp.xtu,tmp.xmau);
tmp.xtu /=u;
tmp.xmau /=u;
return tmp;
}
void main()
{
PhanSo a,b,kq;
cout<<"Nhap phan so thu 1";
a.nhap();
cout<<"Nhap phan so thu 2";
b.nhap();
int key;
cout<<setw(50)<<"\nMenu"<<endl;
cout<<setw(50)<<"\n1. Tinh Tong";
cout<<setw(50)<<"\n2. Tinh Hieu";
cout<<setw(50)<<"\n3. Tinh Tich";
cout<<setw(50)<<"\n4. Tinh Thuong";
cout<<setw(50)<<"\n5. Thoat";
cout<<setw(50)<<"\nBan chon:";
cin>>key;
switch (key)
{
case 1:
kq = a.Cong(b);
kq.xuat();
break;
case 2:
kq = a.Tru(b);
kq.xuat();
break;
case 3:
kq = a.Nhan(b);
kq.xuat();
break;
case 4:
kq = a.Chia(b);
kq.xuat();
break;
case 5:
exit(0);
break;
default: cout<<"nhap sai.\n";
break;
}
}
錯誤說明了一切。你不能輸入字符串。 %d對於'std :: cin'沒有必要。 – chris
'「%d」'是C I/O函數的格式說明符。 – jfly