user-0 提问: if语句

回复:
user: user-1
content: a + b>c || a + c>b || b + c>a 和 a + b>c && a + c>b && b + c>a) 能是一回事吗？  运算符优先级：//C++ Operators //  Operators specify an evaluation to be performed on one of the following: //    One operand (unary operator) //    Two operands (binary operator) //    Three operands (ternary operator) //  The C++ language includes all C operators and adds several new operators. //  Table 1.1 lists the operators available in Microsoft C++. //  Operators follow a strict precedence which defines the evaluation order of //expressions containing these operators.  Operators associate with either the //expression on their left or the expression on their right;    this is called //“associativity.” Operators in the same group have equal precedence and are //evaluated left to right in an expression unless explicitly forced by a pair of //parentheses, ( ). //  Table 1.1 shows the precedence and associativity of C++ operators //  (from highest to lowest precedence). // //Table 1.1   C++ Operator Precedence and Associativity // The highest precedence level is at the top of the table. //+------------------+-----------------------------------------+---------------+ //| Operator         | Name or Meaning                         | Associativity | //+------------------+-----------------------------------------+---------------+ //| ::               | Scope resolution                        | None          | //| ::               | Global                                  | None          | //| [ ]              | Array subscript                         | Left to right | //| ( )              | Function call                           | Left to right | //| ( )              | Conversion                              | None          | //| .                | Member selection (object)               | Left to right | //| ->               | Member selection (pointer)              | Left to right | //| ++               | Postfix increment                       | None          | //| --               | Postfix decrement                       | None          | //| new              | Allocate object                         | None          | //| delete           | Deallocate object                       | None          | //| delete[ ]        | Deallocate object                       | None          | //| ++               | Prefix increment                        | None          | //| --               | Prefix decrement                        | None          | //| *                | Dereference                             | None          | //| &                | Address-of                              | None          | //| +                | Unary plus                              | None          | //| -                | Arithmetic negation (unary)             | None          | //| !                | Logical NOT                             | None          | //| ~                | Bitwise complement                      | None          | //| sizeof           | Size of object                          | None          | //| sizeof ( )       | Size of type                            | None          | //| typeid( )        | type name                               | None          | //| (type)           | Type cast (conversion)                  | Right to left | //| const_cast       | Type cast (conversion)                  | None          | //| dynamic_cast     | Type cast (conversion)                  | None          | //| reinterpret_cast | Type cast (conversion)                  | None          | //| static_cast      | Type cast (conversion)                  | None          | //| .*               | Apply pointer to class member (objects) | Left to right | //| ->*              | Dereference pointer to class member     | Left to right | //| *                | Multiplication                          | Left to right | //| /                | Division                                | Left to right | //| %                | Remainder (modulus)                     | Left to right | //| +                | Addition                                | Left to right | //| -                | Subtraction                             | Left to right | //| <<               | Left shift                              | Left to right | //| >>               | Right shift                             | Left to right | //| <                | Less than                               | Left to right | //| >                | Greater than                            | Left to right | //| <=               | Less than or equal to                   | Left to right | //| >=               | Greater than or equal to                | Left to right | //| ==               | Equality                                | Left to right | //| !=               | Inequality                              | Left to right | //| &                | Bitwise AND                             | Left to right | //| ^                | Bitwise exclusive OR                    | Left to right | //| |                | Bitwise OR                              | Left to right | //| &&               | Logical AND                             | Left to right | //| ||               | Logical OR                              | Left to right | //| e1?e2:e3         | Conditional                             | Right to left | //| =                | Assignment                              | Right to left | //| *=               | Multiplication assignment               | Right to left | //| /=               | Division assignment                     | Right to left | //| %=               | Modulus assignment                      | Right to left | //| +=               | Addition assignment                     | Right to left | //| -=               | Subtraction assignment                  | Right to left | //| <<=              | Left-shift assignment                   | Right to left | //| >>=              | Right-shift assignment                  | Right to left | //| &=               | Bitwise AND assignment                  | Right to left | //| |=               | Bitwise inclusive OR assignment         | Right to left | //| ^=               | Bitwise exclusive OR assignment         | Right to left | //| ,                | Comma                                   | Left to right | //+------------------+-----------------------------------------+---------------+

user: user-2
content: if条件成立才会进。不确定情况加上括号。

user: user-3
content: 引用 8 楼 qq_40430681 的回复:[quote=引用 2 楼 cfjtaishan 的回复:] #include<stdio.h> #include<math.h>  int main() {     float a, b, c, s, area;     printf("input 3 lengths:");     scanf_s("%f,%f,%f", &a, &b, &c);     if (a == 0 || b == 0 || c == 0)    /* 三边有一个为0，则不能组成三角形*/         printf("cannotconstitute a triangle!");     else if (a + b > c && a + c > b && b + c > a)     {         s = 1.0 / 2 * (a + b + c);         area = sqrt(s*(s - a)*(s - b)*(s - c));         printf("a=%7.2f,b=%7.2f,c=%7.2f", a, b, c);         printf("s=%7.2f,area=%7.4f", s, area);     }      return 0; } 判断条件有问题，if 和else if的||和&&正好调换一下；   --------------------------------------------------- 把你的代码贴到我的vs2015上面也是得不到答案，可能是软件的问题？[/quote] 加一句： system("pause"); 试试

user: user-4
content: 2楼的代码没错啊，需要注意的是 scanf_s("%f,%f,%f", &a, &b, &c); 所以你的输入应该是类似“3,4,5”，VS2015+Windows10 64位运行的结果如下  如果不想输入逗号，则输入语句改为 scanf_s("%f%f%f", &a, &b, &c); 输入及输出的结果如下

