c语言除法循环节,随机算式的除法如果答案是无限循环小数,怎么加精度?

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

有高人看看下面的program

#include

#define HOW 10

using namespace std;

char get_oper(int n)

{

switch(n)

{

case 0:return '+';

case 1:return '-';

case 2:return '*';

case 3:return '/';

}

return'*';

}

int main()

{

int num1=0,num2=0,count=0;

double user_answer=0;

float right_answer=0;

int operators=0; //0:+ 1:- 2:* 3:/

int sum = 0;

char cont = 'n';

cout<

cout<

cout<

cout<

cout<

cout<

do

{

for(int i=1;i<=HOW;i++)

{

//randamly create the numbers and operators.

num1=rand()%100 + 1;

num2=rand()%100 + 1;

operators=rand()%4;

if(operators==3)num1=num2*(rand()%100);

//create the questions and wait the user to calculate the answers

cout<

cin>>user_answer;

//give the answers and let the user to check the result

switch(operators)

{

case 0:right_answer=num1+num2;break;

case 1:right_answer=num1-num2;break;

case 2:right_answer=num1*num2;break;

case 3:right_answer=num1/num2;

}

//examin the user's answers

if(right_answer==user_answer)

{

cout<

count++;

sum = sum + 10;

}

else {

cout<

sum = sum - 5; }

}

//output the total marks for the user

cout<

cout<

cout<

cout<

cout<< "Do you want to keep playing?"<

cout<< "Type 'y' for yes and 'n' for no:";

cin>> cont;

}

while (cont=='y');

system ("pause");

return 0;

}

如果出来的除法答案是无限循环小数怎么办啊?输入什么答案都是错呢!

听别人说是需要加精度的,可是怎么加? 给个例子啊!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值