軟工第一個程式:像二柱子那樣,花二十分鐘寫一個能自動產生小學四則運算題目的 “軟體”,要求:除了整數以外,還要支援真分數的四則運算。

來源:互聯網
上載者:User

標籤:

程式設計思想  

      本程式的設計主要基於對C/C++中產生隨機數的應用,在產生算數符號部分主要採用了switch語句,通過隨機產生0-3的數值來隨機產生"+-*/"字元,並進行相應的處理;在處理整數操作部分時,使用try/throw語句,避免除法分母為0,語句採用一維數組,記錄答案結果;在處理真分數部分,採用while語句產生合理的真分數,採用二維數組記錄答案結果。

程式原始碼

#include<iostream.h>#include<stdlib.h>#include<conio.h>void DealFenshu(int m, int a[][2]){    for(int p=0;p<m;p++)    {        int i=(int)rand()%10;        int j=(int)rand()%10;        while(j==0||i>=j)        {            i=(int)rand()%10;            j=(int)rand()%10;        }        int x=(int)rand()%10;        int y=(int)rand()%10;        while(y==0||x>=y)        {            x=(int)rand()%10;            y=(int)rand()%10;        }        int k=(int)rand()%100/25;        switch(k)        {            case 0:                cout<<"("<<i<<"/"<<j<<")"<<"+"<<"("<<x<<"/"<<y<<")"<<"=";                a[p][0]=i*y+x*j;                a[p][1]=j*y;                break;            case 1:                cout<<"("<<i<<"/"<<j<<")"<<"-"<<"("<<x<<"/"<<y<<")"<<"=";                a[p][0]=i*y-x*j;                a[p][1]=j*y;                break;            case 2:                cout<<"("<<i<<"/"<<j<<")"<<"*"<<"("<<x<<"/"<<y<<")"<<"=";                a[p][0]=i*x;                a[p][1]=j*y;                break;            case 3:                a[p][0]=i*y;                a[p][1]=j*x;                cout<<"("<<i<<"/"<<j<<")"<<"/"<<"("<<x<<"/"<<y<<")"<<"=";            }                        if(p%5==4)            {                cout<<endl;            }            else            {                cout<<‘\t‘;            }    }}void DisplayFenshu(int a[][2],int w,int m){    if(w==1)    {        for(int q=0;q<m;q++)        {            if(a[q][0]==0)                cout<<"0"<<‘\t‘;            else                cout<<a[q][0]<<"/"<<a[q][1]<<‘\t‘;            if(q%5==4)            {                cout<<endl;            }        }    }    else    {};}void DealInt(int m,int a[]){            for(int p=0;p<m;p++)    {    int i=(int)rand()%10;    int j=(int)rand()%10;    int k=(int)rand()%100/25;    switch(k)    {    case 0:        cout<<i<<"+"<<j<<"=";            a[p]=i+j;        break;    case 1:        cout<<i<<"-"<<j<<"=";        a[p]=i-j;        break;    case 2:        cout<<i<<"*"<<j<<"=";        a[p]=i*j;        break;    case 3:        try        {        a[p]=i/j;        cout<<i<<"/"<<j<<"=";        }        catch(...)        {            p--;        }            }                if(p%5==4)        {            cout<<endl;        }        else        {            cout<<‘\t‘;        }    }}void DisplayInt(int a[],int w,int m){    if(w==1)    {        for(int q=0;q<m;q++)        {            cout<<a[q]<<‘\t‘;            if(q%5==4)            {                cout<<endl;            }        }    }    else    {};}void main(){    int p;    do    {        system("cls");        int a[1000],b[1000][2];        int m,n,w;        cout<<"請輸入產生的四則運算題個數:";        cin>>m;        cout<<endl;        cout<<"請輸入要產生的四則運算種類(輸入1為整數,否則為真分數):";        cin>>n;        cout<<endl;        if(n==1)        {            DealInt(m,a);            cout<<endl;        }        else        {            DealFenshu(m,b);            cout<<endl;        }        cout<<"是否輸出答案(輸入1則輸出答案否則不輸出答案)"<<endl;        cin>>w;        if(n==1)        {            DisplayInt(a,w,m);                }        else        {            DisplayFenshu(b,w,m);        }        cout<<endl;        cout<<"是否繼續產生運算題(輸入1則產生否則不產生)"<<endl;        cin>>p;        cout<<endl;    }while(1==p);}

程式上課未按時完成的原因:   忘掉了C/C++中的產生隨機數的語句rand()語句

軟工第一個程式:像二柱子那樣,花二十分鐘寫一個能自動產生小學四則運算題目的 “軟體”,要求:除了整數以外,還要支援真分數的四則運算。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.