C语言重点回忆(待补充)
1.员工薪水算法:
#include <stdio.h>
void main()
{
long profit; //所接工程利润
float ratio; // 提成比率
float salary=500; // 薪水, 初始值为保底薪水500
printf("Input profit:"); //提示输入利润
scanf("%d", &profit);
// 计算提成比率
if (profit <= 1000)
ratio=0;
else if (profit <= 2000)
ratio=(float)0.10;
else if (profit <= 5000)
ratio=(float)0.15;
else if (profit <=10000)
ratio=(float)0.20;
else ratio=(float)0.25;
salary += profit * ratio;
printf("salary=%.2f\n",salary);
}
2.计算闰年算法:
#include <stdio.h>
int main()
{
int year, leap=0; # leap=0 ,预设非闰年
printf("please input the year:"); // 提示输入年份
scanf("%d", &year);
if (year % 4 == 0)
if(year % 100 != 0)
leap = 1;
if (year % 400 == 0)
leap = 1;
// 输出结果
if (leap)
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
}
3.运算符 算法
#include <stdio.h>
int main()
{
float a, b; //存放数字变量
int tag=0; //运算合法的标志, 0--合法,1--不合法
char ch; // 运算符变量
float result; // 运算结果变量
printf("input two number: "); // 提示输入两个数
scanf("%f%f", &a, &b);
fflush(stdin); // 清键盘缓冲区
printf("input arithmetic lable(+-*/): "); // 提示输入运算符
scanf("%c", &ch);
switch (ch) // 根据运算符来进行相关的运算
{
case '+': result = a + b; break; //加法运算
case '-': result = a - b; break; //减法运算
case '*': result = a * b; break; //乘法运算
case '/': //除法运算,
if (!b) //判断除数是否为0
{
printf("divisor is zero!\n"); // 显示除数为0
tag = 1; //置运算非法标志
}
else // 除数非0
result = a / b; //计算商
break;
default: printf("iallegal arithmetic lable\n"); //非法运算符
tag = 1; //置运算非法标志
}
if (!tag) // 运算符合法, 显示运算结果
printf("%.2f %c %.2f=%.2f\n", a, ch, b, result);
}
参考图书
C语言程序设计教程(第二版)