C语言代码集合(1)

1,简单的菜单界面交互

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
    //菜单交互
    while (true)//进入到循环当中去
    {
        //绘制菜单,循环的可视化提醒
        printf("--------【xxx】管理系统--------\n");
        printf("\t0.退出系统\n");
        printf("\t1.录入\n");
        printf("\t2.删除\n");
        printf("\t3.修改\n");
        printf("\t4.查找\n");
        printf("------------------------------\n");
        printf("请输入你的选择(0~4)\n");
        int  userkey = 0;//定义输入变量,用户交互按钮
        scanf_s("%d",&userkey);//输入
        switch (userkey)//用户通过输入来实现循环
        {
            case 0:
                break;
            case 1:
                printf("\t录入模块\n");
                break;
            case 2:
                printf("\t删除模块\n");
                break;
            case 3:
                printf("\t修改模块\n");
                break;
            case 4:
                printf("\t查找模块\n");
                break;
            default:
                printf("错误输入!");
                break;
        }
        if (userkey == 0)
        break;//结束循环的条件
        system("pause");//防止闪屏
        system("cls");//清屏
    }
    printf("退出菜单\n");//退出循环,等于退出了菜单系统
	return 0;
}

2,定时关机程序

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


int main()
{
	int key = 0;
	printf("---------关机小程序---------\n");
	printf("\t1.立刻关机\n");
	printf("\t2.定时60秒关机\n");
	printf("\t3.取消关机\n");
	printf("---------------------------\n");
	printf("请输入一个值(0~3)\n");
	scanf_s("%d", &key);
	switch (key)
	{
	case 1:
		printf("即刻关机\n");
		system("shutdown /s /t 3");
		break;
	case 2:
		printf("60秒后自动关机\n");
		system("shutdown -s -t 60");
		break;
	case 3:
		printf("取消关机\n");
		system("shutdown /a");
		break;
	default:
		printf("无效选项\n");
		break;
	}



	


	return 0;
}

3,输入年月份判断多少天

#include<stdio.h>
int main()
{
	int year = 0;
	int month = 0;
	printf("请输入年份\n");
	scanf_s("%d", &year);
	printf("请输入月份\n");
	scanf_s("%d", &month);
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
	{
		if (month == 1)
			printf("%d月份有31天", month);
		else if (month == 2)
			printf("%d月份有30天", month);
		else if (month == 3)
			printf("%d月份有31天", month);
		else if (month == 4)
			printf("%d月份有28天,且该年是闰年", month);
		else if (month == 5)
			printf("%d月份有31天", month);
		else if (month == 6)
			printf("%d月份有30天", month);
		else if (month == 7)
			printf("%d月份有31天", month);
		else if (month == 8)
			printf("%d月份有31天", month);
		else if (month == 9)
			printf("%d月份有30天", month);
		else if (month == 10)
			printf("%d月份有31天", month);
		else if (month == 11)
			printf("%d月份有30天", month);
		else
			printf("%d月份有31天", month);

	}
	else
	{
		if (month == 1)
			printf("%d月份有31天", month);
		else if (month == 2)
			printf("%d月份有30天", month);
		else if (month == 3)
			printf("%d月份有31天", month);
		else if (month == 4)
			printf("%d月份有30天", month);
		else if (month == 5)
			printf("%d月份有31天", month);
		else if (month == 6)
			printf("%d月份有30天", month);
		else if (month == 7)
			printf("%d月份有31天", month);
		else if (month == 8)
			printf("%d月份有31天", month);
		else if (month == 9)
			printf("%d月份有30天", month);
		else if (month == 10)
			printf("%d月份有31天", month);
		else if (month == 11)
			printf("%d月份有30天", month);
		else
			printf("%d月份有31天", month);
	}
	return 0;
}

4,99乘法表

#include<stdio.h>

int main()
{
	for (int i = 0; i <= 9; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			printf("%d*%d=%d\t",i,j,i*j);
		}
		printf("\n");
	}


	
		for (int i = 100; i <= 999; i++)
		{
			int j = 0;
			for (int j = 2; j <= i; j++)
			{
				if (i % j == 0)
				{
					break;
				}
			}
			if (i == j)
			{
				printf("%d\t", i);
			}

		}
		
	


	return 0;
}

4,100~999之间的素数

#include<stdio.h>

int main()
{
	for (int i = 100; i <= 999; i++)
	{
		int j = 0;
		for ( j = 2; j <= i; j++)
		{
			if (i % j == 0)
			{
				break;
			}
		}
		if (i == j)
		{
			printf("%d\t", i);
		}

	}
	return 0;
}

5,flag打印100~999之间的素数

