第5天作业

This commit is contained in:
flykhan 2023-07-08 16:16:58 +08:00
parent bc12f92223
commit 441a7c1ba8
16 changed files with 353 additions and 0 deletions

27
day5/homework/h1.c Normal file
View File

@ -0,0 +1,27 @@
// 请编程定义char变量并赋值\101, 输出这个变量的十进制数和字符内容。
// 【提示】\ddd 代表8进制 % c 输出字符
#include <stdio.h>
void printBinary(char num)
{
int size = sizeof(num);
int i = size * 8 - 1;
while (i--)
{
int bit = (num >> i) & 1;
printf("%d", bit);
}
printf("\n");
}
int main()
{
char x = '\101';
printf("十进制数: %d\n", x);
printf("二进制数: ");
printBinary(x);
printf("八进制数: %o\n", x);
printf("十六进制数: 0x%hhx\n", x);
printf("字符内容: %c\n", x);
return 0;
}

15
day5/homework/h10.c Normal file
View File

@ -0,0 +1,15 @@
// 请编程, 输入一个圆的半径,输出圆的周长和面积
#include <stdio.h>
#define PI 3.14 // 定义 PI
int main()
{
int r; // 定义半径
printf("请输入圆的半径:");
scanf("%d", &r);
printf("圆的周长为:%.2f\n", 2 * PI * r);
printf("圆的面积为:%.2f\n", PI * r * r);
return 0;
}

22
day5/homework/h11.c Normal file
View File

@ -0,0 +1,22 @@
// 请编程, 输入两个整数, 输出两个整数所有公约数。
// 例如, 输入 12 和 18 输出 1 2 3 6。
#include <stdio.h>
int main()
{
int a, b;
printf("请输入两个整数:");
scanf("%d%d", &a, &b);
int min = a < b ? a : b; // 取两个数中较小的一个
while (min)
{
if (a % min == 0 && b % min == 0)
{
printf("%d ", min);
}
min--;
}
printf("\n");
return 0;
}

29
day5/homework/h12.c Normal file
View File

@ -0,0 +1,29 @@
// 请编程, 输入一个整数n 输出斐波那契数列中第n位上的数值
// 【提示】斐波那契数列: 1 1 2 3 5 8 13 ...
// 【示例】n = 1, 输出1; n = 7, 输出13.
#include <stdio.h>
int main()
{
int a = 1, b = 1, c, n;
printf("请输入一个整数 n : ");
scanf("%d", &n);
if (n >= 1)
c = a;
if (n >= 2)
c = b;
int temp = n - 2;
while (n > 2 && temp > 0)
{
c = a + b;
// printf("%d ", c);
a = b;
b = c;
temp--;
}
printf("%d\n", c);
return 0;
}

34
day5/homework/h13.c Normal file
View File

@ -0,0 +1,34 @@
// 请编程,实现以下操作: 定义unsigned char 型变量a赋值0x7f
// 将其第1、5、7位置12、3、6位清0其它位保持不变。
#include <stdio.h>
void printBinary(char num)
{
int size = sizeof(num);
for (int i = size * 8 - 1; i >= 0; i--)
{
int bit = (num >> i) & 1;
printf("%d", bit);
}
printf("\n");
}
int main()
{
unsigned char a = 0x7f; // 十六进制
printf("原样输出: ");
printBinary(a);
a |= 0b1010001; // 0b 表示二进制数的标识符
printf("1、5、7位置1输出: ");
printBinary(a);
a &= ~0b100110;
printf("2、3、6位清0输出: ");
printBinary(a);
printf("结果的十六进制输出: ");
printf("0x%hhx\n", a); // 0x59
return 0;
}

66
day5/homework/h14.c Normal file
View File

@ -0,0 +1,66 @@
// 请编程根据个人收入计算个人所得税免税额为3500
// > 7级超额累进税率 >> 全月应纳税所得额 税率 速算扣除数(元) >> 全月应纳税额不超过1500元 3 % 0 >> 全月应纳税额超过1500元至4500元 10 % 105 >> 全月应纳税额超过4500元至9000元 20 % 555 >> 全月应纳税额超过9000元至35000元 25 % 1005 >> 全月应纳税额超过35000元至55000元 30 % 2755 >> 全月应纳税额超过55000元至80000元 35 % 5505 >> 全月应纳税额超过80000元 45 % 13505 >> 例如:你月收入 : 3000,
// 低于免税额3500不必缴税
// >> 月收入 : 4000,
// 4000 - 3500 = 500 < 1500, 属于第一档,缴税 = (4000 - 3500) * 0.03 - 0 == 15 月收入 : 15000, 15000 - 3500 = 11500, 属于第四档, 缴税 = (15000 - 3500) * 0.25 - 1005 == 1870 >> 要求:输入你的税前收入,输出:你最后的所得以及缴纳的税额
#include <stdio.h>
int main()
{
int pretexIncome; // 税前收入
double rate; // 税率
int deduct; // 速算扣除数
int tex; // 缴纳税额
int aftertexIncome; // 税后收入
printf("请输入你的税前收入: ");
scanf("%d", &pretexIncome);
int diff = pretexIncome - 3500; // 征税差值
if (diff <= 0)
{
rate = 0;
deduct = 0;
}
else if (diff > 0 && diff <= 1500)
{
rate = 0.03;
deduct = 0;
}
else if (diff > 1500 && diff <= 4500)
{
rate = 0.1;
deduct = 105;
}
else if (diff > 4500 && diff <= 9000)
{
rate = 0.2;
deduct = 555;
}
else if (diff > 9000 && diff <= 35000)
{
rate = 0.25;
deduct = 1005;
}
else if (diff > 35000 && diff <= 55000)
{
rate = 0.3;
deduct = 2755;
}
else if (diff > 55000 && diff <= 80000)
{
rate = 0.35;
deduct = 5505;
}
else if (diff > 80000)
{
rate = 0.45;
deduct = 13505;
}
tex = diff * rate - deduct;
aftertexIncome = pretexIncome - tex;
printf("你最后的所得为 %d ,你所缴纳的税额为 %d\n", aftertexIncome, tex);
return 0;
}

24
day5/homework/h15.c Normal file
View File

@ -0,0 +1,24 @@
// 【扩展题】请编程,键盘输入一个整数, 输出它的八进制数
// 【提示】使用int数组完成, int m[32];
#include <stdio.h>
int main()
{
int inputNum;
printf("请输入一个整数:");
scanf("%d", &inputNum);
int m[32];
int i = 0;
while (inputNum)
{
m[i++] = inputNum % 8;
inputNum /= 8;
}
printf("八进制数为:");
while (i)
{
printf("%d", m[--i]);
}
return 0;
}

23
day5/homework/h16.c Normal file
View File

@ -0,0 +1,23 @@
// 【扩展题】请编程,键盘输入一个二进制数的字符串,输出转不同进制的数值
// 【提示】char m[32] = "";
#include <stdio.h>
int main()
{
char m[32] = ""; // 用于存储二进制数的字符串
printf("请输入一个二进制数的字符串:");
scanf("%s", m);
int i = 0;
int size = sizeof(m) / sizeof(m[0]); // 数组的大小
int sum = 0; // 用于存储转换后的十进制数
while (i < size && m[i] != '\0')
{
sum = sum * 2 + m[i] - '0'; // 每次会将高位的结果再次乘 2
i++;
}
printf("十进制数为: %d\n", sum);
printf("八进制数为: 0%o\n", sum);
printf("十六进制数为: 0x%hhx\n", sum);
return 0;
}

8
day5/homework/h2.c Normal file
View File

@ -0,0 +1,8 @@
// 请编程,输出 '\0' == 0 的结果
#include <stdio.h>
int main()
{
printf("'\\0' == 0 的结果为: %d\n", '\0' == 0); // \0 需要转义为 \\0
return 0;
}

13
day5/homework/h3.c Normal file
View File

@ -0,0 +1,13 @@
// 请计算,【提示】考虑运算符优先级 int n = 0, m = 1;
// m = n++, n++, n++, m++;
// 问n, m分别为多少
#include <stdio.h>
int main()
{
int n = 0, m = 1;
// m = n++;
m = n++, n++, n++, m++; // m = 0 , 然后 m++, m = 1
printf("n = %d, m = %d\n", n, m);
return 0;
}

8
day5/homework/h4.c Normal file
View File

@ -0,0 +1,8 @@
// 请计算, 12 ^ 3 && 12 & 3 的结果 【提示】考虑运算符优先级
#include <stdio.h>
int main()
{
printf("12 ^ 3 && 12 & 3 的结果为: %d\n", 12 ^ 3 && 12 & 3);
return 0;
}

13
day5/homework/h5.c Normal file
View File

@ -0,0 +1,13 @@
// 请计算, int i = 1;
// i += --i && -5;
// i的结果是多少
#include <stdio.h>
int main()
{
int i = 1;
i += --i && -5; // i = 1 + 0 && -5 = 1 + 0 = 1
// i = ((i + (--i)) && (-5));
printf("i = %d\n", i);
return 0;
}

12
day5/homework/h6.c Normal file
View File

@ -0,0 +1,12 @@
// 请计算, char n = 1;
// n = ~n;
// n的结果是多少
#include <stdio.h>
int main()
{
char n = 1;
n = ~n; // n = ~n = ~00000001 = 11111110 = -2
printf("n = %d\n", n);
return 0;
}

12
day5/homework/h7.c Normal file
View File

@ -0,0 +1,12 @@
// 请计算, short n = 112;
// n &= ~(3 << 4);
// n的结果是多少
#include <stdio.h>
int main()
{
short n = 112;
n &= ~(3 << 4); // n = n & (~(3 << 4)) = n & (~48) = n & (-49) = n & 65487 = 112 & 65487 = 112
printf("n = %d\n", n);
return 0;
}

20
day5/homework/h8.c Normal file
View File

@ -0,0 +1,20 @@
// 请编程,输入一个整数,输出它的所有因数。
#include <stdio.h>
int main()
{
int num;
printf("请输入一个整数: ");
scanf("%d", &num);
int i = 1;
printf("%d 的因数有: ", num);
while (i <= num)
{
if (num % i == 0)
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}

27
day5/homework/h9.c Normal file
View File

@ -0,0 +1,27 @@
// 请编程, 输入一个整数输出它的二进制数中1和0的个数。
#include <stdio.h>
int main()
{
int inputNum;
int temp = 0, zeroCnt = 0, oneCnt = 0;
printf("请输入一个整数: ");
scanf("%d", &inputNum);
for (int i = 0; temp < inputNum; i++)
{
temp = 1 << i;
// if ((inputNum & temp) == temp)
// oneCnt++;
// else if ((inputNum | ~temp) == ~temp)
// zeroCnt++;
if (inputNum & temp)
oneCnt++;
else
zeroCnt++;
}
printf("%d 的二进制中 1 有 %d 个, 0 有 %d 个", inputNum, oneCnt, zeroCnt);
return 0;
}