Guest User

code

a guest
Apr 9th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.09 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<windows.h>
  3. #include<pthread.h>
  4. #include<conio.h>
  5. #include<string.h>
  6. #include<graphics.h>
  7. #include<time.h>
  8. #define space "\t \t \t"
  9. static int up_price=0;
  10. static int ind_pos=0;
  11. typedef struct list
  12. {
  13. char name[100];
  14. int start_price;
  15. int volume;
  16. int market;
  17. } list_t;
  18. int* whole_data(int st_price,int page,int CURRENT,int *length);
  19. int char_to_int(char*num);
  20. void graph_section(char *name,int shares,int st_price,int page,int CURRENT);
  21. void fun(int exp,int act);
  22. void inserting_the_data(list_t *collect,char*Data,int prev,int cur,int range);
  23. int Main_page();
  24. void insert(list_t *collect,char *Data,int pos);
  25. void view_stock(list_t*Data,int current,int page);
  26. void inserting_the_data(list_t *collect,char*Data,int prev,int cur,int range);
  27. void portfolio(char *Name);
  28. void my_Account(char *Name);
  29. void watch_list(char* Name);
  30. void stock_list(char *Name);
  31. void user_choice(int choice,char *Name);
  32. void trans_sec(char* Name);
  33. void updating_point();
  34. void userpage(char *users);
  35. char *int_char(int src);
  36. void *myThreadFun(void *id);
  37. int array[20]= {0};
  38. void *myThreadFun(void *id)
  39. {
  40. FILE * fp_3=fopen("Prices_list.txt","a+");
  41. char *str;
  42. time_t t;
  43. int random_1,idx,count=20;
  44. random_1 =idx=0;
  45. while(count--)
  46. {
  47. srand((unsigned) time(&t));
  48. random_1=rand()%10;
  49. Sleep(1000);
  50. if(random_1 == count)
  51. {
  52. if(random_1%2==0)
  53. str=int_char(random_1*2);
  54. else
  55. str=int_char(random_1*2*-1);
  56. }
  57. else
  58. {
  59. if(random_1%2!=0)
  60. str=int_char(random_1);
  61. else
  62. str=int_char(random_1*-1);
  63. }
  64. while(str[idx]!='\0')
  65. {
  66. fputc(str[idx],fp_3);
  67. idx++;
  68. }
  69. if(idx==0)
  70. {
  71. fputc('0',fp_3);
  72. }
  73. fputc('#',fp_3);
  74. idx=0;
  75. }
  76. fputc('$',fp_3);
  77. up_price++;
  78. fclose(fp_3);
  79. }
  80. char *int_char(int src)
  81. {
  82. char r,*string=(char*)malloc(100*sizeof(char));
  83. int n=src,j=0,temp=0;
  84. if(src<0)
  85. {
  86. n=n*-1;
  87. temp=1;
  88. }
  89. while(n!=0)
  90. {
  91. r=n%10;
  92. string[j++]=(r+48);
  93. n/=10;
  94. }
  95. if(temp==1)
  96. {
  97. string[j++]=45;
  98. }
  99. string[j]='\0';
  100. strrev(string);
  101. return string;
  102. }
  103. void userpage(char *users)
  104. {
  105. int max=5,select=1,loop=1;
  106. user_sec:
  107. while(loop)
  108. {
  109. if(select >6||select ==0)
  110. {
  111. select =1;
  112. }
  113. system("cls");
  114. updating_point();
  115. //system("color 04");
  116. printf(" Stock Guide\n\n");
  117. if(select ==1)
  118. {
  119. printf("-> portfolio\n");
  120. }
  121. else
  122. {
  123. printf(" portfolio\n");
  124. }
  125. if(select == 2)
  126. {
  127. printf("-> watchlist\n");
  128. }
  129. else
  130. {
  131. printf(" watchlist\n");
  132. }
  133. if(select==3)
  134. {
  135. printf("-> stock list\n");
  136. }
  137. else
  138. {
  139. printf(" stock list\n");
  140. }
  141. if(select==4)
  142. {
  143. printf("-> account\n");
  144. }
  145. else
  146. {
  147. printf(" account\n");
  148. }
  149. if(select ==5)
  150. {
  151. printf("-> Transaction Histroy\n");
  152. }
  153. else
  154. {
  155. printf(" Transaction histroy\n");
  156. }
  157. if(select==6)
  158. {
  159. printf("-> EXIT\n");
  160. }
  161. else
  162. {
  163. printf(" EXIT\n");
  164. }
  165. printf("Use the Arrow key's to move and select\n");
  166. int idx=0;
  167. for(idx=0; idx<20; idx++)
  168. {
  169. printf("%d ",array[idx]);
  170. }
  171. switch(_getch())
  172. {
  173. case 'H':
  174. select--;
  175. // goto user_sec;
  176. break;
  177. case 'P':
  178. select++;
  179. // goto user_sec;
  180. break;
  181. case 13:
  182. if(select!=6)
  183. {
  184. user_choice(select,users);;
  185. }
  186. else
  187. {
  188. loop=0;
  189. }
  190. break;
  191. }
  192. }
  193. }
  194. void user_choice(int choice,char *Name)
  195. {
  196. if(choice==1)
  197. portfolio(Name);
  198. else if(choice==2)
  199. watch_list(Name);
  200. else if(choice==3)
  201. stock_list(Name);
  202. else if(choice==4)
  203. my_Account(Name);
  204. else if(choice==5)
  205. trans_sec(Name);
  206. }
  207. void trans_sec(char* Name)
  208. {
  209.  
  210. }
  211. void watch_list(char* Name)
  212. {
  213.  
  214. }
  215. void stock_list(char *Name)
  216. {
  217. system("cls");
  218. FILE*fp;
  219. char x;
  220. list_t *collection=(list_t*)malloc(sizeof(list_t)*5);
  221. int i,arrow=0;
  222. fp=fopen("COMPANY_DATA.txt","r+");
  223. i=0;
  224. char all[10000];
  225. while((x = fgetc(fp)) != EOF)
  226. {
  227. all[i++] = x;
  228. }
  229. all[i] = '\0';
  230. int prev=4,page=0,loop=1,coin=5;
  231. while(loop)
  232. {
  233. system("cls");
  234. updating_point();
  235. inserting_the_data(collection,all,prev,page,i);
  236. inserting_the_data(collection,all,prev,page,i);
  237. prev=page;
  238. printf(" <<<<<<<<<<<<<<<<<<<Stock list>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n");
  239. printf("COMPANY_NAME ||STARTING_PRICE || VOLUME || MARKET ||cur_price");
  240. printf("\n%s|%d|%d|%d|%d",collection[0].name,collection[0].start_price,collection[0].volume,collection[0].market,array[(page*5)+0]);
  241. if(arrow==0)printf("<-");
  242. printf("\n%s|%d|%d|%d|%d",collection[1].name,collection[1].start_price,collection[1].volume,collection[1].market,array[(5*page)+1]);
  243. if(arrow==1)printf("<-");
  244. printf("\n%s|%d|%d|%d|%d",collection[2].name,collection[2].start_price,collection[2].volume,collection[2].market,array[(5*page)+2]);
  245. if(arrow==2)printf("<-");
  246. printf("\n%s|%d|%d|%d|%d",collection[3].name,collection[3].start_price,collection[3].volume,collection[3].market,array[(5*page)+3]);
  247. if(arrow==3)printf("<-");
  248. printf("\n%s|%d|%d|%d|%d",collection[4].name,collection[4].start_price,collection[4].volume,collection[4].market,array[(5*page)+4]);
  249. if(arrow==4)printf("<-");
  250. printf("\nGo Back");
  251. if(arrow==5)printf("<-");
  252. x=_getch();
  253. switch(x)
  254. {
  255. case 72:
  256. arrow--;
  257. break;
  258. case 80:
  259. arrow++;
  260. break;
  261. case 75:
  262. page--;
  263. break;
  264. case 77:
  265. page++;
  266. break;
  267. case 13:
  268. if(arrow==5)
  269. loop=0;
  270. else
  271. view_stock(collection,arrow,page);
  272. }
  273. if(arrow>5 ||arrow<0)
  274. {
  275. arrow=0;
  276. }
  277. if(page>3 ||page<0)
  278. {
  279. page=0;
  280. arrow =0;
  281. }
  282. }
  283. }
  284. int calcu_eps(int capital,int shares)
  285. {
  286. int out_stan_shares=shares;
  287. return capital/out_stan_shares-14;
  288. }
  289. int calcu_pe(int price,int eps)
  290. {
  291. return eps/price;
  292. }
  293. int calcu_Div(int price)
  294. {
  295. int dividend_yield=7;
  296. return price/dividend_yield;
  297. }
  298. void view_stock(list_t*Data,int current,int page)
  299. {
  300. char x;
  301. int EPS,PE,Dividend,choice=0,loop=1;
  302. EPS=calcu_eps(Data[current].market,Data[current].volume);
  303. PE=calcu_pe(Data[current].start_price,EPS);
  304. Dividend=calcu_Div(Data[current].start_price);
  305. while(loop)
  306. {
  307. system("cls");
  308. updating_point();
  309. printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<View Stock>>>>>>>>>>>>>>>>>>>>>\n\n");
  310. printf("<<<<<<<<<<<<<<<<<<<<<<<<Company Details>>>>>>>>>>>>>>>>>>>>\n");
  311. printf("\t\t\t\t\tFundamentals Analysis\n");
  312. printf("Name:%s \t\t\t\t",Data[current].name);
  313. printf("EPS:%d\n",EPS);
  314. printf("No.of.Shares:%d \t\t\t",Data[current].volume);
  315. printf("PE :%d\n",PE);
  316. printf("Starting price:%d\t\t\t",Data[current].start_price);
  317. printf("Dividend:%d\n",Dividend);
  318. printf("Market Capital:%d\n",Data[current].market);
  319. printf("Current Price:%d\n",array[(5*page)+current]);
  320. printf("\n\n\n");
  321. fun(0,choice);
  322. printf("View current graph\n");
  323. fun(1,choice);
  324. printf("Go back\n");
  325. x= _getch();
  326. switch(x)
  327. {
  328. case 'H':
  329. choice--;
  330. break;
  331. case 'P':
  332. choice++;
  333. break;
  334. case 13:
  335. if(choice==1)
  336. loop=0;
  337. else
  338. graph_section(Data[current].name,Data[current].volume,Data[current].start_price,page,current);
  339. }
  340. if(choice<0 ||choice>1)
  341. {
  342. choice=1;
  343. }
  344. }
  345. }
  346. int* whole_data(int st_price,int page,int CURRENT,int *length)
  347. {
  348. FILE *fp;
  349. char x='k',Data[10000],max_array[3];
  350. fp=fopen("Prices_list.txt","a+");
  351. int pos=0,index=0,new_arr[1000],price=0,var=(5*page)+CURRENT,value=0;
  352. while(value<ind_pos)
  353. {
  354. index=0;
  355. while(x!='$')
  356. {
  357. x=fgetc(fp);
  358. Data[index]=x;
  359. index++;
  360. }
  361. Data[index]='\0';
  362. int cur=0,low=0;
  363. while(cur!=var && low<index)
  364. {
  365. if(Data[low]=='#')
  366. {
  367. cur++;
  368. }
  369. low++;
  370. }
  371. cur=0;
  372. while(Data[low]!='#')
  373. {
  374. max_array[cur++]=Data[low];
  375. low++;
  376. }
  377. max_array[cur]='\0';
  378. int v;
  379. v=char_to_int(max_array);
  380. new_arr[value]=v;
  381. value++;
  382. pos++;
  383. x='k';
  384. }
  385. int *r=&value;
  386. *length=*r;
  387. return new_arr;
  388. }
  389. void graph_section(char *name,int shares,int st_price,int page,int CURRENT)
  390. {
  391. system("cls");
  392. char *str;
  393. int len,*data,pos=0,x1,x2,y1,y2;
  394. data=whole_data(st_price,page,CURRENT,&len);
  395.  
  396. /*int gd = DETECT,gm;
  397. initgraph(&gd, &gm, "C:\\TC\\BGI");
  398. setcolor(YELLOW);
  399. rectangle(0,30,639,450);
  400. settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
  401. setcolor(WHITE);
  402. outtextxy(275,0,"Graph");
  403. /* x1=40;
  404. x2=x1;
  405. y1=450;
  406. y2=y1-data[pos++];
  407. len--;
  408. while(len--)
  409. {
  410. line(x1,y1,x2,y2);
  411. x1=x2;
  412. x2+=10;
  413. y1=y2;
  414. y2-=data[pos];
  415. }
  416. _getch();
  417. closegraph();*/
  418. }
  419. void fun(int exp,int act)
  420. {
  421. printf(space);
  422. if(exp==act)
  423. printf("->");
  424. else
  425. printf(" ");
  426. }
  427. void inserting_the_data(list_t *collect,char*Data,int prev,int cur,int range)
  428. {
  429. int idx=0,position=0;
  430. if(cur==0 && prev!=cur)
  431. {
  432. insert(collect,Data,cur+1);
  433. }
  434. else if(cur==1 && prev!=cur)
  435. {
  436. position=cur*5;
  437. while(position--)
  438. {
  439. while(Data[idx]!='$')
  440. {
  441. idx++;
  442. }
  443. idx+=2;
  444. }
  445. insert(collect,Data,idx);
  446. }
  447. else if(cur==2 && prev!=cur)
  448. {
  449. position=10;
  450. while(position--)
  451. {
  452. while(Data[idx]!='$')
  453. {
  454. idx++;
  455. }
  456. idx+=2;
  457. }
  458. insert(collect,Data,idx);
  459. }
  460. else if(cur==3 && prev!=cur)
  461. {
  462. position=15;
  463. while(position--)
  464. {
  465. while(Data[idx]!='$')
  466. {
  467. idx++;
  468. }
  469. idx+=2;
  470. }
  471. insert(collect,Data,idx);
  472. }
  473. }
  474. void insert(list_t *collect,char *Data,int pos)
  475. {
  476. int count=5,idx=0,range=0;
  477. char temp[100]= {0};
  478. while(range<count)
  479. {
  480. idx=0;
  481. while(Data[pos]!='#')
  482. {
  483. temp[idx++]=Data[pos];
  484. pos++;
  485. }
  486. temp[idx]='\0';
  487. pos++;
  488. strcpy(collect[range].name,temp);
  489. idx=0;
  490. while(Data[pos]!='#')
  491. {
  492. temp[idx]=Data[pos++];
  493. idx++;
  494. }
  495. temp[idx]='\0';
  496. collect[range].start_price=char_to_int(temp);
  497. idx=0;
  498. pos++;
  499. while(Data[pos]!='$')
  500. {
  501. temp[idx]=Data[pos++];
  502. idx++;
  503. }
  504. temp[idx]='\0';
  505. pos+=2;
  506. collect[range].volume=char_to_int(temp);
  507. collect[range].market=((collect[range].start_price)*(collect[range].volume))*2;
  508. range++;
  509. }
  510. }
  511. void my_Account(char *Name)
  512. {
  513. printf("My Account\n\n");
  514. printf("Name :%s\n",Name);
  515. printf("Starting Investment:50,000\n");
  516. // printf("Stock Value :%d",);
  517.  
  518. }
  519. void portfolio(char *Name)
  520. {
  521.  
  522. }
  523. int main()
  524. {
  525. printf("WELCOME\n");
  526. printf(" TO \n");
  527. printf(" STOCK GUIDE \n");
  528. printf("Press any Key\n");
  529. _getch();
  530. Main_page();
  531. return 0;
  532. }
  533. void updating_point()
  534. {
  535. char Data[1000],x,max[3];
  536. int i,pos=0,in=0,count=0,idx=0;
  537. FILE *fp1,*fp;
  538. if((fp1=fopen("COMPANY_DATA.txt","r"))==NULL)
  539. {
  540. fp1=fopen("COMPANY_DATA.txt","w");
  541. char data[]="|venkys#20#500000$|satin#10#2500000$|ultratech#15#2000000$|polaris#17#250000$|Aditya vision#20#700000$|Mrf#35#1000000$|page#25#350000$|Ashok leyland#45#100000$|Tata#15#750000$|jio#40#100000$|kakatiya Cement#33#750000$|Coasboard Industries#27#500000$|Electrotham industries#21#210000$|Eichers#77#1000000$|Rana Sugar#13#500000$|Alkai Metals#39#450000$|Amazon#61#8500000$|JustDial#69#7200000$|Adobe#77#900000$|infoys#55#6500000$";
  542. i=0;
  543. while(data[i]!='\0')
  544. {
  545. x=data[i];
  546. fputc(x,fp1);
  547. i++;
  548. }
  549. fclose(fp1);
  550.  
  551. }
  552. if(ind_pos==0)
  553. {
  554. int new_arr[20]= {20,10,15,17,20,35,25,45,15,40,33,27,21,77,13,39,61,69,77,55};
  555. for(idx=0; idx<20; idx++)
  556. {
  557. array[idx]=new_arr[idx];
  558. }
  559. }
  560. if(ind_pos<up_price)
  561. {
  562. fp=fopen("Prices_list.txt","a+");
  563. idx= pos=in=count =0;
  564. x='k';
  565. while(count<ind_pos)
  566. {
  567. x=fgetc(fp);
  568. if(x=='$')
  569. {
  570. count++;
  571. }
  572. }
  573. count=0;
  574. if(ind_pos!=0)
  575. {
  576. x='k';
  577. }
  578. while(x!='$')
  579. {
  580. x=fgetc(fp);
  581. Data[idx]=x;
  582. idx++;
  583. }
  584. Data[idx]='\0';
  585. while(in<idx)
  586. {
  587. in++;
  588. while(Data[in]!='#' && Data[in]!='$')
  589. {
  590. max[pos++]=Data[in];
  591. in++;
  592. }
  593. if(Data[in]=='$')
  594. {
  595. in++;
  596. }
  597. else
  598. {
  599. if(pos==0)
  600. {
  601. max[pos++]='0';
  602. }
  603. max[pos]='\0';
  604. pos=0;
  605. array[count]+=char_to_int(max);
  606. count++;
  607. }
  608. }
  609. ind_pos++;
  610. pthread_t tid_2;
  611. pthread_create(&tid_2, NULL, myThreadFun, (void*)1);
  612. }
  613. }
  614. int Main_page()
  615. {
  616. updating_point();
  617. FILE *fp;
  618. char choice,x;
  619. char users[100];
  620. char username[100];
  621. char password[100];
  622. int i=0;
  623. pthread_t tid;
  624. pthread_create(&tid, NULL, myThreadFun, NULL);
  625. main_sec:
  626. system("cls");
  627. while(1)
  628. {
  629. fflush(stdin);
  630. printf("Helix\n");
  631. printf("1)login\n");
  632. printf("2)signup\n");
  633. printf("3)exit");
  634. choice=_getch();
  635. if(choice== 49 || choice ==50 ||choice==51)
  636. break;
  637. else
  638. {
  639. printf("\nEnter valid choice\n");
  640. printf("press any key\n");
  641. _getch();
  642. system("cls");
  643. }
  644. }
  645. if(choice==49)
  646. {
  647. char x,*tempstr;
  648. char accountsDB[100];
  649. system("cls");
  650. printf("Login \n");
  651. printf("Username:");
  652. scanf("%s",username);
  653. printf("\n");
  654. i=0;
  655. while(username[i] != '\0')
  656. {
  657. users[i] = username[i];
  658. i++;
  659. }
  660. users[i] = '\0';
  661. i=0;
  662. printf("Password:");
  663. while ((x = _getch()) != '\r')
  664. {
  665. printf("*");
  666. password[i++] = x;
  667. }
  668. password[i] = '\0';
  669. i = 0;
  670. strcat(username, password);
  671. fp = fopen("PROJECT_DATA_FILE.txt","r");
  672. while((x = fgetc(fp)) != EOF)
  673. {
  674. accountsDB[i++] = x;
  675. }
  676. accountsDB[i] = '\0';
  677. fclose(fp);
  678. if(NULL == strstr(accountsDB, username))
  679. {
  680. printf("\n\n"space space"*Username or password is wrong");
  681. _getch();
  682. goto main_sec;
  683. }
  684. else
  685. {
  686. tempstr = strstr(accountsDB, username);
  687. }
  688. i=0;
  689. while(username[i]!='\0')
  690. {
  691. if(username[i]!=tempstr[i])
  692. break;
  693.  
  694. i++;
  695. }
  696. if(i==strlen(username))
  697. {
  698. userpage(users);
  699. goto main_sec;
  700. }
  701. else
  702. {
  703. printf("\n\n*Username or password is wrong");
  704. _getch();
  705. goto main_sec;
  706. }
  707. }
  708. if(choice ==50)
  709. {
  710. system("cls");
  711. printf("Signup\n");
  712. printf("Username:");
  713. scanf("%s",username);
  714. i=0;
  715. while((x = username[i]) != '\0')
  716. {
  717. if(x == '|' || x == '#' || x == '$')
  718. {
  719. printf("\n*Username must not contain special characters like |, #, $ etc.");
  720. _getch();
  721. goto main_sec;
  722. }
  723. i++;
  724. }
  725. char accountsDB[100];
  726. fp = fopen("PROJECT_DATA_FILE.txt","r");
  727. i = 0;
  728. while((x = fgetc(fp)) != EOF)
  729. {
  730. accountsDB[i++] = x;
  731. }
  732. fclose(fp);
  733. char temp_pass[100];
  734. if(NULL == strstr(accountsDB, username))
  735. {
  736. i=0;
  737. printf("Password:");
  738. while ((x = _getch()) != '\r')
  739. {
  740. printf("*");
  741. password[i++] = x;
  742. }
  743. password[i] = '\0';
  744. printf("\nRe-Enter password:");
  745. i = 0;
  746. while((x = _getch()) != '\r')
  747. {
  748. printf("*");
  749. temp_pass[i++] = x;
  750. }
  751. temp_pass[i]='\0';
  752. if(strcmp(temp_pass,password)==0)
  753. {
  754. i = 0;
  755. fp = fopen("PROJECT_DATA_FILE.txt","a");
  756. fputc('|',fp);
  757. while((x = username[i]) != '\0')
  758. {
  759. fputc(x,fp);
  760. i++;
  761. }
  762. i = 0;
  763. while((x = password[i]) != '\0')
  764. {
  765. fputc(x,fp);
  766. i++;
  767. }
  768. fputc('$', fp);
  769. fputc('1', fp);
  770. fputc('#', fp);
  771. fputc('1', fp);
  772. fclose(fp);
  773. printf("\n\nSign up successful");
  774. _getch();
  775. goto main_sec;
  776. }
  777. else
  778. {
  779. printf("\n\n*Passwords do not match, try again");
  780. _getch();
  781. goto main_sec;
  782. }
  783. }
  784. else
  785. {
  786. printf("\n*Username is already taken");
  787. _getch();
  788. goto main_sec;
  789. }
  790. }
  791. if(choice==51)
  792. {
  793. system("cls");
  794. printf("Exit\n\n");
  795. printf("Are You Sure Want to Exit\n");
  796. printf("Enter y/n\n");
  797. x=_getch();
  798. if(x=='n'||x=='N')
  799. goto main_sec;
  800. else
  801. {
  802. printf("Thank u for visiting\n");
  803. }
  804. }
  805. }
  806. int char_to_int(char*num)
  807. {
  808. int length=strlen(num);
  809. int var=1;
  810. int i,c=0,temp;
  811. for(i=0; i<length; i++)
  812. {
  813. if(i==0 && num[i]==45 )
  814. {
  815. var=-1;
  816. }
  817. else
  818. {
  819. temp=(num[i]-48);
  820. c=c*10+temp;
  821. }
  822. }
  823. c=c*var;
  824. return c;
  825. }
Add Comment
Please, Sign In to add comment