阅读背景:

做一个定时的四则运算系统---c++

来源:互联网 
//***********流程**************** /*3/9/2018 by yh site NFU 需求分析: 开始时界面显示自己想要训练的时间, 点击确定 然后出现题目, 全部都是100以内的自然数四则运算, 其中, 减法结果是非负数, 除法必须可以整除 时间到了以后, 作答终止 cmd 出现已经完成题目数量和正确率 有时间, 再加一条, 可以做一个训练系统, 能够重复训练并进行作答分析 设计思路: 先设计一个四则运算函数, 或者类; 查找c++ 类中可以用于控制时间的函数 最后做一个cmd 界面 */ /* 经过查阅资料, 发现一个time_t的类型; 头文件是time.h 用法, time_t t; time(t) 或者 t = time(NULL) 返回的是1970年1月1日00:00:00到系统现在的时间, 以秒计算。精度足够 */ /* 初始思路, 当时想建立3个栈,存储每一个等式的两个操作数和操作符。 然后用两个队列, 存储答案 后来发现在这里不需要, 如果这个系统进行进一步细化, 可以用栈, 便于分析做题结果。 */ /* 有一个易错点, 再频幕接收到回车键的时候, 换行和回车表示不同。 ascii 码值为13是回车, 不是换行。 这里应该用的是\n , 回车。 */ #pragma once #include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<windows.h> #include<iomanip> #include<time.h> using namespace std; typedef long long ll; int length, ans, x, y, tmp = -1, cnt = 0; // length 是自己要做题的时间, 以秒计算, x, y 是两个操作数, cnt 是做题的数量,ans 是正确答案, tmp 是自己输入的答案 char op;//操作符号 double percent = 0;// percent 是正确率 ll start, end; // start , end 分别是开始和结束的时间 void print_start() { cout<<"***********************************"; } void swap(int &x, int &y) { int tmp; tmp = x; x = y; y = tmp; } void calc() { x = 1 + rand() % 100; y = 1 + rand() % 100; int rd = 1 + rand()%4; switch(rd) { case 1: op = '+'; ans = x + y; break; case 2: if(x < y) { swap(x, y); } op = '-'; ans = x - y; break; case 3: op = '*'; ans = x * y; case 4: if(x < y) { swap(x, y); } op = '/'; ans = x / y; while(ans * y != x) { x = 1 + rand() % 100; y = 1 + rand() % 100; ans = x / y; } break; } cout<<x<<' '<<op<<' '<<y<<' '<<'='<<' '; } void Arithmetic() { char ch;//回车 time_t flag; time(&flag); ll end = start + length; //cout<<start<<endl; //cout<<end<<endl; if(end > (ll)flag) { calc(); cin>>tmp; // tmp 是自己对于这道题的答案; cnt ++; //做的题目数量+1 if(tmp == ans) { percent ++; } ch=getchar(); time(&flag); if(ch == '\n') { Arithmetic(); } } else return ; } void Window() { print_start(); cout<<"定时四则运算系统"; print_start(); cout<<endl<<endl; cout<<"请输入自己想要做题的时间, 以秒计算:"<<endl; cout<<endl; cin>>length; } void Result() { percent /= cnt; //得出正确率 percent *= 100; cout<<"您一共做了: "<<cnt<<" "<<"道题 "; cout<<"正确率为: "<<fixed<<setprecision(2)<<percent<<'%'<<endl<<endl; if(percent < 40) { cout<<"评价: "<<"小傻瓜! 别找了, 说的就是你!!!"<<endl; } else if(percent < 60) { cout<<"评价: "<<"离及格还差好大一截呢!!!"<<endl; } else if(percent < 80) { cout<<"评价: "<<"小伙子, 干的不错!!!"<<endl; } else if(percent < 100) { cout<<"评价: "<<"哇, da lao, 你离满分只有一步之遥"<<endl; } else { cout<<"评价: "<<endl <<"黄金白璧买歌笑, 一醉累月轻王侯 ~ ~ ~ ~ ~"<<endl<<endl <<" PERCENT 100 %, YOU ARE NUM ONE!!!"<<endl; } } int main() { char ch1; srand(time(NULL)); time_t t; Window(); ch1 = getchar(); if(ch1 == '\n') { time(&t); start = (ll)t; Arithmetic(); } Result(); } //***********流程**************** /*3/9/2018 by yh



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: