我不明白如何正確使用結構來實現我計算分數的目標(這是必需的)。很坦白地說,我對自己在做什麼沒有太多的想法,這只是我在C++中的第三堂課,我感到失落......這是分配給我們的任務需要幫助理解結構和程序的一些錯誤
您輸入()函數接受用戶的一小部分。如果 是可能的,您的 simpl()函數簡化了它接收的分數。你的display()函數顯示它接收的分數。
您的全局函數使用分數類型。分數類型將 分子的分子和分母作爲單獨的數據成員保存。
這是我的計劃,只有主要除「CIN」和「COUT」和GCF功能是由教授主要我試圖提供的,所有其他功能和結構外做我自己......
#include <iostream>
using namespace std;
void entry (int a, int b);
void simplify (double c);
void display(int x, int y)
int main()
{
struct Fraction fraction;
cout << "Enter a numerator: " << endl;
cin >> fraction.num;
cout << "Enter a denominator: " << endl;
cin >> fraction.den;
cout << "Fraction Simplifier" << endl;
cout << "===================" << endl;
enter(&fraction);
simplify(&fraction);
display(fraction);
}
struct Fraction {
int num;
int den;
}
struct Fraction fraction{
fraction.num;
fraction.den;
}
void display(int num, int den) {
cout << fraction.num << endl;
cout << fraction.den << endl;
}
// Great Common Factor (Euclid's Algorithm), provided by Professor
int gcf(int num1, int num2)
{
int remainder = num2 % num1;
if (remainder != 0)
{
return gcf(remainder,num1);
}
return num1;
}
這些都是我的錯誤:
w2.cpp: In function 'int main()':
w2.cpp: 14: error: aggregate 'Fraction fraction' has incomplete type and cannot be defined
w2.cpp: 23: error: 'enter' was not declared in this scope
w2.cpp: At global scope: w2.cpp:35: error: function definition does not declare parameters
w2.cpp: In function 'void display(int, int)':
w2.cpp: 41: error: 'fraction' was not declared in this scope
我很抱歉這篇文章非常長,但非常感謝所有幫助。 如果有人能指點我一本有用的C++書籍,我可以在家中或在講座時閱讀(因爲語言障礙,我無法很好地理解我的教授)也不勝感激
[The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/636019) – ildjarn
甜蜜,謝謝你的書目清單! – Umeed
在C++中,您必須先將其他函數放在使用它們的函數之前。在main之前鍵入其他函數。你也可以使用前向聲明,但是在這一點上,只需要寫入函數而不是函數調用函數就可以了。你也錯過了一個;在第6行 – Topo