c语言经典题目 91-100

第91题:

链接:C 练习实例91 | 菜鸟教程

分析:先要获取时间戳,然后把时间戳转换成实际时间,再转换成字符串

完整代码:

复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
int main()
{
	time_t rawtime;
	struct tm* timeinfo;

	time(&rawtime);
	timeinfo = localtime(&rawtime);
	printf("Current local time and date: %s", asctime(timeinfo));

	return 0;
}

第92题:

链接:C 练习实例92 | 菜鸟教程

完整代码:

复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>

int main()
{
	time_t start, end;
	start = time(NULL);

	for (int i = 0; i < 300000; i++)
	{
		printf("\n");
	}

	end = time(NULL);

	printf("时间间隔为:%6.3f",difftime(end,start));

	return 0;

}

第93题:

链接:C 练习实例93 | 菜鸟教程

完整代码:

复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>

int main()
{
    long i = 10000000L;
    clock_t start, finish;
    double TheTimes;
    printf("做%ld次空循环需要的时间为", i);
    start = clock();
    while (i--);
    finish = clock();
    TheTimes = (double)(finish - start) / CLOCKS_PER_SEC;
    printf("%f秒。\n", TheTimes);

	return 0;
}

第94题:

链接:C 练习实例94 | 菜鸟教程

完整代码:

复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// 处理是否继续游戏的逻辑
int ask_to_play_again() {
    char begin;
    printf("需要挑战最高级别不?Y/N \n");
    scanf(" %c", &begin);  // 注意前面有个空格,确保跳过上次输入的换行符
    if (begin == 'Y' || begin == 'y') {
        return 1;  // 用户选择继续游戏
    }
    printf("谢谢,再见!\n");
    return 0;  // 用户选择退出游戏
}

// 游戏的主体逻辑
void caizi(void) {
    int n;
    int count = 1;
    srand((unsigned int)time(NULL));  // 更严谨地处理随机数种子
    int m = (rand() % 100) + 1;

    printf("游戏开始,请输入数字:\n");

    while (1) {
        scanf("%d", &n);

        if (n == m) {
            printf("猜中了,使用了 %d 次!\n", count);

            // 根据猜测次数,给出不同的评价
            if (count == 1) {
                printf("你是神级人物了!膜拜\n");
            }
            else if (count <= 5) {
                printf("你是王级人物了!非常赞\n");
            }
            else if (count <= 10) {
                printf("你是大师级人物了!狂赞\n");
            }
            else if (count <= 15) {
                printf("你是钻石级人物了!怒赞\n");
            }
            else {
                printf("你的技术还有待提高哦!\n");
            }

            // 询问是否继续游戏
            if (ask_to_play_again()) {
                caizi();  // 重新开始游戏
            }
            break;
        }
        else if (n < m) {
            puts("太小了!");
            puts("重新输入:");
        }
        else {
            puts("太大了!");
            puts("重新输入:");
        }

        count++;  // 计数器
    }
}

int main(void) {
    caizi();
    return 0;
}

第95题:

链接:C 练习实例95 | 菜鸟教程

完整代码:

复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct programming {
	float constant;
	char* pointer;
};


int main()
{
	struct programming p1;
	char string[] = "hello world";

	p1.constant = 1.23;
	p1.pointer = string;
	printf("%f %s\n",p1.constant,p1.pointer);

	return 0;
}

第96题:

链接:C 练习实例96 | 菜鸟教程

完整代码:

复制代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int i,j,k,TLen,PLen,count=0;
    char T[50],P[10];
    printf("请输入两个字符串,以回车隔开,母串在前,子串在后:\n");
    gets(T);
    gets(P);
    TLen=strlen(T);
    PLen=strlen(P);
    for(i=0;i<=TLen-PLen;i++)
    {
        for(j=0,k=i;j<PLen&&P[j]==T[k];j++,k++)
            ;
        if(j==PLen)count++;
    }
    printf("%d\n",count);
    system("pause");
    return 0;
}

第97题:

链接:

C 练习实例97 | 菜鸟教程

完整代码:

