printf上的方程式使用else if

时间:2014-02-01 06:06:47

标签: c if-statement equation

创建并运行一个程序,该程序将使用给定的表计算从特定网络呈现的电话呼叫的总成本费用。用户将输入呼叫目的地代码(1到5),时间代码(1 =非高峰,2 =高峰)和呼叫持续时间。

  Off-peak Hours                Rate/min
  1 Calls to the same network     P 8
  2 Call to other network         P 8
  3 Call to a landline            P 8
  4 National direct dial          P 11
  5 International direct dial     P 20

  Peak hours                     Rate/min
  1 Calls to the same network     P 3
  2 Call to other network         P 4
  3 Call to a landline            P 4
  4 National direct dial          P 7
  5 International direct dial     P 20

*程序运行但所有条件都遵循eq1,即使我告诉它打印eq2,eq3,eq4,eq5或eq6 ...意味着我输入的每个数字只是将duc乘以8。

---------------------------------------...

#include<stdio.h>
#include<conio.h>

main()
{
int dc, tc, duc, eq1, eq2, eq3, eq4, eq5, eq6;

printf("Time Code:");
scanf("%d", &tc);
printf("Destination Code:");
scanf("%d", &dc);
printf("Duration Call:");
scanf("%d", &duc);

eq1=duc*8;
eq2=duc*11;
eq3=duc*3;
eq4=duc*4;
eq5=duc*7;
eq6=duc*20;

{
if((tc=1) && (dc=1))
printf("%d", eq1);
else if((tc=1) && (dc=2))
printf("%d", eq1);
else if((tc=1) && (dc=3))
printf("%d", eq1);
else if((tc=1) && (dc=4))
printf("%d", eq2);
else if((tc=1) && (dc=5))
printf("%d", eq6);
else if((tc=2) && (dc=1))
printf("%d", eq3);
else if((tc=2) && (dc=2))
printf("%d", eq4);
else if((tc=2) && (dc=3))
printf("%d", eq4);
else if((tc=2) && (dc=4))
printf("%d", eq5);
else if((tc=2) && (dc=5))
printf("%d", eq6);
}

getch();
}

2 个答案:

答案 0 :(得分:4)

if((tc=1) && (dc=1))

ifelse if中的所有条件都使用赋值运算符=,而您应使用==来测试相等性。

答案 1 :(得分:0)

=是赋值运算符
==是等式运算符

if(tc=1)if(1)相同,因此无论tc值如何(即使它不等于1),也始终为真。

让你成为habbit,以=运算符作为分配给变量(就像tc被赋予1的值一样)。虽然==可以读作等于,或等于。当您向其他人解释代码时,它也会有所帮助。