2017-02-16 64 views
-2

我終於解決了並通過鏈接數組來通過strcmp函數和比較數組值。感謝你的幫助!!希望你們可以參考我的問題和帖子中列出的答案。如何通過函數鏈接數組值C++

向所有相關人員發表意見和建議。欣賞它。

我的解決方案是通過使用二維數組 - 字符而不是字符串+使用strcmp函數來比較值集以達到Lab7的目標。

最佳,

MM

// Marcus Moo Lab 7.cpp 
// Full Time Student 
// No plagarism 

#include <iostream> 
#include <string> 
using namespace std; 

// Global Declarations 
const float pointLaw[11] = { 5.0,5.0,4.5,4.0,3.5,3.0,2.5,2.0,1.5,1.0,0.0 }; 
const char gradeLaw[11][3] = { "A+","A","A-","B+","B","B-","C+","C","D+","D","F" }; 

// Convert Grade to Point 
float gradeToPoint(const char grade[]); 

int main() 
{ 
    // Header 
    cout << "\tWelcome to University Of Wollongong" << endl; 
    cout << "Grade Point Consultation System" << endl; 
    cout << endl; 

    // Defining moments 
    char grade[3]; 
    float outcome; 
    char intent; 
    int count; 

    // Purpose of consultation system 
    do 
    { 
     cout << "Enter your grade: "; 
     cin >> grade; 
     outcome = gradeToPoint(grade); 
     if (outcome == -1) { 
      cout << "Invalid Grade" << endl; 
     } 
     else { 
      cout << "Your grade point is " << outcome << endl; 
     } 
     cout << "Continue?: "; 
     cin >> intent; 
     cout << endl; 

    } while ((intent == 'y') || (intent == 'Y')); 

    // The end 
    cout << "All the best" << endl; 
} 

float gradeToPoint(const char grade[]) 
{ 
    int count; 
    float points = -1; 
    for (count = 0; count < 12; count++) 
    { 
     if (strcmp(gradeLaw[count], grade) == 0) { 
      points = pointLaw[count]; 
      cout << gradeLaw[count] << " = " << grade << endl; 
     } 
    } 
    return points; 
} 
+1

使用['std :: map'](http://en.cppreference.com/w/cpp/container/map) – NathanOliver

+0

@NathanOliver不是在學校教std :: map? 還有什麼其他方法? –

+0

@NathanOliver無論如何你爲什麼低調回答這個問題?儘管如此,我試圖以更普遍的方式提出這個問題。 –

回答

2

較容易的方法是模擬的紀錄(想在一個表中的一行),採用的結構:

struct Name_Grade 
{ 
    std::string grade; 
    double  point; 
}; 

這將將分數與分數值進行組合。

現在聲明每個數組:

static const grade_table[] = 
{ 
    {"A+", 5.0}, {"A", 5.0}, {"A-", 4.5}, //... 
}; 
size_t rows_in_table = 
    sizeof(grade_table)/sizeof(grade_table[0]); 

你的函數變爲:

double gradeToPoint(const std::string& grade) 
{ 
    double point = 0.0; 
    for (size_t i = 0; i < rows_in_table; ++i) 
    { 
    if (grade == grade_table[i].grade) 
    { 
     point = grade_table[i].point; 
     break; 
    } 
    } 
    return point; 
} 

使用平行陣列:

double grade_to_point(const std::string& grade) 
{ 
    static const std::string grade_texts[]= 
    {"A+", "A", "A-", 
     "B+", "B", "B-", 
     "C+", "C", "C-", 
     "D+", "D", 
     "F"}; 
    static const double point[] = 
    {5.0,5.0,4.5, 
     3.5,3.0,2.5, 
     2.5,2.0,1.5, 
     1.0,0.5, 
     0.0}; 
    static const size_t grade_quantity = 
     sizeof(point)/sizeof(point[0]); 

    double grade_value = 0.0; 
    for (size_t i = 0; i < grade_quantity; ++i) 
    { 
    if (grade == grade_texts[i]) 
    { 
     grade_value = point[i]; 
     break; 
    } 
    } 
    return grade_value; 
} 

一個與平行陣列的問題是,陣列可以是不同的尺寸,或者是不同步的。因此,更安全和更強大的方法是將等級文本保留爲點值(方法struct)。

+0

爲什麼無符號整型作爲尺寸比例的類型?否則這裏有些好東西。 – Bathsheba

+0

我的講師特別希望我們通過我在問題中描述的方式來做到這一點。我希望我可以使用你建議的方法,儘管 –

+0

@Bathsheba:'unsigned int'因爲大小不能是負數。 :-)是的,我應該使用'size_t'。 –

1

我意識到你說你沒有教過地圖,但爲了將來的參考,這可以很容易地解決一個。萬一你最終想要使用地圖,可以清晰地做,像這樣:

std::unordered_map<std::string, double> grades ({{"A+",5.0},{"A", 5.0},{"A-", 4.5}, 
    {"B+", 4.0},{"B", 3.5},{"B-", 3.0},{"C+", 2.5},{"C", 2.0},{"C-", 1.5}, 
    {"D+", 1.0},{"D", 0.5},{"D-", 0.0},{"F", 0.0}}); 

然後你就可以通過grade["yourLetterGradeHere"]符號獲得的成績。 只需確保#include <unordered_map>在您的程序頂部

+0

所以unordered_map本身是一個庫函數? –

+0

是的,unordered_map是包含在C++標準模板庫中的庫數據結構 –

+0

謝謝! michael欣賞它 –