user: user-5
content: 引用 6 楼 qq_40430681 的回复:[quote=引用 5 楼 zhao4zhong1 的回复:] 或、且 不是一回事啊！ 引用 1 楼 liaoyaonline 的回复:我运行了一下，没发现错误啊 #include<stdio.h> #include<math.h> void main() { 	float a, b, c, s, area; 	printf("input 3 lengths:"); 	scanf_s("%f,%f,%f", &a, &b, &c); 	if (a == 0 && b == 0 && c == 0) 		printf("cannotconstitute a triangle!"); 	else if ((a + b)>c || a + c>b || b + c>a)这里有错误，你少打了括号，或者说多打了括号。 { 	s = 1.0 / 2 * (a + b + c); 	area = sqrt(s*(s - a)*(s - b)*(s - c)); 	printf("a=%7.2f,b=%7.2f,c=%7.2f", a, b, c); 	printf("s=%7.2f,area=%7.4f", s, area); } 	else; } 当我输入：3，4，5 输出：a=   3.00,b=   4.00,c=   5.00s=   6.00,area= 6.0000。。 符合你的表达式啊。  ---------------------------------------- 我看书上也没括号 ，我是vs 2015，你是什么[/quote] 我用的是vs2017 哦哦，是我看错了，不过我按照你的程序去运行了，结果和我自己按照你的算法的结果一致，没发现什么错误。 但是你要是按照这个来判断是不是三角形的话，你的第一个if就是错误的了，因为它判断的是如果三条边都是<=0输出cannotconstitute a triangle!

user: user-0
content: 引用 2 楼 cfjtaishan 的回复:#include<stdio.h> #include<math.h>  int main() {     float a, b, c, s, area;     printf("input 3 lengths:");     scanf_s("%f,%f,%f", &a, &b, &c);     if (a == 0 || b == 0 || c == 0)    /* 三边有一个为0，则不能组成三角形*/         printf("cannotconstitute a triangle!");     else if (a + b > c && a + c > b && b + c > a)     {         s = 1.0 / 2 * (a + b + c);         area = sqrt(s*(s - a)*(s - b)*(s - c));         printf("a=%7.2f,b=%7.2f,c=%7.2f", a, b, c);         printf("s=%7.2f,area=%7.4f", s, area);     }      return 0; } 判断条件有问题，if 和else if的||和&&正好调换一下；    --------------------------------------------------- 把你的代码贴到我的vs2015上面也是得不到答案，可能是软件的问题？

user: user-0
content: 引用 5 楼 zhao4zhong1 的回复:或、且 不是一回事啊！ 引用 1 楼 liaoyaonline 的回复:我运行了一下，没发现错误啊 #include<stdio.h> #include<math.h> void main() { 	float a, b, c, s, area; 	printf("input 3 lengths:"); 	scanf_s("%f,%f,%f", &a, &b, &c); 	if (a == 0 && b == 0 && c == 0) 		printf("cannotconstitute a triangle!"); 	else if ((a + b)>c || a + c>b || b + c>a)这里有错误，你少打了括号，或者说多打了括号。 { 	s = 1.0 / 2 * (a + b + c); 	area = sqrt(s*(s - a)*(s - b)*(s - c)); 	printf("a=%7.2f,b=%7.2f,c=%7.2f", a, b, c); 	printf("s=%7.2f,area=%7.4f", s, area); } 	else; } 当我输入：3，4，5 输出：a=   3.00,b=   4.00,c=   5.00s=   6.00,area= 6.0000。。 符合你的表达式啊。  ---------------------------------------- 我看书上也没括号 ，我是vs 2015，你是什么

user: user-1
content: 或、且 不是一回事啊！

user: user-6
content: 去看看运算符的优先级。不确定的情况下打括号，不然编译器不知道你要做什么

user: user-7
content: 注意输入格式，如果判断是不是三角形，要任意两边之和大余第三边，用&&吧

user: user-3
content: #include<stdio.h> #include<math.h>  int main() {     float a, b, c, s, area;     printf("input 3 lengths:");     scanf_s("%f,%f,%f", &a, &b, &c);     if (a == 0 || b == 0 || c == 0)    /* 三边有一个为0，则不能组成三角形*/         printf("cannotconstitute a triangle!");     else if (a + b > c && a + c > b && b + c > a)     {         s = 1.0 / 2 * (a + b + c);         area = sqrt(s*(s - a)*(s - b)*(s - c));         printf("a=%7.2f,b=%7.2f,c=%7.2f", a, b, c);         printf("s=%7.2f,area=%7.4f", s, area);     }      return 0; } 判断条件有问题，if 和else if的||和&&正好调换一下；

user: user-5
content: 我运行了一下，没发现错误啊 #include<stdio.h> #include<math.h> void main() { 	float a, b, c, s, area; 	printf("input 3 lengths:"); 	scanf_s("%f,%f,%f", &a, &b, &c); 	if (a == 0 && b == 0 && c == 0) 		printf("cannotconstitute a triangle!"); 	else if ((a + b)>c || a + c>b || b + c>a)这里有错误，你少打了括号，或者说多打了括号。 { 	s = 1.0 / 2 * (a + b + c); 	area = sqrt(s*(s - a)*(s - b)*(s - c)); 	printf("a=%7.2f,b=%7.2f,c=%7.2f", a, b, c); 	printf("s=%7.2f,area=%7.4f", s, area); } 	else; } 当我输入：3，4，5 输出：a=   3.00,b=   4.00,c=   5.00s=   6.00,area= 6.0000。。 符合你的表达式啊。