复制代码
#include<stdio.h>
#include<stdlib.h>
int main()
{
    FILE*fp=NULL;
    char filename[25];
    char ch;
    printf("输入你要保存到的文件的名称:\n");
    gets(filename);
    if((fp=fopen(filename,"w"))==NULL)
    {
        printf("error: cannot open file!\n");
        exit(0);
    }
    printf("现在你可以输入你要保存的一些字符,以#结束:\n");
    getchar();
    while((ch=getchar())!='#'){
        fputc(ch,fp);
    }
    fclose(fp);
    system("pause");
    return 0;
}

第98题:

链接:C 练习实例98 | 菜鸟教程

完整代码:

复制代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
int main()
{
    FILE*fp=NULL;
    char str[50];
    int i,len;
    printf("输入一个字符串:\n");
    gets(str);
    len=strlen(str);
    for(i=0;i<len;i++)
    {
        if(str[i]<='z'&&str[i]>='a')
            str[i]-=32;
    }
    if((fp=fopen("test","w"))==NULL)
    {
        printf("error: cannot open file!\n");
        exit(0);
    }
    fprintf(fp,"%s",str);
    fclose(fp);
    
    system("pause");
    return 0;
}

第99题:

链接:C 练习实例99 | 菜鸟教程

完整代码:

复制代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    FILE*fa,*fb,*fc;
    int i,j,k;
    char str[100],str1[100];
    char tem;
    if((fa=fopen("A.txt","r"))==NULL) // A.txt 文件需要存在
    {
        printf("error: cannot open A file!\n");
        exit(0);
    }
    fgets(str,99,fa);
    fclose(fa);
    if((fb=fopen("B.txt","r"))==NULL)  // B.txt 文件需要存在
    {
        printf("error: cannot open B file!\n");
        exit(0);
    }
    fgets(str1,100,fb);
    fclose(fb);
    strcat(str,str1);
    for(i=strlen(str)-1;i>1;i--)
        for(j=0;j<i;j++)
            if(str[j]>str[j+1])
            {
                tem=str[j];
                str[j]=str[j+1];
                str[j+1]=tem;
            }
    
    if((fc=fopen("C.txt","w"))==NULL)  // 合并为 C.txt
    {
        printf("error: cannot open C file!\n");
        exit(0);
    }
    fputs(str,fc);
    fclose(fc);
    system("pause");
    return 0;
}

第100题:

链接:C 练习实例100 | 菜鸟教程

完整代码:

复制代码
#include<stdio.h>
#include<stdlib.h>
typedef struct{
    int ID;
    int math;
    int English;
    int C;
    int avargrade;
    char name[20];
}Stu;
int main()
{
    FILE*fp;
    Stu stu[5];
    int i,avargrade=0;
    printf("请输入5个同学的信息:学生号,姓名,3门成绩:\n");
    for(i=0;i<5;i++)
    {
        scanf("%d %s %d %d %d",&(stu[i].ID),stu[i].name,&(stu[i].math),&(stu[i].English),&(stu[i].C));
        stu[i].avargrade=(stu[i].math+stu[i].English+stu[i].C)/3;
    }
    
    if((fp=fopen("stud","w"))==NULL)
    {
        printf("error :cannot open file!\n");
        exit(0);
    }
    for(i=0;i<5;i++)
        fprintf(fp,"%d %s %d %d %d %d\n",stu[i].ID,stu[i].name,stu[i].math,stu[i].English,
                stu[i].C,stu[i].avargrade);
    
    fclose(fp);
    // system("pause");
    return 0;
}
相关推荐
进击的小头2 小时前
行为型模式:策略模式的C语言实战指南
c语言·开发语言·策略模式
爱编码的小八嘎3 小时前
C语言对话-5.通过任何其他名字
c语言
定偶5 小时前
C语言入门指南
c语言·开发语言
的卢马飞快6 小时前
【C语言进阶】给数据一个“家”:从零开始掌握文件操作
c语言·网络·数据库
我能坚持多久6 小时前
D17—C语言结构体详解:从声明、对齐到位段应用
c语言·开发语言
傻乐u兔7 小时前
C语音进阶————数据在内存中的存储2
c语言·开发语言·算法
二年级程序员7 小时前
自定义类型:联合体与枚举
c语言
麒qiqi8 小时前
ADC 的原理与实战
c语言·开发语言·单片机·嵌入式硬件
比昨天多敲两行8 小时前
C/C++内存管理
c语言·开发语言·jvm