-
04-10-2015, 07:46 PM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
viết hàm đọc viết số phức ra file ??? Mình viết nó bị lỗi mọi người xem dùm mình với :((
#include <iostream>
#include <fstream>
using namespace std;
class Complex{
private:
int _im;
int _real;
public:
Complex(){
this -> _im = 0;
this -> _real = 0;
}
Complex(int im, int _ireal){
this -> _im = im;
this -> _real = _ireal;
}
friend istream & operator >> (istream &is, Complex &sp){
is >> sp._real;
is >> sp._im;
return is;
}
friend ostream & operator << (ostream &os, Complex &sp){
os << sp._real;
os << sp._im;
return os;
}
Complex operator / (const Complex &sp){
return Complex(((_real*sp._real+_im*sp._im)/(sp._real*sp._real+sp._im*sp._im)),((_im*sp._real-_real*sp._im)/(sp._real*sp._real+sp._im*sp._im)));
}
Complex operator - (const Complex &sp){
return Complex((_real - sp._real),(_im - sp._im));
}
Complex operator + (const Complex &sp){
return Complex((_real + sp._real),(_im + sp._im));
}
int readComplex(char* filename, Complex c[], int &n){
fstream f ("input.txt", ios :: in);
if(!f){
cout << "File khong mo duoc" << endl;
return 0;
}
f >> n;
int i = 0;
while(!f.eof()){
f >> c[i];
i++;
}
f.close();
return 1;
}
int writeComplex(char *filename, Complex c[], int &n){
fstream f("output.txt", ios :: app);
if(!f){
cout << "File khong mo duoc" << endl;
return 0;
}
for(int i = 0; i < n; i++){
f << c[i] << " ";
}
f.close();
return 1;
}
};
int main(int argc, char* argv[]){
Complex c[100];
Complex f;
int n;
cout << "Nhap n: ";
cin >> n;
f.readComplex(argv[2], c, n);
f = (c[0] - c[1]);
f = (c[0] - c[1])/c[2];
f.writeComplex(argv[2], c, n);
}View more random threads:
- Xây dựng Danh sách liên kết trong C#
- Tạo Cursor cho chương trình C#
- xin hướng dẫn cách lấy danh sách bạn bè facebook bằng c#
- Lập trình Socket với C#
- tính tổng
- Cách dùng delegate trong lập trình C#
- Tạo 1 chuỗi ngẫu nhiên cực mạnh trong lập trình C#
- Sử dụng một phần menu chính cho menu ngữ cảnh trong lập trình C#
- XXX.tab already registrered. This file not registrered for access.
- Xem nội dung file pdf trên web
Ngọc Bích xanh bản chất thuộc dòng đá đá hoa (jade). Và cẩm thạch là tên gọi chung của ngọc bích. Vì thực chất chúng thuộc dòng đá đa khoáng được hình thành từ chất Silicat dưới dạng dioxy. Ngọc bích...
Chia sẻ Vòng tay ngọc bích xanh là...