如何让用户能够将“浮动”变量输入为分数,例如“1/2”? [英] How do I make the user able to enter the 'float' variables as fractions, such as "1/2"?

查看:63
本文介绍了如何让用户能够将“浮动”变量输入为分数,例如“1/2”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。它的目的是取两个整数,找出总和,差异和产品。然后,第二部分取两个分数,再次找到总和,差异和乘积。



  //   Assignment01:截止于9月23日星期五至8 212 E115提交框。 

#include stdafx.h
#include < ; stdio.h >
#include < iostream >
使用 namespace std;

int main()
{
goto 最佳;
top:
// 第1部分& 2
int x; int y; int sum1; int difference1; int product1; // 声明要输入的整数的变量。

cout< < 输入一个数字:\ n; // 要求用户输入整数...
cin>> X; // ...然后将该值分配给x。

cout<< 输入另一个数字:\ n; // 询问用户另一个整数...
cin>> Ÿ; // ...然后将该值分配给y。

sum1 = x + y; difference1 = x - y; product1 = x * y; // 定义每个函数,以获得两个整数的和,差和乘积。
cout<< \ n这两个数字的总和为:<< sum1<< \ n这两个数字的区别在于:<<差异1<< \ n这两个数字的乘积是:<< product1<< \ n \ n;
// 上面一行打印出以前函数给用户的答案。

// 第3部分& 4
float a; float b; float sum2; float difference2; float product2; // 声明要输入的数字的变量。

cout< < 现在输入一个分数(十进制形式):\ n; // 要求用户输入分数(如果'/'符号用于分数,程序将终止。
cin>> a;

cout<< 输入另一个分数(再次,以十进制形式):\ n; // 要求用户输入分数(如果'/'符号用于分数,程序将终止。
cin>> b;

sum2 = a + b; difference2 = a - b; product2 = a * b; // 定义每个函数,得到总和,差异和两个数字的乘积。
cout<< \ n这两个数字的总和是:<< sum2<< \ n这两个数字是:<<差异2<< \ n这两个数字的乘积是:<<产品2;
// 上面一行打印出以前函数给用户的答案。

int n;

cout<< \ n\\\
按0然后输入以结束程序,或按1并再次输入以运行程序.\\\
;
// 允许用户查看答案,然后手动结束程序,或重新启动程序以运行再一次。
cin>> N;

if (n == 1 // 如果语句将接受用户的输入并决定用户是否希望再次运行该程序。
{
system( CLS);
goto top;
}
else if (n == 0
{
return 0 ;
}
}

解决方案

在一个简单的方法中,你得到两个整数(分子和分母)用户并使用它们来创建浮动。

例如(错误处理lefts作为练习)



  int  n,d; 
char sep;
cout<< 输入分数< int> /< int><< ENDL;
cin>> n>> sep>> d;
float f =( float )n / d;


Here is my code. Its purpose is to take two integers and find the sum, difference, and product. Then, the second part takes two fractions, and again finds the sum, difference, and product.

//Assignment01: due Friday 9/23 to 8 212 E115 submission box.

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
	goto top;
top:
	//Parts 1 & 2
	int x; int y; int sum1; int difference1; int product1; //Declares variables for the integers to be input.

	cout << "Enter a number: \n"; //Asks the user to enter an integer...
	cin >> x; //...and then assigns that value to 'x'.

	cout << "Enter another number: \n"; //Asks the user for another integer...
	cin >> y; //...and then assigns that value to 'y'.

	sum1 = x + y; difference1 = x - y; product1 = x * y; //Each function is defined, to get the sum, difference, and product of the two integers.
	cout << "\nThe sum of the two numbers is: " << sum1 << "\nThe difference of the two numbers is: " << difference1 << "\nThe product of the two numbers is: " << product1 << "\n\n";
	//The above line prints out the answers of the previous functions to the user.

	//Parts 3 & 4
	float a; float b; float sum2; float difference2; float product2; //Declares variables for the numbers to be inputed.

	cout << "Now enter a fraction (in decimal form): \n"; //Asks the user to enter a fraction (program will terminate if the '/' symbol is used to make the fraction.
	cin >> a;

	cout << "Enter another fraction (again, in decimal form): \n"; //Asks the user to enter a fraction (program will terminate if the '/' symbol is used to make the fraction.
	cin >> b;

	sum2 = a + b; difference2 = a - b; product2 = a * b; //Each function is defined, to get the sum, difference, and product of the two numbers.
	cout << "\nThe sum of the two numbers is: " << sum2 << "\nThe difference of the two numbers is: " << difference2 << "\nThe product of the two numbers is: " << product2;
	//The above line prints out the answers of the previous functions to the user.

	int n;

	cout << "\n\nPress 0 and then enter to end the program, or press 1 and enter to run the program again.\n";
	//Allows the user to view answers and then manually end the program, or to restart the program to run it again.
	cin >> n;

	if (n == 1) //If statement will take the user's input and decide whether the user wishes to run the program again or not.
	{
		system("CLS");
		goto top;
	}
	else if (n == 0)
	{
		return 0;
	}
}

解决方案

In a simple approach, you get the two integers (numerator and denominator) from the user and use them to create the float.
For example (error handling lefts as exercise)

int n,d;
char sep;
cout << "enter a fraction <int>/<int>" << endl;
cin >> n >> sep >> d;
float f = (float)n/d;


这篇关于如何让用户能够将“浮动”变量输入为分数,例如“1/2”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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