Calc in C

Mahen Mahen is offline Offline Sep 20th, 2004, 8:19 am |
0
Hi everyone,
I've been working on this calculator for days, and now i am making it opensource so that everyone can bring his own contribution, please comment all your codes very well.
Quick reply to this message  
C Syntax
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. int main(int argc, char **argv)
  7. {
  8. double a, b;
  9. char buf[25];
  10. if ( argc > 3 )
  11. {
  12. if ( sscanf(argv[1], "%lf", &a) == 1 && sscanf(argv[3], "%lf", &b) == 1 )
  13. {
  14. switch ( argv[2][0] )
  15. {
  16. case '+':
  17. printf("%g PLUS %g = %g\n", a, b, a + b);
  18. char first[60];
  19. char second[60];
  20. char result[60];
  21. itoa(a, first, 10);
  22. itoa(b, second, 10);
  23. itoa(a+b, result, 10);
  24. FILE *cpy;
  25. cpy = fopen("c:\\calc.txt", "a");
  26. fprintf(cpy, first);
  27. fprintf(cpy, " PLUS ");
  28. fprintf(cpy, second);
  29. fprintf(cpy, " = ");
  30. fprintf(cpy, result);
  31. fprintf(cpy, " @ ");
  32. fprintf(cpy, strdate(buf));
  33. fprintf(cpy, " & ");
  34. fprintf(cpy, strtime(buf));
  35. fprintf(cpy, "\n");
  36. break;
  37. case '-':
  38. printf("%g MINUS %g = %g\n", a, b, a - b);
  39. char firstm[60];
  40. char secondm[60];
  41. char resultm[60];
  42. itoa(a, firstm, 10);
  43. itoa(b, secondm, 10);
  44. itoa(a-b, resultm, 10);
  45. FILE *cpym;
  46. cpy = fopen("c:\\calc.txt", "a");
  47. fprintf(cpy, firstm);
  48. fprintf(cpy, " MINUS ");
  49. fprintf(cpy, secondm);
  50. fprintf(cpy, " = ");
  51. fprintf(cpy, resultm);
  52. fprintf(cpy, " @ ");
  53. fprintf(cpy, strdate(buf));
  54. fprintf(cpy, " & ");
  55. fprintf(cpy, strtime(buf));
  56. fprintf(cpy, "\n");
  57. break;
  58. case '*':
  59. printf("%g MULTIPLY BY %g = %g\n", a, b, a * b);
  60. char firstmu[60];
  61. char secondmu[60];
  62. char resultmu[60];
  63. itoa(a, firstmu, 10);
  64. itoa(b, secondmu, 10);
  65. itoa(a*b, resultmu, 10);
  66. FILE *cpymu;
  67. cpy = fopen("c:\\calc.txt", "a");
  68. fprintf(cpy, firstmu);
  69. fprintf(cpy, " MULTIPLY ");
  70. fprintf(cpy, secondmu);
  71. fprintf(cpy, " = ");
  72. fprintf(cpy, resultmu);
  73. fprintf(cpy, " @ ");
  74. fprintf(cpy, strdate(buf));
  75. fprintf(cpy, " & ");
  76. fprintf(cpy, strtime(buf));
  77. fprintf(cpy, "\n");
  78. break;
  79. case '/':
  80. printf("%g DIVIDE BY %g = %g\n", a, b, a / b);
  81. char firstd[60];
  82. char secondd[60];
  83. char resultd[60];
  84. itoa(a, firstd, 10);
  85. itoa(b, secondd, 10);
  86. itoa(a/b, resultd, 10);
  87. FILE *cpyd;
  88. cpy = fopen("c:\\calc.txt", "a");
  89. fprintf(cpy, firstd);
  90. fprintf(cpy, " DIVIDE ");
  91. fprintf(cpy, secondd);
  92. fprintf(cpy, " = ");
  93. fprintf(cpy, resultd);
  94. fprintf(cpy, " @ ");
  95. fprintf(cpy, strdate(buf));
  96. fprintf(cpy, " & ");
  97. fprintf(cpy, strtime(buf));
  98. fprintf(cpy, "\n");
  99. break;
  100. default:
  101. puts("Error");
  102. break;
  103. }
  104. }
  105. }
  106. return 0;
  107. }
  108.  

Message:


Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC