您好,登录后才能下订单哦!
判断用户名和密码是否正确。
#include <stdio.h>
int main(void) {
    // 定义变量,用来表示用户名和密码
    //char name;
    char name[32];
    //int password;
    char password[16];
    // 输入用户名和密码
    printf("请输入用户名:");
    scanf("%s", name);
    printf("请输入密码:");
    scanf("%s", password);
    if (strcmp(name, "admin") == 0 && 
        strcmp(password, "123456") == 0) {
        // 打印功能菜单
        printf("---交换机后台管理---\n");
        printf("1. 创建账号\n");
        printf("2. IP管理\n");
        printf("3. 退出\n");
    } else {
        printf("用户名或密码错误!\n");
    }
    return 0;
}![[小白到大牛之路5] 交换机后台管理之权限判断](https://cache.yisu.com/upload/information/20200302/59/9525.jpg)
可参考c/c++手册
百度网盘链接:https://pan.baidu.com/s/1dZJLwE
#include <string.h>
int strcmp( const char str1, const char str2 );
比较规则:
按顺先从前往后比较
同序号的字符按“ASCII”码值比较
直到遇到对应字符不等或者字符串结束
返回值:
str1  <  str2时, 返回值< 0(有些编译器返回 -1)
str1  >  str2时, 返回值> 0(有些编译器返回 1)
str1 等于 str2时, 返回值== 0
demo
#include <stdio.h>
int main(void) {
    char name[32];
    int ret;
    printf("请输入您的姓名:");
    scanf("%s", name);
    ret = strcmp(name, "Rock");
    printf("ret=%d\n", ret);
    return 0;
}#include <string.h>
int strncmp( const char str1,  const char str2,  size_t count );
最多比较字符串str1和str2的前count个字符。
demo
#include <stdio.h>
#include <string.h>
int main(void) {
    char name1[32] = "Rock";
    char name2[32];
    int ret;
    fgets(name2, sizeof(name2), stdin); //输入Rock
    ret = strcmp(name1, name2);
    printf("ret=%d\n", ret);
    //
    ret = strncmp(name1, name2, strlen(name1));
    printf("ret=%d\n", ret);
    return 0;
}### 2.其他数据类型的比较运算
char, int, float, double数据的比较都使用:
大于:         >    
大于或等于:      >=  
小于:         <
小于或等于:  <=
不等于:        !=      
等于:         ==     (注意:不是 = )
比较运算的结果:(逻辑值)
结果为“真”:  1
结果为“假”:  0
#include <stdio.h>
int main(void) {
    int a = 100;
    int b = 200;
    int ret;
    ret = a > b;
    printf("ret=%d\n", ret);   //ret=0
    ret = a < b;
    printf("ret=%d\n", ret);   //ret=1
    return 0;
}比较运算的使用场合:
用于“条件判断”
![[小白到大牛之路5] 交换机后台管理之权限判断](https://cache.yisu.com/upload/information/20200302/59/9526.jpg)
使用0和1表示逻辑值![[小白到大牛之路5] 交换机后台管理之权限判断](https://cache.yisu.com/upload/information/20200302/59/9527.jpg)
dome
#include <stdio.h>
int main(void) {
    int a = 100;
    int b = 200;
    int  ret;     //或者 char ret; 
    ret = a > b;
    if (ret) {
        printf("a > b\n");
    } else {
        printf("a <= b\n");
    }
    return 0;
}使用bool类型表示逻辑类型
使用 true 表示真
使用 false表示假
注意:需要包含头文件 stdbool.h
#include <stdio.h>
#include <stdbool.h>
int main(void) {
    int a = 100;
    int b = 200;
    //int ret;     
    bool  ret;
    ret = a > b;
    if (ret) {  //即: if (ret == true) 
        printf("a > b\n");
    } else {
        printf("a <= b\n");
    }
    //true和false是"bool类型的常量"
    printf("true=%d\n", true);
    printf("false=%d\n", false);
    return 0;
}注意:大部分C项目使用的是C89标准中的逻辑值表示方式。
逻辑与  &&![[小白到大牛之路5] 交换机后台管理之权限判断](https://cache.yisu.com/upload/information/20200302/59/9528.jpg)
都为真,逻辑与才是真
只要有一个是假, 逻辑与就是假
相当于“而且”
应用场景:
当需要两个条件都满足时,就使用逻辑与
特别注意:
条件1  &&  条件2
当条件1为真时,才去判断条件2
当条件1为假时,就不再判断条件2
#include <stdio.h>
int main(void) {
    int x = 0;
    int a;
    printf("请输入一个整数:");
    scanf("%d", &a);
    if ((a > 5) && ((x=100) > 90)) {
        printf("OK\n");
    }
    printf("x=%d\n", x);
    return 0;
}### 逻辑或 ||![[小白到大牛之路5] 交换机后台管理之权限判断](https://cache.yisu.com/upload/information/20200302/59/9531.jpg)
都为假,逻辑与才是真
只要有一个是真, 逻辑与就是真
相当于“或者”
应用场景:
只需要满足任意一个条件时,就使用逻辑或
特别注意:
条件1  ||  条件2
当条件1为真时,才不再判断条件2
当条件1为假时,才判断条件2
#include <stdio.h>
int main(void) {
    int x = 0;
    int a;
    printf("请输入一个整数:");
    scanf("%d", &a);
    if ((a > 5) || ((x=100) > 90)) {
        printf("OK\n");
    }
    printf("x=%d\n", x);
    return 0;
}![[小白到大牛之路5] 交换机后台管理之权限判断](https://cache.yisu.com/upload/information/20200302/59/9533.jpg)
相当于“不”
应用场景:
当需要不满足某条件时,就使用逻辑或
特别注意:
逻辑非,只对一个条件进行运算!
是一种“单目运算符”
#include <stdio.h>
int main(void) {
    int age;
    printf("请输入您的年龄: ");
    scanf("%d", &age);
    //特别注意要使用()
    //if ( ! age >= 30) 将导致非预期结果, !会和age结合
    if ( !(age >= 30) ) { 
        printf("您还不到30\n");
    } else {
        printf("您已过而立之年!\n");
    }
    return 0;
}算术运算![[小白到大牛之路5] 交换机后台管理之权限判断](https://cache.yisu.com/upload/information/20200302/59/9535.jpg)
x = 10;  //把x的值设置为10, 把10写到变量x中。
x = 10 + a;
左边必须是变量
“优先级”很低,只比 ","(逗号元素符)高。
x = (3 + 5);  //先计算"+", 再计算“=”
x += 10;    //  x = x + 10
x -= 10;    //  x = x - 10
类的还有: *= , /=, %= 等。
在后续章节中学习。
自增自减运算![[小白到大牛之路5] 交换机后台管理之权限判断](https://cache.yisu.com/upload/information/20200302/59/9537.jpg)
![[小白到大牛之路5] 交换机后台管理之权限判断](https://cache.yisu.com/upload/information/20200302/59/9539.jpg)
注意:
1.只能对变量做++和--运算,不能对变量和表达式做++和--运算
5++;  //ERROR
(3+x)++;  //ERRO
2.建议尽量使用前缀自增(自减),以避免错误。
逗号运算符
优先级最低。
#include <stdio.h>
int main(void) {
    int x;
    // 先计算 x = 3+5,  再计算3*5
    x = 3+5, 3*5, 10/5;
    printf("x=%d\n", x);  //x=8
    //取最后一个表达式的值,作为整个“逗号表达式”的值
    x = (3+5, 3*5, 10/5);  
    printf("x=%d\n", x); //x=2
    return x;
}条件 ? 表达式1 :表达式2
如果条件为真,就取表达式1作为整个表达式的值
如果条件为假,就取表达式2作为整个表达式的值
#include <stdio.h>
int main(void) {
    int year;
    int holiday;
    printf("请输入您的工作年限: ");
    scanf("%d", &year);
    holiday = year > 10 ? 20 : 5; 
    printf("您的年假有%d天\n", holiday);
    return 0;
}类型转换的概念
为什么需要“类型转换”
参与运算的两个操作数的数据类型,必须相同!
类型转换的类别:
1.隐式类型转换
自动完成转换!
1)算数转换
2)赋值转换
3)输出转换
算数转化
(+,-,*,/,%)
char ,   int,   long,   long long,  float,  double 
赋值转换
#include <stdio.h>
int main(void) {
    int x; 
    x = 3.14 * 10;  // 31.4 转换为int类型,因为赋值符号的左边变量的类型是int类型
    printf("%d\n", x);
    return 0;
}
输出转换
#include <stdio.h>
int main(void) {
    printf("%c\n", 255+50);  //305  ->  49 ('1');
    printf("%d\n", 255+50);
    return 0;
}int类型数据, 按照%f格式输出时,将得到错误的输出
float(或double) 类型数据,按照%d格式输出时,将得到错误的输出
强制类型转化
#include <stdio.h>
int main(void) {
    int x = 257 + 100;
    printf("%d\n", x);
    x = (char)257 + 100;
    printf("%d\n", x);
    return 0;
}一共有15个级别!
不需强制记忆,只需要掌握以下常用的优先级:
最高优先级:( )和[ ]
倒数第二低优先级:赋值和复合赋值(=, +=,  -=  ...)
最低优先级:逗号表达式
! > 算术运算符 > 关系运算符 > && > || > 赋值运算符
x =  ! 3 + 4 < 5 && 6 > 7 || 8 > 7;
等效于:
x =  ((!3 + 4 < 5) && (6 > 7)) || (8 > 7); 
demo1
``
#include <stdio.h>
int main(void) {
int salary;
printf("请输入你的期望年薪:");
scanf("%d", &salary);
if (salary >= 200000) {
    printf("你需要精通C/C++开发\n");
} 
printf("OK\n");
return 0;}
**demo2**#include <stdio.h>
int main(void) {
char answer[16];
printf("你有房吗? ");
scanf("%s", answer);
if (strcmp(answer, "yes") == 0) {
    printf("OK");
} else {
    printf("你是一个好人!\n");
}
return 0;}
**demo3**#include <stdio.h>
int main(void) {
char answer[16];
printf("有房吗? ");
scanf("%s", answer);
if (strcmp(answer, "yes") == 0) {
    printf("有房,不错\n");
} else if (printf("有车吗? ") && 
    scanf("%s", answer) && 
    strcmp(answer, "yes")==0) {
    printf("有车,还行\n");
} else if (printf("有病吗? ") && 
    scanf("%s", answer) && 
    strcmp(answer, "no")==0) {
    printf("健康就好!\n");
} else {
    printf("你是一个好人!\n");
}
return 0;}
流程图


**项目练习**
1.独立实现该项目。
2.让用户输入一个成绩,然后输出这个成绩的等级。
   0-59:  不及格
   60-79: 及格
   80-89: 良好
   90-100: 优秀
   其它:非法成绩免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。