#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <ctime>
#define EPS 1e-5
using namespace std;
namespace NameCComplex
{
class CComplex
{
private:
double Real , Image;
public:
CComplex(double real = 0 , double image = 0)
{
Real = real;
Image = image;
}
friend istream& operator>>(istream& is , CComplex& com);
friend ostream& operator<<(ostream& os , CComplex& com);
CComplex operator+(CComplex& com);
CComplex operator-(CComplex& com);
CComplex operator*(CComplex& com);
CComplex operator+=(CComplex& com);
CComplex operator-=(CComplex& com);
CComplex operator*=(CComplex& com);
CComplex operator++();
CComplex operator--();
double mod(void);
int operator>(CComplex& com);
int operator<(CComplex& com);
int operator!=(CComplex& com);
int operator==(CComplex& com);
};
struct User
{
char szName[20]; //用户名
int nTime; //使用次数
int nTest; //测试次数
double dlAve; //平均成绩
int nAdd; //加法次数
int nSub; //减法次数
int nMul; //乘法次数
double dlScore[3]; //三次测试得分
} user;
CComplex CComplex::operator++()
{
Real ++;
Image ++;
return *this;
}
CComplex CComplex::operator--()
{
Real --;
Image --;
return *this;
}
double CComplex::mod()
{
return Real * Real + Image * Image;
}
int CComplex::operator>(CComplex& com)
{
if (this->mod() > com.mod())
{
return 1;
}
else
{
return 0;
}
}
int CComplex::operator<(CComplex& com)
{
if ( this->mod() < com.mod())
{
return 1;
}
else
{
return 0;
}
}
int CComplex::operator!=(CComplex& com)
{
if (*this == com)
{
return 0;
}
else
{
return 1;
}
}
istream& operator >> (istream & is , CComplex& com)
{
cout << "输入复数:";
char s[80];
is >> s;
int len = strlen(s);
int n = 0 , sign = 1;//n当前从字符串中提取出来的数字,初始化为0;sign是n的符号;
com.Image = com.Real = 0;
for ( int k = 0 ; k < len ; k ++)
{
if ((s[k] < '0' || s[k] > '9') && (s[k] != '+' && s[k] != '-' && s[k] != 'i'))
{
cout << "error" << endl;
return is;
}
}
for ( k = 0 ; k <len ; )
{
if ( n != 0 && (s[k] == '-' || s[k] == '+'))
{
com.Real = sign * n;
n = 0;
}
if ( s[k] == '-')
{
sign = -1;
k ++;
}
if ( s[k] == '+')
{
sign = 1;
k ++;
}
if ( s[k] == 'i')
{
if ( k != len -1)
{
cout << "error" << endl;
}
else
{
com.Image = sign * n;
}
break;
}
while ( s[k] > '0' && s[k] <= '9')
{
n = n * 10 + s[k] - '0';
k++;
}
}
if ( s[len - 1] != 'i' && n != 0)
{
com.Real = n * sign;
}
return is;
}
ostream& operator << (ostream& os , CComplex& com)
{
if ( fabs(com.Image) < EPS)
{
os << com.Real;
}
else if ( (fabs(com.Real) < EPS))
{
os << com.Image << "i";
}
else if (com.Image > 0)
{
os << com.Real << "+" << com.Image << "i";
}
else
{
os << com.Real << com.Image << "i";
}
return os;
}
CComplex CComplex::operator + (CComplex& com)
{
CComplex sum;
sum.Real = this->Real + com.Real;
sum.Image = this->Real + com.Image;
return sum;
}
CComplex CComplex::operator - (CComplex& com)
{
CComplex sub;
sub.Real = this->Real - com.Real;
sub.Image = this->Image - com.Image;
return sub;
}
CComplex CComplex::operator * (CComplex& com)
{
CComplex multi;
multi.Real = this->Real * com.Real - this->Image * com.Image;
multi.Image = this->Real * com.Image + this->Image * com.Real;
return multi;
}
CComplex CComplex::operator += (CComplex& com)
{
this->Real = this->Real + com.Real;
this->Image = this->Image + com.Image;
return *this;
}
CComplex CComplex::operator -= (CComplex& com)
{
this->Real = this->Real - com.Real;
this->Image = this->Image - com.Image;
return *this;
}
CComplex CComplex::operator *= (CComplex& com)
{
double nReal = this->Real * com.Real - this->Image * com.Image;
double nImage = this->Real * com.Real + this->Image * com.Real;
this->Real = nReal;
this->Image = nImage;
return *this;
}
int CComplex::operator == (CComplex& com)
{
if ( this->Real == com.Real && this->Image == com.Image)
{
return 1;
}
else
{
return 0;
}
}
void Test(void)
{
user.nTest ++;
cout << "共10道题,作10以内的加减运算,满分100分:" << endl;
double real1 , real2 , image1 , image2 , real3 , real4 ,image3 , image4;
CComplex answer , temp;
int score = 0;
char op;
for ( int i = 0 ; i <= 9 ; i ++)
{
real1 = rand() % 200 - 100;
image1 = rand() % 200 - 100;
real2 = rand() % 200 - 100;
image2 = rand() % 200 - 100;
CComplex a(real1 , image1) , b(real2 , image2);
real3 = rand() % 20 - 10;
image3 = rand() % 20 - 10;
real4 = rand() % 20 - 10;
image4 = rand() % 20 - 10;
CComplex c(real3 , image3) , d(real4 , image4);
op = rand() % 3;
switch ( op )
{
case 0:
answer = a + b;
cout << a << "加上" << b << "等于" ;
break;
case 1:
answer = a - b;
cout << a << "减去" << b << "等于" ;
break;
case 2:
answer = a * b;
cout << a << "乘以" << b << "等于" ;
break;
}
cin >> temp;
if ( answer == temp)
{
score += 10;
}
else
{
cout << "此题做错了!" << endl;
cout <<"正确答案为:" << answer << endl;
}
}
cout << "你的最后得分为: " << score << endl;
if (user.nTest <= 3)
{
user.dlAve = 0;
user.dlScore[user.nTest - 1] = score;
for ( int i = 0 ; i < user.nTest ; i ++)
{
user.dlAve += user.dlScore[i];
}
user.dlAve = user.dlAve / user.nTest;
}
else
{
user.dlScore[0] = user.dlScore[1];
user.dlScore[1] = user.dlScore[2];
user.dlScore[2] = score;
for ( i = 0 ,user.dlAve = 0 ; i < 3 ; i ++)
{
user.dlAve += user.dlScore[i];
}
user.dlAve = user.dlAve / 3;
}
cout << "按任意键继续" << endl;
cout.flush();
cin.get();
cin.get();
}
void Add()
{
user.nAdd ++;
CComplex num1 , num2 , sum , zero(0 , 0);
cout << "加法计算" << endl << "最少输入两个复数,并且以零结束" << endl;
cout << "第一个复数:";
cin >> num1;
cout << "第二个复数:";
cin >> num2;
sum = num1 + num2;
cout << "第三个复数:";
cin >> num1;
int i = 4;
while ( !(num1 == zero))
{
sum = sum + num1;
cout << "第" << i << "个复数:";
cin >> num1;
i ++;
}
cout << "结果是:" << sum << endl;
cout << "按任意键继续" << endl;
cout.flush();
cin.get();
cin.get();
}
void Sub()
{
user.nSub ++;
CComplex num1 , num2 , sub , zero(0 , 0);
cout << "加法计算" << endl << "最少输入两个复数,并且以零结束" << endl;
cout << "第一个复数:";
cin >> num1;
cout << "第二个复数:";
cin >> num2;
sub = num1 - num2;
cout << "第三个复数:";
cin >> num1;
int i = 4;
while ( !(num1 == zero))
{
sub = sub + num1;
cout << "第" << i << "个复数:";
cin >> num1;
i ++;
}
cout << "结果是:" << sub << endl;
cout << "按任意键继续" << endl;
cout.flush();
cin.get();
cin.get();
}
void Mul()
{
user.nMul ++;
CComplex num1 , num2 , mul , zero(0 , 0);
cout << "加法计算" << endl << "最少输入两个复数,并且以零结束" << endl;
cout << "第一个复数:";
cin >> num1;
cout << "第二个复数:";
cin >> num2;
mul = num1 * num2;
cout << "第三个复数:";
cin >> num1;
int i = 4;
while ( !(num1 == zero))
{
mul = mul * num1;
cout << "第" << i << "个复数:";
cin >> num1;
i ++;
}
cout << "结果是:" << mul << endl;
cout << "按任意键继续" << endl;
cout.flush();
cin.get();
cin.get();
}
void Add1()
{
user.nAdd ++;
CComplex num1;
cin >> num1;
num1 ++;
cout << "自加结果为:" << num1 << endl;
cout << "按任意键继续" << endl;
cout.flush();
cin.get();
cin.get();
}
void Sub1()
{
user.nSub ++;
CComplex num1;
cin >> num1;
cout << "自减的结果为:" << num1 << endl;
cout << "按任意键继续" << endl;
cout.flush();
cin.get();
cin.get();
}
void Compare()
{
CComplex num1 , num2;
cout << "输入两个复数" << endl;
cout << "第一个复数为:";
cin >> num1;
cout << "第二个复数为:";
cin >> num2;
if ( num1 == num2)
{
cout << "这两个复数相等" << endl;
}
else if ( num1 > num2)
{
cout << num1 << "的模大于" << num2 << "的模" << endl;
}
else if ( num1 < num2)
{
cout << num1 << "的模小于" << num2 << "的模" << endl;
}
else
{
cout << "这两个复数的模相等" << endl;
cout << "按任意键继续" << endl;
}
cin.get();
cin.get();
}
void userprint()
{
cout << user.szName << "使用的次数为:" << user.nTime << endl;
cout << "其中:\t加法次数:" << user.nAdd << "\t减法次数:" << user.nSub <<
"\t乘法次数:" << user.nMul << endl;
cout << "\t测试次数:" << user.nTest << "\t平均成绩:" << user.dlAve << endl;
}
void Login()
{
char szName[20];
cout << "请输入你的姓名:";
cin.getline(szName , 20);
ifstream infile;
User user1;
infile.open("user.dat" , ios::binary || ios::in);
if (!infile)
{
cout << "没有原始记录文件, 你是第一位用户" << endl;
strcpy(user.szName ,szName);
user.nTime ++;
return;
}
infile.read((char*)&user1 , sizeof(User));
while(!infile.eof())
{
if (strcmp(user1.szName , szName) == 0)
{
user = user1;
user.nTime ++;
cout << "欢迎再次使用复数计算器";
userprint();
cin.get();
infile.close();
return ;
}
infile.read((char*)&user1 ,sizeof(User));
}
cout << "欢迎使用复数计算器";
strcpy(user.szName , szName);
user.nTime ++;
infile.close();
return;
}
void SaveFile()
{
userprint();
fstream file;
User user1;
file.open("user.dat" , ios::binary || ios::out);
if (!file)
{
cout << "文件打开错误,不能将记录更新" << endl;
return;
}
file.seekp(0 , ios::beg);
while(!file.eof())
{
file.read((char*)&user1 , sizeof(User));
if (strcmp(user1.szName , user.szName) == 0)
{
file.seekp(-(sizeof(User)) , ios::cur);
file.write((char*)&user , sizeof(User));
file.close();
return;
}
}
file.close();
fstream outfile;
outfile.open("user.dat" , ios::binary || ios::app);
outfile.write((char*)&user , sizeof(User));
outfile.close();
return;
}
}
using namespace NameCComplex;
int main(void)
{
srand(time(NULL));
Login();
char strChoice[20];
do {
system("cls");
cout << "\t这是一个简单的复数计算器程序,可以实现以下功能,请按对应的键进入\n\n\n";
cout << "======================================================================================" << endl;
cout << "\t1:多复数加法,以0结束" << endl;
cout << "\t2:多复数减法,以0结束" << endl;
cout << "\t3:测试100以内的复数加减乘法运算,1次测试10道题" << endl;
cout << "\t4:多复数乘法,以0结束" << endl;
cout << "\t5:复数自加" << endl;
cout << "\t6:复数自减" << endl;
cout << "\t7:复数比较" << endl;
cout << "\t0:退出计算器程序" << endl << endl;
cout << "\t请输入你的选择:";
cin >> strChoice;
if (strcmp(strChoice , "1") == 0)
{
Add();
}
else if (strcmp(strChoice , "2") == 0)
{
Sub();
}
else if (strcmp(strChoice , "3") == 0)
{
Test();
}
else if (strcmp(strChoice , "4") == 0)
{
Mul();
}
else if (strcmp(strChoice , "5") == 0)
{
Add1();
}
else if (strcmp(strChoice , "6") == 0)
{
Sub1();
}
else if (strcmp(strChoice , "7") == 0)
{
Compare();
}
else if (strcmp(strChoice , "0") == 0)
{
cout << "\n\n\t欢迎下次继续使用复数计算器" << endl << endl;
break;
}
else
{
cout << "\n\t输入错误,请按任意键继续后继续输入" << endl << endl;
cin.get();
cin.get();
}
} while((strcmp(strChoice , "0")));
SaveFile();
return 0;
}