#include<stdio.h>
#include<stdbool.h>
int main()
{
    int count = 1;
    while (count <= 3)
    {
        printf("第%2d次\n", count);
        count++;
    }
    count = 1;
    while (count++ <= 3)
    {
        printf("第%2d次\n", count);
    }
    do
    {
        printf("yo~bro\n");
    } while (false);

    count = 1;
    do
    {
        printf("第%2d次\n",count);
    } while (count++ <= 3);

    //for (int i = 0; i < 3; i++)//i 出了for的循环就无效了
    //{
    //    printf("第%2d次打印\n", i);
    //}
    for (int i = 0, j = 8; i < j; i++, j--)
    {
        printf("i=%d,j=%d\n", i, j);
    }
    int f1 = 1, f2 = 1, sum = 0, n = 0;
    scanf_s("%d", &n);
    if (n == 1 || n == 2)
    {
        printf("1\n");
    }
    else
    {
        for (int i = 3; i <= n; i++)//i 控制循环次数
        {
            sum = f1 + f2;
            f1 = f2;
            f2 = sum;
        }
        printf("求和的结果是%2d\n", sum);
    }
    for (int i = 0; i < 4; i++)  //行
    {
        for (int j = 0; j < 3; j++)  //列
        {
            printf("%d-%d\t", i, j);
        }
        printf("\n");

    }
    for (int i = 1; i <= 4; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            printf("*");
        }
        printf("\n");
    }
    for (int i = 1; i <= 5; i++)
    {
        for (int j = 4; j >= i; j--)
        {
            printf(" ");
        }
        for (int k = 1; k <= 2*i-1; k++)
        {
            printf("*",k);
        }
        printf("\n");
    }
   /* for (int i = 100; i <= 1000; i++)
    {
        for (int j = 2; j <= 1000; j++)
        {
            if (i % j != 0)
            {
                printf("素数有:%d\t", i);
            }

        }
    }*/
   
    for (int i = 100; i <= 1000; i++)
    {
        int flag = 0;
        for (int j = 2; j < i; j++)
        {
            if (i % j == 0)
            {
                flag = 1;
                //不做跳转会浪费时间
            }
            if (flag == 0)
            {
                printf("%d\t", i);
            }
        }
    }
	return 0;
}

6,数据的输入排序查找删除修改和for循环

#include<stdio.h>//用户输入数据,-1为结束标志
int main()
{
	printf("input nums:\n");
	int arr[100] = { 0 };
	int count = 0;
	for (int i = 0; i < 100; i++)
	{
		scanf_s("%d", &arr[i]);
		if (arr[i] == -1)
			break;
		count++;
		for (int j = count - 1; j > 0; j--)//实现数组的排序count-1是数组下标的最大值
		{
			if (arr[j] < arr[j - 1])
			{
				int temp = 0;
				temp = arr[j - 1];
				arr[j - 1] = arr[j];
				arr[j] = temp;
			}
			else
			{
				break;
			}
		}
	}
	printf("arr: ");
	for (int i = 0; i < count; i++)
	{
		printf("  %d\t", arr[i]);
	}
	printf("\n");
	printf("count:%d\n", count);
	printf("what arr[] do you want to find?:");//这种方法有缺陷,会找到实际数据之外的数值0或者-1
	int findpos = 0;
	scanf_s("%d", &findpos);
	printf("find:%d\n", arr[findpos]);//不够精确
	printf("what arr[] do you want to del?:");//依据数组下标实现数组的指定某个数据的删除
	int delpos = 0;
	scanf_s("%d", &delpos);
	for (int i = delpos; i < count; i++)//删除数据思想,找到待删除数据的下标,
	{
		arr[i] = arr[i + 1];//用后面的数据去覆盖掉前面的数据
	}
	count--;//实现真正的删除
	printf("afterdel: ");
	for (int i = 0; i < count; i++)
	{
		printf(" %d\t", arr[i]);
	}
	printf("\nwhat arr[] do you want to modify?:");
	int ipos = 0;
	scanf_s("%d", &ipos);
	int inum = 0;
	printf("\ninum: ");
	scanf_s("%d", &inum);
	arr[ipos] = inum;
	printf("\naftermodify: ");
	for (int i = 0; i < count; i++)
	{
		printf(" %d\t", arr[i]);
	}
	return 0;
}//代码执行正确结果

7,数组的输入排序while循环

#include<stdio.h>//用户输入数据,-1为结束标志
#include<stdbool.h>
int main()
{
    printf("input nums:\n");
    int arr[100] = { 0 };
    int count = 0;
    int Curside = 0;
    while (true)
    {
        int key = 0;
        scanf_s("%d", &key);
        if (key == -1)
            break;
        arr[Curside++] = key;
        count++;
        for (int i = Curside - 1; i > 0; i--)
        {//数组下标做了i+n或者i-n的操作,就需要考虑临界值
            if ( arr[i]<arr[i-1])
            {
                int temp = i;
                arr[i] = arr[i - 1];
                arr[i - 1] = temp;
            }
            else
            {
                break;
            }
        }
    }
    printf("your arr: \n");
    for (int i = 0; i < Curside; i++)
    {
        printf("%d   ", arr[i]);
    }
    printf("\nnums of arr:\n%d\n",count);//可以避免出现搜出的数值是0的情况
    int pos = -1;
    int Pos = 0;
    printf("what arr[] do you want to find?");
    scanf_s("%d",&Pos);
    for (int k = 0; k < Curside; k++)
    {
        if (arr[k] == arr[Pos])
        {
            pos = arr[Pos];
            break;
        }
    }
    printf("\nfind:%d\n",pos);
    return 0;
}

8,冒泡排序

#include<stdio.h>

int main()
{
	 //冒泡排序法
	int arr[10] = { 0 };
	printf("请输入数据\n");
	for (int i = 0; i < 10; i++)
	{
		scanf_s("%d",&arr[i]);
	}
	int len = 10;
	for (int i = 0; i < len; i++)//遍历区间上的每个值,区间上一共10个数字就需要遍历十次。
	{
		//第一次,i=0的时候把最大值找出来,第二次,i=1的时候找到第二大的数字
		for (int j = 0; j < len-1; j++)//如果用j<len,用j<len-1-i可以优化算法,优化计算次数
		{
			if (arr[j] > arr[j + 1])//条件判定的时候会造成数据溢出,arr栈堆损坏,因为会出现arr[9]>arr[10]的情况
			{
				int temp = 0;
				temp = arr[j+1];
				arr[j + 1] = arr[j];
				arr[j] = temp;
			}
		}
	}
	printf("\narr:");
	for (int i = 0; i < len; i++)
	{
		printf("%d\t",arr[i]);
	}

	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值