计算器实现
来源:互联网 发布:(,) 矩阵 编辑:程序博客网 时间:2024/06/15 19:11
小。。。小。。。小练习。。。
用c++实现计算器加减乘除基本运算和向量运算
vector.h文件:
#include<iostream>#include<stdlib.h>using namespace std;class Vector//定义一个Vector类{ public: Vector(double x=0.0,double y=0.0):x(x),y(y){}//Vector的含参构造函数 Vector operator+(Vector &c);//运算符重载+ Vector operator-(Vector &c);//运算符重载- Vector operator*(Vector &c);//运算符重载* void display()//输出向量 { cout<<"Vector:("<<x<<','<<y<<')'<<endl; } private: double x;double y;//Vector的两个私有成员};Vector Vector::operator+(Vector &c){ return Vector(x+c.x,y+c.y);}Vector Vector::operator -(Vector &c){ return Vector(x-c.x,y-c.y);}Vector Vector::operator*(Vector &c){ cout<<x*c.x+y*c.y<<endl;}
complex.h文件
#include<iostream>using namespace std;class Complex//定义一个Complex类{ public: Complex(){}//Complex类中无参构造函数 Complex(int x,int y):x(x),y(y){}//Complex类中含参构造函数 Complex operator+(Complex &c1);//运算符重载+ Complex operator-(Complex &c1);//运算符重载- int display()//输出复数 { cout<<x<<'+'<<y<<'i'<<endl; } private: int x,y;//定义两个私有成员};Complex Complex::operator+(Complex &c1){ return Complex(x+c1.x,y+c1.y);}Complex Complex::operator-(Complex &c1){ return Complex(x-c1.x,y-c1.y);}
calculate.h文件
#include<iostream>#include<string.h>#include<cstdio>using namespace std;double jjcc(double st[],int k)//定义一个函数jjcc用于计算数组中的四则运算{ for(int j=1;j<k;j++)//对数组中的每一个进行循环 { if(st[j]=='*'||st[j]=='/')//判断数组中是否含有*或/ { if(st[j]=='*')//若含有*,进行*运算,否则进行/运算 st[j-1]=st[j-1]*st[j+1]; else st[j-1]=st[j-1]/st[j+1]; int y=j; int n=0; while(n<k-j-2) { st[y]=st[y+2]; y++; n++; } k=k-2; j--; } } double sum=st[0]; for(int m=1;m<k-1;m++)//在循环中含有+,进行求和 if(st[m]=='+') sum=sum+st[m+1]; else if(st[m]=='-') sum=sum-st[m+1]; return sum;}
主函数文件
#include<iostream>#include<string.h>#include<cstdio>#include<stdlib.h>#include"Complex.h"#include"Vector.h"#include"calculate.h"using namespace std;void menu() //menu函数,输出的主界面,包含有整个工程的所有功能模块{ cout<<"|============================================|"<<endl; cout<<"| 计算器 |"<<endl; cout<<"|============================================|"<<endl; cout<<"| |"<<endl; cout<<"| 所有功能: |"<<endl; cout<<"| <1> 简单数值的加减乘除 |"<<endl; cout<<"| <2> 两复数运算 |"<<endl; cout<<"| <3> 两矢量运算 |"<<endl; cout<<"| <4> 退出 |"<<endl; cout<<"| 请选择服务项目 |"<<endl; cout<<"|+------------------------------------------+|"<<endl;}double jjcc(double st[],int k);int calculate()//进行四则运算的函数{ system("cls");//清屏 int sum1=0; int i; int c=0; cout<<"请输入算式最后以”=“结束"<<endl; char st1[100]; double st2[100],k,sum2; getchar(); gets(st1);//输入st1 { for(int i=0;st1[i]!='=';) { sum2=0; k=1; if(st1[i]>47&&st1[i]<58) { sum1=(st1[i]-48)+sum1*10; i++; if(st1[i]=='.') { i++; while(st1[i]>47&&st1[i]<58) { k=k*0.1; sum2=(st1[i]-48)*k+sum2; i++; } } st2[c]=sum1+sum2; } else { st2[++c]=st1[i]; i++; sum1=0; c++; } } cout<<jjcc(st2,c+1)<<endl; } menu();}int vectorcalculate()//矢量运算的函数{ system("cls"); double a,b,c,d; while(true)//循环输入两个向量的坐标 { cout<<"两矢量运算!"<<endl; cout<<"请输入两点坐标:"<<endl; cout<<"A点:x="; cin>>a; cout<<" y="; cin>>b; cout<<"B点:x="; cin>>c; cout<<" y="; cin>>d; { Vector c1(a,b);//定义vector的两个对象 Vector c2(c,d); cout<<"你所输入的两矢量坐标为:"<<endl; c1.display();//调用display函数 c2.display(); cout<<"你所要进行的运算是:+......or......-......or......*"<<endl; Vector c; char q,p,l; cin>>q>>p>>l;//输入所要进行的操作 { if(p=='+'&&((q=='A'&&l=='B')||(q=='B'&&l=='A'))) { c=c1+c2; c.display(); } else if(p=='-'&&((q=='A'&&l=='B')||(q=='B'&&l=='A'))) { c=c1-c2; c.display(); } else if(p=='*'&&((q=='A'&&l=='B')||(q=='B'&&l=='A')))c=c1*c2; else cout<<"error"<<endl; } cout<<"是否继续进行运算?y.....or.....n?"<<endl; char z; cin>>z; if(z=='n')break; } } system("cls"); menu();}int complexclaculate()//定义一个函数进行复数运算{ system("cls"); while(true)//循环输入复数 { cout<<"请输入两个复数:"<<endl; int a,b,c,d; cout<<"复数A:实部为:"; cin>>a; cout<<" 虚部为:"; cin>>b; cout<<"复数B:实部为:"; cin>>c; cout<<" 虚部为:"; cin>>d; { Complex c1(a,b),c2(c,d),c3;//定义3个复数 cout<<"你所输入的两个复数为:"<<endl; c1.display();//调用display()函数 c2.display(); char q,p,l; cout<<"你所要进行的运算是+.....or......-"<<endl; cin>>q>>p>>l; {if(p=='-'&&((q=='A'&&l=='B')||(q=='B'&&l=='A'))) { c3=c1-c2; c3.display(); } else if(p=='+'&&((q=='A'&&l=='B')||(q=='B'&&l=='A'))) { c3=c1+c2; c3.display(); } else cout<<"error!"<<endl; } cout<<"是否继续进行运算?y.....or.....n?"<<endl; char z; cin>>z; if(z=='n')break; } } system("cls"); menu();}void tuichu()//退出功能,在退出之前询问是否确定退出,倘若确定,则退出。{ char a; system("cls"); cout<<"确定退出吗?y......or.......n"<<endl; while(cin>>a) { if(a=='y') { cout<<"按任意键关闭窗口"<<endl; exit(0); } else { menu(); int b; while(cin>>b) { switch(b) { case 1:calculate();break; case 2:complexclaculate();break; case 3:vectorcalculate();break; case 4:tuichu();break; } } } }}int main() //主函数中调用menu函数,输出界面,然后根据用户键盘输入来选择相应的功能。{ cout<<"你好!!!<<< user >>>"<<endl<<endl<<endl; int choice; while(true) { menu(); while(cin>>choice) { switch(choice) { case 1:calculate();break; case 2:complexclaculate();break; case 3:vectorcalculate();break; case 4:tuichu();break; default:cout<<"你所选择的项目不存在,请重新输入。。。"<<endl; } } }}运行截图。。。。
阅读全文
0 0
- 计算器实现
- 计算器实现
- 实现计算器
- 计算器简易实现
- c语言实现计算器
- 计算器c++实现
- 计算器的实现
- c#实现科学计算器
- JS实现科学计算器
- c++实现计算器
- 实现计算器--框体
- 计算器c#实现
- java实现计算器
- javascript实现简易计算器
- 计算器功能简单实现
- C#实现计算器代码
- html实现计算器
- 使用JSF实现计算器
- hdu 3487 Play with Chain splay 区间翻转,插入,删除
- BZOJ[4896] Trie
- 【LeetCode】217.Contains Duplicate解题报告
- Android进程间通信-AIDL
- 215. Kth Largest Element in an Array
- 计算器实现
- SpringMVC 入门项目,非注解
- 2017.6.4 problem b 失败总结
- 点击tableView后面的button,通过响应者链条原理实现
- 基本聚类算法
- Zookeeper 数据与存储
- ISO 8583报文
- CSS常见兼容性问题及解决方法
- spark2.0 AFTSurvivalRegression算法