我試圖為此代碼遵循的規則是:
計算折扣百分比、門票總折扣價和節省的金額。
告訴用戶折扣、門票的總價以及用戶節省了多少。
用戶可以輸入大寫和小寫 Y/N
例如,當用戶輸入 10 張票時,有一個學生,但沒有一個客戶。折扣是 10%,90 美元,10 美元節省。
然而
當用戶輸入 6 張票時,他們是新客戶,但不是學生。折扣應該是 15%、51 美元和 9 美元的節省,但程式不會計算這個并說折扣是 0%、60 美元和 0 美元的節省......
我試圖將最后一個條件從else ifto更改else,唯一的區別是另一個計算是錯誤的。這使我相信,從根本上說,我沒有正確地做某事。
請記住,我對 C 比較陌生。這是代碼:
#include <iostream>
using namespace std;
float numberTickets;
char customer;
char student;
float totalPrice;
int main()
{
float discountCustomer;
float discountStudent;
float discountBoth;
float savings;
float savingsCustomer;
float savingsStudent;
cout<<"How many tickets are you purchasing? ";
cin>>numberTickets;
cout<<"Are you a new customer (Y/N): ";
cin>>customer;
cout<<"Are you a student (Y/N): ";
cin>>student;
if (customer=='N' || customer=='n' && student=='N' || student=='n'){
cout<<"You will receive a discount of 0%"<<endl;
}else if (student=='Y' || student=='y' && customer=='N' || customer=='n'){
cout<<"You will receive a discount of 10%"<<endl;
}else if (customer=='Y' || customer=='y' && student=='N' || student=='n'){
cout<<"You will receive a discount of 15%"<<endl;
}else if (customer=='Y' || customer=='y' && student=='Y' || student=='y'){
cout<<"You will receive a discount of 25%"<<endl;
}
totalPrice = (numberTickets)*10;
discountCustomer = (totalPrice)*(0.85);
discountStudent = (totalPrice)*(0.90);
discountBoth = (totalPrice)*(0.75);
savings = (totalPrice)-(discountBoth);
savingsCustomer = (totalPrice)-(discountCustomer);
savingsStudent = (totalPrice)-(discountStudent);
if (customer=='N' || customer=='n' && student=='N' || student=='n'){
cout<<"Your price for "<<numberTickets<<" ticket(s) is $"<<totalPrice<<endl;
}else if (student=='Y' || student=='y' && customer=='N' || customer=='n'){
cout<<"Your price for "<<numberTickets<<" ticket(s) is $"<<discountStudent<<endl;
}else if (customer=='Y' || customer=='y' && student=='N' || student=='n'){
cout<<"Your price for "<<numberTickets<<" ticket(s) is $"<<discountCustomer<<endl;
}else if (customer=='Y' || customer=='y' && student=='Y' || student=='y'){
cout<<"Your price for "<<numberTickets<<" ticket(s) is $"<<discountBoth<<endl;
}
if (customer=='N' || customer=='n' && student=='N' || student=='n'){
cout<<"You saved $0"<<endl;
}else if (student=='Y' || student=='y' && customer=='N' || customer=='n'){
cout<<"You saved $"<<savingsStudent<<endl;
}else if (customer=='Y' || customer=='y' && student=='N' || student=='n'){
cout<<"You saved $"<<savingsCustomer<<endl;
}else if (customer=='Y' || customer=='y' && student=='Y' || student=='y'){
cout<<"You saved $"<<savings<<endl;
}
return 0;
}
uj5u.com熱心網友回復:
仔細檢查運算子優先級的規則。特別要注意 的&&優先級高于||。
| 優先級 | 操作員 | 描述 |
|---|---|---|
| 10 | == != |
對于等式運算子 = 和 ≠ 分別 |
| 14 | && |
邏輯與 |
| 15 | || |
邏輯或 |
因此,考慮如下一行:
if (customer=='N' || customer=='n' && student=='N' || student=='n')
添加一些額外的括號以明確編譯器如何對它們進行分組:
if ((customer=='N') || ((customer=='n') && (student=='N')) || (student=='n'))
當你真正的意思是:
if ((customer=='N' || customer=='n') && (student=='N' || student=='n'))
uj5u.com熱心網友回復:
對我來說,似乎最好避免使用if非常復雜的陳述句。您基本上有四種選擇:不打折、學生打折、新客戶打折和兩者都打折。
我首先設定一個變數來指示他們是否是新客戶和/或學生:
unsigned char student;
unsigned char customer;
int is_student = std::tolower(student) == 'y';
int is_customer = std::tolower(customer) == 'y';
由于我們使用的是tolower,我們需要確保這些值不是負數,所以我已經使用unsigned char了它們。
然后我會將這兩個事實組合成一個數字,這樣我就可以將它們用作索引來在表格中查找正確的折扣:
double discount_table[] = { 100.0, 0.85, 0.90, 0.75 };
std::size_t discount_index = is_customer 2 * is_student;
double discounted_price = raw_price * discount_table[discount_index];
uj5u.com熱心網友回復:
我讀了你的代碼,你知道它很簡單,但是寫得又長又復雜,我不確定我能告訴你最簡單的代碼,但是比現在的代碼更好,而不是使用這么多 if/else 你可以選擇開關,我用開關解決了你的問題,所以看看,所有變數都是一樣的,我只是在代碼中定義了一個新的額外變數'num'。我正在共享影像,請查看代碼的代碼行號以按順序理解它。 在此處輸入影像描述
在此處輸入影像描述
在此處輸入影像描述
在此處輸入影像描述
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/512685.html
