码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算3

时间:2016-03-19 21:06:33      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

我和王春帆一组。

设计思路:1.先设置出题的数量

              2.设置题的难度

              3.再利用YorN(判断答案是否正确函数)判断对错。

#include<iostream>
#include "stdlib.h"
#include "time.h"
#include "jisuan.h"
#include "iomanip"
using namespace std;
//判断回答是否正确
bool YorN(jisuan &Q, int answer)
{
    if (Q.sign == 0)
    {
        Q.answer = Q.num1 + Q.num2;
    }
       else if (Q.sign == 1)
             {
                 Q.answer = Q.num1 - Q.num2;
             }
       else if (Q.sign == 2)
             {
                 Q.answer = Q.num1*Q.num2;
             }
         else
                 Q.answer = Q.num1 / Q.num2;
         if (answer == Q.answer)
                 return true;
         else
                 return false;
     }
//重新生成算术题
void ReBuild(jisuan &Q)
{
         if (para[4] == 1)
                Q.sign = rand() % 4;
        else
                Q.sign = rand() % 2;
        Q.num1 = rand() % (para[6] - para[5] + 1) + para[5];
        Q.num2 = rand() % (para[6] - para[5] + 1) + para[5];
}
//*****输出函数*****
void Display1(LinkC C, jisuan &Q)
{
        int temp, count = 0;                                    //count记录重新生成题目的次数
        for (int i = 1; i <= para[1]; i++)
           {
               cout << "(" << i << ")";
               if (para[4] == 1)
                         Q.sign = rand() % 4;                            //有乘除法
                 else
                        Q.sign = rand() % 2;                            //没有乘除法
                Q.num1 = rand() % (para[6] - para[5] + 1) + para[5];        //随机的(下限~上限)以内的整数
                Q.num2 = rand() % (para[6] - para[5] + 1) + para[5];
         RB:        ReBuild(Q);                                        //检查是否有出过的题目
                 if (count>((para[6] - para[5] + 1)*(para[6] - para[5] + 1) * 100))
                     {
                     cout << endl << "该难度的题目已出完,请更改出题设置!" << endl;
                    break;
               }
            switch (Q.sign)
                {
                         case 0:
                                 if (ExistQues(C, Q))
                                 {
                                     count++;
                                    goto RB;
                                }
                             cout << Q.num1 << "+" << Q.num2 << "=" << setw(5);
                            break;
                            case 1:
                               if ((para[7] == 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() % (para[6] - para[5] + 1) + para[5];
                                           if (!para[8])
                                             {
                                                 while ((Q.num1%Q.num2) != 0 || Q.num2 == 0)
                                                {                                        //重新生成
                                                       Q.num1 = rand() % (para[6] - para[5] + 1) + para[5];
                                                   Q.num2 = rand() % (para[6] - para[5] + 1) + para[5];
                                                }
                                            }
                                  if (ExistQues(C, Q))
                                            {
                                            count++;
                                                goto RB;
                                        }
                                         cout << Q.num1 << "/" << Q.num2 << "=" << setw(5); break;
                            }
            InsertQues(C, Q);
            if (i%para[2] == 0)                                    //一行打印完规定列数,换行
                     for (int j = 0; j <= para[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 <= para[1]; i++)
        {
            cout << "(" << i << ")";
             if (para[4] == 1)
                     Q.sign = rand() % 4;                            //有乘除法
                 else
                     Q.sign = rand() % 2;                            //没有乘除法
             Q.num1 = rand() % (para[6] - para[5] + 1) + para[5];                        //随机的(下限~上限)以内的整数
             Q.num2 = rand() % (para[6] - para[5] + 1) + para[5];
     RB:        ReBuild(Q);                                        //检查是否有出过的题目
                if (count>((para[6] - para[5] + 1)*(para[6] - para[5] + 1) * 100))
                {
                         cout << endl << "该难度的题目已出完,请更改出题设置!" << endl;
                         break;
                 }
             switch (Q.sign)
                    {
                        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 ((para[7] == 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)
                                                     Q.num2 = rand() % (para[6] - para[5] + 1) + para[5];
                                            if (!para[8])
                                                 {
                                                     while ((Q.num1%Q.num2) != 0 || Q.num2 == 0)
                                                         {                                        //重新生成
                                                             Q.num1 = rand() % (para[6] - para[5] + 1) + para[5];
                                                             Q.num2 = rand() % (para[6] - para[5] + 1) + para[5];
                                                         }
                                                }
                                            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 para)
 {
         if (para)
                 cout << "";
        else
                 cout << "";
    }
//判断输入正确
void Reset(int i)
{
    do
    {
        cout << "请重新设置(是1/否0):";
        cin >> para[i];                            //此处输入字符出错,但没解决
    } while (para[i] != 0 && para[i] != 1);
}
//设置题目难度
 void SetLevel()
 {
         system("cls");
         char move2;
         cout << "\t/*************设置题目难度*************/" << endl;
         cout << "\t            0.是否有乘除法("; YesOrNo(para[4]); cout << ")" << endl;
         cout << "\t            1.数值范围(" << para[5] << "~" << para[6] << ")" << endl;
         cout << "\t            2.是否有负数("; YesOrNo(para[7]); cout << ")" << endl;
         cout << "\t            3.是否有余数("; YesOrNo(para[8]); 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 >> para[5];
                         cout << "新的数值上限:";
                         cin >> para[6];
                         if (para[5] >= para[6])
                             {
                                 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 >> para[1];
                     if (para[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);
                            cout << "感谢您的使用,再见!" << endl;
                             para[0] = 0; break;
                     }
     }

 int main(int argc, char* argv[])
 {
       srand((unsigned)time(NULL));    //srand()函数产生一个以当前时间开始的随机种子
         LinkC Cacu;
        jisuan ques;
        InitList(Cacu);
         ReadQues(Cacu);
         while (para[0])
             {
                 system("cls");
                MainMenu(Cacu, ques);
            system("pause");
        }
         return 0;
}
//jisuan.h
#include<iostream>
#include <fstream>
using namespace std;
int para[10] = { 1, 30, 3, 0, 0, 0, 5, 0, 0, 0 };  
//*****四则算术题的数据结构*****
typedef struct
{
    int num1;
    int num2;
    int sign;
    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;
                 para[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.sign == Q.sign))
            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 >> para[i];
    for (int i = 0; i<para[9]; i++)                //读取出过的题目
    {
        temp = new CNode;
        infile >> temp->ques.num1;
        infile >> temp->ques.num2;
        infile >> temp->ques.sign;
        temp->next = NULL;
    }
}
//******写入文件******
void WriteQues(LinkC C)
{
    LinkC temp;
    ofstream outfile("question.txt");
    if (!outfile)
    {
        cout << "文件存储失败!" << endl;
        exit(0);
    }
    for (int i = 0; i<10; i++)
        outfile << para[i] << " ";
    for (temp = C->next; temp; temp = temp->next)
    {
        outfile << temp->ques.num1 << " ";
        outfile << temp->ques.num2 << " ";
        outfile << temp->ques.sign << " ";
    }
}

技术分享技术分享技术分享技术分享

项目计划总结:

 日期  开始时间 中断事件  中断事件  净时间 活动
 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

缺陷记录日志:在这次编程中,把以前的数据结构变成给忘记了,还得翻书。以后要好好复习。

四则运算3

标签:

原文地址:http://www.cnblogs.com/qinxian0/p/5296087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!