Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › A program in C that can solve arithmetic expressions... It seems buggy. Can anyone help me, please?
New Posts  All Forums:Forum Nav:

# A program in C that can solve arithmetic expressions... It seems buggy. Can anyone help me, please?

Hey guys. I've been working on this code for a school project. what it does is to solve arithmetic expressions. I'll show you some input and output samples:

Input: 5
Output: 5

Input: -5
Output: -5

Input: 1337*42/14+650-320*500-500000000
Output: -500155339

Input: 56154/14+650-320*500
Output: -155339

Input: 3+5*6-4/2+6*9
Output: 85

All these samples above work.

Here's the link for my source code http://pastebin.com/kLwzNNZE

The program is coded to respect PEMDAS and support negative numbers. I've been trying to implement decimal numbers, as well, but there are a few problems:

If I input something like -5-7+0.4*0.2, the program, for some reason, loops. Funny enough, if I do each operation separately, it seems to work just fine (As in -5-7=-12, or 0.4*0.2=0.08). The problem is when doing them together. Debugging it, it does the first operation: -5-7, replaces that with a -12 and then proceeds to do the multiplication, but then it loops. Same with 1+0.2*0.2 as an input or 1+0.3*0.2.

Do you guys have ANY idea whatsoever what's going on here and how I can fix it? Been trying to sort it out for hours, but so far, no luck.

Any help is appreciated. Thanks!
Edited by Icekilla - 11/21/12 at 9:57pm
 fr0sty (20 items) SnowFlake (5 items)
CPURAMHard DriveHard Drive
Intel Core i7 3520M  Corsair Vengeance DDR3 2x 8GB Samsung EVO SSD Hitachi Apple Hard Drive 5400RPM
OS
macOS Sierra
 fr0sty (20 items) SnowFlake (5 items)
CPURAMHard DriveHard Drive
Intel Core i7 3520M  Corsair Vengeance DDR3 2x 8GB Samsung EVO SSD Hitachi Apple Hard Drive 5400RPM
OS
macOS Sierra
Print out the intermediary values for debugging.

I see you have them commented out?
 Once again... (13 items)
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz]
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro
 Once again... (13 items)
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz]
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro
Affirmative. I commented them.
 fr0sty (20 items) SnowFlake (5 items)
CPURAMHard DriveHard Drive
Intel Core i7 3520M  Corsair Vengeance DDR3 2x 8GB Samsung EVO SSD Hitachi Apple Hard Drive 5400RPM
OS
macOS Sierra
 fr0sty (20 items) SnowFlake (5 items)
CPURAMHard DriveHard Drive
Intel Core i7 3520M  Corsair Vengeance DDR3 2x 8GB Samsung EVO SSD Hitachi Apple Hard Drive 5400RPM
OS
macOS Sierra
I'd be willing to bet your looping is due to the flaws with scanf and family.
 Ol' Sandy (28 items) "Zeus" (12 items) Elite Preview (6 items)
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8)
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM
Hard DriveOS
256GB SSD Windows 10 Insider Preview
 Ol' Sandy (28 items) "Zeus" (12 items) Elite Preview (6 items)
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8)
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM
Hard DriveOS
256GB SSD Windows 10 Insider Preview
I changed the scanf in the main function for a gets(op), which is safer. It does nothing to solve the problem, though.

I think the problem is in the loops and the of statements. It's doing something that it shouldn't, but I can't figure it out.
 fr0sty (20 items) SnowFlake (5 items)
CPURAMHard DriveHard Drive
Intel Core i7 3520M  Corsair Vengeance DDR3 2x 8GB Samsung EVO SSD Hitachi Apple Hard Drive 5400RPM
OS
macOS Sierra
 fr0sty (20 items) SnowFlake (5 items)
CPURAMHard DriveHard Drive
Intel Core i7 3520M  Corsair Vengeance DDR3 2x 8GB Samsung EVO SSD Hitachi Apple Hard Drive 5400RPM
OS
macOS Sierra
Sorry for the late reply. I want to show you what I've got so far. Right now, as it is, it works with exponentials, multiplication, division, addition and substraction. It's just missing support for parenthesis.

Anybody wants to try it out?

http://pastebin.com/GaXdiW8V

Also, how can I apply parenthesis? My code pretty much works by parsing everything and detecting signs, so I guess I'd just have to add a few conditions here and there, and add another function.

Any suggestions?
 fr0sty (20 items) SnowFlake (5 items)
CPURAMHard DriveHard Drive
Intel Core i7 3520M  Corsair Vengeance DDR3 2x 8GB Samsung EVO SSD Hitachi Apple Hard Drive 5400RPM
OS
macOS Sierra
 fr0sty (20 items) SnowFlake (5 items)
