1.输入年月,判断这个月有多少天
int judge_month(int year, int month)
{
int day_num;
if (month = 2)
{
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
day_num = 29;
}
else
{
day_num = 28;
}
}
else
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day_num = 31;
break;
case 4:
case 6:
case 9:
case 11:
day_num = 30;
break;
default:
break;
}
}
return day_num;
}
2.判断一个数是否是素数
bool pri_num(int num)
{
for (int i = 2; i < num; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
3.计算出100以内的全部素数。
void pri_number()
{
for (int i = 1; i <= 100; i++)
{
if (i <= 3)
{
printf("%5d", i);
continue;
}
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{
break;
}
else if(j==i-1)
{
printf("%5d", i);
}
}
}
}
4.
int sum(int a,int n)
{
//方法一
/*int sum = 0;
int sum1 = 0;
for (int i = 0; i < n; i++)
{
sum1 = a * (pow(10,i))+sum1; //函数pow(10,i)10是底数,i是指数
sum = sum + sum1;
}*/
//方法二
int sum = a;
int sum1 = a;
for (int i = 1; i < n; i++)
{
sum1 = a + sum1* 10;
sum = sum + sum1;
}
return sum;
}
5.//求一个数是否是水仙花数
bool daff_num(int target)
{
int a = target / 100;
int b = target % 100/10;
int c = target % 10;
if (pow(a, 3) + pow(b, 3) + pow(c, 3) == target)
{
return true;
}
else
{
return false;
}
}
6.制作一个简易计算器
counter.h
#define _CRT_SECURE_NO_WARNINGS
#include"counter.h"
int main()
{
int x, y;
char a, b = 'y';//a是运算符,b是y/n
do
{
printf("请输入需要计算的式子:");
scanf("%d%c%d", &x, &a, &y);
counter(x, a, y);
printf("是否继续(请输入y/n):");
rewind(stdin);
scanf("%c", &b);
} while (b == 'Y' || b == 'y');
return 0;
}
counter.c
#include"counter.h"
int add(int a, int b)
{
int ret = a + b;
return ret;
}
int sub(int a, int b)
{
int ret = a - b;
return ret;
}
int div(int a, int b)
{
int ret = a / b;
return ret;
}
int mul(int a, int b)
{
int ret = a * b;
return ret;
}
void counter(int x, char op, int y)
{
switch (op)
{
case '+':printf("结果=%d\n", add(x, y)); break;
case '-':printf("结果=%d\n", sub(x, y)); break;
case '*':printf("结果=%d\n", mul(x, y)); break;
case '/':if (y == 0)
{
printf("非法输入,请重新输入\n");
}
else
{
printf("结果=%d\n", div(x, y));
}
break;
default:
printf("非法输入");
break;
}
}
main.c
#define _CRT_SECURE_NO_WARNINGS
#include"counter.h"
int main()
{
int x, y;
char a, b = 'y';//a是运算符,b是y/n
do
{
printf("请输入需要计算的式子:");
scanf("%d%c%d", &x, &a, &y);
counter(x, a, y);
printf("是否继续(请输入y/n):");
rewind(stdin);
scanf("%c", &b);
} while (b == 'Y' || b == 'y');
return 0;
}