注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

小木舟的博客

笔墨生活随想,记录似水年华。

 
 
 

日志

 
 

大一的C++大作业  

2009-03-14 11:13:02|  分类: 编程学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#include<iostream.h>
#include <stdlib.h>
#include<iomanip.h>
#include<conio.h>
#include<string.h>
#include<time.h>
#include<math.h>

const double eps=1e-5;

class Complex                    //复数类的定义
{public:
   Complex(double r=0,double i=0)
   {
    real=r;image=i;
   }
   Complex(double r)
   {
    real=r;image=0;
   }
   friend ostream & operator<<(ostream & output,Complex & com);
   friend istream & operator>>(istream & input,Complex & com);
   friend Complex operator+(Complex & com1,Complex & com2);
   friend Complex operator-(Complex & com1,Complex & com2);
   friend Complex operator*(Complex & com1,Complex & com2);
   friend Complex operator/(Complex & com1,Complex & com2);
   Complex operator+=(Complex & com);
   Complex operator-=(Complex & com);
   Complex operator*=(Complex & com);
   Complex operator++();
   Complex operator--();
   Complex conjugate_complex();
   double model();
   int operator>(Complex & com);
   int operator<(Complex & com);
   int operator!=(Complex & com);
   int operator==(Complex & com);
private:
double real,image;
};
                                                   
ostream & operator<<(ostream & output,Complex & com) //运算符的重载
{
if(com.image>0)
   output<<com.real<<"+"<<com.image<<"i";
   else
    if(fabs(com.image)<eps)
     output<<com.real;
    else
     if(fabs(com.real)<eps)
      output<<com.image<<"i";
     else
         output<<com.real<<com.image<<"i";
    return output;
}

istream& operator >> (istream & input , Complex & com)
{
com.image = com.real = 0;
cout << "请输入一个复数:";
char s[100];
input >> s;
int length = strlen(s);
int number = 0 , symbol = 1;   //number当前从字符串中提取出来的数字,初始化为0;symbol是n的符号;
for ( int k = 0 ; k < length ; k ++)
{
   if ( (s[k] < '0' || s[k] > '9')&&(s[k] != '+' && s[k] != '-' && s[k] != 'i'))
   {
    cout << "the input is wrong!" << endl;
    return input;
   }
}

for ( k = 0 ; k <length ; )
{
   if ( number != 0 && (s[k] == '-' || s[k] == '+'))
   {
    com.real = symbol * number;
    number = 0;
   }

   if ( s[k] == '-')
   {
    symbol = -1;
    k ++;
   }
   if ( s[k] == '+')
   {
    symbol = 1;
    k ++;
   }
   if ( s[k] == 'i')
   {
    if ( k != length -1)
    {
     cout << "the input is wrong!" << endl;
    }
    else
    {
     com.image = symbol * number;
    }
    break;
   }

   while ( s[k] > '0' && s[k] <= '9')
   {
    number = number * 10 + s[k] - '0';
    k++;
   }
}

if ( s[length - 1] != 'i' && number != 0)
{
   com.real = number * symbol;
}

return input;
}
Complex operator+(Complex & com1,Complex & com2)
{
double rr=com1.real+com2.real;
    double ii=com1.image+com2.image;
return Complex(rr,ii);
}
Complex operator-(Complex & com1,Complex & com2)
{
double rr=com1.real-com2.real;
    double ii=com1.image-com2.image;
return Complex(rr,ii);
}
Complex operator*(Complex & com1,Complex & com2)
{
double rr=com1.real*com2.real-com1.image*com2.image;
    double ii=com1.real*com2.image+com1.image*com2.real;
return Complex(rr,ii);
}
Complex operator/(Complex & com1,Complex & com2)
{
if(com2.real==0&&com2.image==0)
{
   cout<<"该除法没意义!"<<endl;
     return Complex(0,0);
}
   else
   {
    double rr=(com1.real*com2.real+com1.image*com2.image)/(com2.real*com2.real+com2.image*com2.image);
    double ii=(com1.real*com2.image+com1.image*com2.real)/(com2.real*com2.real+com2.image*com2.image);
    return Complex(rr,ii);
   }
}
Complex Complex::operator+=(Complex & com)
{
real=real+com.real;
image=image+com.image;
return *this;
}
Complex Complex::operator-=(Complex & com)
{
real=real-com.real;
image=image-com.image;
return *this;
}
Complex Complex::operator*=(Complex & com)
{
double real0=real*com.real-image*com.image;
double image0=real*com.image+image*com.real;
real=real0;
image=image0;
return *this;
}
Complex Complex::operator++()
{
real++;
image++;
return *this;
}
Complex Complex::operator--()
{
real--;
image--;
return *this;
}
Complex Complex::conjugate_complex() //复数的共轭复数
{
double rr=real;
double ii=-image;
return Complex(rr,ii);
}