CPURAMHard DriveHard Drive
Intel Core i7 3520M  Corsair Vengeance DDR3 2x 8GB Samsung EVO SSD Hitachi Apple Hard Drive 5400RPM
OS
macOS Sierra
Well...it would require a bit of rewriting, but you could use an operator precedence parser to add support for parentesis...
 Meh (14 items) Toshiba Satellite P55-A5312 (14 items)
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 Ghz GA-P55-USB3 Gigabyte GTX 460 16GB(4x4) Corsair Vengeance
Hard DriveOptical DriveCoolingOS
Samsung Spinpoint F3 1TB Asus Cheapo Corsair H70 Arch Linux
MonitorKeyboardPowerCase
LG 26LH20 HDTV Das Model S Ultimate Silent Corsair HX850 Lian Li(Lancool) PC-K62
Logitech MX 518 My desk.
CPUMotherboardGraphicsRAM
Intel Core i5-4200U Toshiba Intel HD Graphics 4400 6 GB DDR3L @1600 Mhz
Hard DriveOptical DriveCoolingOS
750 GB 5400RPM SATA HDD DVD SuperMulti drive Stock fans Arch Linux
MonitorKeyboardPowerCase
15.6" 1920x1080 Screen Toshiba Toshiba battery and A/C adapter Toshiba
MouseAudio
 Meh (14 items) Toshiba Satellite P55-A5312 (14 items)
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 Ghz GA-P55-USB3 Gigabyte GTX 460 16GB(4x4) Corsair Vengeance
Hard DriveOptical DriveCoolingOS
Samsung Spinpoint F3 1TB Asus Cheapo Corsair H70 Arch Linux
MonitorKeyboardPowerCase
LG 26LH20 HDTV Das Model S Ultimate Silent Corsair HX850 Lian Li(Lancool) PC-K62
Logitech MX 518 My desk.
CPUMotherboardGraphicsRAM
Intel Core i5-4200U Toshiba Intel HD Graphics 4400 6 GB DDR3L @1600 Mhz
Hard DriveOptical DriveCoolingOS
750 GB 5400RPM SATA HDD DVD SuperMulti drive Stock fans Arch Linux
MonitorKeyboardPowerCase
15.6" 1920x1080 Screen Toshiba Toshiba battery and A/C adapter Toshiba
MouseAudio
That requires data structures, right? I ask because I haven't taken that class yet (Next semester).

Anyway, I took a look at the code, di you think I could add another function that parses for parenthesis and creates a new string?
Edited by Icekilla - 11/27/12 at 5:38pm
 fr0sty (20 items) SnowFlake (5 items)
CPURAMHard DriveHard Drive
Intel Core i7 3520M  Corsair Vengeance DDR3 2x 8GB Samsung EVO SSD Hitachi Apple Hard Drive 5400RPM
OS
macOS Sierra
 fr0sty (20 items) SnowFlake (5 items)
CPURAMHard DriveHard Drive
Intel Core i7 3520M  Corsair Vengeance DDR3 2x 8GB Samsung EVO SSD Hitachi Apple Hard Drive 5400RPM
OS
macOS Sierra
Actually, if you follow the pseudo-code on Wikipedia, it can be done without the use of a parse tree for a simple calculator. You will need(or at least it'll be much easier if you do) to scan and tokenize input first though.
 Meh (14 items) Toshiba Satellite P55-A5312 (14 items)
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 Ghz GA-P55-USB3 Gigabyte GTX 460 16GB(4x4) Corsair Vengeance
Hard DriveOptical DriveCoolingOS
Samsung Spinpoint F3 1TB Asus Cheapo Corsair H70 Arch Linux
MonitorKeyboardPowerCase
LG 26LH20 HDTV Das Model S Ultimate Silent Corsair HX850 Lian Li(Lancool) PC-K62
Logitech MX 518 My desk.
CPUMotherboardGraphicsRAM
Intel Core i5-4200U Toshiba Intel HD Graphics 4400 6 GB DDR3L @1600 Mhz
Hard DriveOptical DriveCoolingOS
750 GB 5400RPM SATA HDD DVD SuperMulti drive Stock fans Arch Linux
MonitorKeyboardPowerCase
15.6" 1920x1080 Screen Toshiba Toshiba battery and A/C adapter Toshiba
MouseAudio
 Meh (14 items) Toshiba Satellite P55-A5312 (14 items)
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 Ghz GA-P55-USB3 Gigabyte GTX 460 16GB(4x4) Corsair Vengeance
Hard DriveOptical DriveCoolingOS
Samsung Spinpoint F3 1TB Asus Cheapo Corsair H70 Arch Linux
MonitorKeyboardPowerCase
LG 26LH20 HDTV Das Model S Ultimate Silent Corsair HX850 Lian Li(Lancool) PC-K62
Logitech MX 518 My desk.
CPUMotherboardGraphicsRAM
Intel Core i5-4200U Toshiba Intel HD Graphics 4400 6 GB DDR3L @1600 Mhz
Hard DriveOptical DriveCoolingOS
750 GB 5400RPM SATA HDD DVD SuperMulti drive Stock fans Arch Linux
MonitorKeyboardPowerCase
15.6" 1920x1080 Screen Toshiba Toshiba battery and A/C adapter Toshiba
MouseAudio