C语言重点回忆

2 篇文章 0 订阅
2 篇文章 0 订阅

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);
}

4. 求最大公因子

#include <stdio.h>
int main()
{
	int m, n, r;
	printf("please input two positive integer: ");
	scanf("%d%d", &m, &n);
	while (n != 0)
	{
		r = m % n;
		m = n;
		n = r;
	}
	printf("Their greatest common divisor is %d\n", m);
}

5.求1~100的累计和

do…while:

#include <stdio.h>
int main()
{
	int i=1, sum=0;
	do
	{
		sum += 1;
		i++;
	}
	while (i <= 100);
	printf("sum=%d\n", sum);
}

for:

#include <stdio.h>
int main()
{
	int i, sum=0;
	for(i=1;i <= 100; i++)
		sum += 1;
	printf("sum=%d\n", sum);
}

6.转换大小写算法

#include <stdio.h>
int main()
{
	char c;
	while (1)
	{
		c=getchar();       //读取一个字符
		if(c>='a' && c<='z')    //是小写字母
			putchar(c-'a'+'A');
		else
			break;     //循环推出
	}
}

7.输入十个整数中正数的个数及其平均值

#include <stdio.h>
int main()
{
	int i, a, num=0;
	float sum=0;
	for (i=0; i<10; i++)
	{
		scanf("%d", &a);
		if (a <= 0)
			continue;
		num++;
		sum += a;
	}
	printf("%d plus integer's sum: %.0f\n", num, sum);
	printf("average value: %.2f\n", sum / num);
}

8.三角形边长 求面积

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
	float a, b, c;
	float s, area;
	printf("input the length of three edgee of triangle: ");
	scanf("%f%f%f", &a, &b, &c);
	if(a <=0 || b <= 0 || c <= 0)
	{
		printf("the length of three edges of triangle is error!\n");
		exit (-1);
	}
	s = (a + b + c) / 2;
	s = s * (s-a) * (s-b) * (s-c);
	if (s<0)
	{
		printf("the length of three edges of triangle is error!\n ");
		exit (-1);
	}
	area = (float)sqrt(s);
	printf("area=%.2f\n", area);
}

参考图书

C语言程序设计教程(第二版)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值