四则运算3

我和秦冰超一组。秦冰超的博客地址 http://www.cnblogs.com/qinxian0/

设计思路:1.将题目的难度属性放在数组中,并赋予初值。

         2.定义四则运算的数据结构。

         3.利用链表判断题目是否重复。

         4.调用jisuan.h中的answer判断答案是否正确

         5.利用rand%4函数表示运算符。0+,1-,2*,3/

#include<iostream>
#include "stdlib.h"
#include "time.h"
#include "jisuan.h"
#include "iomanip"
using namespace std;
//判断回答是否正确
bool YorN(jisuan &Q, int answer)
{
    if (Q.yunsuanfu == 0)//yunsaunfu为运算符0+,1-,2*,3/
    {
        Q.answer = Q.num1 + Q.num2;
    }
       else if (Q.yunsuanfu == 1)
             {
                 Q.answer = Q.num1 - Q.num2;
             }
       else if (Q.yunsuanfu == 2)
             {
                 Q.answer = Q.num1*Q.num2;
             }
         else
                 Q.answer = Q.num1 / Q.num2;
         if (answer == Q.answer)
                 return true;
         else
                 return false;
     }
//生成算术题
void Build(jisuan &Q)
{
    if (tideshuxing[2] == 1)//有乘除
        Q.yunsuanfu = rand() % 4;
    else//没有乘除
        Q.yunsuanfu = rand() % 2;
        Q.num1 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];//生成在取值范围间的随机数
        Q.num2 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];
}
//输出函数
void Display1(LinkC C, jisuan &Q)
{
    int temp, count = 0;                                    //count记录重新生成题目的次数
    for (int i = 1; i <= tideshuxing[1]; i++)
    {
        cout << "(" << i << ")";
        if (tideshuxing[2] == 1)
            Q.yunsuanfu = rand() % 4;                          
        else
            Q.yunsuanfu = rand() % 2;
        Q.num1 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];       
        Q.num2 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];
    RB: Build(Q); //检查是否有重复的题目
        switch (Q.yunsuanfu)
        {
        case 0:
            if (ExistQues(C, Q))//调用jisuan.h中的ExistQues
            {
                count++;
                goto RB;
            }
            cout << Q.num1 << "+" << Q.num2 << "=" << setw(5);
            break;
        case 1:
            if ((tideshuxing[5] == 0) && (Q.num1<Q.num2))//若为负数,则交换
            {                                        
                temp = Q.num1;
                Q.num1 = Q.num2;
                Q.num2 = temp;
            }
            if (ExistQues(C, Q))
            {
                count++;
                goto RB;
            }
            cout << Q.num1 << "-" << Q.num2 << "=" << setw(5); break;
        case 2:
            if (ExistQues(C, Q))
            {
                count++;
                goto RB;
            }
            cout << Q.num1 << "*" << Q.num2 << "=" << setw(5); break;
        case 3:
            while (Q.num2 == 0)
                Q.num2 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];
            if (!tideshuxing[6])
            {
                while ((Q.num1%Q.num2) != 0 || Q.num2 == 0)
                {                                        //重新生成
                    Q.num1 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];
                    Q.num2 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];
                }
            }
            if (ExistQues(C, Q))
            {
                count++;
                goto RB;
            }
            cout << Q.num1 << "/" << Q.num2 << "=" << setw(5); break;
        }
        InsertQues(C, Q);
        if (i%tideshuxing[2] == 0)                                    //一行打印完规定列数,换行
        for (int j = 0; j <= tideshuxing[3]; j++)
            cout << endl;
    }
    cout << endl << endl;
 }
 //回答题目函数
