用c++编写一个给一个分数约分的程序。
时间: 2024-05-02 07:19:25 浏览: 13
好的,这是一个简单的分数约分程序:
```c
#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main()
{
int numerator, denominator, divisor;
printf("请输入分子和分母,用空格分隔:\n");
scanf("%d %d", &numerator, &denominator);
divisor = gcd(numerator, denominator);
numerator /= divisor;
denominator /= divisor;
printf("约分后的结果为:%d/%d\n", numerator, denominator);
return 0;
}
```
输入示例:
```
请输入分子和分母,用空格分隔:
24 36
```
输出示例:
```
约分后的结果为:2/3
```
相关问题
C++编程语言,编写一个班级分数等级的程序
当然,我可以为您提供一个简单的班级分数等级的程序示例:
```cpp
#include <iostream>
#include <vector>
using namespace std;
// 定义等级划分
const vector<pair<int, string>> gradeLevels = {
{90, "A"},
{80, "B"},
{70, "C"},
{60, "D"},
{0, "F"}
};
// 根据分数获取等级
string getGrade(int score) {
for (const auto& level : gradeLevels) {
if (score >= level.first) {
return level.second;
}
}
return "Invalid";
}
int main() {
int numStudents;
cout << "请输入学生人数: ";
cin >> numStudents;
vector<int> scores(numStudents);
cout << "请输入每个学生的分数: " << endl;
for (int i = 0; i < numStudents; ++i) {
cout << "学生 #" << (i + 1) << ": ";
cin >> scores[i];
}
cout << "学生成绩等级: " << endl;
for (int i = 0; i < numStudents; ++i) {
cout << "学生 #" << (i + 1) << ": " << getGrade(scores[i]) << endl;
}
return 0;
}
```
这个程序会首先要求您输入班级中学生的人数,然后逐个输入每个学生的分数。最后,程序会根据输入的分数计算出每个学生的等级,并输出到屏幕上。
请注意,这只是一个简单的示例程序,并没有包含任何输入验证或错误处理。在实际应用中,您可能需要加入更多的代码来确保输入的有效性和处理异常情况。
c++编写一个分数运算使用函数重载和友元函数
下面是一个使用函数重载和友元函数实现分数运算的C++程序:
```cpp
#include <iostream>
using namespace std;
class Fraction {
public:
Fraction(int n = 0, int d = 1) : num(n), den(d) {}
friend ostream& operator<<(ostream& out, const Fraction& f);
friend istream& operator>>(istream& in, Fraction& f);
friend Fraction operator+(const Fraction& f1, const Fraction& f2);
friend Fraction operator-(const Fraction& f1, const Fraction& f2);
friend Fraction operator*(const Fraction& f1, const Fraction& f2);
friend Fraction operator/(const Fraction& f1, const Fraction& f2);
private:
int num, den;
};
ostream& operator<<(ostream& out, const Fraction& f) {
out << f.num << "/" << f.den;
return out;
}
istream& operator>>(istream& in, Fraction& f) {
char c;
in >> f.num >> c >> f.den;
return in;
}
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
Fraction operator+(const Fraction& f1, const Fraction& f2) {
int n = f1.num * f2.den + f2.num * f1.den;
int d = f1.den * f2.den;
int g = gcd(n, d);
return Fraction(n / g, d / g);
}
Fraction operator-(const Fraction& f1, const Fraction& f2) {
int n = f1.num * f2.den - f2.num * f1.den;
int d = f1.den * f2.den;
int g = gcd(n, d);
return Fraction(n / g, d / g);
}
Fraction operator*(const Fraction& f1, const Fraction& f2) {
int n = f1.num * f2.num;
int d = f1.den * f2.den;
int g = gcd(n, d);
return Fraction(n / g, d / g);
}
Fraction operator/(const Fraction& f1, const Fraction& f2) {
int n = f1.num * f2.den;
int d = f1.den * f2.num;
int g = gcd(n, d);
return Fraction(n / g, d / g);
}
int main() {
Fraction f1, f2;
char op;
cout << "Enter first fraction: ";
cin >> f1;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter second fraction: ";
cin >> f2;
switch (op) {
case '+':
cout << f1 << " + " << f2 << " = " << f1 + f2 << endl;
break;
case '-':
cout << f1 << " - " << f2 << " = " << f1 - f2 << endl;
break;
case '*':
cout << f1 << " * " << f2 << " = " << f1 * f2 << endl;
break;
case '/':
cout << f1 << " / " << f2 << " = " << f1 / f2 << endl;
break;
default:
cout << "Invalid operator" << endl;
}
return 0;
}
```
在这个程序中,我们定义了一个Fraction类来表示分数,其中num表示分子,den表示分母。我们重载了输出运算符<<和输入运算符>>,以便能够直接使用cout和cin输出和输入分数。我们还定义了四个友元函数,分别是加法运算符+、减法运算符-、乘法运算符*和除法运算符/,用于进行分数的加减乘除运算。在每个运算符函数中,我们先计算出运算结果的分子和分母,然后使用辗转相除法求出最大公约数,最后将分子和分母同时除以最大公约数,得到最简分数形式的结果。在main函数中,我们首先使用cin输入两个分数和一个运算符,然后根据运算符调用相应的运算符函数进行运算,并使用cout输出结果。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)