课后总结记录的代码笔记,有事没事就翻翻看,帮助理解记忆。
for语句九九乘法表: int main() ++i等于i=i+1 i++等于 最后结果+1 ++i += i++ 等于 i=i+1 + i++
{ 用逻辑运算符将俩边的变量串起来 称为逻辑表达式
for (int i = 1; i < 10; i++) & 与 符号俩边同时为真,结果为真
{ && 符号俩边同时为真,结果为真
for (int j = 1; j <= i; j++) 若前面的为假,则后面不用算 (短路与·短路或)
{ | 或 :符号俩边有一个为真,则结果为真
printf("%d*%d=%d\t", j, i, j*i); || 符号前为真,则不进行后面的计算 !非:与符号后的值相反
} ^异或:符号俩边结果不同为真,相同为假
printf("\n"); 三元运算符:?: int i=7,j=6
} int k=j>i ? j:i ;
} printf("%d\n",k) 输出k=6
代码 : \n:换行 !=:不等号 加+ 减- 乘* 除/ \转义字符 \t 8个空白符 %占位符 判断为真,则将符号前的值赋给k
if语句编写格式: int main() 循环语句编写格式: int main()
{ {
int n; int i;
printf("\n1.写作业\n2.玩游戏); while (i<10);
scanf_s("%d",&n); {
if( n1) printf("i%d\n",i);
printf(“写作业”); i=i+1;
int s; }
printf(”\n1.画图\n2.抠图”);
scanf_s("%d",&s);
if(s==1)
{
printf("画图”)
}
写作业:画图 、抠图 while语句编出菱形: int main()
{
int i = 1;
while (i < 7)
{
int j = 1; 定义 空格
while (j <7 - i)
{
printf(" “);
j = j + 1;
}
int k = 1;
while (k <= i)
{
printf(”* “);
k = k + 1;
}
printf(”\n");
i = i + 1;
} 正三角形
int s = 1;
while (s < 7)
{
int j = 1;
while (j <=s)
{
printf(" “);
j = j + 1;
}
int k = 1;
while (k <8- s)
{
printf(”* “);
k = k + 1;
}
printf(”\n");
s = s + 1;
}
}
用一个if和俩个while输出三角形:
int main()
{
int i = 1;
while (i < 7)
{
int j = 1;
while (j < 7)
{
if (j <7-i)
{
printf(" “);
}
else
{
printf(”* ");
}
j = j + 1;
}
printf("\n");
i = i + 1;
}
}
用do while语句编写正三角形:(先执行后判断)
int main()
{
int i = 1;
do
{
int j = 1;
do
{
printf(" “);
j = j + 1;
}
while (j < 8 - i);
int k = 1;
do
{
printf(”* “);
k = k + 1;
} while (k <= i);
printf(”\n");
i = i + 1;
}
while (i < 7);
}
switch语句编写 :根据输入月份输出天数
int main()
{ int i;
printf(“输入月份”);
scanf_s("%d", &i);
switch (i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf(“31天”); break;
case 2:
printf(“28天”); break;
case 4:
case 6:
case 9:
case 11:
printf(“30天”); break;
default:
printf(“无此月份”);} }
switch语句中用了布尔类型编写根据输入的年月日显示具体天数和星期:
int main()
{
int i, j, m, sum = 0;
bool runnian;
printf(“输入年份”);
scanf_s("%d", &i);
printf(“输入月份”);
scanf_s("%d", &j);
printf(“输入日期”);
scanf_s("%d", &m);
if ((i % 4 == 0 & i % 100 != 0) || i % 400 == 0)
{
printf(“闰年\n”); runnian = true;
}
else
{
printf(“平年\n”); runnian = false;
}
switch (j)
{
case 2:
if (true)
{
printf(“29天\n”);
}
else
{
printf(“28天\n”);
}break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf(“31天\n”); break;
case 4:
case 6:
case 9:
case 11:
printf(“30天\n”); break;
default:
printf(“无此月份\n”); break;
}
switch (j)
{
case 12:
sum = sum + 30;
case 11:
sum = sum + 31;
case 10:
sum = sum + 30;
case 9:
sum = sum + 31;
case 8:
sum = sum + 31;
case 7:
sum = sum + 30;
case 6:
sum = sum + 31;
case 5:
sum = sum + 30;
case 4:
sum = sum + 31;
case 3:
if (runnian)
{
sum = sum + 29;
}
else
{
sum = sum + 28;
}
case 2:
sum = sum + 31;
}
sum = (365 * (i - 1) + (i - 1) / 4 - 3 * (i - 1) / 400 + m + sum);
switch (sum % 7)
{
case 1:
printf(“周一\n”); break;
case 2:
printf(“周二\n”); break;
case 3:
printf(“周三\n”); break;
case 4:
printf(“周四\n”); break;
case 5:
printf(“周五\n”); break;
case 6:
printf(“周六\n”); break;
default:
printf(“周日\n”);
}
}
//3种不同的表达方法倒序输出数字:
1: int i , a[10];
for (i = 0; i <= 9; i++)
a[i] = i;
for (i = 9; i >= 0; i–)
printf("%d", a[i]);
printf("\n");*/
int a[10] = { 0,1,2,3,4,5,6,7,8,9, };
printf("%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n", a[9], a[8], a[7], a[6], a[5], a[4], a[3], a[2], a[1],a[0]);*/
2: int a[4] = { 0,1,2,3 };
a[0] = 0;
a[1] = 1;
a[2] = 2;
a[3] = 3;
printf("%d\n", a[3]);
printf("%d\n", a[2]);
printf("%d\n", a[1]);
printf("%d\n", a[0]);*/
3: int i = 0;
while (i <= 3)
{
i = i + 1;
}
while (i > 0)
{
i = i - 1;
printf("%d\n", i);
}
//冒泡法排序:
int main()
{
int arr[4], m;
printf(“请输入4个数字:”);
for (int i = 0; i < 4; i++)
{
scanf_s("%d", &arr[i]); -------将输入的4个数给a[0]到a[3]
}
for (int i = 0; i < 3; i++)-----------------要进行3趟
{
for (int j = 0; j < 3 - i; j++)-------------每趟进行多少次比较
{
if (arr[j] > arr[j + 1])
{
m = arr[j];
arr[j] = arr[j + 1];
arr[j + 1]=m;
}
}
}
for (int i = 0; i < 4; i++)-----------------输出a[0]到a[3]
{
printf("%d\n",arr[i]);
}
}
//用二维数组将输入的数字进行列表排列:
int main()
{
int arr[2][3];---------二行三列
printf(“请输入6个数”);
for (int i = 0; i < 2; i++) ----数字赋给前两行a[0]到a[2]
{
for (int j = 0; j < 3; j++)
{
scanf_s("%d", &arr[i][j]);
}
}
for (int i = 0; i < 2; i++)--------将数字以两行三列的形式输出:
{
for (int j = 0; j < 3; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
}
//杨辉三角形前十行:
int main()
{
int a[10][10];
for (int i = 0; i < 10; i++)
{
a[i][0] = 1;
a[i][i] = 1;
}
for (int i = 2; i < 10; i++)
{
for (int j = 1; j < i; j++)
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j <= i; j++)
{
printf("%4d", a[i][j]);
}
printf("\n");
}
}
//查找排序:
int main()
{
int arr[6], m;
printf(“请输入6个数字\n”);
for (int i = 0; i < 6; i++)
{
scanf_s("%d", &arr[i]);
}
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < i; j++)
{
if (arr[i] > arr[j])
{
m = arr[i];
arr[i] = arr[j];
arr[j] = m;
}
}
}
for (int i = 0; i < 6; i++)
{
printf("%d\n", arr[i]);
}
}