void Display2(LinkC C, jisuan &Q)
{
    int temp, count = 0, answer = 0, right = 0;                    //count记录重新生成题目的次数
        for (int i = 1; i <= tideshuxing[1]; i++)
        {
            cout << "(" << i << ")";
             if (tideshuxing[2] == 1)
                     Q.yunsuanfu = rand() % 4;                            
                 else
                     Q.yunsuanfu = rand() % 2;                            
             Q.num1 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];                     
             Q.num2 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];
         RB:        Build(Q); //检查是否有出过的题目
             switch (Q.yunsuanfu)
                    {
             case 0:
                 if (ExistQues(C, Q))
                 {
                     count++;
                     goto RB;
                 }
                 cout << Q.num1 << "+" << Q.num2 << "=";
                 cin >> answer;
                 if (YorN(Q, answer))
                 {
                     cout << "\t正确";
                     right++;
                 }
                 else
                     cout << "\t错误";
                 break;
             case 1:
                 if ((tideshuxing[5] == 0) && (Q.num1<Q.num2))//若为负数,则交换
                 {                                        
                     temp = Q.num1;
                     Q.num1 = Q.num2;
                     Q.num2 = temp;
                 }
                 if (ExistQues(C, Q))
                 {
                     count++;
                     goto RB;
                 }
                 cout << Q.num1 << "-" << Q.num2 << "=";
                 cin >> answer;
                 if (YorN(Q, answer))
                 {
                     cout << "\t正确";
                     right++;
                 }
                 else
                     cout << "\t错误";
                 break;
             case 2:
                 if (ExistQues(C, Q))
                 {
                     count++;
                     goto RB;
                 }
                 cout << Q.num1 << "*" << Q.num2 << "=";
                 cin >> answer;
                 if (YorN(Q, answer))
                 {
                     cout << "\t正确";
                     right++;
                 }
                 cout << "\t错误";
                 break;
             case 3:
                 while (Q.num2 == 0)//被除数不能为0
                     Q.num2 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];
                 if (!tideshuxing[6])
                 {
                     while ((Q.num1%Q.num2) != 0 || Q.num2 == 0) //重新生成
                     {                                       
                         Q.num1 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];
                         Q.num2 = rand() % (tideshuxing[4] - tideshuxing[3] + 1) + tideshuxing[3];
                     }
                 }
                 if (ExistQues(C, Q))
                 {
                     count++;
                     goto RB;
                 }
                 cout << Q.num1 << "/" << Q.num2 << "=";
                 cin >> answer;
                 if (YorN(Q, answer))
                 {
                     cout << "\t正确";
                     right++;
                 }
                 cout << "\t错误";
                 break;
             }
             InsertQues(C, Q);
             cout << endl;
        }
        cout << "共回答正确" << right << "道题。";
        cout << endl << endl;
}

 //判断难度
 void YesOrNo(int tideshuxing)
 {
     if (tideshuxing)
         cout << "是";
     else
         cout << "否";
    }
//判断输入是否正确
void Reset(int i)
{
    do
    {
        cout << "请重新设置(是1/否0):";
        cin >> tideshuxing[i];                           
    }
    while (tideshuxing[i] != 0 && tideshuxing[i] != 1);
}
//设置题目难度
 void SetLevel()
 {
         system("cls");
         char move2;
         cout << "\t/*************设置题目难度*************/" << endl;
         cout << "\t            0.是否有乘除法("; YesOrNo(tideshuxing[2]); cout << ")" << endl;
         cout << "\t            1.数值范围(" << tideshuxing[3] << "~" << tideshuxing[4] << ")" << endl;
         cout << "\t            2.是否有负数("; YesOrNo(tideshuxing[5]); cout << ")" << endl;
         cout << "\t            3.是否有余数("; YesOrNo(tideshuxing[6]); cout << ")" << endl;
         cout << "\t            4.返回主菜单" << endl;
         cout << "\t/**************************************/" << endl;
         cout << "请选择后续操作(0~4):";
         cin >> move2;
         while (move2<'0' || move2>'4')
             {
                 cout << "错误!请正确输入操作序号(0~4):";
                 cin >> move2;
             }
         switch (move2)
             {
                 case '0':Reset(4); break;
                 case '1':                              
             reset1 : cout << "新的数值下限:";                
                         cin >> tideshuxing[3];
                         cout << "新的数值上限:";
                         cin >> tideshuxing[4];
                         if (tideshuxing[3] >= tideshuxing[4])
                             {
                                 cout << "出错!请重新输入数值范围!" << endl;
                                 goto reset1;
                             }
                         break;
                     case '2':Reset(7); break;
                     case '3':Reset(8); break;
                     case '4':break;
                 }
     }
