天天看點

結對程式設計項目---四則運算

同組人:胡媛http://www.cnblogs.com/hy0926/

基本功能:

1) 實作一個帶有使用者界面的四則運算。

2) 生成的題目不能重複。

3) 支援負數。

需要支援的基本設定參數

1) 題目的數量

2) 數值的範圍

3) 題目中最多幾個運算符

4) 題目中或運算過程中有無有分數

5) 題目中是否有乘除法

6) 題目中是否有括号

7) 題目中或運算過程中有無負數

工作配置設定:

兩人共同參與并一起完成

代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using caculate.math.Biz;

namespace 四則運算
{
public partial class Form1 : Form
{
//加減運算
private String[] strs1 = new String[] { "+", "-", };
private Random r10 = new Random();
//乘除運算
private String[] strs2 = new String[] { "*","/"};
private Random r11 = new Random();
//混合運算
private String[] strs3 = new String[] { "+","-","*", "/" };
private Random r12 = new Random();
public Form1()
{
InitializeComponent();
}
private string GetOperationString(Operation operation)
{
switch (operation)
{
case Operation.Add:
return "+";
case Operation.Minus:
return "-";
case Operation.Multiply:
return "×";
case Operation.Divide:
return "÷";
}
return "+";
}
private TextBox GetTextBox(int index, int num)
{


foreach (Control col in this.Controls)
{
if (col.Name == "txtNUM" + index + num)
{
return (TextBox)col;
}

}
return null;
int main()
{
int count=0 ;
srand(time(NULL));//用系統目前時間設定rand()随機序列種子,保證每次運作随機序列不一樣 
char ch;
while(count<30)
{
cout<<"選擇做整數運算(輸入'a')或真分數運算(輸入'b')"<<endl;
//産生整數的運算
cin>>ch;
if(ch=='a')
{cout<<"開始産生四則運算:"<<endl;
int shu1=0,shu2=0;
//随機數産生0-100
//shu1=srand(1000);


shu1=0+rand()%100;
shu2=0+rand()%100;

int sum=0;
int fushu=0;
string fuhao[4]={"+","-","*","/"};
fushu=((0+rand()%4)+4)%4;
//cout<<fushu<<endl;

//判斷shu2是否為0和是否為除法 若為則重新生成
while(shu2==0&fushu==3)
{
shu2=0+rand()%100;
}
//随機産生的符号
switch(fushu)
{
case 0:cout<<shu1<<fuhao[fushu]<<shu2<<endl;break;
case 1:cout<<shu1<<fuhao[fushu]<<shu2<<endl;break;
case 2:cout<<shu1<<fuhao[fushu]<<shu2<<endl;break;
case 3:cout<<shu1<<fuhao[fushu]<<shu2<<endl;break;
}
}

//産生真分數的運算
if(ch=='b')
{
int zhenfens1_m=0,zhenfens1_z=0,zhenfens2_m=0,zhenfens2_z=0;

zhenfens1_m=0+rand()%100;
zhenfens1_z=0+rand()%100;

//判斷産生的真分數正确性
while(zhenfens1_m<zhenfens1_z)
{
zhenfens1_m=0+rand()%100;
zhenfens1_z=0+rand()%100;
}

zhenfens2_m=0+rand()%100;
zhenfens2_z=0+rand()%100;

//判斷産生的真分數正确性
while(zhenfens2_m<zhenfens2_z)
{
zhenfens2_m=0+rand()%100;
zhenfens2_z=0+rand()%100;
}

string fuhao[4]={"+","-","*","/"};
int fushu=((0+rand()%4)+4)%4;
//cout<<fushu<<endl;

//判斷shu2是否為0和是否為除法 若為則重新生成
//随機産生的符号
switch(fushu)
{
case 0:cout<<"("<<zhenfens1_z<<"/"<<zhenfens1_m<<")"<<fuhao[fushu]<<"("<<zhenfens2_z<<"/"<<zhenfens2_m<<")"<<endl;break;
case 1:cout<<"("<<zhenfens1_z<<"/"<<zhenfens1_m<<")"<<fuhao[fushu]<<"("<<zhenfens2_z<<"/"<<zhenfens2_m<<")"<<endl;break;
case 2:cout<<"("<<zhenfens1_z<<"/"<<zhenfens1_m<<")"<<fuhao[fushu]<<"("<<zhenfens2_z<<"/"<<zhenfens2_m<<")"<<endl;break;
case 3:cout<<"("<<zhenfens1_z<<"/"<<zhenfens1_m<<")"<<fuhao[fushu]<<"("<<zhenfens2_z<<"/"<<zhenfens2_m<<")"<<endl;break;
}

}
count++;
}
}      

 總結:由于基礎不好,我與同組胡媛上網查閱資料以及詢問同學。還是艱難的把這個做出來了。感覺到自己的不足和與别人的差距。

通過這次鍛煉也明白應該認真做好每件事這樣才能對自己的鍛煉有很大的幫助

截圖:

結對程式設計項目---四則運算
結對程式設計項目---四則運算
結對程式設計項目---四則運算