首页 > 其他分享 >结对实验

结对实验

时间:2022-11-13 20:48:21浏览次数:36  
标签:文件 结对 cout int 括号 实验 include

(1) 本人角色

我在本次结对项目中担任驾驶员角色。

结对伙伴杨宁为领航员。

(1) 系统实现过程

生成式子的功能:

       先写好生成操作数和包含1~3个操作符的式子的四个函数,再通过一个函数一起调用,随机生成带括号的式子。除号和乘号÷、×先用*、/代替C语言里面的除号和乘号÷、×不能直接从文件读出来,所以就放弃了从文件里读出来计算的想法,改为生成的时候,把式子放进数组再来计算,计算不小于0,才把数组存进文件,存进文件的时候把*、/这两个符号替换成÷、×。

计算功能:

       通过每个输入类型的不同进行分类计算。

比较题目文件,判断答案文件中的对错:

       这个可以说是取巧了一下,因为文件的生成,帮随着答案的生成,所以比较的是标准答案文件和新答案文件。原本是想从文件里面再读出式子拿来计算,但是写进文件后有了符号÷、×,但是那两个符号÷、×读出来会出错,因为他们不是普通字符型数据的长度。

(2) 算法设计

        设计一个窗口界面。根据功能分别设置操作指令。其中功能有“输入算式数量、输入最大数、输出运算个数、输入运算符、选择数字类型、选择是否需要括号 、请选择输出形式”。

         通过函数分成很多类型进行挑选运行:整形数无括号 intno(int n);整形数有括号intyes(int n);生成整数四则运算 intys();小数无括号floatno(int n);小数有括号floatyes(int n);生成小数四则运算doubleys()。

         其中随机数使用了rand进行随机输出,符号也进行随机输出。在生成四则运算时会根据界面的判断是否有括号以及是否数字类型。在主函数中设置一个随机种子,每次运行都能保证随机种子不同。

(4)结对工作照片:

 

(5)源代码:

  1 #include<iostream>
#include<stdlib.h>
#include<cstring>
#include<time.h>
#include<cmath>
#include<fstream>
int num; //算式数量
int imax;  //最大数
int nums;  //因子个数
int symbol;  //用户输入的运算符
int kuohao; //是否有括号
int xiaoshu;  //是否有小数
int shuchu;//输出形式
char fuhao[4]={'+','-','*','/'};  //运算符
using namespace std;
ofstream outfile;//数据写入文件

void intno(int n)//整形数无括号
{
    int i,a;
    a=(rand()%imax)+1;//得到一个1到imax的随机数a
    cout<<a;
    outfile<<a;
    for(i=1;i<nums;i++)
    {
        a=(rand()%imax)+1;//得到一个1到imax的随机数a
        cout<<fuhao[n-1]<<a;
        outfile<<fuhao[n-1]<<a;
    }
    cout<<"="<<endl;
    outfile<<"="<<endl;
}
void intyes(int n)//整形数有括号
{
    int k,i,a;
    n--;
    k=rand()%2;//随机生成"0"或"1"
    a=(rand()%imax)+1;//得到一个1到imax的随机数a
    if(k==0)
    {
        cout<<a;
        outfile<<a;
        a=(rand()%imax)+1;
        for(i=1;i<nums;i++)
        {
            a=(rand()%imax)+1;
            cout<<fuhao[n];
            outfile<<fuhao[n];
            k=rand()%2;
            if(k==1)
            {
                if(nums-i==1)
                {
                    cout<<a;
                    outfile<<a;
                    continue;
                }
                cout<<"("<<a<<fuhao[n];
                outfile<<"("<<a<<fuhao[n];
                a=(rand()%imax)+1;
                cout<<a<<")";
                outfile<<a<<")";
                i++;
            }
            else
            {
                cout<<a;
                outfile<<a;
            }
        }
        cout<<"="<<endl;
        outfile<<"="<<endl;
    }
    else
    {
        cout<<"("<<a<<fuhao[n];
        outfile<<"("<<a<<fuhao[n];
        a=(rand()%imax)+1;
        cout<<a<<")";
        outfile<<a<<")";
        for(i=2;i<nums;i++)
        {
            a=(rand()%imax)+1;
            cout<<fuhao[n];
            outfile<<fuhao[n];
            k=rand()%2;
            if(k==1)
            {
                if(nums-i==1)
                {
                    cout<<a;
                    outfile<<a;
                    continue;
                }
                cout<<"("<<a<<fuhao[n];
                outfile<<"("<<a<<fuhao[n];
                a=(rand()%imax)+1;
                cout<<a<<")";
                outfile<<a<<")";
                i++;
            }
            else
            {
                cout<<a;
                outfile<<a;
            }
        }
        cout<<"="<<endl;
        outfile<<"="<<endl;
    }
}

void intys()//生成整数四则运算
{
	int a;
	int x,i;
	if(kuohao==1)//不需要括号
	{
		if(symbol==1||symbol==2||symbol==3||symbol==4)//执行"+"运算
		{
		    intno(symbol);
		}
		else if(symbol==5)//执行混合运算
		{
		    a=(rand()%imax)+1;//得到一个1到imax的随机数a
		    cout<<a;
            for(i=1;i<nums;i++)
            {
                x=rand()%4;//随机得到"+" "-" "*" "/"中的一个
                a=(rand()%imax)+1;//得到一个1到imax的随机数a
                cout<<fuhao[x]<<a;
                outfile<<fuhao[x]<<a;
            }
            cout<<"="<<endl;
            outfile<<"="<<endl;
		}
	}
	else
	{
	    if(symbol==1||symbol==2||symbol==3||symbol==4)
        {
            intyes(symbol);
        }
        else if(symbol==5)
        {
            int k,i,a;
            k=rand()%2;//随机生成"0"或"1"
            a=(rand()%imax)+1;//得到一个1到imax的随机数a
            if(k==0)
            {
                cout<<a;
                outfile<<a;
                a=(rand()%imax)+1;
                for(i=1;i<nums;i++)
                {
                    x=rand()%4;//随机得到"+" "-" "*" "/"中的一个
                    a=(rand()%imax)+1;
                    cout<<fuhao[x];
                    outfile<<fuhao[x];
                    k=rand()%2;
                    if(k==1)
                    {
                       if(nums-i==1)
                       {
                           cout<<a;
                           outfile<<a;
                           continue;
                       }
                       x=rand()%4;//随机得到"+" "-" "*" "/"中的一个
                       cout<<"("<<a<<fuhao[x];
                       outfile<<"("<<a<<fuhao[x];
                       a=(rand()%imax)+1;
                       cout<<a<<")";
                       outfile<<a<<")";
                       i++;
                     }
                     else
                     {
                         cout<<a;
                         outfile<<a;
                     }
                 }
                 cout<<"="<<endl;
                 outfile<<"="<<endl;
             }
             else
             {
                 x=rand()%4;//随机得到"+" "-" "*" "/"中的一个
                 cout<<"("<<a<<fuhao[x];
                 outfile<<"("<<a<<fuhao[x];
                 a=(rand()%imax)+1;
                 cout<<a<<")";
                 outfile<<a<<")";
                 for(i=2;i<nums;i++)
                 {
                     a=(rand()%imax)+1;
                     x=rand()%4;//随机得到"+" "-" "*" "/"中的一个
                     cout<<fuhao[x];
                     outfile<<fuhao[x];
                     k=rand()%2;
                     if(k==1)
                     {
                         if(nums-i==1)
                         {
                            cout<<a;
                            outfile<<a;
                            continue;
                     }
                     x=rand()%4;//随机得到"+" "-" "*" "/"中的一个
                     cout<<"("<<a<<fuhao[x];
                     outfile<<"("<<a<<fuhao[x];
                     a=(rand()%imax)+1;
                     cout<<a<<")";
                     outfile<<a<<")";
                     i++;
                     }
                     else
                     {
                         cout<<a;
                         outfile<<a;
                     }
                  }
                  cout<<"="<<endl;
                  outfile<<"="<<endl;
              }
        }
    }
}
void floatno(int n)//小数无括号
{
    int i;
    float a;
    a=(float)rand()/RAND_MAX*imax+1.0;
    cout<<a;
    outfile<<a;
    for(i=1;i<nums;i++)
    {
        a=(float)rand()/RAND_MAX*imax+1.0;
        cout<<fuhao[n-1]<<a;
        outfile<<fuhao[n-1]<<a;
    }
    cout<<"="<<endl;
    outfile<<"="<<endl;
}
void floatyes(int n)//小数有括号
{
    int k,i;
    float a;
    n--;
    k=rand()%2;//随机生成"0"或"1"
    a=(float)rand()/RAND_MAX*imax+1.0;
    if(k==0)
    {
        cout<<a;
        outfile<<a;
        a=(float)rand()/RAND_MAX*imax+1.0;
        for(i=1;i<nums;i++)
        {
            a=(float)rand()/RAND_MAX*imax+1.0;
            cout<<fuhao[n];
            outfile<<fuhao[n];
            k=rand()%2;
            if(k==1)
            {
                if(nums-i==1)
                {
                    cout<<a;
                    outfile<<a;
                    continue;
                }
                cout<<"("<<a<<fuhao[n];
                outfile<<"("<<a<<fuhao[n];
                a=(float)rand()/RAND_MAX*imax+1.0;
                cout<<a<<")";
                outfile<<a<<")";
                i++;
            }
            else
            {
                cout<<a;
                outfile<<a;
            }
        }
        cout<<"="<<endl;
        outfile<<"="<<endl;
    }
    else
    {
        cout<<"("<<a<<fuhao[n];
        outfile<<"("<<a<<fuhao[n];
        a=(float)rand()/RAND_MAX*imax+1.0;
        cout<<a<<")";
        outfile<<a<<")";
        for(i=2;i<nums;i++)
        {
            a=(float)rand()/RAND_MAX*imax+1.0;
            cout<<fuhao[n];
            outfile<<fuhao[n];
            k=rand()%2;
            if(k==1)
            {
                if(nums-i==1)
                {
                    cout<<a;
                    outfile<<a;
                    continue;
                }
                cout<<"("<<a<<fuhao[n];
                outfile<<"("<<a<<fuhao[n];
                a=(float)rand()/RAND_MAX*imax+1.0;
                cout<<a<<")";
                outfile<<a<<")";
                i++;
            }
            else
            {
                cout<<a;
                outfile<<a;
            }
        }
        cout<<"="<<endl;
        outfile<<"="<<endl;
    }
}
void doubleys()//生成小数四则运算
{
	float a;
	int x,i;
	x=rand()%4;
	if(kuohao==1)
	{
		if(symbol==1||symbol==2||symbol==3||symbol==4)
		{
			floatno(symbol);
		}
		else if(symbol==5)
		{
		    a=(float)rand()/RAND_MAX*imax+1.0;
		    cout<<a;
		    for(i=1;i<nums;i++)
            {
                x=rand()%4;
                a=(float)rand()/RAND_MAX*imax+1.0;
                cout<<fuhao[x]<<a;
                outfile<<fuhao[x]<<a;
            }
            cout<<"="<<endl;
            outfile<<"="<<endl;
		}
	}
	else
	{
		if(symbol==1||symbol==2||symbol==3||symbol==4)
		{
			floatyes(symbol);
		}
		else if(symbol==5)
		{
		    int k;
			k=rand()%2;//随机生成"0"或"1"
            a=(float)rand()/RAND_MAX*imax+1.0;
            if(k==0)
            {
               cout<<a;
               outfile<<a;
               a=(float)rand()/RAND_MAX*imax+1.0;
               for(i=1;i<nums;i++)
               {
                   a=(float)rand()/RAND_MAX*imax+1.0;
                   x=rand()%4;//随机得到"+" "-" "*" "/"中的一个
                   cout<<fuhao[x];
                   outfile<<fuhao[x];
                   k=rand()%2;
                   if(k==1)
                   {
                      if(nums-i==1)
                      {
                         cout<<a;
                         outfile<<a;
                         continue;
                      }
                      x=rand()%4;//随机得到"+" "-" "*" "/"中的一个
                      cout<<"("<<a<<fuhao[x];
                      outfile<<"("<<a<<fuhao[x];
                      a=(float)rand()/RAND_MAX*imax+1.0;
                      cout<<a<<")";
                      outfile<<a<<")";
                      i++;
                   }
                   else
                   {
                      cout<<a;
                      outfile<<a;
                  }
               }
               cout<<"="<<endl;
               outfile<<"="<<endl;
            }
            else
            {
                x=rand()%4;//随机得到"+" "-" "*" "/"中的一个
                cout<<"("<<a<<fuhao[x];
                outfile<<"("<<a<<fuhao[x];
                a=(float)rand()/RAND_MAX*imax+1.0;
                cout<<a<<")";
                outfile<<a<<")";
                for(i=2;i<nums;i++)
                {
                    a=(float)rand()/RAND_MAX*imax+1.0;
                    x=rand()%4;//随机得到"+" "-" "*" "/"中的一个
                    cout<<fuhao[x];
                    outfile<<fuhao[x];
                    k=rand()%2;
                    if(k==1)
                    {
                       if(nums-i==1)
                       {
                           cout<<a;
                           outfile<<a;
                           continue;
                       }
                       x=rand()%4;//随机得到"+" "-" "*" "/"中的一个
                       cout<<"("<<a<<fuhao[x];
                       outfile<<"("<<a<<fuhao[x];
                       a=(float)rand()/RAND_MAX*imax+1.0;
                       cout<<a<<")";
                       outfile<<a<<")";
                       i++;
                   }
                   else
                   {
                       cout<<a;
                       outfile<<a;
                   }
                }
                cout<<"="<<endl;
                outfile<<"="<<endl;
             }
		}
	}
}
int main()
{
    cout<<"-----小学生四则运算练习系统-----"<<endl;
	cout<<"******************************** "<<endl;
	cout<<"      1: 请输入算式数量: "<<endl;
	cin>>num;
	cout<<"      2: 请输入最大数: "<<endl;
	cin>>imax;
	cout<<"      3: 请输出运算个数:"<<endl;
	cin>>nums;
	cout<<"      4: 请输入运算符: "<<endl;
	cout<<"      1代表+ 2代表- 3代表* 4代表/ 5代表混合:"<<endl;
	cin>>symbol;
	cout<<"      5: 请选择数字类型: "<<endl;
	cout<<"      1代表整数 2代表小数"<<endl;
	cin>>xiaoshu;
	cout<<"      6: 请选择是否需要括号 "<<endl;
	cout<<"      1代表不需要括号 2代表需要括号"<<endl;
	cin>>kuohao;
	cout<<"      7: 请选择输出形式 "<<endl;
	cout<<"      1代表输出到文件 2代表打印机或打印到屏幕"<<endl;
	cin>>shuchu;
	if(shuchu==1)
    {
        outfile.open("test.txt");//数据写入文件
    }
	if(xiaoshu==1)
	{
		srand(time(NULL));//设置一个随机种子,每次运行都能保证随机种子不同
		for(int i=0;i<num;i++)
		{
			intys();
		}
	}
	else
	{
		srand(time(NULL));
		for(int i=0;i<num;i++)
		{
			doubleys();
		}
	}
	outfile.close();
	cout<<"******************************** "<<endl;
	return 0;
}

标签:文件,结对,cout,int,括号,实验,include
From: https://www.cnblogs.com/xuyuting/p/16874418.html

相关文章

  • 实验三:朴素贝叶斯算法实验
    实验三:朴素贝叶斯算法实验 姓名许珂学号201613344 【实验目的】理解朴素贝叶斯算法原理,掌握朴素贝叶斯算法框架。【实验内容】针对下表中的数据,编写......
  • 评价及实验二总结
    本次编程过程中,领航员为李金泽,他为我的工作做出了很大的帮助,因为我的编程工作几乎都是在家里完成的,所以与领航员的但部分交流也是在网上进行的。这次编程过程中,我编写了不......
  • 实验三:朴素贝叶斯算法实验
    【实验目的】理解朴素贝叶斯算法原理,掌握朴素贝叶斯算法框架。【实验内容】针对下表中的数据,编写python程序实现朴素贝叶斯算法(不使用sklearn包),对输入数据进行预测;熟......
  • CSS 样式属性书写顺序对渲染性能到底有没有影响(实验论证-2022.11)
    在前端领域有一定工作年限,并且接触过前端蛮荒时代(前后端不分离,jQuery,纯HTML+CSS+JS)开发的小伙伴,应该对CSS样式非常熟悉。早在那个年代,前端在编写CSS代码时,已经听说业界对......
  • 软件工程基础实验二源代码
    #include<stdio.h>#include<stdlib.h>#include<windows.h>#include<time.h>voidMenu();intgetChoice();voiddoExercise(intch);voidinteger(intchoice);voidd......
  • 实验三:朴素贝叶斯算法
    【实验目的】理解朴素贝叶斯算法原理,掌握朴素贝叶斯算法框架。【实验内容】针对下表中的数据,编写python程序实现朴素贝叶斯算法(不使用sklearn包),对输入数据进行预测;熟悉s......
  • 实验三:朴素贝叶斯算法实验
    实验三:朴素贝叶斯算法实验班级:20大数据(3)班学号:201613341【实验目的】理解朴素贝叶斯算法原理,掌握朴素贝叶斯算法框架。【实验内容】针对下表中的数据,编写python程......
  • 实验三:朴素贝叶斯算法实验
    【实验目的】理解朴素贝叶斯算法原理,掌握朴素贝叶斯算法框架。【实验内容】针对下表中的数据,编写python程序实现朴素贝叶斯算法(不使用sklearn包),对输入数据进行预测;熟悉s......
  • 软件工程第一次结对编程
    软件工程第一次结对编程1.1具体分工领航员(本人):郑一鸣 学号: 223201062109驾驶员(伙伴):张雨晴   学号:2232010621102.1实验过程2.1.1实验代码https://github.com......
  • 软件工程第二次实验
    实验过程1.本人角色本人在本次实验中担任驾驶员;姓名:邢巧巧   学号:223201062209我的结队伙伴是:庞玮洋2232010622302.任务分工驾驶员:邢巧巧博客链接:l 负责四则......