m0_68885233 2022-11-27 00:37 采纳率: 75%
浏览 39
已结题

模拟字符串进入缓存的操作

【程序功能】
模拟字符串进入缓存的操作。
c_replace函数的形参str指向用作缓存的二维数组,temp指向准备进入缓存的字符串,cache指向一个指针数组,该指针数组的NUM个元素按进入缓存的字符串先后顺序指向缓存中的NUM个字符串(例如,cathe[0]指向缓存中最早进入的字符串)。
c_replace函数功能:若temp字符串已存在于str缓存中(命中),则函数返回1。若不命中,则当str缓存未满时,直接将temp字符串追加复制到str缓存中,将缓存中新存入的字符串地址保存到cache数组中,函数返回0;当str缓存已满时,则用temp字符串替换最早进入str缓存的字符串,并调整cache数组中的顺序,函数返回0。
主函数调用c_replace函数并依次向c_replace函数传递10个字符串的地址、缓存(str数组)起始地址及指针数组起始地址,按照指针数组元素的顺序依次输出所指向的最后留在缓存中的字符串,并输出命中率。
【测试数据及运行结果】
输入:
"a ","b ","abc","a ","d ","e ","a ","f ","a ","b "
上面双引号之间都是三个字母,比如a就是三个a

输出:
d e f a b
这也是三个相同字母
hit=0.200000
【含有错误的源程序】

#include<stdio.h>
#include<string.h>
#define NUM 5
int c_replace(char *cache,char str[][80],char *temp)  
{  
int i;
char *t;
   for(i=0;i<NUM&&cache[i]!=NULL;i++)
        if(cache[i]==temp)  return 1;          
   if(i<NUM&&cache[i]==NULL) { 
           strcpy(str[i],temp);
        cache[i]=str[i];
   }
   else{
           t=cache[0];
        for(i=NUM-2;i>=0;i--)      
            cache[i]=cache[i+1];
        cache[NUM-1]=t;
        strcpy(cache[NUM-1],temp);
    }
    return 0;
}
int main(void)
{   
    char * cache[NUM]={0},str[NUM][80];
    char input[80];
        int i,count=0;
float hit;
        for(i=0;i<10;i++){
           gets(input);
            count+=c_replace(cache,str,input);
    }
        hit=count/10;
        for(i=0;i<NUM;i++)
        printf("%s  ",cache[i]);
        printf("hit=%f\n",hit);
           return;
}

img

  • 写回答

1条回答 默认 最新

  • 时语-shine 2022-11-27 18:09
    关注
    
    #include<stdio.h>
    #include<string.h>
    #define NUM 5
    int c_replace(char** cache, char str[][80], char* temp)//char* cache
    {
        int i;
        char* t;
        for (i = 0; i < NUM && cache[i] != NULL; i++)
            if (strcmp(cache[i],temp)==0)  return 1;//cache[i] == temp
        if (i < NUM && cache[i] == NULL) {
            strcpy(str[i], temp);
            cache[i] = str[i];
        }
        else {
            t = cache[0];
            for (i = 0; i <NUM-1; i++)//i=NUM-2;i>=0;i--
                cache[i] = cache[i + 1];
            cache[NUM - 1] = t;
            strcpy(cache[NUM - 1], temp);
        }
        return 0;
    }
    int main(void)
    {
        char* cache[NUM] = { 0 }, str[NUM][80];
        char input[80];
        int i, count = 0;
        float hit;
        for (i = 0; i < 10; i++) {
            gets(input);
            count += c_replace(cache, str, input);
        }
        hit = count / 10.0;// hit = count / 10
        for (i = 0; i < NUM; i++)
            printf("%s  ", cache[i]);
        printf("hit=%f\n", hit);
        return 0;
    }
    

    img


    运行结果如上,改动的地方我都有注释在旁边了,调试不易,若有帮助还请采纳非常感谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月5日
  • 已采纳回答 11月27日
  • 赞助了问题酬金15元 11月27日
  • 修改了问题 11月27日
  • 展开全部

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题