[ create a new paste ] login | about

Project: ec
Link: http://ec.codepad.org/UsNDoEEy    [ raw code | fork ]

C++, pasted on Feb 25:
//RATIONAL.H
#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
#include <cmath>
using namespace std;

class Rational {
	int m_n; // number=n/d
	int m_d;
public: 
	Rational(const int n=0,int d=1):m_n(n) {
		if (d==0) {
			cout<<"The D number cannot be zero. Reset to 1."<<endl;
			d=1;
		}
		m_d=d;
	}
	void Read(istream &i=cin) {
		char ch;
		i>>m_n>>ch>>m_d;
	}
	void print(ostream &o=cout) const {
		o<<m_n<<"/"<<m_d;
	}

	friend Rational operator+(const Rational &l,const Rational &r);
	friend Rational operator-(const Rational &l,const Rational &r);
	friend Rational operator*(const Rational &l,const Rational &r);
	friend Rational operator/(const Rational &l,const Rational &r);
	Rational Pow(const Rational &r) const {
		return Rational((int)pow(m_n,(float)r.m_n/r.m_d),(int)pow(m_d,(float)r.m_n/r.m_d)); 
	}
};

inline Rational operator+(const Rational &l,const Rational &r) { 
	return Rational(l.m_n*r.m_d+r.m_n*l.m_d,r.m_d*r.m_d);
}
inline Rational operator-(const Rational &l,const Rational &r) { 
	return Rational(l.m_n*r.m_d-r.m_n*l.m_d,r.m_d*r.m_d);
}
inline Rational operator*(const Rational &l,const Rational &r) { 
	return Rational(l.m_n*r.m_n,l.m_d*r.m_d);
}
inline Rational operator/(const Rational &l,const Rational &r) { 
	return Rational(l.m_n*r.m_d,l.m_d*r.m_d);
}
inline Rational operator^(const Rational &l,const Rational &r) { 
	return l.Pow(r);
}
inline ostream &operator<<(ostream &o,const Rational &R) {
	R.print(o);
	return o;
}
#endif

//Calc.h
#ifndef CALC_H
#define CALC_H
#include <iostream>
#include "Rational.h"
/*********** in the next line I get these errors:
1>c:\users\home\documents\visual studio 2008\projects\nachum\nachum\calc.h(6) : error C2146: syntax error : missing ';' before identifier 'std'
1>c:\users\home\documents\visual studio 2008\projects\nachum\nachum\calc.h(6) : error C2873: 'namspace' : symbol cannot be used in a using-declaration
1>c:\users\home\documents\visual studio 2008\projects\nachum\nachum\calc.h(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\home\documents\visual studio 2008\projects\nachum\nachum\calc.h(6) : error C2365: 'std' : redefinition; previous definition was 'namespace'
*/
using namspace std;

class calc {
public:
	calc(istream &i=cin,ostream &o=cout):in(i),out(o) {}
	void function (char op, Rational &r1,Rational &r2) {
		switch(op) {
			case '*': out<<r1*r2; break;
			case '/': out<<r1/r2; break;
			case '-': out<<r1-r2; break;
			case '+': out<<r1+r2; break;
/********* in the next line I get this error:
1>c:\users\home\documents\visual studio 2008\projects\nachum\nachum\calc.h(17) : error C2678: binary '^' : no operator found which takes a left-hand operand of type 'std::ostream' (or there is no acceptable conversion)
1>        c:\users\home\documents\visual studio 2008\projects\nachum\nachum\rational.h(47): could be 'Rational operator ^(const Rational &,const Rational &)'
1>        while trying to match the argument list '(std::ostream, Rational)'
*/
			case '^': out<<r1^r2; break;
			default: out<<"wrong operation!"<<endl;
		}
	}
	int main() {
		char op;
		Rational R1,R2;
		while (!in && !in.eof() ) {
			cout<<"Getting info..."<<endl;
			R1.Read(in);
			in>>op;
			R2.Read(in);
			function(op,R1,R2);
		}
	}
private:
	istream &in;
	ostream &out;
};
#endif

//MainCalcRational.cpp
#pragma warning(disable: 4996)
#pragma warning(disable: 4244)
#include <iostream>
#include "Calc.h"
#include "Rational.h"
#include <direct.h>
#include <fstream>
using namespace std;

int main() {
	char *FileName=new char [20];
	char *CurrentWorkingDirectory=new char[50];
	strcpy(FileName,"ifstream.txt");

	//ifstream
	ifstream ifile(FileName);
	while (!ifile) {  
		if ((CurrentWorkingDirectory=_getcwd(NULL,0))==NULL) {
			perror("_getcwd error");
			cout<<"can't open file: "<<FileName<<" and can't open the current working directory path."<<endl
			<<"Please write again the file name to get orders from: ";
		}
		else
			cout<<"can't open file: "<<FileName<<" see if it is in : "<<CurrentWorkingDirectory<<endl
			<<"Please write again the file name to get orders from: ";
		ifile.clear();
		cin.getline(FileName,20);
		ifile.open(FileName);
	}

	//ofstream
	strcpy(FileName,"ofstream.txt");
	ofstream ofile(FileName);
	while (ofile) {  
		if ((CurrentWorkingDirectory=_getcwd(NULL,0))==NULL) {
			perror("_getcwd error");
			cout<<"can't open file: "<<FileName<<" and can't open the current working directory path."<<endl
			<<"Please write again the file name to get orders from: ";
		}
		else
			cout<<"can't open file: "<<FileName<<" see if it is in : "<<CurrentWorkingDirectory<<endl
			<<"Please write again the file name to get orders from: ";
		ifile.clear();
		cin.getline(FileName,20);
		ifile.open(FileName);
	}
	calc Calculations(ifile,ofile);
	Calculations.main();

	int a;
	cin>>a;
}


Create a new paste based on this one


Comments: