这个c程序如果输入20的话,是不是只执行最后一个else后面的语句???

问题描述

这个c程序如果输入20的话,是不是只执行最后一个else后面的语句???

// Exercise 3.3 Calculate a discounted price

// I interpreted this exercise as implying that the 10% applies to items 31 to 50
// and the 15% applies to items in excess of 50.
// That is, you don't get 15% discount on the whole price when you order 51 items.

// There is more than one way of doing this so different is not necessarily wrong.

#include

int main(void)
{
const int level1 = 30; // Quantity over this level are at discount1
const int level2 = 50; // Quantity over this level are at discount2
const double discount1 = 0.10; // 10% discount
const double discount2 = 0.15; // 15% discount
const double unit_price = 5.0; // Basic unit price
int quantity = 0;
int qty_full_price = 0; // 0 to 30 at full price
int qty_level1 = 0; // 31 to 50 at level1 price
int qty_level2 = 0; // Over 50 at level2 price
printf("Enter the quantity that you require: ");
scanf("%d", &quantity);

if(quantity > 50) // Quantity over 50
{
qty_full_price = level1;
qty_level1 = level2 - level1;
qty_level2 = quantity - level2;
}
else if(quantity > 30) // Quantity is from 30 to 50
{
qty_full_price = level1;
qty_level1 = quantity - level1;
}
else
qty_full_price = quantity;

printf("The total price for %d items is $%.2lfn", quantity,
unit_price*(qty_full_price + (1.0 - discount1)*qty_level1 + (1.0 - discount2)*qty_level2));
return 0;
}

解决方案

 是的,小于等于30的数都走最后一个else分支。也就是qty_full_price = quantity;这条语句。再后面的语句是公共的,不管哪个分支最后都会执行

解决方案二:

你最后一个else最好加一个花括号将执行的程序包括起来,这样你自己看着也方便简单些。
是的执行最后一个else,但是你的判断语句好像有问题,

解决方案三:

对,只执行最后一个。if...else...排他的

解决方案四:

但是这样程序就算不对,我用电脑输入20最后结果是100.但是我用笔算怎么也算不出100啊,我算出0了。。。。你算算看

解决方案五:

你觉得是

 else if (...) {  }

其实是

 else
{
    if (...) {  }
}

因为是单条语句,else后面的块括号不用写。另外你要明白,C++不管你的缩进的。
好比

 if (xxx)
   aaa;
     bbb;
ccc;

bbb总是执行。

解决方案六:

qty_full_price = quantity; -- qty_full_price = 数量20吗?

printf("The total price for %d items is $%.2lfn", quantity, -- 数量20
unit_price*(qty_full_price + (1.0 - discount1)*qty_level1 + (1.0 - discount2)*qty_level2));
-- qty_level1 = 0、qty_level2 =0、
那么unit_price*(qty_full_price + (1.0 - discount1)*qty_level1 + (1.0 - discount2)*qty_level2)) = 5.0 *(20+0 +0)=100,不是很正确吗?

查查输入20时,qty_level1 和 qty_level2有没有赋值?

解决方案七:

C语言风格
if ( xxx)
a;
b;
如条件成立 a 和 b都执行,不成立 b执行
if ( xxx)
a;
else
b;
c;
如条件成立 a c 执行,不成立 b c执行

时间: 2024-08-31 16:39:45

这个c程序如果输入20的话,是不是只执行最后一个else后面的语句???的相关文章

c++-编写程序,输入字符串S1和S2 以及插入位置n,在字符串S1中的指定位置n处插入字符串S2。

问题描述 编写程序,输入字符串S1和S2 以及插入位置n,在字符串S1中的指定位置n处插入字符串S2. 编写程序,输入字符串S1和S2 以及插入位置n,在字符串S1中的指定位置n处插入字符串S2.例如,输入"jiangsu"."123"和位置3,则输出"ji123angsu". 解决方案 #include <iostream> #include <string> using namespace std; int main(

emoji-写好的程序在输入iphone自带输入法的EMOJI表情时会崩溃。

问题描述 写好的程序在输入iphone自带输入法的EMOJI表情时会崩溃. 每次在对话框里输入IPHONE自带的EMOJI表情都会崩溃,NSSTRING不能接收EMOJI表情么?应该用什么接收啊,还是需要在程序里添加什么东西来满足EMOJI表情的需求么?

编码-霍夫曼树程序,输入字符串统计字符出现次数并译码。请问如何改成从文件读入字符串?

问题描述 霍夫曼树程序,输入字符串统计字符出现次数并译码.请问如何改成从文件读入字符串? //生成HuffmanCode文件的两个函数void HuffmanEncoding(HuffmanTree HTHuffmanCode HC){//根据HuffmanTreeHT求HuffmanCode表HC int cpi; char cd[n]; int start; cd[num] = ''; for(i = 1;i <= num;i++){ start = num; c = i; while((p

linux shell,将数据流重定向作为下一个程序的输入,由于有缓冲机制,数据流无法实时进行处理

问题描述 linux shell,将数据流重定向作为下一个程序的输入,由于有缓冲机制,数据流无法实时进行处理 上述问题可以简化为以下问题: python脚本如下: #coding=utf-8 import sys import os import time if __name__ == '__main__': while True: print time.strftime('%Y-%m-%d %H:%M:%S') time.sleep( 5 ) 然后通过linux命令行:python produ

求c语言编写的经纬高转换成XYZ的程序,只要能简单运行的程序,悬赏20个币,求帮忙

问题描述 求c语言编写的经纬高转换成XYZ的程序,只要能简单运行的程序,悬赏20个币,求帮忙 求c语言编写的经纬高转换成XYZ的程序,只要能简单运行的程序,不要复杂的界面,悬赏20个币,求帮忙.程序要求能读取存储经纬高数据的文本,转换成XYZ存成文本,给力的话可以再加币,多谢多谢! 解决方案 纬度和高度都是可以直接转换的.只有经度,经度根据纬度可以换算,公式是cos(维度)*(赤道周长/360) 解决方案二: 以地球为严格的球体来做的话,以球心画出一个参考的三围坐标系,不难得到 #include

数据-卡尔曼滤波matlab程序怎么输入矩阵变量?

问题描述 卡尔曼滤波matlab程序怎么输入矩阵变量? 一般卡尔曼的程序都是一个固定值,我的数据是18~31度的温度数据,这个怎么才能输入呢?还有后面的程序涉及加减法的怎么改呢? 解决方案 matlab矩阵输入直接输入不就行么? 如>>a=18:1:31;

c++-折半查找程序,输入后就崩溃,谁能帮忙看一下

问题描述 折半查找程序,输入后就崩溃,谁能帮忙看一下 #include #include using namespace std; int half(int,vector ); int low=0; int high=7; int main(){ int b[10]={1,10,15,17,18,21,22,35,54,65}; vector a(b,b+10); cout<<"请输入要査找的数:"; int number; cin>>number; if(ha

c++-C++编写一个程序,输入一个广义表,对广义表遍历并且计算广义表的个数。

问题描述 C++编写一个程序,输入一个广义表,对广义表遍历并且计算广义表的个数. C++编写一个程序,输入一个广义表,对广义表遍历并且计算广义表的个数. 解决方案 http://blog.csdn.net/jack_wong2010/article/details/6910200

怎样做一个程序对另一个程序的输入进行测试?

问题描述 怎样做一个程序对另一个程序的输入进行测试? 例如: 现在有一个程序,一打开就要输入一个数,输入正确就显示OK,输入错误就显示Error. 现在我想做一个程序,对上面的程序要输入的数进行测试.怎样获得对上面程序的输入?怎样获得对上面程序的输入后的返回值? 解决方案 什么程序呢,可以通过截获内存信息的方式来解析.