一.运算符
1.算术运算符
- + * /(除) %(取余数)
2.关系运算符
> >= < <= !=(不等于) ==(等于)
3.逻辑运算符
!(非) &&(并且)``||(或)
4.赋值运算符
= += *= /= -=
运算符优先级
算术>关系>逻辑>赋值
注:遇到优先级问题时,为提高代码可读性,尽量使用括号,不要写出有歧义的代码。
5.除法与取模运算符
测试取模运算符的例子
#include <stdio.h>
int main()
{
printf("%d %d %d %d %d %d \n",3%3,13%-3,-13%3,-13%-3,-13%23,3%5);
return 0;
}
二.流程控制
由此可见:if默认只能控制一个语句的执行或不执行,如果想控制多个语句的执行或不执行,就必须把这些语句用{}括起来。
1.if else 语句
#include <stdio.h>
int main()
{
float score;
printf("请输入您的考试成绩:");
scanf("%f", &score);
if (score >100)
printf("这是做梦!\n");
else if (score>=90 &&score<=100)
printf("优秀!\n");
else if (score>=80 &&score<90)
printf("良好!\n");
else if (score>=60 &&score<80)
printf("及格!\n");
else if (score>=0 &&score<60)
printf("不及格!\n");
return 0;
}
2.for 语句
#include <stdio.h>
int main(void)
{
int i;
int sum=0;
for(i=3;i<=10;++i)
{
if(i%3==0)
sum=sum+i;
printf("sum=%d\n",sum);
}
return 0;
}
3.while 语句
#include <stdio.h>
int main()
{
int val,m;
int sum=0;
printf("请输入你需要判断的数字:");
scanf("%d",&val);
while(m)
{
sum=sum*10+m/10;
m/10;
}
if(sum==val)
printf("Yes!\n");
else
printf("No!\n");
return 0;
}
三.作业
- 5.10 第七题 第十三题
a. x--; or --x; or x = x - 1;
b. m = n % k ;
c. p = q / (b - a);
d. x = (a + b) / (c * d)
- 5.11 第三题 第八题
#include "stdio.h"
int main()
{
int n,w,d;
printf("Input days:\n");
scanf("%d",&n);
w=n/7;
d=n%7;
printf("%d days are %d weeks,%d days.\n",n,w,d);
}
#include <stdio.h>
int main()
{
int a, b;
printf("This program computes moduli.\n");
printf("Enter an integer to serve as the second operand: ");
scanf("%d",&a);
printf("Now enter the first operand: ");
scanf("%d",&b);
printf("%d / %d is %d",b,a,b%a);
printf("\nEnter next number for the first operand: ");
scanf("%d",&b);
while(b>0)
{
printf("%d / %d is %d",b,a,b%a);
printf("\nEnter next number for the first operand: ");
scanf("%d",&b);
}
printf("Done");
return 0;
}
- 6.16 第五题 第十二题 (计算机二级内容) 第十八题
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char ch;
int row,r;
scanf("%c", &ch);
row = ch - 'A' + 1;
for (r = 1; r <= row; r++)
{
//空格的循环
int space;
for(space=row-r+1; space >=1; space--)
{
printf(" ");
}
//正序字母的循环
int n1;
char ch1;
for (ch1 = 'A', n1 = 1; n1 < r; n1++, ch1++)
{
printf("%c", ch1);
}
//倒序字母的循环
int n2;
char ch2;
for (ch2 = 'A'+r-1, n2 = row; n2 >=1&&ch2>='A'; n2--,ch2--)
{
printf("%c", ch2);
}
printf("\n");
}
system("pause");
return 0;
}
- 7.11 第二题 第十题
-
number>=90&&number<100
-
ch!=q||k
-
number>=1&&number<=9&&nu7mber!=5
-
number<1||number>9
#include <stdio.h>
int main()
{
char ch;
while ((ch = getchar()) != '#')
{
if (ch != '\n')
{
printf ("Step1\n");
if (ch != 'c')
{
if (ch == 'b')
break;
else {
if (ch == 'g')
printf ("Step3\n");
else {
printf ("Step2\n");
printf ("Step3\n");
}
}
}
}
}
printf ("Done\n");
}