C language Notes: cyclic array

Original link: http://c.biancheng.net/c/

Article Directory

machine translation

Problem description:
a translation of a student software installed on your computer, the principle is followed from start to finish each English word used to replace the corresponding Chinese meaning. The software will first look for each English word in memory of the Chinese word meaning, if there is memory, translation software will use it; if there is no memory, the software will look for in the external memory in the dictionary, find out the Chinese word meaning and translation, and Italian translation of this word into memory and facilitate the subsequent search and translation, is required

  • Two input lines, the first line of the input two positive integers M (not more than 100) and N, representing the memory capacity and the length of the article (i.e. the number of words in a sentence in English); a second input line is N nonnegative integer, according to the article order, followed by the input of each word (here using numbers instead of English words)
  • The number of external memory output software need to check dictionary

Implementation code:

#include <stdio.h>
#include <stdlib.h>
int Search(int a[],int n,int x);//在n个元素数组a中查找查找x,找到返回1,否则返回0
int main(void)
{
 int M,N;//存储内存的容量以及单词的个数
 int x;//用整数代表(模拟)单词
 scanf("%d %d",&M,&N);
 int *a=(int *)malloc(sizeof(int)*M);//动态申请内存存放单词
 int n=0;//存入内存中的单词数
 int k=0;//待存放内存中的单词下标
 int times=0;//查外存字典的次数
 while(N--)
 {
  scanf("%d",&x);
  if(!Search(a,n,x))//如果在内存中没有找到单词x 
  {
   times++;//查字典次数加1 
   a[k]=x;//将单词存入内存中,若已满,则会用新单词替换最早进入内存的单词,体现在下一行的求余语句 
   k=(k+1)%M;//求下次存放内存中时单词对应的下标(求余思想求下标) 
   if(n<M)
   {
    n++;//内存没满,内存中单词数加1 
   }
  } 
 } 
 printf("总共需要查找外部词典的次数:%d\n",times);
} 
int Search(int a[],int n,int x)
{
 int i=0;//数组a下标从0开始与x比较
 while(i<n)//n为内存中的单词数 
 {
  if(a[i]==x)
  {
   return 1;//找到x,返回1 
  }
  i++; 
 }
 return 0;//没有找到,返回0 
}

Output:
Here Insert Picture Description
Explanation:
entering the first word, "1": In this case the dictionary memory (accommodate three words) without any recording, dictionary lookup external memory 1 stores the dictionary memory (i.e., array A), this time to find an external memory Dictionary 1

When the input of the second word, "2": In this case the dictionary memory (also store two words) No record of 2, the dictionary lookup external memory 2 into memory dictionary (i.e., the array A), this time to find the external memory Dictionary 2 times

"1" when entering the third word: At this point there is record of a dictionary memory, no external memory dictionary lookup

When entering the fourth word, "5": At this point Memory dictionary (also store a word) no record of 5, 5 dictionary lookup external memory into memory dictionary (ie, the array a), this time to find external memory dictionary 3 times

When entering the fifth word, "4": At this point the dictionary memory (memory dictionary is full) no record of 4 and look for external memory dictionary will be updated for the next 4 elements labeled 0 (1 Replace the 4), this time Find 4 times the external memory dictionary

When "4" Input sixth word: At this point there is about 4 recording memory dictionary, no dictionary lookup external memory

Enter the seventh word "1": As the memory before this time has been updated to the dictionary 1 4 1 no relevant records and look for external memory dictionary will be updated to 1 element at index 1 (2 will be replaced by 1) In this case the dictionary to find the external memory 5.

Guess you like

Origin blog.csdn.net/weixin_42124234/article/details/102392231