代码块:
方法一:(利用if-else结构)
#include#include int main() { int price; double discount, final; printf("Please enter price: "); scanf_s("%d", &price); while(price<0){ printf("Error! Retry!nPlease enter price: "); scanf_s("%d", &price); } if(price<100) discount=1.0; else if(price>=100 && price<300) discount=0.95; else if(price>=300 && price<500) discount=0.9; else discount=0.85; final=double(price)*discount; printf("Final Price: %.2fn", final); system("pause"); return 0; }
方法二:(利用switch结构)
#include#include int main() { int price, n; double discount, final; printf("Please enter price: "); scanf_s("%d", &price); while(price<0){ printf("Error! Retry!nPlease enter price: "); scanf_s("%d", &price); } n=price/100; switch(n){ case 0: discount=1.0; break; case 1: case 2: discount=0.95; break; case 3: case 4: discount=0.9; break; default: discount=0.85; break; } final=double(price)*discount; printf("Final Price: %.2fn", final); system("pause"); return 0; }
方法三:(利用函数设计)
#include#include void inputPrice(int *price); double discount(int *price); int main() { int *price=(int*)malloc(sizeof(int)); double final; inputPrice(price); final=double(*price)*discount(price); printf("Final Price: %.2fn", final); system("pause"); return 0; } void inputPrice(int *price){ printf("Please enter price: "); scanf_s("%d", price); while(*price<0){ printf("Error! Retry!nPlease enter price: "); scanf_s("%d", price); } } double discount(int *price){ if(*price<100) return 1.0; else if(*price>=100 && *price<300) return 0.95; else if(*price>=300 && *price<500) return 0.9; else return 0.85; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)