第四周项目3-小学生算术题
生活随笔
收集整理的這篇文章主要介紹了
第四周项目3-小学生算术题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題及代碼:
/**copyright (c) 2014,煙臺大學計算機學院*All rights reserved.*文件名稱:test.cpp*作 者:李一波*完成日期:2016年3月24號*版 本 號:v1.0**問題描述:設計一個程序,用來實現幫助小學生進行算術練習,它具有以下功能:提供10道加、減、乘、除四種基礎算術運算的題目,每道題中的操作數是隨機產生的,且操作數不超過2位正整數;練習者根據顯示的題目輸入自己的答案,程序自動判斷輸入的答案是否正切并顯示出相應的信息。最后顯示做對了幾道題。*輸入描述:輸入自己的答案*程序輸出:對或錯和共做對了幾道題*/#include<iostream> #include<cstdlib> using namespace std; int main() {int a,b,c,sum,s;int i=0,count=0;while(i<10){a=rand()%100;b=rand()%100;c=rand()%4;if(c==1){cout<<a<<"+"<<b<<"=";cin>>s;sum=a+b;if(sum==s){cout<<"right"<<endl;count++;}elsecout<<"wrong"<<endl;}else if(c==2){cout<<a<<"-"<<b<<"=";cin>>s;sum=a-b;if(sum==s){cout<<"right"<<endl;count++;}elsecout<<"wrong"<<endl;}else if(c==3){cout<<a<<"*"<<b<<"=";cin>>s;sum=a*b;if(sum==s){cout<<"right"<<endl;count++;}elsecout<<"wrong"<<endl;}else{cout<<a<<"/"<<b<<"=";cin>>s;sum=a/b;if(sum==s){cout<<"right"<<endl;count++;}elsecout<<"wrong"<<endl;}i++;}cout<<"共有"<<count<<"道題做對了"<<endl;return 0; }運行結果:
總結
以上是生活随笔為你收集整理的第四周项目3-小学生算术题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows下使用VSCode远程搭建
- 下一篇: 1097: 零起点学算法04——再模仿一