Simple implementation of lru c ++ and lru implementation
Class LRUCache provides two interfaces: get (int key) and set (int key, value)
#include<iostream>using namespace std;class LRUCache{public: LRUCache(int cap):current(0),capacity(cap){ A=new node[cap]; } int get(int key) { for(int i=0;i<current;i++) { if(key==A[i].key) { int temp=A[i].value; node tt=A[i]; for(int k=i;k<current-1;k++) { A[k]=A[k+1]; } A[current-1]=tt; return temp; } } return -1; } void set(int key, int value) { node t; t.key=key; t.value=value; bool has=false; for(int i=0;i<current;i++) { if(key==A[i].key) { A[i].value=value; node tt=A[i]; for(int k=i;k<current-1;k++) { A[k]=A[k+1]; } A[current-1]=tt; has=true; break;} } if(!has) { if(current<capacity) { A[current++]=t; }else{ for(int k=0;k<current-1;k++) { A[k]=A[k+1]; } A[current-1]=t; } } print(); } void print() { for(int i=0;i<current;i++) cout<<A[i].key<<" "; cout<<endl; }private: int current; int capacity; struct node{int key;int value; }; void swap(node &a,node &b) {node temp;temp=a;a=b;b=temp; } node *A;};int main(){LRUCache lru(4);lru.set(2,1);lru.set(2,2);lru.set(3,2);lru.set(4,2);lru.set(5,2);lru.set(1,2);lru.set(4,2);lru.set(3,2);lru.set(4,2);lru.get(1);lru.print();}
Test:
C language implements simple addition, subtraction, multiplication, division
After running the program according to your program... When entering the c value
The program ends directly... In addition, after each case statement, remember to add break to jump out, but you can also skip break based on your specific needs. However, you need to add break to this program;
The reason is: the carriage return is also a character, so after you press back, the program thinks that the value of c is entered, and the value of B is successfully entered because
The carriage return is not a digit.
The following are the programs I have modified, which are well compiled and run.
# Include <stdio. h>
# Include <math. h>
Void main ()
{
Float a, B;
Char c;
Printf ("\ n input :");
Scanf ("% f", & );
Printf ("\ n input B :");
Scanf ("% f", & B );
// This situation is often encountered .... After the input, press enter to be captured by the next scanf ("% c", & c.
// Because the carriage return is also a character, after you press enter, the program assumes that the value of c is entered.
// Add the following line to remove the carriage return ....
Getchar ();
Printf ("\ n input c :");
Scanf ("% c", & c );
// Printf ("\ n % c", c );
Switch (c)
{
Case '+': printf ("a + B = % f", a + B); break;
Case '-': printf ("a-B = % f", a-B); break;
Case '*': printf ("a * B = % f", a * B); break;
Case '/': printf ("a/B = % f", a/B); break;
Default: break;
}
// For good looks
Printf ("\ n ");
}
Use a simple C language to implement arithmetic operations with parentheses
It is impossible to consider complex expressions.
# Include <stdlib. h>
# Include <math. h>
# Include <conio. h>
# Include <stdio. h>
# Include <string. h>
Void SplitK (char * str );
Void SplitPrem (char * str );
Void Combine (float f1, char Operate, float f2 );
Void StringBracket (char * str );
Char str1 [100], str2 [100], str3 [100], sub_str1 [100], sub_str2 [100], sub_str3 [20];
Char sub_str31 [20], sub_str32 [20], str_4 [100], operate;
Int pause = 0, left = 0, k1, k2, k3;
Float f1 = 0.0, f2 = 0.0, f = 0.0;
Void main ()
{
Float temp;
Printf ("Please input a description: \ n ");
Scanf ("% s", & str1 );
SplitK (str1 );
Temp = atof (str1 );
Printf ("description value is %. 2f \ n", temp );
}
Void SplitK (char * str)
{
Int I, j, k, l, jk;
Left = 0;
While (* str ){
If (* str = '(') left ++;
Str ++;
}
Str2 [0] = '\ 0 ';
Str3 [0] = '\ 0 ';
If (left> 0)
{
StringBracket (str1 );
J = strlen (str1 );
For (I = 0; I <j; I ++)
{
If (str1 [I] = '(')
{
Jk = 0;
For (k = I; k <j; k ++)
{
If (str1 [k] = ') break;
For (l = I + 1; l <K-1; l ++)
{
If (str1 [l] = '(') {jk ++; break ;}
}
}
If (jk = 0)
{
For (l = 0; l <I; l ++) str2 [l] = str1 [l];
Str2 [I] = '\ 0 ';
For (l = k + 1; l <j; l ++) str3 [l-k-1] = str1 [l];
Str3 [j-k-1] = '\ 0 ';
For (l = I + 1; l <= K-1; l ++) str1 [l-i-1] = str1 [l];
Str1 [k-i-1] = '\ 0 ';
Break;
}
}
}
... The remaining full text>