日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言零基础入门习题(八)四则运算

發布時間:2024/3/12 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言零基础入门习题(八)四则运算 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

????????C語言是大多數小白走上程序員道路的第一步,在了解基礎語法后,你就可以來嘗試解決以下的題目。放心,本系列的文章都對新手非常友好。


Tips:題目是英文的,但我相信你肯定能看懂

一、四則運算 題目

(Math tutor) Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is greater than or equal to number2.? For a division question such as number1 / number2, number2 is not zero.

<Output>

Main menu

1: Addition

2: Subtraction

3: Multiplication

4: Division

5: Exit

Enter a choice: 1<enter icon>

What is 1 + 7? 8<enter icon>

Correct

Main menu

1: Addition

2: Subtraction

3: Multiplication

4: Division

5: Exit

Enter a choice: 1<enter icon>

What is 4 + 0? 5<enter icon>

Your answer is wrong. The correct answer is 4

Main menu

1: Addition

2: Subtraction

3: Multiplication

4: Division

5: Exit

Enter a choice: 4<enter icon>

What is 4 / 5? 1<enter icon>

Your answer is wrong. The correct answer is 0

Main menu

1: Addition

2: Subtraction

3: Multiplication

4: Division

5: Exit

Enter a choice:

<End Output>

二、代碼示例

#include <stdio.h> #include <stdlib.h> #include <time.h>int ranNum (void); void printIn (int);int main() {int a,n1,n2,c;while(a!=5){n1=ranNum ();n2=ranNum ();printf("Main menu\n1: Addition\n2: Subtraction\n3: Multiplication\n4: Division\n5: Exit\nEnter a choice: ");scanf("%d",&a);if (a==1){printf("What is %d + %d?",n1,n2);c=n1+n2;printIn (c);}if (a==2){printf("What is %d - %d?",n1,n2);while (n1<n2){n1=ranNum ();}c=n1-n2;printIn(c);}if (a==3){printf("What is %d * %d?",n1,n2);c=n1*n2;printIn(c);}if (a==4){printf("What is %d / %d?",n1,n2);while (n2==0){n2=ranNum ();}c=n1/n2;printIn(c);}}return 0; }int ranNum (){int n;srand (time(NULL)+rand());n=rand()%10;return (n);}void printIn(int c){int b;scanf("%d",&b);if (b==c)printf("Correct\n\n");elseprintf("Your answer is wrong. The correct answer is %d\n\n",c);}


總結

以上就是本文全部內容,你學會了嗎?

總結

以上是生活随笔為你收集整理的C语言零基础入门习题(八)四则运算的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。