📄 dosconter.txt
字号:
#include<iomanip>
#include<iostream>
#include<cmath>
#include<ctime>
#include<conio.h>
using namespace std;
//////////////////////////////////////
struct Jade{
int state;
char data;
Jade * next;
};
//////////////////////////////////////
class Needle{
Jade * head,*tail;int bit;
public:
Needle(){Set();}
void Set(){bit=0;}
void Start(void){ //生成头结点
head=new Jade;
tail=head;
tail->state=0;
}
void Create(char x){ //生成新节点,并将字符X放在该节点中
Jade * temp=new Jade;
temp->data=x;
temp->next=0;
if((int)x<48){
if(x!='.')temp->state=1;
else temp->state=0;
}
else temp->state=0;
tail->next=temp;
tail=temp;
++bit;
}
void Charge(void){ //获得输入的字符并存入链表
char wmA;
Start();
while(cin.get(wmA)&&(int)wmA!=10)
{
if((int)wmA<39||(int)wmA==44||(int)wmA>57)
continue;
else Create(wmA);
}
}
void show(){ //输出存储的计算式
Jade *p=head;
do{
p=p->next;
cout<<p->data;
}while(p->next);
}
Jade * Share(){ //共享链表头指针;
return head;
}
int Check(){ //检查是否有0作为除数
if(bit==0)return 0;
Jade *king=head;
while(king->next){
if(king->data=='/'&&king->next->data=='0')return 0;
king=king->next;
}
return 1;
}
~Needle(){
if(bit==0) exit(0);
Jade * p;
while(head){
p=head;
head=head->next;
delete p;
}
}
};
///////////////////////////////////////////////
struct Ball{
int feel;
double data;
char word;
Ball * qian,* hou;
};
///////////////////////////////////////////////
class Think{
Ball * bhead,* btail;
Jade * old;int rec;
public:
Think(){rec=0;}
void Check(){ //检查计算式中的除法运算是否可除尽,有着将除数变成可除尽的
Ball * xing=bhead->hou;int w;
while(xing->word!='e'){
if(xing->qian->word=='/'){ //寻找除号,调整除数与被除数
if(xing->qian->qian->data<xing->data){
w=xing->qian->qian->data;
xing->qian->qian->data=xing->data;
xing->data=w;
}
if((int)xing->qian->qian->data%(int)xing->data) //调整使之可以除尽
xing->qian->qian->data-=(int)xing->qian->qian->data%(int)xing->data;
}
xing=xing->hou;
}
}
void Enjoy(Jade * x){ //获得头指针
old=x;
}
void Start(){ //生成头结点
bhead=new Ball;
bhead->feel=0;
bhead->data=0;
bhead->word='s';
btail=bhead;
}
void Create(double x){ //生成新节点并将double存入节点
Ball * p=new Ball;
p->feel=0;
btail->hou=p;
p->qian=btail;
p->hou=0;
p->data=x;
p->word=0;
btail=p;
}
void Create(char x){ //生成新节点并将char存入节点
Ball * p=new Ball;
p->feel=1;
btail->hou=p;
p->qian=btail;
p->hou=0;
p->word=x;
p->data=0;
p->feel=1;
btail=p;
++rec;
}
double Shu(){ //从由Jade组成的链表中提取数字
double ll=0.000000;Jade * r;int point=0;
while(old->state==0){
if(old->data=='.'){ //检测小数点
point=1;
r=old;
old=old->next;if(old==0)break;
continue;
}
if(point==0){
ll=10*ll+old->data-48;
r=old;
old=old->next;if(old==0)break;
}
if(point){
double x=1.00000;
for(int zz=point;zz>0;zz--)x*=10.00000;
point++;
ll=ll+(old->data-48)/x;
r=old;
old=old->next;if(old==0)break;
}
if(old==0)break;
}
old=r;
return ll;
}
void End(){ //生成尾节点
bhead->hou->qian=bhead;
btail->hou=new Ball;
btail->hou->qian=btail;
btail->hou->feel=0;
btail->hou->word='e';
btail->hou->data=0;
}
void Translate(){ //将Jade组成的字符链表翻译成由Ball组成的链表
Start();
do{
old=old->next;
if(old->state)Create(old->data);
else Create(Shu());
}while(old->next);
End();
}
void Szys(Ball * at){ //用于四则运算的函数
double a,b;char ww;
at->feel=0;
a=at->qian->data;
b=at->hou->data;
ww=at->word;
if(at->qian->qian==0&&at->hou->hou==0){ //sA*Be类型的处理
delete at->qian;
delete at->hou;
bhead->hou->hou=at;
at->hou=0;
goto wee;
}
if(at->qian->qian==0&&at->hou->hou){ //sA*B...e类型的处理
Ball * p1=at->hou;
bhead->hou->hou=at;
delete at->qian;
at->hou->hou->qian=at->hou->qian;
at->hou=at->hou->hou;
delete p1;
goto wee;
}
if(at->qian->qian&&at->hou->hou==0){ //s...A*Be类型的处理
Ball * p2=at->qian;
at->qian->qian->hou=at->qian->hou;
at->qian=at->qian->qian;
delete p2;
goto wee;
}
if(at->qian->qian&&at->hou->hou){ //s...A*B...e类型的处理
Ball * p1=at->qian;
Ball * p2=at->hou;
at->qian->qian->hou=at->qian->hou;
at->qian=at->qian->qian;
at->hou->hou->qian=at->hou->qian;
at->hou=at->hou->hou;
delete p1;
delete p2;
goto wee;
}
wee: switch(ww){
case '+':at->data=a+b;at->word=0;break;
case '-':at->data=a-b;at->word=0;break;
case '*':at->data=a*b;at->word=0;break;
case '/':at->data=a/b;at->word=0;break;
}
}
void AnA(Ball * at){ //用于去除括号
Ball * p1=at->qian,* p2=at->hou;
at->qian->qian->hou=at->qian->hou;
at->qian=at->qian->qian;
at->hou->hou->qian=at->hou->qian;
at->hou=at->hou->hou;
delete p1;
delete p2;
}
int Seek(){ //检验链表中是否包含运算符
Ball * x=bhead;
while(x->hou->word!='e'){
if(x->feel>0)return (1);
x=x->hou;
}
return 0;
}
void Find(){ //检索各个运算符的位置并交与Szys()处理
Ball * io=bhead,* temple;
do{
io=io->hou;
if((io->qian->word=='s'||io->qian->word=='(')&&io->word=='-'&&io->hou->feel==0){ //如果+ - 挨着( )则不予理会
temple=io->hou;
io->feel=0;io->data=io->hou->data*(-1);io->word=0;
io->hou->hou->qian=io->hou->qian;
io->hou=io->hou->hou;
delete temple;
}
}while(io->word!='e');
io=bhead;
do{
io=io->hou;
if(io->qian&&io->qian->feel==0&&io->word=='/'&&io->hou&&io->hou->feel==0)Szys(io);//检索除法
}while(io->hou->word!='e');
io=bhead;
do{
io=io->hou;
if(io->qian&&io->qian->feel==0&&io->word=='*'&&io->hou&&io->hou->feel==0)Szys(io);//检索乘法
}while(io->hou->word!='e');
io=bhead;
do{
io=io->hou;
if(io->qian&&io->qian->word==0&&io->word=='-'&&io->hou&&io->hou->feel==0){ //检索加法
if(io->qian->qian->word=='s'){Szys(io);break;}
if(io->hou->hou->word=='e'){Szys(io);break;}
if(io->qian->qian->qian&&io->qian->qian->qian->word==')')continue;
if(io->hou->hou->hou&&io->hou->hou->hou->word=='(')continue;
Szys(io);
}
}while(io->hou->word!='e');
io=bhead;
do{
io=io->hou; //检验减法
if(io->qian->word==0&&io->word=='+'&&io->hou->feel==0){
if(io->qian->qian->word=='s'){Szys(io);break;}
if(io->hou->hou->word=='e'){Szys(io);break;}
if(io->qian->qian->qian&&io->qian->qian->qian->word==')')continue;
if(io->hou->hou->hou&&io->hou->hou->hou->word=='(')continue;
Szys(io);
}
}while(io->hou->word!='e');
io=bhead;
do{ //最后脱括号
io=io->hou;
if(io->qian->word=='('&&io->feel==0&&io->hou->word==')')AnA(io);
}while(io->hou->word!='e');
}
void Explorer(){ //以循环不断运算,脱括号,直到又剩下结果
while(Seek()){
Find();
rec--;
if(rec<0){ //循环次数若超过运算符的个数,则必然有无
cout<<endl<<"该算式有问题"<<endl;
goto wrong;
}
}
cout<<setprecision(17)<<"="<<bhead->hou->data<<endl;
wrong: cout<<endl;
}
int Explorer(int x){ //对Explorer的重载
while(Seek())Find();
return bhead->hou->data;
}
void Ask(){ //遍历输出算式
Ball * ask=bhead->hou;
while(ask->word!='e'){
if(ask->feel)cout<<ask->word;
else cout<<ask->data;
ask=ask->hou;
}
}
};
///////////////////////////////////////////////
class Tears{ //界面类,以下没必要注释
char light;
public:
void Face(){
cout<<'\4'<<" "<<"实 数 计 算 器"<<" "<<'\4'<<endl;
cout<<"------------------------"<<endl;
cout<<'\4'<<" "<<"正式版本"<<" "<<'\4'<<endl;
cout<<"------------------------"<<endl;
cout<<'\1'<<" "<<1<<" "<<"实数混合运算"<<" "<<'\1'<<endl;
cout<<'\2'<<" "<<2<<" "<<"开平方运算"<<" "<<'\2'<<endl;
cout<<'\3'<<" "<<3<<" "<<"温度转换"<<" "<<'\3'<<endl;
cout<<'\4'<<" "<<4<<" "<<"算术测试"<<" "<<'\4'<<endl;
cout<<'\5'<<" "<<5<<" "<<"关于本计算器"<<" "<<'\5'<<endl;
cout<<"------------------------"<<endl;
cout<<'\4'<<" "<<"其它任意键退出"<<" "<<'\4'<<endl<<endl;
light=getch();
switch(light){
case '1': DropDown();break;
case '2': Salty();break;
case '3': Translate();break;
case '4': Kiss();break;
case '5': Details();break;
default:exit(0);
}
}
void DropDown(){
Needle ass;
big: cout<<'\4'<<" "<<"输入您的计算式"<<" "<<'\4'<<endl;
ass.Charge();
Think me;
if(ass.Check())me.Enjoy(ass.Share());
else{
cout<<"没有检测到算式或者存在0为除数,请重新输入算式:"<<endl;
ass.Set();
goto big;
}
me.Translate();
ass.show();
me.Explorer();ass.Set();
cout<<'\3'<<" 1 继续计算 "<<'\3'<<endl;
cout<<'\4'<<" 2 返回目录 "<<'\4'<<endl;
cout<<'\5'<<" 3 退出程序 "<<'\5'<<endl;
light=getch();
switch(light){
case '1':goto big;break;
case '3':exit(0);break;
default:system("cls");Face();break;
}
}
void Salty(){
float num,x=1,tmp;
large: cout<<'\5'<<" 输入要开平方的数字"<<endl;
cin>>num;
if(num<0){cout<<'\2'<<" 被开放数不能为负! "<<'\2'<<endl<<endl;goto large;}
do
{
tmp=x;
x=(x+num/x)/2;
}while(fabs(x-tmp)>=1e-10);
cout<<"sqr("<<num<<")="<<' '<<x<<endl<<endl;
cout<<'\3'<<" 1 继续计算 "<<'\3'<<endl;
cout<<'\4'<<" 2 返回目录 "<<'\4'<<endl;
cout<<'\5'<<" 3 退出程序 "<<'\5'<<endl;
light=getch();
switch(light){
case '1':goto large;break;
case '3':exit(0);break;
default:system("cls");Face();break;
}
}
void Translate(){
tiny: cout<<1<<" 从摄氏度转化到华氏温度"<<endl<<2<<" 从华氏温度转化到摄氏度"<<endl<<endl;
char aa;aa=getch();
switch(aa){
case '1':double a;cout<<'\1'<<" 摄氏度:";cin>>a;cout<<'\1'<<" 转换后为:"<<a*9/5+32<<endl;break;
case '2':double b;cout<<'\2'<<" 华氏温度:";cin>>b;cout<<'\2'<<" 转换后为:"<<(b-32)*5/9<<endl<<endl;break;
default:cout<<"没有该选项"<<endl;
}
cout<<'\3'<<" 1 继续计算 "<<'\3'<<endl;
cout<<'\4'<<" 2 返回目录 "<<'\4'<<endl;
cout<<'\5'<<" 3 退出程序 "<<'\5'<<endl;
light=getch();
switch(light){
case '1':goto tiny;break;
case '3':exit(0);break;
default:system("cls");Face();break;
}
}
void Kiss(){
srand((unsigned)time( NULL ));
Think you; int line=0,chu=0,cheng=0,xu=1;
super: for(int i=1;i<11;i++){ //产生随机算式
you.Start();
for(int i=rand()%3;i<3;i++){
float a=1+rand()%20;you.Create(a);
while(1){
char j=42+rand()%6;
if(j==44||j==46)continue;
if(j==47&&chu){++chu;continue;}
if(j==42&&cheng){++cheng;continue;}
you.Create(j);break;
}
}
float a=1+rand()%20;you.Create(a);
you.End();
you.Check();cout<<'\3'<<" 第 "<<xu<<" 题 "<<'\3'<<endl<<" ";
you.Ask();
cout<<"结果是多少?"<<endl;
int ans;cout<<" ";cin>>ans;
if(ans==you.Explorer(1)){
cout<<'\2'<<" 好 棒 哦 ! "<<'\2'<<endl<<endl;
line+=10;
}
else {
cout<<'\3'<<" 呵呵,你没算对... "<<'\3'<<endl<<"!"<<" 正确答案";
you.Explorer();
}
++xu;
}
cout<<" 你的总分为"<<line<<endl;
cout<<'\3'<<" 1 继续计算 "<<'\3'<<endl;
cout<<'\4'<<" 2 返回目录 "<<'\4'<<endl;
cout<<'\5'<<" 3 退出程序 "<<'\5'<<endl;
light=getch();
switch(light){
case '1':goto super;break;
case '3':exit(0);break;
default:system("cls");Face();break;
}
}
void Details(){
cout<<'\1'<<" 本计算器采用链表存 "<<'\1'<<endl;
cout<<'\2'<<" 储并分析四则运算式, "<<'\2'<<endl;
cout<<'\3'<<" 支持 + - * / ( ) 运算"<<'\3'<<endl;
cout<<'\4'<<" 符,可用于实数运算。 "<<'\4'<<endl;
cout<<'\5'<<" 算式测试随机生成, "<<'\5'<<endl;
cout<<'\6'<<" 另外两个模块很简单, "<<'\6'<<endl;
cout<<'\1'<<" 就不多说了。 "<<'\1'<<endl;
cout<<'\2'<<" 注意开平方与温度转 "<<'\2'<<endl;
cout<<'\3'<<" 换模块没有使用过滤, "<<'\3'<<endl;
cout<<'\4'<<" 请不要输入字符。 "<<'\4'<<endl;
cout<<'\5'<<" 唉,懒得再写过滤, "<<'\5'<<endl;
cout<<'\6'<<" 就这样先吧。 "<<'\6'<<endl;
cout<<'\2'<<" 1 返回目录 "<<'\2'<<endl;
cout<<'\3'<<" 2 退出程序 "<<'\3'<<endl;
light=getch();
switch(light){
case '2':exit(0);break;
default:system("cls");Face();break;
}
}
};
///////////////////////////////////////////////
int main(){
Tears tear;tear.Face();return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -