Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › advanced basic C calculator with color api coded by me enjoy
New Posts  All Forums:Forum Nav:

advanced basic C calculator with color api coded by me enjoy - Page 2

post #11 of 17
Simple trick tongue.gif

use
Code:
scanf(" %d%c%d");

instead of
Code:
scanf("%d%c%d");

That space before the first %d allows to "clean" the buffer so you don't have any leftovers from the previous scanf. If you want to properly clean the buffer just do
Code:
fpurge(stdin);

fpurge

This is for linux but I bet you can easily find the equivalent for windows
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
post #12 of 17
Thread Starter 
update FEB 14 2012

Code:

#include <stdio.h>
#include <conio.h>
#include <windows.h>



typedef struct _calculator{
  
  int num1;
  int num2;
  char operation;     
       
}calculator;

int main(){

calculator fun;

printf("please input something to calculate\n");

scanf("%d%c%d",&fun.num1,&fun.operation,&fun.num2);

if(fun.operation == '+') printf("%d",fun.num1+fun.num2);

else if(fun.operation == '-') printf("%d",fun.num1-fun.num2);

else if(fun.operation == '/') printf("%d",fun.num1/fun.num2);

else if(fun.operation == '*') printf("%d",fun.num1*fun.num2);
 
else{
     
    HANDLE hStdout;
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hStdout, FOREGROUND_RED);
    
    printf("please input somthing vaild\n");   
    getchar();
}   
    getchar();

}

My computer
(18 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core 2 Duo E6600 DG965SS powercolor 6670   
RAMHard DriveOptical DriveCooling
G.Skill Seagate  DVD WRITER heat sink + fan 
OSOSOSMonitor
Windows 7 Windows XP Linux Ubuntu FLATRON L1740B 
KeyboardPowerCaseMouse
Microsoft Basic 500 WATT PSU TOWER Microsoft Basic 
AudioOther
IDT intergrated AUDIO 5.1 speakers 
  hide details  
Reply
My computer
(18 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core 2 Duo E6600 DG965SS powercolor 6670   
RAMHard DriveOptical DriveCooling
G.Skill Seagate  DVD WRITER heat sink + fan 
OSOSOSMonitor
Windows 7 Windows XP Linux Ubuntu FLATRON L1740B 
KeyboardPowerCaseMouse
Microsoft Basic 500 WATT PSU TOWER Microsoft Basic 
AudioOther
IDT intergrated AUDIO 5.1 speakers 
  hide details  
Reply
post #13 of 17
Thread Starter 
Quote:
Originally Posted by lambecrikas View Post

Simple trick tongue.gif
use
Code:
scanf(" %d%c%d");
instead of
Code:
scanf("%d%c%d");
That space before the first %d allows to "clean" the buffer so you don't have any leftovers from the previous scanf. If you want to properly clean the buffer just do
Code:
fpurge(stdin);
fpurge
This is for linux but I bet you can easily find the equivalent for windows

ok that is usefull to know smile.gif
My computer
(18 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core 2 Duo E6600 DG965SS powercolor 6670   
RAMHard DriveOptical DriveCooling
G.Skill Seagate  DVD WRITER heat sink + fan 
OSOSOSMonitor
Windows 7 Windows XP Linux Ubuntu FLATRON L1740B 
KeyboardPowerCaseMouse
Microsoft Basic 500 WATT PSU TOWER Microsoft Basic 
AudioOther
IDT intergrated AUDIO 5.1 speakers 
  hide details  
Reply
My computer
(18 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core 2 Duo E6600 DG965SS powercolor 6670   
RAMHard DriveOptical DriveCooling
G.Skill Seagate  DVD WRITER heat sink + fan 
OSOSOSMonitor
Windows 7 Windows XP Linux Ubuntu FLATRON L1740B 
KeyboardPowerCaseMouse
Microsoft Basic 500 WATT PSU TOWER Microsoft Basic 
AudioOther
IDT intergrated AUDIO 5.1 speakers 
  hide details  
Reply
post #14 of 17
now i know why we have so many issue if a base as simple as this can be mistakenly done imagine the rest of the stuff we use!hahaha.ty for this post just what the doctor ordered (a good laugh)
Edited by drbaltazar - 2/14/12 at 5:58am
post #15 of 17
This caused me alot of problems when I was doing a work in C in my first year in university biggrin.gif
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
Rubbish
(15 items)
 
  
CPUMotherboardGraphicsRAM
E7400 @ 4ghz 1.376v Asus P5Q3 Sapphire HD6870 Kingston 4GB DDR3 1333MHZ 
Hard DriveHard DriveOSOS
Samsung Spinpoint F3 WD Green Windows 7 64bit Lubuntu 64bit 
PowerCaseMouse
OCZ ModXStream 700W Cooler Master 690 II with side window Razer Deathadder Black Edition 
  hide details  
Reply
post #16 of 17
printf? That must be C because I'm used to saying cout <<
it kind of reminds me of Java..... system.out.println(" ");
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
post #17 of 17
Quote:
Originally Posted by thecoredude98 View Post

bellow is the source code it is written by me because i was bored enjoy
#include
#include
#include
//////////////////////////////////////////////////////////////////////////////////////////////////
int main(){
int num1;
int num2;
int color = 15;
char operation;
//////////////////////////////////////////////////////////////////////////////////////////////////
printf("Please enter a value to calculate\n");
scanf("%d%c%d",&num1,&operation,&num2);
if(operation == '+') printf("%d+%d=%d\n",num1,num2,num1+num2);
if(operation == '/') printf("%d/%d=%d\n",num1,num2,num1/num2);
if(operation == '*') printf("%d*%d=%d\n",num1,num2,num1*num2);
if(operation == '-') printf("%d-%d=%d\n",num1,num2,num1-num2);
else if(operation == '#' || operation == '@' || operation == '$' || operation == '%' || operation == '&' || operation == '(' || operation == ')'){
////////////////////////////////////////////////////////////////////////////////////////////////////
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, FOREGROUND_RED);
printf("Error please input a valid arithmetic value\n");
SetConsoleTextAttribute(hStdout, 15);
printf("press enter twice to exit\n");
getch();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
else{
printf("Program coded in C/C++ by thecoredude98 allthough most of its in C");
getch();
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
do what ever you want with the source code i don't care since i was bored and coded somthing
tell me what you think for someone who hasn't programmed c in a long.... time

A calculator that handles only two numbers, and only handles basic arithmetic, written entirely within the body of main(), using scanf and printf instead of cout, cin, stringstream, etc.

This is about as basic as it gets. However, for a new programmer this is pretty good.

Advanced would handle things like graphing, square roots, exponents, factorials, trigonometry, conversions between degrees/radians, conversions between binary/hexidecimal/decimal, log base x, log base 10, variables, and possibly some calculus. It would also do lexical analysis in order to handle expressions that include parenthesis such as: (x^(2+y) * (y/2))/log(e). Additionally, you might add some error checking, what if someone enters a letter when you ask for a number?

Here's an example of a simple arithmetic calculator I wrote about 5-6 years ago. It handles parenthesis, and does error checking. It's written in C#, but if you want to glance over it you should get the gist as it's incredibly similar to C++. Hope it helps.
Edited by lordikon - 2/14/12 at 10:32pm
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › advanced basic C calculator with color api coded by me enjoy