軟體工程課堂練習-隨機產生30道四則運算練習題

來源:互聯網
上載者:User

標籤:

習題要求:隨機產生30道四則運算題

以下是程式碼:

 1 #include "stdafx.h" 2 #include "stdio.h" 3 #include "time.h" 4 #include "stdlib.h"      //隨機器函數標頭檔 5  6 void print() 7 { 8     srand((int)time(0)); 9     for(int i=0;i<=29;i++)10     {11         int x;    12         int a=rand()%100;    13         int b=rand()%100;14 15         x=1+rand()%4;     //隨機產生四種不同的演算法    16         switch (x)                                17         {18             case 1: {printf("%d+%d=\t\t",a,b);break;}19             case 2: {printf("%d-%d=\t\t",a,b);break;}20             case 3: {printf("%d*%d=\t\t",a,b);break;}21             case 4: {22                 if (a!=0)23                  printf("%d/%d=\t\t",a,b);24                 else25                 {    26                     a=a+1;27                     printf("%d/%d=\t\t",a,b);28                 }29                 break;30                     }31             default:{"輸出有誤!";}32         }        33     }34 }35 int main(int argc, char* argv[])36 {37     print();38     return 0;39 }
View Code

 

設計思路:

其實寫的代碼很簡單,主要是要解決如何隨機產生數字以及產生的數字如何進行隨機的四則運算這倆個問題。因為代碼簡單,具體的思路已經通過注釋在代碼中體現出來。

下面是結果的

這次課堂練習之所以沒能在課上及時完成是因為對編程的生疏以及好多東西都忘了,沒能想起隨機函數,編程這東西還是要多敲多練才可以。

軟體工程課堂練習-隨機產生30道四則運算練習題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.