累了,上代码(应该没人注意到我拖更了吧)
小框架:
#include<bits/stdc++.h>
#include<windows.h>
#include"fs.h"
using namespace std;
long double random(long double a,long double b,int n);
long double select(int n,long double a,long double b,long double c,long double d);
long double calculate(int grade);
size_t cd(const string& str);
void READ();
void SAVE();
void sj(int n,char a);
void start();
int changdu_shuzi(int n);
fs select(int n,fs a,fs b,fs c,fs d);
struct man{
string name,xb;
int jibie;
int dyc;
}yonghu;
//The main Function
int main()
{
return 0;
}
The Functions//
long double random(long double a,long double b,int n){
long double s=0;
double s1=1;
for(int i=1;i<=n;i++)
s1*=10;
srand(time(0));
s=rand()%int(b*s1-a*s1+1)+a*s1;
s=s/s1;
return s;
}
long double select(int n,long double a,long double b,long double c,long double d){
switch(n){
case 1:
return a;
break;
case 2:
return b;
break;
case 3:
return c;
break;
case 4:
return d;
break;
default:
return EOF;
break;
}
}
fs select(int n,fs a,fs b,fs c,fs d){
switch(n){
case 1:
return a;
break;
case 2:
return b;
break;
case 3:
return c;
break;
case 4:
return d;
break;
default:
return fs();
break;
}
}
long double calculate(int grade){
long double x=0,y=0,n=0;
long double l1=0,r1=0,l2=0,r2=0;
bool s[5]={};
int o=0,fp=0;
switch(grade){
case 1:
l1=l2=1;
r1=r2=100;
s[0]=s[1]=1;
fp=0;
break;
case 2:
l1=l2=0.1;
r1=100;
r2=10.0;
s[0]=s[1]=s[2]=s[3]=1;
fp=1;
break;
case 3:
l1=l2=0;
r1=200;
r2=20.0;
s[0]=s[1]=s[2]=s[3]=1;
fp=2;
break;
case 4:
l1=l2=-50;
r1=100;
r2=100.0;
s[0]=s[1]=s[2]=s[3]=1;
fp=3;
break;
}
x=select(random(1,2,0),random(l1,r1,0),random(l2,r2,fp),0,0);
Sleep(1000);
y=select(random(1,2,0),random(l1,r1,0),random(l2,r2,fp),0,0);
if(x<=0&&y<=0) y=-y;
do{
o=random(0,3,0);
}while(!s[o]);
switch(o){
case 0:
if(y>=0) cout<<x<<" + "<<y<<" = ?";
else cout<<x<<" + ("<<y<<") = ?";
n=x+y;
break;
case 1:
if(x<=y&&grade<=2) swap(x,y);
if(y>=0) cout<<x<<" - "<<y<<" = ?";
else cout<<x<<" - ("<<y<<") = ?";
n=x-y;
break;
case 2:
if(y>=0) cout<<x<<" × "<<y<<" = ?";
else cout<<x<<" × ("<<y<<") = ?";
n=x*y;
break;
case 3:
if(y==0) y=random(pow(10,-1*fp),select(random(1,2,0),r1,r2,0,0),fp);
if(y>=0) cout<<x<<" ÷ "<<y<<" = ?";
else cout<<x<<" ÷ ("<<y<<") = ?";
n=x/y;
if(int(n*pow(10,fp))/pow(10,fp)!=n) n=int(n*pow(10,fp))/pow(10,fp);
break;
}
cout<<endl;
return n;
}
void READ(){
ifstream in("用户档案.txt");
in >> yonghu.name >> yonghu.xb >> yonghu.jibie >> yonghu.dyc;
}
void SAVE(){
ofstream out("用户档案.txt");
out << yonghu.name << endl << yonghu.xb << endl << yonghu.jibie << endl << yonghu.dyc;
}
void sj(int n,char a){
for(int i=1;i<=n;i++){
putchar(a);
}
}
size_t cd(const string& str) {
size_t length = 0;
for (size_t i = 0; i < str.length(); ++i) {
if (str[i] <= 127) {
length += 1;
} else {
length += 2;
}
}
return length;
}
void start(){
READ();
if(yonghu.dyc==1){
cout<<"第一次来到这里,注册个账户吧!\n用户名:";
cin>>yonghu.name;
cout<<"性别:";
cin>>yonghu.xb;
while(yonghu.xb!="男" && yonghu.xb!="女"){
system("cls");
cout<<"第一次来到这里,注册个账户吧!\n用户名:";
cout<<yonghu.name;
cout<<"\n性别:";
cin>>yonghu.xb;
}
yonghu.jibie=1;
yonghu.dyc=0;
}
system("cls");
printf("----------------------------------------------------------------------------------------\n");
printf("-------------------------------------数学硬核小游戏-------------------------------------\n");
printf("姓名: ");
cout<<yonghu.name;
sj(cd("----------------------------------------------------------------------------------------")-cd("姓名:")-cd(yonghu.name),'-');
printf("\n性别:");
cout<<yonghu.xb;
sj(cd("----------------------------------------------------------------------------------------")-cd("姓名:")-cd(yonghu.xb),'-');
printf("\n等级:");
cout<<yonghu.jibie;
sj(cd("----------------------------------------------------------------------------------------")-cd("姓名:")-(yonghu.jibie>=10?2:1),'-');
printf("\n----------------------------------------------------------------------------------------\n");
printf("----------------------------------------------------------------------------------------\n");
}
int changdu_shuzi(int n){
if(n>=0&&n<=9) return 1;
if(n<0) return changdu_shuzi(-n)+1;
return changdu_shuzi(n/10)+1;
}
fs.h
#ifndef FS_H
#define FS_H
#include<bits/stdc++.h>
using namespace std;
class fs{
int fz,fm;
int d_fz,d_fm,d_zs;
double shuzhi;
private:
void zh(int n){//j->d:1 d->j:2
if(n==1){
d_fz=fz%fm;
d_zs=fz/fm;
d_fm=fm;
shuzhi=double(fz)/fm;
}else{
fz=d_fz+d_zs*d_fm;
fm=d_fm;
shuzhi=double(fz)/fm;
}
}
public:
fs(){
shuzhi=fz=d_fz=d_fm=d_zs=0;
fm=d_fm=1;
}
fs(const fs& a){
this->fz=a.fz;
this->fm=a.fm;
this->zh(1);
}
fs(int fz,int fm){
this->fz=fz;
this->fm=fm;
this->zh(1);
}
fs(int dfz,int dfm,int dzs){
this->d_fm=dfm;
this->d_fz=dfz;
this->d_zs=dzs;
this->zh(2);
}
fs(double sz){
this->shuzhi=sz;
double x=sz;int p=0;
do{
x*=10;
p++;
}while(fabs(int(sz)-sz)<0.000000001);
fm=pow(10,p);
fz=x;
this->zh(1);
}
void jfs_out(){
cout<<"("<<fz<<"/"<<fm<<")";
}
void dfs_out(){
cout<<"("<<d_zs<<"."<<d_fz<<"/"<<d_fm<<")";
}
void sz_out(){
cout<<shuzhi;
}
void jfs_input(){
scanf("(%d/%d)",&fz,&fm);
zh(1);
}
void dfs_input(){
scanf("(%d.%d/%d)",&d_zs,&d_fz,&d_fm);
zh(2);
}
void yf(){
int r=gcd(fz,fm);
fz/=r;
fm/=r;
zh(1);
}
void tf(fs &a,fs &b){
int r=lcm(a.fm,b.fm);
a.fz*=(r/a.fm);
b.fz*=(r/b.fm);
a.fm=b.fm=r;
}
fs operator+(fs other){
return fs(this->fz*other.fm+this->fm*other.fz,this->fm*other.fm);
}
fs operator-(fs other){
return fs(this->fz*other.fm-this->fm*other.fz,this->fm*other.fm);
}
fs operator*(fs other){
return fs(this->fz*other.fz,this->fm*other.fm);
}
fs operator/(fs other){
return fs(this->fz*other.fm,this->fm*other.fz);
}
fs operator=(fs other){
this->fz=other.fz;
this->fm=other.fm;
this->zh(1);
return other;
}
fs operator+=(fs other){
*this=other+*this;
return *this;
}
fs operator-=(fs other){
*this=*this-other;
return *this;
}
fs operator*=(fs other){
*this=other*(*this);
return *this;
}
fs operator/=(fs other){
*this=(*this)/other;
return *this;
}
bool operator==(fs other){
fs a1,a2,function;
a1=*this;
a2=other;
function.tf(a1,a2);
return a1.fz==a2.fz;
}
bool operator<(fs other){
fs a1,a2,function;
a1=*this;
a2=other;
function.tf(a1,a2);
return a1.fz<a2.fz;
}
bool operator<=(fs other){
fs a1,a2,function;
a1=*this;
a2=other;
function.tf(a1,a2);
return a1.fz<=a2.fz;
}
bool operator!=(fs other){
fs a1,a2,function;
a1=*this;
a2=other;
function.tf(a1,a2);
return a1.fz!=a2.fz;
}
bool operator>(fs other){
fs a1,a2,function;
a1=*this;
a2=other;
function.tf(a1,a2);
return a1.fz>a2.fz;
}
bool operator>=(fs other){
fs a1,a2,function;
a1=*this;
a2=other;
function.tf(a1,a2);
return a1.fz>=a2.fz;
}
double this_fs_internal_data(int code){
switch (code) {
case 1:
return fz;
case 2:
return fm;
case 3:
return d_zs;
case 4:
return d_fz;
case 5:
return d_fm;
case 6:
return shuzhi;
default:
return EOF;
break;
}
}
};
#endif
注:fs.h保存时一定要在下方选上头文件!!!!↓
下一章: