C语言入门常见的代码

自学C语言一段时间了,分享几个常见的代码玩CSDN有一段时间了,第一次发帖子有什么不足的地方多多包涵。

选择语句结构加粗样式

//成绩判断平均值
    int a, b, ave = 0;
    printf("请输入成绩\n");
    scanf("%d%d", &a, &b);
    ave = (a + b) / 2;
    if (ave>=90)
    {
        printf("优秀");
    }
    else
    {

        printf("加油");
    }
//大小写转换
    char zf;
    zf = getchar();
    if (zf>='A' && zf<= 'Z')
    {
        printf("大写转小写:%c\n", zf + 32);
    }
    else if (zf >= 'a' && zf <= 'z')
    {
        printf("小写转大写:%c\n", zf - 32);
    }
    else
    {
        printf("这是一个其他字符:%c\n", zf);
    }
//闰年
    int year;
    scanf("%d",&year);
    if (year % 400 == 0 || year % 4 == 0 && year % 100 != 100)
    {
        printf("%d是闰年\n",year);
    }
    else
    {
        printf("%d不是闰年\n",year);
    }
//一元二次方程
    float a, b, c, d, x1, x2;
    scanf("%f%f%f",&a,&b,&c);
    d = b*b - 4 * a*c;
    if (d>=0)
    {
        x1 = (-b + sqrt(d)) / (2 * b);
        x2 = (-b - sqrt(d)) / (2 * b);
        printf("x1=%fx2=%f\n", x1,x2);
    }
    else
    {
        printf("no root\n");
    }

switch选择结构

int a;
    printf("请输入1~7的数字\n");
    scanf("%d",&a);
    switch (a)
    {
    case 1:
    printf("Monday\n");
    break;
    case 2:
    printf("Tuesday\n");
    break;
    case 3:
    printf("Wednesday\n");
    break;
    case 4:
    printf("Thursday\n");
    break;
    case 5:
    printf("Friday\n");
    break;
    case 6:
    printf("Saturday\n");
    break;
    case 7:
    printf("Sunday\n");
    break;
    default:
    printf("输入有误请重新输入\n");
    break;
//平均值判断优秀(未优化)
    int cj1,cj2;
    scanf("%d", &cj1);
    int aver=cj1/10;
    //aver/10;
    printf("%d", aver);
    switch (aver)
    {
    case 10:
    case 9 :
        printf("优秀\n");
        break;
    case 8 :
        printf("良好\n");
        break;
    case 7:
        printf("中等\n");
        break;
    case 6:
        printf("及格\n");
        break;
    default:
        printf("不及格\n");
        break;
    }

while命令

//算某个学生的平均成绩
	int cj = 0, sum = 0,i = 1;
	while (i<=2)
	{
		printf("第%d科目的成绩\n", i);
		scanf("%d",&cj);
		sum += cj;
		i++;
	}
	printf("该学生的平均值是%d\n", sum / 2);

do-while命令

//算某个学生的平均成绩
	int cj = 0, sum = 0, i = 1;
	do
	{
		printf("第%d科目的成绩\n", i);
		scanf("%d", &cj);
		sum += cj;
		i++;

	} while (i<=2);
	printf("该学生的平均值是%d\n", sum / 2);

for命令

//算某个学生的平均成绩
	int cj = 0, sum = 0;
	for (int  i = 1; i <= 2; i++)
	{
		scanf("%d", &cj);
		sum += cj;
	}
	printf("该学生的平均值是%d\n", sum / 2);
//100~200范围内不能被3整除的数输出
	for (int n = 100; n <= 200; n++)
	{
		if (n % 3 == 0)
			continue;//跳过循环(基本不考)
		printf("%5d", n);
	}
//简易三角形
	for (int  i = 1; i <=5; i++)
	{
		for (int  j = 1; j <=i; j++)
		{
			putchar('*');
		}
		putchar('\n');

	}

循环练习题

//去掉一个最高分和一个最低分后,计算出的平均分为比赛选手得分。
	int i, max = 0, min = 100;
	int score, sum = 0;
	for (i = 1; i <= 6; i++)
	{
		printf("Input score %d: ", i);
		scanf("%d", &score);
		if (max<score)
			max = score;
		if (min>score)
			min = score;
		sum += score;
	}
	printf("Average score:%d\n", (sum - max - min) / (6 - 2));
//超简洁算法(斐波那契数列)
	int f1 = 1, f2 = 1,f3 = 0;
	for (int i = 0; i <40; i++)
	{
		printf("%d\n",f3);
		f3 = f1 + f2;
		f1 = f2;
		f2 = f3;
	}
//判断素数
	int B = 1,n=0;
	scanf("%d",&n);
	for (int i= 2; i <=sqrt(n); i++)
	{
		if (n%i==0)
		{
			B = 0;
			break;
		}
	}
	if (B)
	{
		printf("yes");
	}
	else
	{
		printf("NO");
	}
//99乘法表
	for (int  i = 1; i<=9; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			printf("%d*%d=%d\t", j, i, i*j);
		}
		printf("\n");
	}
//统计字符
	int countint=0;//暂不优化成数组模式
	char ch;
	ch = getchar();
	while (ch != '\n')
	{
		if (ch >= '0'&&ch<='9')
			countint++;
		ch = getchar();
	}
	printf("Total:%d", countint);

数组

//冒泡排序
int main()
{
    int a[100];
    int t;
    for (int  i = 0; i < 6; i++)
    {
        scanf("%d",&a[i]);
    }
    for (int  i = 1; i < 6; i++)
        for (int j = 0; j <6-i; j++)
        {
            if (a[j]>a[j+1])
            {
                t = a[j];
                a[j]=a[j+1];
                a[j+1] = t;
            }
        }
    for (int i = 0; i < 6; i++)
    {
        printf(" %d",a[i]);
    }
system("pause");
return 0;
}
//斐波那契前20项
int main()
{	
	int fb[20];
	fb[0]=1;
	fb[1] = 1;
	for (int i =2; i <20; i++)
	{
		fb[i] = fb[i-1] + fb[i-2];
	}
	for (int  i = 0; i <20; i++)
	{
		printf("%d\n",fb[i] );
	}
	system("pause");
	return 0;
}

你可能感兴趣的:(笔记)