美文网首页
结对编程练习

结对编程练习

作者: 琼珶和予 | 来源:发表于2018-05-02 19:11 被阅读0次

1.题目描述

某公司程序员二柱的小孩在上小学二年级,老师让家长每天出30道(100以内)四则运算给小学生做。二柱立马就想到写一个程序来做这件事。

2.代码

#include<fstream> 
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#include<time.h>
#include<cmath>  
#include<string>  
#include<stdio.h> 
using namespace std;
#define random() (rand()%100000) 

//符号生成
char create_symbol(int n)
{
    int n1,j;    
    char symbol[1];
    if(n==0)
    {
        n1=2;
    }
    else if(n=1)
    {
        n1=4;
    }
    j=random()%n1;
    if(j==0) symbol[0]='+';
    else if(j==1) symbol[0]='-';
    else if(j==2) symbol[0]='*';
    else symbol[0]='/';
    return symbol[0];      
 } 
 //把数字转换成字符串型
 string int_string(int number)
 {
     int temp=abs(number);
    char str[200];
    itoa(temp,str,10);
    string str_=str;
    return str_; 
  } 
 //运算式转换成一个字符串
 string combination(string str1,string str2,char k) 
 {
     string equation;
     equation=str1+k+str2;
     return equation;
 }
 //主函数 
int main()
{
    srand((int)time(NULL));  //设置时间种子 ,使得程序每次运行的结果都不同 
    int num1,num2,count,n,change,amount,shuchu,range,j,repeat=0,bracket;
    string str_num1,str_num2,temp;
    cout<<"有无乘除法?1有,0没有:"<<endl;
    cin>>n;
    cout<<"是否有括号?1有,0没有:"<<endl;
    cin>>bracket; 
    cout<<"题目是否在文件中输出?1是,0不是:"<<endl;
    cin>>shuchu;
    cout<<"请输入数字范围:"<<endl;
    cin>>range; 
    cout<<"请输入出题数量:"<<endl;
    cin>>amount; 
    string Equation[amount];
    char symbol;
    ofstream fout;
    if(shuchu==1)
    {
        fout.open("fl.txt");
        fout<<amount<<"道四则运算题如下:"<<endl; 
    }
    else 
    {
        cout<<amount<<"道四则运算题如下:"<<endl; 
    }
    for(int i=0;i<amount;i++)
    {    
        num1=random()%range+1;
        num2=random()%range+1;
        count=random()%9+2;
        symbol=create_symbol(n);
        str_num1=int_string(num1);
        str_num2=int_string(num2);
        Equation[i]=combination(str_num1,str_num2,symbol);
        if(count>2)
        {
            for(count;count>2;count--)
            {
                symbol=create_symbol(n);
                str_num1=Equation[i];
                if(bracket==1)
                {
                    change=random()%2;
                    if(change==0)
                    {
                       str_num1='('+str_num1+')';
                     }
                } 
                symbol=create_symbol(n);
                num2=random()%range+1;
                str_num2=int_string(num2);
                change=random()%2;
                if(change==0)
                {
                    temp=str_num1;
                    str_num1=str_num2;
                    str_num2=temp;
                }                
                Equation[i]=combination(str_num1,str_num2,symbol);
            }
        }
        //判断是否重复
         for(j=0;j<i;j++)
         {
             if(Equation[j]==Equation[i])
             {
                i=i-1;
                repeat=1;
                break;
             }
         }
         if(repeat!=1)//若不重复,则输出 
         {
             if(shuchu==1)
            {
                fout<<Equation[i]<<"="<<endl;
            }
            else
            {
                cout<<Equation[i]<<"="<<endl;
            }
         }
       
    }
    if(shuchu==1)
    {
        fout.close();
    }
}

3.运行结果




4.结对编程

小组队员:彭海佳,庞柄宇




相关文章

  • 结对编程练习

    1.题目描述 2.代码 3.运行结果 4.结对编程 小组队员:彭海佳,庞柄宇

  • 结对编程

    以前你跟我讲结对编程,我以为就是找个小伙伴抱着键盘坐在面对面,然后各写各的,遇到不明白的问题可以相互打个招呼探讨一...

  • 结对编程

    TW出品的极限编程实践-结对编程 https://martinfowler.com/articles/on-pai...

  • Agile,Pair Programming 1+1>n

    Pair Programming - 结对编程,敏捷中非常重要的一项实践。 之前参与的几个项目,都有结对编程的实践...

  • 编程工作的破窗效应与童子军原则(4)

    编程工作的破窗效应与童子军原则(3) 结对编程是贯彻童子军原则的首选策略。结对编程中的两个程序员不仅在编程,而且自...

  • 结对编程指南

    前言 俗话说,”三个臭皮匠顶个诸葛亮“,可见组织内的团队合作并非颠覆性理念。但在普遍倾向于自由工作的编程领域,要求...

  • 关于结对编程

    如果在百度上搜索结对编程的话,偶尔会搜到一些奇怪的内容,比如“和妹子结对编程是什么样的体验”、基情四射的好基友结对...

  • 一张图读懂结对编程与单人开发的差异

    一张图读懂结对编程与单人开发的差异。结对编程的好处(尤其适合软件模块之间耦合很强的场景): 减少返工时间(因为在结...

  • 你真的喜欢结对编程吗

    什么是结对编程 结对编程(英语:Pair programming)是一种敏捷软件开发的方法,两个程序员在一个计算机...

  • 极限编程之结对编程

    原文链接:https://www.jianshu.com/p/6b77fd80e941 在敏捷开发(Agile)越...

网友评论

      本文标题:结对编程练习

      本文链接:https://www.haomeiwen.com/subject/xsocrftx.html