>temp;//选择类型getchar();//以下为整个程序的核心部分,以一个多" />
文档库 最新最全的文档下载
当前位置:文档库 › 计算器模拟系统

计算器模拟系统

//计算器.cpp
#include"计算器函数.h"
int main()
{
int n=0; //统计计算次数
char *temp=new char[];//字符数组,屏蔽输入过多字符
welcome(); //欢迎界面
//功能实现部分
while(1) //循环计算
{
if(n!=0)
system("cls");
for(int i=0;i<37;i++)//在屏幕上输出"┈"
cout<<"┈";
cout<<"\n\a【主菜单】: 1.实数 2.分数 3.看记录 4.退出程序\n"
<<"输入1、2、3、4选择:";
cin>>temp;//选择类型
getchar();

//以下为整个程序的核心部分,以一个多分枝语句为主体
switch(*temp)
{
case '1'://浮点型数据
n=shishu(n);//参数n为统计计算次数
break;
case '2'://数据分数型
n=fenshu(n);
break;
case '3'://查看记录
view();
break;
case '4'://退出程序
goout(n); //调用程序结束函数
default ://缺省
cout<<"你的选择无效。";
break;
}
}
return 0;//返回给操作系统
}//计算器.cpp结束


//计算器函数.h
#include
#include
#include//文件系统
#include
#include"分数类.h"
using namespace std;

//文件操作
ofstream outf("计算器操作记录.log",ios_base::app);

//时间操作,内联函数
inline timeout(int x) //x有两个参数:1,2
{
SYSTEMTIME time;
GetLocalTime(&time);
if(x==1) //到显示器
{
cout.fill('0');
cout<<"计算于:"<<cout.fill(' ');
}
else if(x==2) //到文件
{
outf<<<}
}
//显示欢迎界面
void welcome()
{
//制作载入效果
for(int i=0;i<=10;i++)
{
cout<<"\n\n\n\t\t\t系统 载入 中……"<system("cls");//刷新屏幕
}
cout<<"\n\n\n\t\t\t载入成功!!!\n";
Sleep(1000);//暂停1000毫秒
system("cls");

cout<cout<<" \t┏━━━━━━━━━━━━━━━━━━━━━━━┓"<cout<<" \t┃ 模拟计算器系统 ┃"<cout<<" \t┠───────────────────────┨"<
<<"欢迎使用!"<}
//强制退出程序的函数
inline goout(int n)
{
system("cls");
cout<<'\a';
for(int i=0;i<=6;i++)
{
cout<<"\n\n\n\t\t\t系统 退出 中……"<<15*i<<"%";
if(i==100)
Sleep(500);
system("cls");
}
cout<<"\n\n共计算了"<<<"\n谢谢使用\n\t程序即将【退出】……";
Sleep(3700);//延迟退出
exit(0); //程序结束函数
}
//模板函数
template
T compute(T a,char p,T b,bool&pr,int&n)
{ n++; //计算次数
switch(p)
{
case '+':return a+b;
case '-':return a-b;

case '*':return a*b;
case '/':return a/b;
default :
cout<<"运算符错误!!!请检查输入是否有误?\n";
pr=0; //pr置零,输出语句关闭
break;
}
return a;
}
//计算实数
int shishu(int n)
{
char p; //存放 +,-,*,/
bool pr=1; //输出结果开关,如果没有计算则不输出
double a,b,s;
system("cls");//刷新屏幕
do
{
cout<<"\a【实数】输入式子(示例:a+b):";
cin>>a>>p>>b;
cin.get();//读掉最后面的回车符
if(p=='/'&&b==0)
cout<<"被除数不能为 0 ,";
}
while(p=='/'&&b==0); //若分母为 0 ,则重新输入
s=compute(a,p,b,pr,n); //计算pr为输出开关n为计算次数


//个性化显示
if(pr==1)
{
cout<<"结果为:"
<<"┏*************┓\n"
<<<"\t┃"<<timeout(1);//写入时间到显示器
cout<<"\t┗*************┛\n";
//输出到文件:计算器操作记录.log
timeout(2);//写入时间到文件记录
outf<}
cout<<"即将返回 【主菜单】……";
Sleep(3500);//暂停3500毫秒
system("cls");//刷新屏幕
return n;//返回计算次数
}
//计算分数
int fenshu(int n)
{
char p; //存放 +,-,*,/
bool pr=1; //输出结果开关
FenShu A,B,C;
int c,d,e,f;
char *ptemp=new char[2];//存放分数线
system("cls");
do
{
cout<<"\a【分数】输入式子(示例:a/b+c/d):";
cin>>c>>*ptemp>>d>>p>>e>>*(ptemp+1)>>f;
cin.get();
if(d==0||f==0)
cout<<"分母不能为 0 ,";
}
while(d==0||f==0);

A.Setxy(c,d);B.Setxy(e,f);
C=compute(A,p,B,pr,n);//计算

if(pr==1)
{
//个性化显示
cout<<"结果为:"
<<"┏********┓\n"
<<"\t┃";
if(C.Gety()==1)//如果分母为1,则只输出分子
cout<else
cout<<
cout<timeout(1);
cout<<"\t┗********┛\n";
//输出到文件:计算器操作记录.log
timeout(2);
outf<}
cout<<"即将返回 【主菜单】……";
Sleep(3000);//暂停3000毫秒
system("cls");//刷新屏幕
return n;
}