//主页面
 void MainMenu(LinkC &C, jisuan &Q)
 {
         char move, save;
         cout << "\t               0.输入出题数量" << endl;
         cout << "\t               1.设置题目难度" << endl;
         cout << "\t               2.开始出题" << endl;
         cout << "\t               3.开始答题" << endl;
         cout << "\t               4.退出系统" << endl;
         cout << "请选择后续操作(0~4):";
         cin >> move;
         while (move<'0' || move>'4')
             {
                 cout << "错误!请正确输入操作序号(0~4):";
                 cin >> move;
             }
         switch (move)
             {
                 case '2':Display1(C, Q); break;
                 case '0':
            reset4 : cout << "请设置出题数量:";
                     cin >> tideshuxing[1];
                     if (tideshuxing[1] <= 0)
                        {
                            cout << "出错!请重新输入!" << endl;
                            goto reset4;
                        }
                    break;
                   case '1':SetLevel(); break; 
                   case '3':Display2(C, Q); break;
                   case '4':
                        cout << "是否保存出题记录(是1/否0):";
                        cin >> save;
                        while (save != '1'&&save != '0')
                            {
                            cout << "出错!请正确输入(是1/否0):";
                            cin >> save;
                            }
                            if (save == '1')
                                    WriteQues(C);//调用jisuan.h中的WriteQues
                            cout << "感谢您的使用,再见!" << endl;
                             tideshuxing[0] = 0; break;
                     }
     }

 int main(int argc, char* argv[])
 {
     srand((unsigned)time(NULL));    //srand()函数产生一个以当前时间开始的随机种子
     LinkC Cacu;
     jisuan ques;
     InitList(Cacu);
     ReadQues(Cacu);
     while (tideshuxing[0])
     {
         system("cls");
         MainMenu(Cacu, ques);
         system("pause");
     }
     return 0;
}
//jisuan.h
#include<iostream>
#include <fstream>
using namespace std;
int tideshuxing[10] = { 1, 30, 0, 0, 5, 0, 0, 0 }; 
//将题的属性放在数组中,并赋予初值。0退出、1出题数量、2乘除、3数值范围下限、4数值范围上限、5负数、6余数、7出过的题目数  
//四则算术题的数据结构
typedef struct
{
    int num1;
    int num2;
    int yunsuanfu;
    int answer;
}jisuan;
typedef struct CNode     //结点
{
    jisuan ques;
    struct CNode * next;
}CNode, *LinkC;
//题目初始化
void InitList(LinkC &C)
{
    C = new CNode;
    C->next = NULL;
}
//添加题目信息
void InsertQues(LinkC &C, jisuan Q)
{//尾插入
    LinkC tail, temp;
    tail = C;
    while (tail&&tail->next != NULL)
        tail = tail->next;
               temp = new CNode;
                 temp->ques = Q;
                 temp->next = NULL;
                 tail->next = temp;
                 tail = temp;
                 tideshuxing[9]++;
             }
//判断题目存在
int ExistQues(LinkC C, jisuan Q)
{
    LinkC temp;
    temp = C->next;
    while (temp)
    {
        if ((temp->ques.num1 == Q.num1) && (temp->ques.num2 == Q.num2) && (temp->ques.yunsuanfu == Q.yunsuanfu))
            return 1;                    //当两个数字和算符与链表中的一样,则存在
        else
            temp = temp->next;
    }
    return 0;
}
//读取出过的问题
void ReadQues(LinkC &C)
{
    LinkC temp;
    ifstream infile("question.txt");
    for (int i = 0; i<10; i++)                //读取参数表
        infile >> tideshuxing[i];
    for (int i = 0; i<tideshuxing[9]; i++)                //读取出过的题目
    {
        temp = new CNode;
        infile >> temp->ques.num1;
        infile >> temp->ques.num2;
        infile >> temp->ques.yunsuanfu;
        temp->next = NULL;
    }
}
//保存到txt
void WriteQues(LinkC C)
{
    LinkC temp;
    ofstream outfile("question.txt");
    if (!outfile)
    {
        cout << "文件存储失败!" << endl;
        exit(0);
    }
    for (int i = 0; i<10; i++)
        outfile << tideshuxing[i] << " ";
    for (temp = C->next; temp; temp = temp->next)
    {
        outfile << temp->ques.num1 << " ";
        outfile << temp->ques.num2 << " ";
        outfile << temp->ques.yunsuanfu << " ";
    }
}
复制代码






项目计划总结:




 

项目计划总结:


 日期  开始时间 中断事件  中断事件  净时间 活动
 3/14  14:00 15:50  10分钟  100分钟 上课
   16:30 17:30  0  60分钟 写作业
 3/15  13:00 14:30  0  90分钟 写作业
3/16 15:00 16:00 60分钟 写作业
 3/17  15:00 16:00  0  60分钟 写作业
3/18 14:00 15:00 60分钟 写作业

 

日期&&任务 听课 编写程序 阅读相关书籍 网上查找资料 日总结
周一 2H 2H 0H 0.5H 4.5H
周二   1H 0.5H 0.5H 2H
周三   2H 1H 0.5H 3.5H
周四 2H 0H 0H 0H 2H
周五   3H 1H 1H 5H
周六   4H 2H 2H 8H
周日   0H 0H 0H 0H
周总结 4H 12H 4.5H 4.5H 25H

 

缺陷记录日志:在这次结对编程汇中,我没有很好地适应结对训练,在开发中,对数据结构这一部分不是很熟悉。

posted @ 2016-03-17 21:55  言凡  阅读(562)  评论(2编辑  收藏  举报