double Complex::model()             //复数的模长
{
return sqrt(real*real+image*image);
}
int Complex::operator>(Complex & com)
{
if((*this).model()>com.model())
   return 1;
   else
    return 0;
}
int Complex::operator<(Complex & com)
{
if((*this).model()<com.model())
   return 1;
   else
    return 0;
}
int Complex::operator!=(Complex & com)
{
if(((*this).real!=com.real)&&((*this).image!=com.image))
   return 1;
   else
    return 0;
}
int Complex::operator==(Complex & com)
{
if(((*this).real==com.real)&&((*this).image==com.image))
   return 1;
   else
    return 0;
}

void Test()            //复数的随机运算小测验
{
cout<<endl;

cout<<"             *****欢迎进入复数的基本运算小测验*****\n\n";
cout << "===本次测验共有10道题,做100以内的简单复数加减乘除运算,满分为100分===\n\n" << endl;
double r1 , r2 , i1 , i2 ;
Complex answer , temp;
int score = 0;
char op;

for ( int i = 0 ; i < 10 ; i ++)
{

   r1 = rand() % 200 - 100;
   i1 = rand() % 200 - 100;
   r2 = rand() % 200 - 100;
   i2 = rand() % 200 - 100;
   Complex a(r1 , i1) , b(r2 , i2);
  
   op = rand() % 4;

   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;
   case 3:
    answer = a / b;
    cout <<"( "<< a << ")/(" << b << ")= " ;
    break;

   }
   cout<<endl;
   cin >> temp;
   if ( answer == temp)
   {
    score += 10;
cout<<"恭喜你做对了!"<<endl;
   }
   else
   {
    cout << "此题做错了!" << endl;
    cout <<"正确答案为:" << answer << endl;
   }
}

cout << "你的最后得分为: " << score << endl;
cout<<endl;
cout<<"=====该进程结束=====\n";
cout<<endl;
cout<<"==按任意键继续=="<<endl;
getch();
system("cls");
}

void addition()           //复数的加法运算函数
{
cout<<endl;
cout<<"             *****欢迎进入复数的加法运算模块*****\n\n";
cout<<"===说明:至少输入两个复数,并以输入错误(比如输入abc)结束该进程===\n";
cout<<"===如果一开始就连续三次输入错误,该进程将自动结束===\n";
cout<<endl;
Complex c1,c2,c3,sum,end(0,0);
   
cout<<"input the 1st complex: \n";
cin>>c1;
cout<<"input the 2nd complex: "<<endl;
cin>>c2;
sum=c1+c2;
cout<<"input the 3rd complex: "<<endl;
cin>>c3;
for(int i=4;c3!=end;i++)
{
   sum=sum+c3;
   cout<<"input the "<<i<<"th complex: "<<endl;
   cin>>c3;
  
}
   cout<<"最后结果是"<<sum<<endl;
   cout<<"=====该进程结束=====\n";
  
   cout<<endl;
  
   cout<<"==按任意键继续=="<<endl;
   getch();
   system("cls");
}
void substruction()      //复数的减法运算函数
{
cout<<endl;
cout<<"             *****欢迎进入复数的减法运算模块*****\n\n";
cout<<"===说明:至少输入两个复数,并以输入错误(比如输入abc)结束该进程===\n";
cout<<"===如果一开始就连续三次输入错误,该进程将自动结束===\n";
cout<<endl;
Complex c1,c2,c3,sum,end(0,0);
   
cout<<" input the 1st(也即被减数) complex: \n";
cin>>c1;
cout<<"===以下输入皆为减数===\n";
cout<<"input the 2nd complex: "<<endl;
cin>>c2;

sum=c1-c2;
cout<<"input the 3rd complex: "<<endl;
cin>>c3;

for(int i=4;c3!=end;i++)
{
   sum=sum-c3;
   cout<<"input the "<<i<<"th complex: "<<endl;
   cin>>c3;
  
}
   cout<<"最后结果是"<<sum<<endl;
   cout<<"=====该进程结束=====\n";
   cout<<endl;
  
   cout<<"==按任意键继续=="<<endl;
   getch();
   system("cls");
}
void multiplication()           //复数的乘法运算函数
{
cout<<endl;
cout<<"             *****欢迎进入复数的乘法运算模块*****\n\n";
cout<<"===说明:至少输入两个复数,并以输入错误(比如输入abc)结束该进程===\n";
cout<<"===如果一开始就连续三次输入错误,该进程将自动结束===\n";
cout<<endl;
Complex c1,c2,c3,sum,end(0,0);
   
cout<<" input the 1st complex: \n";
cin>>c1;
cout<<"input the 2nd complex: "<<endl;
cin>>c2;
sum=c1*c2;
cout<<"input the 3rd complex: "<<endl;
cin>>c3;

for(int i=4;c3!=end;i++)
{
   sum=sum*c3;
   cout<<"input the "<<i<<"th complex: "<<endl;
   cin>>c3;
  
}
   cout<<"最后结果是"<<sum<<endl;
   cout<<"=====该进程结束=====\n";
   cout<<endl;
  
   cout<<"==按任意键继续=="<<endl;
   getch();
   system("cls");
}
void division()             //复数的除法运算函数
{
cout<<endl;
cout<<"             *****欢迎进入复数的除法运算模块*****\n\n";
cout<<"===说明:至少输入两个复数,并以输入错误(比如输入abc)结束该进程===\n";
cout<<"===如果一开始就连续三次输入错误,该进程将自动结束===\n";
cout<<endl;
Complex c1,c2,c3,sum,end(0,0);
cout<<" input the 1st(也即被除数) complex: \n";
cin>>c1;
cout<<"===以下输入皆为除数===\n";
cout<<"input the 2nd complex: "<<endl;
cin>>c2;
    sum=c1/c2;
cout<<"input the 3rd complex: "<<endl;
cin>>c3;

for(int i=4;c3!=end;i++)
{
   sum=sum/c3;
   cout<<"input the "<<i<<"th complex: "<<endl;
   cin>>c3;
  
}
   cout<<"最后结果是"<<sum<<endl;
   cout<<"=====该进程结束=====\n";
   cout<<endl;
  
   cout<<"==按任意键继续=="<<endl;
   getch();
   system("cls");
}
void addself()          //复数的自加函数
{
cout<<endl;
cout<<"             *****欢迎进入复数的自加运算模块*****\n\n";
cout<<endl;
    Complex c;
    cin>>c;
++c;
cout<<"复数自加后的结果为: ";
cout<<c<<endl;
cout<<"=====该进程结束=====\n";
cout<<endl;
   
    cout<<"==按任意键继续=="<<endl;
    getch();
    system("cls");
}
void subself()         //复数的自减函数
{
cout<<endl;
cout<<"             *****欢迎进入复数的自减运算模块*****\n\n";
cout<<endl;
    Complex c;
    cin>>c;
--c;
cout<<"复数自减后的结果为: ";
cout<<c<<endl;
cout<<"=====该进程结束=====\n";
cout<<endl;
   
    cout<<"==按任意键继续=="<<endl;
    getch();
    system("cls");
}
void compare()        //复数的比较函数
{
cout<<endl;
cout<<"             *****欢迎进入复数的比较运算模块*****\n\n";
cout<<"==当输入复数的虚部是1时,请以1i的形式输入==\n";
cout<<endl;
Complex c1,c2;
cout<<"input the first complex: ";
cin>>c1;
cout<<"input the second complex: ";
cin>>c2;
if(c1==c2)
{
   cout<<"这两个复数相等!"<<endl;
}
else
   if(c1.model()>c2.model())
   {
    cout<<"第一个复数的模比第二个复数的模大!"<<endl;
   }
     else
      if(c1.model()<c2.model())
      {
       cout<<"第一个复数的模比第二个复数的模小!"<<endl;
      }
        else
     {
      cout<<"两个复数的模相等!"<<endl;
     }
    cout<<"=====该进程结束=====\n";
cout<<endl;
   
    cout<<"==按任意键继续=="<<endl;
    getch();
system("cls");
}
void modlength()          //复数的求模长函数
{
cout<<endl;
cout<<"             *****欢迎进入复数的求模长运算模块*****\n\n";
cout<<endl;
Complex c;
cin>>c;
cout<<"该复数的模长为: "<<c.model()<<endl;
    cout<<"=====该进程结束=====\n";
cout<<endl;
   
    cout<<"==按任意键继续=="<<endl;
    getch();
    system("cls");
}
void set_conjugate_complex()      //复数的求共轭复数函数
{
cout<<endl;
cout<<"             *****欢迎进入复数的求共轭复数运算模块*****\n\n";
cout<<endl;
Complex c;
cin>>c;
    cout<<"该复数的共轭复数为: "<<c.conjugate_complex()<<endl;
    cout<<"=====该进程结束=====\n";
cout<<endl;
    cout<<"==按任意键继续=="<<endl;
    getch();
    system("cls");
}
void main()
{
cout<<"    ***********        ***********\n";
cout<<"    ********简单复数计算器********\n";
cout<<"    ***********        ***********\n\n"
     <<"    ******作者:小木舟 软件(2)班\n"
   <<"    ******时间: 2008/5\n"
   <<"    ******版本:正式版\n\n\n"
        <<"==按任意键进入计算器=="<<endl;
   getch();
        system("cls");

cout<<"             **********这是一个简单的复数计算器**********\n\n\n"<<endl;
cout<<"===说明:请按给出的选项以及提示选择你所想要进行的运算===\n";
cout<<"====================================================================\n\n";

srand(time(0));
    char c;
do
{  
   cout<<endl;
   cout<<"====================================================================\n";
   cout << "\ta:复数运算小测试,赶快开始你的测验之旅吧" << endl;
        cout << "\tb:复数加法计算" << endl;
        cout << "\tc:复数减法计算" << endl;
        cout << "\td:复数乘法计算" << endl;
        cout << "\te:复数除法计算" << endl;
        cout << "\tf:复数自加运算" << endl;
        cout << "\tg:复数自减运算" << endl;
   cout << "\th:复数的比较" << endl;
        cout << "\ti:复数的求模长运算" << endl;
   cout << "\tj:复数的求共轭复数运算" << endl;
        cout << "\t0:退出计算器程序" << endl << endl;
       
   cout << "\t请输入你的选择,按回车键确认:";
  
   cin>>c;
   system("cls");
  
     switch(c)
     {
     case 'a':Test();
      break;
     case 'b': addition();
      break;
     case 'c': substruction();
      break;
     case 'd': multiplication();
      break;
     case 'e': division();
      break;
     case 'f': addself();
      break;
     case 'g': subself();
      break;
     case 'h': compare();
      break;
     case 'i': modlength();
      break;
     case 'j': set_conjugate_complex();
      break;
     case '0': cout<<endl
                <<"             ====退出计算器程序,欢迎再次使用===="<<endl;
      break;
     default:
      cout<<"输入错误,请重新输入!"<<endl;
     }


}while(c!='0');
cout<<endl;
}

  评论这张
 
阅读(474)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017