64,654
社区成员
发帖
与我相关
我的任务
分享
//definition.h
#ifndef DEFINITION_H_INCLUDED
#define DEFINITION_H_INCLUDED
using namespace std;
class Complex
{
public:
Complex();
Complex(double r,double v);
Complex operator+(Complex right);
Complex operator-(Complex right);
Complex operator*(Complex right);
friend ostream &operator<<(ostream &output,Complex &a);
friend istream &operator>>(istream &input,Complex &a);
bool operator!=(const Complex& b);
bool operator==(const Complex& b);
void print();
private:
double Real;
double Imaginary;
};
#endif // DEFINITION_H_INCLUDED
//realization.cpp
#include <iomanip>
#include "definition.h"
#include <iostream>
using namespace std;
Complex::Complex()
{
}
Complex::Complex(double r,double v)
{
Real = r;
Imaginary = v;
return;
}
Complex Complex::operator+(Complex right)
{
Real = Real + right.Real;
Imaginary = Imaginary + right.Imaginary;
return *this;
}
Complex Complex::operator-(Complex right)
{
Real = Real - right.Real;
Imaginary = Imaginary - right.Imaginary;
return *this;
}
Complex Complex::operator*(Complex right)
{
Real = Real * right.Real - Imaginary * right.Imaginary ;
Imaginary = Real * right.Imaginary + Imaginary * right.Real;
return *this;
}
ostream &operator<<(ostream &output,Complex &a)
{
output<<a.Real<<"+"<<a.Imaginary<<"i";
return output;
}
istream &operator>>(istream &input,Complex &complex2)
{
input>>setw(1)>>complex2.Real;
input.ignore();
input>>setw(1)>>complex2.Imaginary;
input.ignore();
return input;
}
bool Complex::operator!=(const Complex &right)
{
if(Real != right.Real ||Imaginary != right.Imaginary)
return true;
else
return false;
}
bool Complex::operator==(const Complex & right)
{
if(Real == right.Real && Imaginary == right.Imaginary)
return true;
else
return false;
}
void Complex::print()
{
cout<<"%lf + %lfi"<<Real<<Imaginary<<std::endl;
}
//main.cpp
#include <iostream>
#include "definition.h"
using namespace std;
int main()
{
Complex c1,c2(2,3),c3;
cin>>c3;
cout<<"c3=";
c3.print();
if(c3 == c2)
cout<<"c3 = c2";
else
cout<<"c3 != c2";
c1 = c3 - c2;
c1.print();
c1 = c3 + c2;
c1.print();
c1 = c3 * c2;
c1.print();
return 0;
}
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
using namespace std;
enum Token_value {
NAME, NUMBER, END,
PLUS='+', MINUS='-', MUL='*', DIV='/',
PRINT=';', ASSIGN='=',LP='(', RP=')'
};
Token_value curr_tok = PRINT;
double number_value;
string string_value;
map<string,double> table;
int no_of_errors;
double error(const string& s)
{
no_of_errors++;
cerr<<"error:"<<s<<'\n';
return 1;
}
Token_value get_token()
{
char ch=0;
cin>>ch;
switch(ch) {
case 0:
return curr_tok=END;
case ';':
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '=':
return curr_tok=Token_value(ch);
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '.':
cin.putback(ch);
cin>>number_value;
return curr_tok=NUMBER;
default:
if (isalpha(ch)) {
cin.putback(ch);
cin>>string_value;
return curr_tok=NAME;
}
error("bad token");
return curr_tok=PRINT;
}
}
double expr(bool);
double prim(bool get)
{
if (get) get_token();
switch (curr_tok) {
case NUMBER:
{ double v = number_value;
get_token();
return v;
}
case NAME:
{
double& v = table[string_value];
if (get_token() == ASSIGN) v = expr(true);
return v;
}
case MINUS:
return -prim(true);
case LP:
{
double e = expr(true);
if (curr_tok != RP) return error(") expected");
get_token();
return e;
}
default:
return error("primary expected");
}
}
double term(bool get)
{
double left = prim(get);
for (;;) {
switch (curr_tok) {
case MUL:
left *= prim(true);
break;
case DIV:
if (double d = prim(true)) {
left /= d;
break;
}
default:
return left;
}
}
}
double expr(bool get)
{
double left = term(get);
for (;;)
switch (curr_tok) {
case PLUS:
left += term(true);
break;
case MINUS:
left -= term(true);
break;
default:
return left;
}
}
int main(int argc, char *argv[])
{
table["pi"] = 3.1415926535897932385;
table["e"] = 2.7182818284590452354;
while (cin) {
get_token();
if (curr_tok == END) break;
if (curr_tok == PRINT) continue;
cout<<expr(false)<<'\n';
}
system("PAUSE");
return no_of_errors;
}