//查看记录
int view()
{
char temp;
ifstream *inf=new ifstream("计算器操作记录.log",ios_base::in);
system("cls");
if((temp=(*inf).get())==EOF)
cout<<"\n我们没有找到相关记录!\n\t\t你是不是没有创建?\n";
else
{
cout<<"\a记录如下:\n";
for(int i=0;i<11;i++)//在屏幕上输出
cout<<"┈┬┈";
cout<while((temp=(*inf).get())!=EOF)
cout.put(temp);//读取数据

for(i=0;i<11;i++)
cout<<"┴┈┴";
(*inf).close();
cout<<"\n\t要删除记录,请按“D”:(回车返回)";
if((temp=cin.get())=='d'||temp=='D')
{
cout<<"请输入系统密码:";
char *temp=new char[11];
cin>>temp;//读取密码
if(temp[0]=='0'&&temp[1]=='1'&&temp[2]=='2')
{//判断密码是否正确
ofstream tout("计算器操作记录.log",ios_base::out);
tout.close();
system("cls");
cout<<"\n记录清除成功。\n\t即将返回 【主菜单】……\n";
Sleep(1000);
system("cls");
return 0;
}
else
{
cout<<"\n密码错误!\n\t即将返回【主菜单】……\n";
Sleep(1500);
system("cls");
return 0;
}
}
else
system("cls");
return 0;
}
cout<<"\n系统即将返回【主菜单】……";
Sleep(1500);
system("cls");
return 0;
}//计算器函数.h结束

//分数.h
//该头文件用于定义 分数类x/y
#include
using namespace std;//使用标准命名空间
class FenShu
{
private:
int x,y;
int GongYueShu(int xx,int yy); //求最大公约数
public:
FenShu(){}; //默认构造函数
FenShu(int xx,int yy) //带参数构造函数
{ x=xx;y=yy;}
//计算,操作符重载
FenShu operator + (FenShu p);
FenShu operator - (FenShu p);
FenShu operator * (FenShu p);
FenShu operator / (FenShu p);
//输出
int Getx(); //提取x
int Gety(); //提取y
//设置参数
void Setxy(int xx,int yy); //更改xy
};

//实现
int FenShu::GongYueShu(int xx,int yy) //求最大公约数
{
int i=xx>yy?yy:xx;
for(;i>0;i--)
if(xx%i==0&&yy%i==0)
return i;
return 0;
}
FenShu FenShu::operator + (FenShu p)
{ int m=y*p.y; //分母
int n=x*p.y+p.x*y; //分子
int s=GongYueShu(n,m);
return FenShu(n/s,m/s); //建立一个临时无名分数对象
}
FenShu FenShu::operator - (FenShu p)
{ int m=y*p.y; //分母
int n=x*p.y-p.x*y; //分子
if(n==0)
return FenShu(0,y);
int s=GongYueShu(abs(n),m);
return FenShu(n/s,m/s); //临时建立一个无名分数对象
}
FenShu FenShu::operator * (FenShu p)
{ int m=y*p.y;//分母
int n=x*p.x;//分子
int s=GongYueShu(n,m);
return FenShu(n/s,m/s);
}
FenShu FenShu::operator / (FenShu p)
{ int m=y*p.x;//分母
int n=x*p.y;//分子
int s=GongYueShu(n,m);
return FenShu(n/s,m/s);
}
//提取 x
int FenShu::Getx()
{ return x;}
//提取 y
int FenShu::Gety()
{ return y;}
//设置 xy
void FenShu::Setxy(int xx,int yy)
{ x=xx;y=yy;}
//分数类.h结束

//源程序结束

相关文档
相关文档 最新文档