Esempio n. 1
0
int search2(int *arr, int low, int high, int val) {
    // return smallest i such that a[i] >= val
    //   and low <= i < high
    // if all a[i] < val, return high
    if (low == high) return high;
    if (arr[low] >= val) return low;
    if (low == high - 1) return high;
    int mid = (low + high) / 2;
    if (arr[mid] <= val) return search2(arr, mid, high, val);
    return search2(arr, low, mid, val);
}
Esempio n. 2
0
//menu
void menu()
{
     int choice; 
     do 
     { 
                               printf(" \033[36m     DICTIONARY\n"); 
                               printf("1:Xoa Tu\n"); 
                               printf("2:Tim Kiem Tu \n"); 
                               printf("3:Tim Kiem Tu Su Dung Tab \n");
                               printf("4:Them Tu \n");
                               printf("5:Tim Kiem Nang Cao \n");
                               printf("6:Thoat \033[0m\n"); 
        printf("Lua chon: "); scanf("%d",&choice); 
        printf("-------------------------*_*---------------------------\n\n");
        switch (choice) 
      { 
         case 1: delete_key();      system("clear");  break; 
         case 2: search();          system("clear");  break; 
         case 3: search_tab();      system("clear");  break; 
         case 4: edit();            system("clear");  break; 
         case 5: search2();         system("clear");  break;
         case 6:                    system("clear");  break; 
      } 
     } 
   while (choice != 6); 
}
Esempio n. 3
0
bool cCharStuff::cBankerAI::DoAI(int c, P_CHAR pBanker, const QString& comm)
{
	P_CHAR pc_currchar = currchar[c];

	string search2("BALANCE");
	string search3("WITHDRAW") ;
	string search4("CHECK") ;

	if (SrvParams->useSpecialBank())
	{
		if ((comm.contains(SrvParams->specialBankTrigger(), false)) &&(!(pc_currchar->dead)))
		{
			openspecialbank(c, currchar[c]);
			return true;
		}
	}
    else if ((comm.contains("BANK")) &&(!(pc_currchar->dead)))
	{
		OpenBank(c);
		return true;
	}
    else if ((comm.contains("BALANCE")) &&(!(pc_currchar->dead)))
	{
		return Balance(c, pBanker);
	}
	else if ((comm.contains("WITHDRAW")) &&(!(pc_currchar->dead)))
	{
		return Withdraw(c, pBanker, comm.latin1());
	}
	else if ((comm.contains("CHECK")) &&(!(pc_currchar->dead)))
	{
		return BankCheck(c, pBanker, comm.latin1());
	}
	return true;
}
Esempio n. 4
0
int main(int argc, char **argv) {
    int arr[] = {1, 3, 4, 7, 11, 18};
    int len = 6;
    int i;
    int val;

    for (i = 0; i < len; ++i) {
        val = arr[i];
        printf("%d %d\n", search2(arr, 0, len, val), val);
    }
    val = 0; printf("%d %d\n", search2(arr, 0, len, val), val);
    val = 10; printf("%d %d\n", search2(arr, 0, len, val), val);
    val = 20; printf("%d %d\n", search2(arr, 0, len, val), val);

    return 0;
}
Esempio n. 5
0
int
solve(struct solver *self, int *puzzle) {
  int nsol;
  int clues = applyclues(self, puzzle);
  /* printgrid(puzzle, stdout); */
  if (clues == -1)
    return 0;
  nsol = search2(self, clues);

  return nsol;
}
Esempio n. 6
0
File: 25.c Progetto: 13436120/Cgames
main( )/*只是为了引用函数search2( )*/
{
	int *vp,i;
	int a[ ]={1,3,5,7,9,13,15,27,29,37};
	int b[ ]={2,4,6,8,10,13,14,27,29,37};
	clrscr();
	puts("The elements of array a is:");
	for(i=0;i<sizeof(a)/sizeof(a[0]);i++)
		printf(" %d",a[i]);
	puts("\nThe elements of array b is:");
	for(i=0;i<sizeof(b)/sizeof(b[0]);i++)
		printf(" %d",b[i]);
	vp=search2(a,b,sizeof a/sizeof a[0],sizeof b/sizeof b[0]);
	if(vp) printf("\nThe first same number in both arrays is %d\n",*vp);
	else printf("Not found!\n");
	puts("\n Press any key to quit...\n");
	getch();

}
Esempio n. 7
0
int
easier(struct solver *self, int *puzzle)
{
  int n;
  int clues = applyclues(self, puzzle);
  self->solver_backtracks = 0;
  self->first_branch = -1;
  n = search2(self, clues);
  assert(n == 1);
  if (self->first_branch != -1) {
    int r = self->solution_row[self->first_branch];
    int cell = r/9;
    int newval = r%9+1;

    dbprintf("first_branch = %d; r = %d\n", self->first_branch, r);

    dbprintf("changing puzzle[%d] from %d to %d\n", cell, puzzle[cell], newval);
    puzzle[cell] = newval;
    return 1;
  }
  return 0;
}
Esempio n. 8
0
main()
{
    int choose;
    stud *head,*searchpoint,*forepoint;
    char fullname[20];


    while(1)
    {
        menu();
        scanf("%d",&choose);
        switch(choose)
        {
            case 1:
            	clrscr();
            	head=creat();
            	puts("Linklist created successfully! \nPress any key to return...");
            	getch();
            break;
            case 2:
            	clrscr();
                printf("Input the student's name which you want to find:\n");
                scanf("%s",fullname);
                searchpoint=search(head,fullname);
                printf("The stud name you want to find is:%s",*&searchpoint->name);
                printf("\nPress any key to returen...");
	    		getchar();
	    		getchar();
                break;
            case 3:
                clrscr();
                insert(head);
                print(head);
                printf("\nPress any key to returen...");
                getchar();getchar();
                break;
            case 4:
                clrscr();
                print(head);
                printf("\nInput the student's name which you want to delete:\n");
                scanf("%s",fullname);
                searchpoint=search(head,fullname);
                forepoint=search2(head,fullname);
                del(forepoint,searchpoint);
                print(head);
	    puts("\nDelete successfully! Press any key to return...");
	    getchar();
	    getchar();
                break;
            case 5:print(head);
                printf("\nPress any key to return...");
                getchar();getchar();
                break;
            case 6:quit();
                break;
            default:
                clrscr();
                printf("Illegal letter! Press any key to return...");
                menu();
                getchar();
        }
    }
}
Esempio n. 9
0
void adajoint_chr(char **R_file_prefix, int *R_method,
int *R_nperm, int *R_seed, 
int *R_nthread, int *R_nsnp, int *R_ngene, 
double *R_vU, double *R_score0, double *R_vV, 
int *R_vgene_idx, int *R_gene_start, int *R_gene_end, 
int *R_vgene_cutpoint, 
int *R_gene_cutpoint_start, int *R_gene_cutpoint_end, 
double *R_gene_pval, int *R_arr_rank, 
int *R_sel_id, int *R_marg_id){
  
  int len_file_prefix = strlen(*R_file_prefix);
  char *file_prefix = new char[len_file_prefix + 1];
  file_prefix[0] = '\0';
  strcat(file_prefix, *R_file_prefix);
  
  int method = *R_method;
  assert(method == 1 || method == 2);
  
  int nperm = *R_nperm;
  int seed = *R_seed;
  int nthread = *R_nthread;
  int nsnp = *R_nsnp;
  int ngene = *R_ngene;
  
  fvec score0;
  fmat V;
  fmat U;
  load_score0(R_score0, score0, nsnp);
  load_cov(R_vV, V, nsnp);
  load_U(R_vU, U, nsnp);
  
  imat gene_idx; // index of SNPs in a gene
  
  load_gene_idx(R_vgene_idx, R_gene_start, R_gene_end, 
  gene_idx, ngene);
  
  imat cutpoint;
  load_gene_cutpoint(R_vgene_cutpoint, R_gene_cutpoint_start, R_gene_cutpoint_end, 
  cutpoint, ngene);
  
  string fprefix (file_prefix);
  svec gene_out (ngene, fprefix);
  for(int g = 0; g < ngene; ++g){
    ostringstream gid;
    gid << g;
    gene_out[g] = gene_out[g] + string("GID.") + gid.str() + string(".bin");
  }
  
  // write obs statistics for all genes
  imat sel_id(ngene);
  ivec marg_id(ngene);
  for(int g = 0; g < ngene; ++g){
    fstream gout(gene_out[g].c_str(), ios::out | ios::binary);
  	if(!gout){
  		error("Fail to write observed statistics to file");
  	}
  	
  	fvec S;
  	fmat Sigma;
  	extract_score(S, score0, gene_idx[g]);
  	extract_cov(Sigma, V, gene_idx[g]);
  	fvec s;
    int ncp = cutpoint[g].size();
    int mc = cutpoint[g][ncp - 1];
    
    if(method == 1){
      search1(s, sel_id[g], marg_id[g], S, Sigma, mc);
    }else{//assert(method == 2)
      search2(s, sel_id[g], marg_id[g], S, Sigma, mc);
    }
    
    for(int k = 0; k < ncp; ++k){
      float u = s[cutpoint[g][k]];
      gout.write((char*)(&u), sizeof(u));
    }
  	gout.close();
  }
  
  int i_sel_id = -1;
  for(int g = 0; g < ngene; ++g){
    R_marg_id[g] = gene_idx[g][marg_id[g]] + 1;
    for(int k = 0; k < sel_id[g].size(); ++k){
      ++i_sel_id;
      R_sel_id[i_sel_id] = gene_idx[g][sel_id[g][k]] + 1;
    }
    int nn = gene_idx[g].size() - sel_id[g].size();
    while(nn){
      ++i_sel_id;
      R_sel_id[i_sel_id] = -1;
      --nn;
    }
  }
  
  int ngap = min(10000, nperm);
  int nblock = nperm / ngap;
  
  for(int b = 0; b < nblock; ++b){
  	fmat null(ngap, fvec (nsnp, .0f));
  	drand48_data buf;
  	// compute null score
  	#pragma omp parallel num_threads(nthread) private(buf)
  	{
  		srand48_r(seed + b * nthread + omp_get_thread_num(), &buf);
  		#pragma omp for
	  	for(int i = 0; i < ngap; ++i){
	  		fvec rn;
	  		rnorm(buf, nsnp, rn);
	  		for(int j = 0; j < nsnp; ++j){
	  			null[i][j] = .0f;
	  			for(int k = 0; k < nsnp; ++k){
	  				null[i][j] += rn[k] * U[k][j];
	  			}
	  		}
	  	}
	  }
	  
	  // write null statistics to local files (per gene)
	  #pragma omp parallel num_threads(min(nthread, ngene))
	  {
	  	#pragma omp for
	  	for(int g = 0; g < ngene; ++g){
	  		ofstream gout;
	  		gout.open(gene_out[g].c_str(), ios::out | ios::binary | ios::app);
	  		if(!gout){
	  			error("Fail to write null statistics to file");
	  		}
	  		
	  		fmat Sigma;
	  		extract_cov(Sigma, V, gene_idx[g]);
	  		
        int ns = gene_idx[g].size();
        int ncp = cutpoint[g].size();
        int mc = cutpoint[g][ncp - 1];
	  		for(int i = 0; i < ngap; ++i){
	  			fvec S;
	  			extract_score(S, null[i], gene_idx[g]);
	  			
	  			fvec s;
          ivec sel_id;
          int marg_id;
          if(method == 1){
            search1(s, sel_id, marg_id, S, Sigma, mc);
          }else{
            search2(s, sel_id, marg_id, S, Sigma, mc);
          }
          
          for(int k = 0; k < ncp; ++k){
            float u = s[cutpoint[g][k]];
            gout.write((char*)(&u), sizeof(u));
          }
	  		}
	  		gout.close();
	  	}
	  }
	  //fmat().swap(null);
  }
  
  // read null statistics (per gene)
  int irk = -1;
  for(int g = 0; g < ngene; ++g){
  	int ncp = cutpoint[g].size();
  	vector<VecStat> stat(ncp, VecStat (nperm + 1, STAT0));
    fstream gin(gene_out[g].c_str(), ios::in | ios::binary);
    
  	for(int i = 0; i < nperm + 1; ++i){
  		for(int j = 0; j < ncp; ++j){
  			float s = .0f;
  			gin.read((char*)(&s), sizeof(s));
  			stat[j][i].stat = s;
  			stat[j][i].id = i;
  		}
  	}
  	gin.close();
    
    if(remove(gene_out[g].c_str())){
      error("Cannot delete gene output file");
    }
  	
  	imat arr_rank(ncp, ivec (nperm + 1, 0));
  	#pragma omp parallel num_threads(min(ncp, nthread))
  	{
      #pragma omp for
  		for(int j = 0; j < ncp; ++j){
  			sort(stat[j].begin(), stat[j].end(), descending);
  			for(int i = 0; i < nperm + 1; ++i){
  				int id = stat[j][i].id;
  				arr_rank[j][id] = i;
  			}
  		}
  	}
  	
  	vector<VecStat>().swap(stat);
    
    ivec gene_min_p (nperm + 1, -1);
    ivec subsum(nthread, 0);
    ivec subtie(nthread, 0);
    int m = nperm + 1;
    for(int j = 0; j < ncp; ++j){
      ++irk;
      R_arr_rank[irk] = arr_rank[j][0];
      if(arr_rank[j][0] < m){
        m = arr_rank[j][0];
      }
    }
    gene_min_p[0] = m;
    
    #pragma omp parallel num_threads(nthread)
    {
      #pragma omp for
      for(int i = 1; i < nperm + 1; ++i){
        int tid = omp_get_thread_num();
        int m = nperm + 1;
        for(int j = 0; j < ncp; ++j){
          if(arr_rank[j][i] < m){
            m = arr_rank[j][i];
          }
        }
        gene_min_p[i] = m;
        if(gene_min_p[i] < gene_min_p[0]){
          subsum[tid] += 1;
        }else if(gene_min_p[i] == gene_min_p[0]){
          subtie[tid] += 1;
        }else{
          ;
        }
      }
    }
    
    R_gene_pval[g] = 1.0;
    int rep = 0;
    for(int t = 0; t < nthread; ++t){
      R_gene_pval[g] += subsum[t];
      rep += subtie[t];
    }
    R_gene_pval[g] += rep / 2.0;
    R_gene_pval[g] /= nperm + 1;
    
    fstream gout(gene_out[g].c_str(), ios::out | ios::binary);
    if(!gout){
      error("Fail to write gene statistics to file");
    }
    for(int i = 0; i < nperm + 1; ++i){
      gout.write((char*)(&(gene_min_p[i])), sizeof(gene_min_p[i]));
    }
    gout.close();
    
  }
  
  
  delete[] file_prefix;
  
}
int search(int A[], int n, int target) {
    if (random()%2){
        return search1(A, n, target);
    }
    return search2(A, n, target);
}
//funkcja usuwajac napis w tablicy
void del(char word[]) {
strcpy(napisy[search2(word)], "-------");
}
Esempio n. 12
0
int main(int argc, char *argv[]) {
	int i, s;
	list q;
	list1 q1;
	list2 q2;
	record d;
	personal f;
	record *ptr, *pt;
	personal *ptr2, *pt2;
	FILE *fp, *fp1, *fp2, *fp3, *fp4, *fp6, *fp8, *fp9, *fp10;
	fp1 = fopen("b.txt", "a+");
	node2 *st2;
	init(&q);
	init1(&q1);
	init2(&q2);
	char a[48];
	char br[32];
	int m, posi, choice, roll, size, s1, s2, s3, s4, num, posii, yr, MIS;
	while(1) {
		choice = printmenu();
		switch(choice) {
			case 1 :
				printf("\nEnter the Name, MIS, Branch, Year of student and the MIS before which you wish to insert the record :\n");  /*if it is the first record 'posi' can be anything*/
				scanf(" %[^\n]%d%s%d%d", a, &m, br, &yr, &posi);
				fp = fopen("k.txt", "a+");
				Add_academic(&q, a, m, br, posi, yr, fp);
				fclose(fp);
				print(&q);
				break;
			case 2 :
				printf("Enter the MIS of the student record to be deleted :\n");
				scanf("%d", &posii);
				fp = fopen("k.txt", "a+");
				fp4 = fopen("d.txt", "w+");
				if(pt = delet_academic(&q, posii,fp,fp4)) {
					printf("Name:%s\n MIS:%d\n Branch:%s\n Year :%d\n", pt->name, pt->MIS, pt->branch, pt->year);
				}
				else {
					printf("INVALID ENTRY\n");
				}
				print(&q);
				fclose(fp);
				fclose(fp4);
				break;
			case 3 :
				printf("Enter the MIS of student :\n");
				scanf("%d", &roll);
				if(ptr = search(&q, roll)) {

					printf("\nName :%s\tMIS :%d\tBranch :%s\tYear :%d\n", ptr->name, ptr->MIS, ptr->branch, ptr->year);

				}
				else {

					printf("\nRECORD NOT FOUND\n");
				}
				break;
			case 4 :
				printf("Enter the Name, MIS, Branch, Year of student :\n");
				scanf(" %[^\n]%d%s%d", a, &m, br, &yr);
				fp = fopen("k.txt", "a+");
				append_academic(&q, a, m, br, yr, fp);
				print(&q);
				fclose(fp);
				break;
			case 5 :
				size = length(&q);
				printf("Number of student records(academic) : %d\n", size);
				break;
			case 6 :
				printf("Enter the MIS of student :\n");
				scanf("%d", &roll);
				creatingID(&q, roll);
				break;
			case 7 :
				fp = fopen("k.txt", "a+");
				fp3 = fopen("a.txt", "r");
				academicfromfile(&q, fp3, fp);
				print(&q);
				fclose(fp);
				fclose(fp3);
				break;
			case 8 :
				printf("Enter the Name, MIS, Branch and Year of student:\n");
				scanf(" %[^\n]%d%s%d", a, &m, br, &yr);  /*MIS should be same*/
				fp = fopen("k.txt", "a+");
				fp6 = fopen("n.txt", "w+");
				Modify_academic(&q, m, a, br, yr, fp, fp6); 
				print(&q);
				fclose(fp);
				fclose(fp6);
				break;
			case 9 :
				num = length1(&q1);
				printf("No of students who appeared for final exam : %d\n", num);
				break;
			case 10 :
				printf("Enter MIS, T1 marks of 4 subjects ,MIS before which you wish to enter the record and the semester :\n");
				scanf("%d%d%d%d%d%d%d",&roll,&s1,&s2,&s3,&s4,&m, &i);
				T1Marks(&q1,roll,s1,s2,s3,s4,m,i); 
				printT1(&q1, i);
				break;	
			case 11 :
				printf("Enter MIS and T2 marks of 4 subjects and the semester :\n");
				scanf("%d%d%d%d%d%d",&roll,&s1,&s2,&s3,&s4, &i);
				T2Marks(&q1,roll,s1,s2,s3,s4,i);
				printT2(&q1, i);
				break;
			case 12 :
				printf("Enter MIS and ESE marks of 4 subjects and the semester :\n");
				scanf("%d%d%d%d%d%d",&roll,&s1,&s2,&s3,&s4,&i);
				ESEMarks(&q1,roll,s1,s2,s3,s4,i);
				printESE(&q1, i);
				break;
			case 13 :
				printf("Enter the MIS and the semester :\n");
				scanf("%d%d", &MIS, &i);
				Total(&q1, MIS,i, fp1);
				printTOT(&q1, i);
				break;
			case 14 :
				printf("Enter the Name, MIS, Mobile no., Email Id of student and the MIS before which you wish to insert the record :\n");  /*if it is the first record, posi can be anything*/
				scanf(" %[^\n]%d%d%s%d", a, &m, &yr, br, &posi);
				fp2 = fopen("c.txt", "a+");
				Add2_personal(&q2, a, m, yr, br, posi, fp2);
				print2(&q2);
				fclose(fp2);
				break;
			case 15 : 
				printf("Enter the MIS of the student record to be deleted :\n");
				scanf("%d", &posii);
				fp2 = fopen("c.txt", "a+");
				fp9 = fopen("s.txt", "w+");
				if(pt2 = delet2_personal(&q2, posii,fp2, fp9)) {
					printf("Name:%s\n MIS:%d\n Mobile No:%d\n Email Id :%s\n", pt2->name, pt2->MISS, pt2->mobile, pt2->email_Id);
				}
				else {
					printf("INVALID ENTRY\n");
				}
				print2(&q2);
				fclose(fp2);
				fclose(fp9);
				break;
			case 16 :
				printf("Enter the MIS of student :\n");
				scanf("%d", &roll);
				if(ptr2 = search2(&q2, roll)) {

					printf("\nName :%s\tMIS :%d\tMobile No :%d\tEmail Id :%s\n", ptr2->name, ptr2->MISS, ptr2->mobile, ptr2->email_Id);
				}
				else {

					printf("\nRECORD NOT FOUND\n");
				}
				break;	
			case 17 :
				printf("Enter the Name, MIS, Mobile no. and Email Id of student:\n");
				scanf(" %[^\n]%d%d%s", a, &m, &yr, br);  /*MIS should be same*/
				fp8 = fopen("w.txt", "w+");
				fp2 = fopen("c.txt", "a+");
				Modify2_personal(&q2, m, a, yr, br,fp2, fp8);
				print2(&q2);
				fclose(fp2);
				fclose(fp8);
				break;
			case 18 :
				printf("\nEnter the MIS of the student and the semester :\n");
				scanf("%d%d", &m, &s);
				link(&q, &q1, &q2, m, s);
				break;
			case 19 :
				fp2 = fopen("c.txt", "a+");
				fp10 = fopen("p.txt", "r");
				personalfromfile(&q2, fp10, fp2);
				print2(&q2);
				fclose(fp2);
				fclose(fp10);
				break;
			case 20 :
				fclose(fp1);
				exit(1);
				break;
		}
	}
}