分数计算器两个问题! [英] Fraction Calculator Two Problems!

查看:78
本文介绍了分数计算器两个问题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。当我运行我的代码时,输​​入很奇怪,在我输入操作之前它不会显示任何内容,我很难解释这个但是为自己构建它并且你很可能会看到问题



2.我现在已经制作了一个基本计算器,如何将它作为分数计算器。你能否指出我正确的方向?





1. When I run my code the input is weird and it won't display anything until I type an operation, it's kind of hard for me to explain this but build it for yourself and you'll most likely see the problem

2. I've made a basic calculator now how do I make it a fraction calculator. Could you perhaps point me in the right direction?


#include<iostream>
#include<fstream> 
#include<string>
#include<iomanip>
using namespace std;
//typedef will simplify declaration of variables
typedef int i;
typedef char c;
typedef bool b;
typedef double d;
void Operands(d&n, d &d); //returning: n = numerator d = denominator

d x = 1.0, y = 1.0, result; 
c operands;
ofstream outfile;
int main(){
	Operands (x,y);
	
	b numerator, denominator;
	cout << "Input operation: ";
	cin >> x >> operands >> y;
	cout << "The result is: " << result << endl;
	system("pause");
	return 0;
}
//function header
void Operands(d &n, d &d){
	x = n;
	y = d;

	switch (operands)
	{
	case '+':
		result = x + y;
		break;

	case '-':
		result = x - y;
		break;

	case '*':
		result = x * y;
		break;

	case '/':
		result = x / y;
		break;
		
	default:
		cin >> x >> operands >> y;
		Operands(x, y);
		break;
	}
}

推荐答案

//typedef will simplify declaration of variables

不要这样做。这使代码难以为其他人阅读。



你应该在调用操作数()之前要求操作。因此,在输入行后面移动调用。



将操作模式传递给函数并返回结果而不是使用全局变量也是很好的风格:

Don't do that. It makes the code hard to read for others.

You should ask for the operation before calling Operands(). So move the call behind the input line.

It is also good style to pass the operation mode to the function and return the result rather than using global variables:

double Operands(char operation, double &n, double &d)
{
    double result = 0.0;
    switch (operation)
    {
    // handle supported onces here
    default:
        cout << "Unknown operation "  << operation << endl;
    }
    return result;
}



比你还可以删除这个函数输入的数据。



关于分数计算见 modf [ ^ ]功能。


这篇关于分数计算器两个问题!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