Question

GUI Calculator in C language

Asked by: heyhey84

Hi experts,

Is it possible to write a GUI calculator with C? is it difficult?

I know writing a normal calculator program is not that tough though. I am interested in GUI calculator.

Thanks

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-10-07 at 09:25:03ID22016492
Tags

gui

Topic

C Programming Language

Participating Experts
5
Points
50
Comments
54

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Javascript calculate compound interest
    Hi, i have a question. how to create a Javascript and create a HTML documents that enable user to calculate compound interest. TEXT component will include(user enter): 1) principal amount 2) yearly interest rate 3) number of years formula a=p(1+r)n where p is the princip...
  2. Matrix Calculator GUI
    I am interested in developing a matrix calculator in Microsoft C++ similar to matlab which runs under Windows. I know how to implement the number crunching and parsing but I can't figure out how to develop the GUI (other than a console application). Any suggestions or examp...
  3. Java GUI Mortgage Calculator will not Calculate
    Here is my code for a Java Gui Calculator assignment that does not perform calculation when I mouse click on Calculate JButton. Please advise. Don't know what I am doing wrong here. /* * MortCalUP407.java * * Created on February 27, 2005, 3:08 PM *** * @author Paul ...
  4. mortgage calculator with GUI
    how can i modify this code so that i can be within a graphical user interface, must have user input for amount of mortgage. and Allow the user to loop back and enter new data or quit. //Version 1.1.1 find similar code //Version 1.2.1 remove unneeded code from program //Vers...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: bpmurrayPosted on 2006-10-07 at 13:57:28ID: 17683613

The issue here is the GUI side of things. Each platform has its own GUI API, so you have to decide which platform you want to use. Us old timers remember using C to create Windows apps, with the dreadful 100-line switch statement. The tendency today is to use C++/MFC without a basic understanding of how the APIs work, so it's interesting to see someone ask about it. Anyway, assuming you want to target Windows, here's a tutorial: http://www.winprog.org/tutorial/

 

by: heyhey84Posted on 2006-10-07 at 20:53:16ID: 17684985

Its becoz i come across this visopsys OS. then i want to use this platform and do a GUI calculator.

Hope u can advice me.

 

by: bpmurrayPosted on 2006-10-08 at 02:48:10ID: 17685606

I looked around, and could find nothing to indicate how to write anything at all for visopsys. I think your best bet is to download it, find the source for a few appplications, and use that as a template for your own apps.

 

by: efnPosted on 2006-10-08 at 08:25:02ID: 17686314

Yes, it is possible.  Yes, it is difficult.

 

by: heyhey84Posted on 2006-10-08 at 08:34:03ID: 17686344

Can you help find the source for me and how i can use it. I not quite sure how can i start.....

 

by: efnPosted on 2006-10-08 at 09:28:36ID: 17686491

The source for visopsys is here:

http://visopsys.org/files/visopsys/visopsys-0.63-src.zip

The sketchy window system documentation is here:

http://visopsys.org/developers/window_library.html

This is not sufficient to write an application that works.  You will have to study the source code to figure how to code an application.

It will be easier to develop a calculator with a GUI for a more established, better documented platform.

 

by: babypinkPosted on 2006-10-08 at 19:19:15ID: 17688335

Can guild me how to start. i am quite lost

 

by: AronRubinPosted on 2006-10-16 at 21:02:56ID: 17744791

Is this a simple calculator like what you would buy at dollar store or a scientific calculator? What operators will it support? Will it support variables?

Aron

 

by: fridomPosted on 2006-10-16 at 22:33:24ID: 17744982

How about starting very very simple?

Use the GUI tool of you choice. then add one output media. Add a few button representing the numbers, add one command  button like + and then see that this stuff works. Over time you simply can add more and more stuff and maybe you can look into sources of existing "calculators".

You can get some calculator (Windows GUI) at:
http://www.johnfindlay.plus.com/lcc-win32/sci_math/Numero.zip it's just about 17K.. So not that big really

Regards
Friedrich

 

by: heyhey84Posted on 2006-10-17 at 04:54:49ID: 17746721

Hi AronRubin , i would be just a simple GUI cal with simple operation like add,minus,mutiple and divide. My problem is the window API of visopsys OS.

Hope you u can advice

 

by: fridomPosted on 2006-10-17 at 05:43:32ID: 17746991

Visopsys seems to be an own operating system. If you do not have it, GUI libraries targeting this platform are definitly not what you can use. With high likliness you are either on Linux or Windows the for Windows you should visit the page I mentioned because this is calculator is written for Windows.

Regards
Friedrich

 

by: heyhey84Posted on 2006-10-18 at 17:22:12ID: 17762399

How can i use the Numero.zip and change the window Gui to least visopsys Gui first?

If it can be run in visopsy then will try to add in button etc for the caluculation.

 

by: AronRubinPosted on 2006-10-18 at 18:45:23ID: 17762717

this should get you pretty close:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/api.h>

objectKey answerLabel = NULL;
objectKey btnCE = NULL;
objectKey btnC = NULL;
objectKey btnBS = NULL;
objectKey btneq = NULL;
objectKey btnnum[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
objectKey btnadd = NULL;
objectKey btnsub = NULL;
objectKey btnmul = NULL;
objectKey btndiv = NULL;
objectKey btnsgn = NULL;
objectKey btndot = NULL;

static void eventHandler( objectKey obj, windowEvent *event ) {
  static float value = 0, lastval = 0;
  static char answer[128];
  static int operands = 0;
  static int operation;
  static int dot = 0;

  if(        obj == btnCE ) {
    value = 0;
    lastval = 0;
    operands = 0;
  } else if( obj == btnC ) {
    if( operands > 0 )
      operands--;
    if( operands > 1 )
      value = lastval;
  } else if( obj == btnBS ) {
  } else if( obj == btneq  || obj == btnadd || obj == btnsub ||
           obj == btnmul || obj == btndiv ) {
    if( operands > 1 ) {
      switch( operation ) {
      case 0: lastval = value = lastval + value; break;
      case 1: lastval = value = lastval - value; break;
      case 2: lastval = value = lastval * value; break;
      case 3: lastval = value = lastval / value; break;
      }
      operands = 1;
      if( obj != btneq ) {
      }
    }
    if( operands > 0 ) {
      if( obj == btnadd ) {
      lastval = value;
      operands++;
      operation = 0;
      } else if( obj == btnsub ) {
      lastval = value;
      operands++;
      operation = 1;
      } else if( obj == btnmul ) {
      lastval = value;
      operands++;
      operation = 2;
      } else if( obj == btndiv ) {
      lastval = value;
      operands++;
      operation = 3;
      }
    }
  } else {
    for( i = 0; i < 10; i++ )
      if( obj == btnnum[i] )
      value = value*10.0 + (float)i;
  }

  sprintf( answer, "%g", value );
  windowComponentSetData( answerLabel, answer, strlen( answer ) );
}

int main( int argc, char *argv[] ) {
  int status = 0;
  componentParameters params;

  int count;

  // Create a new window
  top = windowNew(multitaskerGetCurrentProcessId(), "Calc");
  if( !top )
    exit( ERR_NOTINITIALIZED );

  bzero(&params, sizeof(componentParameters));
  params.gridWidth = 4;
  params.gridHeight = 1;
  params.padLeft = 5;
  params.padRight = 5;
  params.padTop = 5;
  params.padBottom = 5;
  params.orientationX = orient_left;
  params.orientationY = orient_middle;
  params.flags |= WINDOW_COMPFLAG_FIXEDWIDTH;
  answerLabel = windowNewTextLabel( top, "0", &params );

  params.gridWidth = 1;
  params.gridY = 1;
  params.orientationX = orient_center;

  btnCE = windowNewButton( top, "CE", NULL, &params );
  params.gridX++;
  btnC = windowNewButton( top, "C", NULL, &params );
  params.gridX++;
  btnBS = windowNewButton( top, "BS", NULL, &params );
  params.gridX++;
  btneq = windowNewButton( top, "=", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[7] = windowNewButton( top, "7", NULL, &params );
  params.gridX++;
  btnnum[6] = windowNewButton( top, "8", NULL, &params );
  params.gridX++;
  btnnum[5] = windowNewButton( top, "9", NULL, &params );
  params.gridX++;
  btndiv = windowNewButton( top, "/", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[4] = windowNewButton( top, "4", NULL, &params );
  params.gridX++;
  btnnum[5] = windowNewButton( top, "5", NULL, &params );
  params.gridX++;
  btnnum[6] = windowNewButton( top, "6", NULL, &params );
  params.gridX++;
  btnmul = windowNewButton( top, "*", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[1] = windowNewButton( top, "1", NULL, &params );
  params.gridX++;
  btnnum[2] = windowNewButton( top, "2", NULL, &params );
  params.gridX++;
  btnnum[3] = windowNewButton( top, "3", NULL, &params );
  params.gridX++;
  btnsub = windowNewButton( top, "-", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[0] = windowNewButton( top, "0", NULL, &params );
  params.gridX++;
  btnsgn = windowNewButton( top, "-+", NULL, &params );
  params.gridX++;
  btndot = windowNewButton( top, ".", NULL, &params );
  params.gridX++;
  btnadd = windowNewButton( top, "+", NULL, &params );

  windowRegisterEventHandler( top, &eventHandler);

  windowGuiRun();
}

 

by: heyhey84Posted on 2006-10-18 at 19:34:39ID: 17762902

I was wondering visopsys uses it own windows Api. Does this works?

i think no right?

 

by: AronRubinPosted on 2006-10-18 at 20:10:15ID: 17763015

yes I bothered to read the source.

 

by: heyhey84Posted on 2006-10-18 at 20:30:40ID: 17763082

Yes u mean visopsys can works?

 

by: AronRubinPosted on 2006-10-18 at 20:34:25ID: 17763098

it uses the visopsys API. I cannot claim that it will run because I could not compile, debug, or test.

 

by: heyhey84Posted on 2006-10-19 at 01:51:08ID: 17764089

Ic
Thanks

I will try to figure out how to complie in visipsys.

 

by: heyhey84Posted on 2006-10-20 at 03:50:39ID: 17772596

I cant seem to figure out how i can complie the calc.c  in visopsys which AronRubin  posted.

 

by: babypinkPosted on 2006-10-20 at 10:21:34ID: 17775386

Can someone teach me how to compile, debug and test.

 

by: heyhey84Posted on 2006-10-20 at 10:56:27ID: 17775670

i try to complie and get this error

calc.c: In function `eventHandler':
calc.c:19: warning: unused parameter `event'  
calc.c: In function `main':
calc.c:79: warning: unused parameter `argc'
calc.c:79: warning: unused parameter `argv'


line 19 -> static void eventHandler( objectKey obj, windowEvent *event )
line 79 -> int main( int argc, char *argv[] )

 

by: AronRubinPosted on 2006-10-20 at 12:18:05ID: 17776411

Those are not errors, they are warnings.

 

by: AronRubinPosted on 2006-10-20 at 12:21:43ID: 17776443

babypink,
  I imagine some people could. They have whole courses on this stuff ;) What problems are you having that need improvement?

 

by: heyhey84Posted on 2006-10-20 at 18:09:18ID: 17778244

Hi AronRubin

then how can i run it?

 

by: AronRubinPosted on 2006-10-21 at 07:43:21ID: 17780430

persumably by double clicking on the executable produced. If you did not give it a name, traditionally it would be called a.out

 

by: heyhey84Posted on 2006-10-21 at 08:24:02ID: 17780569

In i am using Bloodshed Dev-C++

Are u refering to linux platform?

 

by: heyhey84Posted on 2006-10-21 at 08:40:06ID: 17780629

Hi AronRubin,

I try in Fedora core 5 using GCC and windowa using Bloodshed Dev-C++.

Cant get it fix

Can u tell me ur detail step in compiling. like what platform, what software etc?....

Thanks

 

by: AronRubinPosted on 2006-10-21 at 17:26:37ID: 17782237

I did not compile for visopsys because I do not run that software. If you were looking for a easy gui api on Linux you could try http://otk.sf.net or if you are looking for a main stream gui api you could try gtk+.

for otk
windowNewTextLabel above becomes OtkMakeTextLabel
windowNewButton above becomes OtkMakeButton
etc.

for gtk+
windowNewTextLabel becomes gtk_label_new and gtk_container_add
windowNewButton becomes gtk_button_new_with_label and gtk_container_add
etc.

 

by: heyhey84Posted on 2006-10-22 at 23:18:31ID: 17786721

Hi AronRubin ,

sorry to trouble u
Can u try to compile visopsys.
And tell me what the step to do it
I seem cant to get in work...

 

by: AronRubinPosted on 2006-10-23 at 09:54:54ID: 17789835

nope, I have negative time for such things.

 

by: heyhey84Posted on 2006-10-24 at 08:03:39ID: 17796378

anyone out there can help?

 

by: heyhey84Posted on 2006-10-25 at 06:30:14ID: 17803127

i try to compile and get this warning...

I think need to solve this warning in order to to compile properly in visopsys

Experts pls help to solve

Thank in advance

calc.c: In function `eventHandler':
calc.c:19: warning: unused parameter `event'  
calc.c: In function `main':
calc.c:79: warning: unused parameter `argc'
calc.c:79: warning: unused parameter `argv'


line 19 -> static void eventHandler( objectKey obj, windowEvent *event )
line 79 -> int main( int argc, char *argv[] )

 

by: heyhey84Posted on 2006-10-26 at 21:17:14ID: 17817098

is there any kind soul out there?

 

by: AronRubinPosted on 2006-10-28 at 07:52:15ID: 17825560

MS Style solution:
#define UNREFERENCED_PARAMETER(P) {(P)=(P);}

then later:
UNREFERENCED_PARAMETER(event);
etc.

 

by: heyhey84Posted on 2006-10-28 at 09:20:44ID: 17825934

i am using fedora 5..............

 

by: heyhey84Posted on 2006-10-28 at 10:43:54ID: 17826232

Hi i manage to get it into Visopsys
However some problem with the program. pls refer to the image link
http://img131.imageshack.us/my.php?image=calczf6.jpg

below is the source code...
===================================

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/api.h>
#include <sys/window.h>

objectKey answerLabel = NULL;
objectKey btnCE = NULL;
objectKey btnC = NULL;
objectKey btnBS = NULL;
objectKey btneq = NULL;
objectKey btnnum[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
objectKey btnadd = NULL;
objectKey btnsub = NULL;
objectKey btnmul = NULL;
objectKey btndiv = NULL;
objectKey btnsgn = NULL;
objectKey btndot = NULL;



static void eventHandler( objectKey obj, windowEvent *event ) {
      
      static objectKey window = NULL;
 
  window =0;
  event =0;
 
 
 
  static float value = 0, lastval = 0;
  static char answer[128];
  static int operands = 0;
  static int operation;
  static int i;
  /*static int dot = 0;*/

  if(obj == btnCE ) {
    value = 0;
    lastval = 0;
    operands = 0;
  } else if( obj == btnC ) {
    if( operands > 0 )
      operands--;
    if( operands > 1 )
      value = lastval;
  } else if( obj == btnBS ) {
  } else if( obj == btneq  || obj == btnadd || obj == btnsub ||
          obj == btnmul || obj == btndiv ) {
    if( operands > 1 ) {
      switch( operation ) {
      case 0: lastval = value = lastval + value; break;
      case 1: lastval = value = lastval - value; break;
      case 2: lastval = value = lastval * value; break;
      case 3: lastval = value = lastval / value; break;
      }
      operands = 1;
      if( obj != btneq ) {
      }
    }
    if( operands > 0 ) {
      if( obj == btnadd ) {
     lastval = value;
     operands++;
     operation = 0;
      } else if( obj == btnsub ) {
     lastval = value;
     operands++;
     operation = 1;
      } else if( obj == btnmul ) {
     lastval = value;
     operands++;
     operation = 2;
      } else if( obj == btndiv ) {
     lastval = value;
     operands++;
     operation = 3;
      }
    }
  } else {
    for( i = 0; i < 10; i++ )
      if( obj == btnnum[i] )
     value = value*10.0 + (float)i;
  }

  sprintf( answer, "%g", value );
  windowComponentSetData( answerLabel, answer, strlen( answer ) );
}

void main(int argc, char *argv[]) {
 
 
argc = 0;
argv =0;      
 
  /*int status = 0;*/
  componentParameters params;
  static objectKey top = NULL;

  /*int count;*/

  // Create a new window
  top = windowNew(multitaskerGetCurrentProcessId(), "Calc");
 
   

  bzero(&params, sizeof(componentParameters));
  params.gridWidth = 4;
  params.gridHeight = 1;
  params.padLeft = 5;
  params.padRight = 5;
  params.padTop = 5;
  params.padBottom = 5;
  params.orientationX = orient_left;
  params.orientationY = orient_middle;
  /*params.flags |= WINDOW_COMPFLAG_FIXEDWIDTH;*/
  answerLabel = windowNewTextLabel( top, "0", &params );

  params.gridWidth = 1;
  params.gridY = 1;
  params.orientationX = orient_center;

  btnCE = windowNewButton( top, "CE", NULL, &params );
  params.gridX++;
  btnC = windowNewButton( top, "C", NULL, &params );
  params.gridX++;
  btnBS = windowNewButton( top, "BS", NULL, &params );
  params.gridX++;
  btneq = windowNewButton( top, "=", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[7] = windowNewButton( top, "7", NULL, &params );
  params.gridX++;
  btnnum[6] = windowNewButton( top, "8", NULL, &params );
  params.gridX++;
  btnnum[5] = windowNewButton( top, "9", NULL, &params );
  params.gridX++;
  btndiv = windowNewButton( top, "/", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[4] = windowNewButton( top, "4", NULL, &params );
  params.gridX++;
  btnnum[5] = windowNewButton( top, "5", NULL, &params );
  params.gridX++;
  btnnum[6] = windowNewButton( top, "6", NULL, &params );
  params.gridX++;
  btnmul = windowNewButton( top, "*", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[1] = windowNewButton( top, "1", NULL, &params );
  params.gridX++;
  btnnum[2] = windowNewButton( top, "2", NULL, &params );
  params.gridX++;
  btnnum[3] = windowNewButton( top, "3", NULL, &params );
  params.gridX++;
  btnsub = windowNewButton( top, "-", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[0] = windowNewButton( top, "0", NULL, &params );
  params.gridX++;
  btnsgn = windowNewButton( top, "-+", NULL, &params );
  params.gridX++;
  btndot = windowNewButton( top, ".", NULL, &params );
  params.gridX++;
  btnadd = windowNewButton( top, "+", NULL, &params );

  windowRegisterEventHandler( top, &eventHandler);

  windowGuiRun();
}





 
 

 

by: heyhey84Posted on 2006-10-28 at 10:44:49ID: 17826236

AronRubin once again will need your help...

Thanks in advance

 

by: heyhey84Posted on 2006-10-28 at 11:43:30ID: 17826450

Hi AronRubin

i have manage to solve the above program...

however when i click the number... the screen will not change the value.

meaning whatever i click the value remain at "0"

 

by: heyhey84Posted on 2006-10-28 at 20:12:49ID: 17827628

is it this part of code have the problem?

  printf( answer, "%c", value );
  windowComponentSetData( answerLabel, answer, strlen( answer ) );

Somehow it does not show anything in the answerLabel

 

by: AronRubinPosted on 2006-10-29 at 09:08:42ID: 17829278

You altered the sprintf line, was that intentional?

 

by: AronRubinPosted on 2006-10-29 at 09:17:46ID: 17829313

you could try two things
1. change the above line to:
windowComponentSetData( answerLabel, answer, strlen( answer ) + 1 );
2. you could try forcing a redraw after the value change:
windowComponentDraw( answerLabel );

 

by: heyhey84Posted on 2006-10-29 at 19:38:24ID: 17831578

It still cant work. I copy n paste all the code here. Please help me to solve the problem. Thanks

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/api.h>
#include <sys/window.h>

static objectKey answerLabel = NULL;
static objectKey btnCE = NULL;
static objectKey btnC = NULL;
static objectKey btnBS = NULL;
static objectKey btneq = NULL;
static objectKey btnnum[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
static objectKey btnadd = NULL;
static objectKey btnsub = NULL;
static objectKey btnmul = NULL;
static objectKey btndiv = NULL;
static objectKey btnsgn = NULL;
static objectKey btndot = NULL;
static objectKey top = NULL;

static void eventHandler( objectKey obj, windowEvent *event ) {
 
  static float value = 0, lastval = 0;
  static char answer[128];
  static int operands = 0;
  static int operation;
 
  int i;
  event=0;

  if( obj == btnCE ) {
    value = 0;
    lastval = 0;
    operands = 0;
  } else if( obj == btnC ) {
    if( operands > 0 )
      operands--;
    if( operands > 1 )
      value = lastval;
  } else if( obj == btnBS ) {
  } else if( obj == btneq  || obj == btnadd || obj == btnsub ||
          obj == btnmul || obj == btndiv ) {
    if( operands > 1 ) {
      switch( operation ) {
      case 0: lastval = value = lastval + value; break;
      case 1: lastval = value = lastval - value; break;
      case 2: lastval = value = lastval * value; break;
      case 3: lastval = value = lastval / value; break;
      }
      operands = 1;
      if( obj != btneq ) {
      }
    }
    if( operands > 0 ) {
      if( obj == btnadd ) {
     lastval = value;
     operands++;
     operation = 0;
      } else if( obj == btnsub ) {
     lastval = value;
     operands++;
     operation = 1;
      } else if( obj == btnmul ) {
     lastval = value;
     operands++;
     operation = 2;
      } else if( obj == btndiv ) {
     lastval = value;
     operands++;
     operation = 3;
      }
    }
  } else {
    for( i = 0; i < 10; i++ )
      if( obj == btnnum[i] )
     value = value*10.0 + (float)i;
  }
   
  sprintf( answer, "%g", value );  
  windowComponentSetData( answerLabel, answer, strlen( answer ) + 1 );
  windowComponentDraw( answerLabel );
 



}



int main( int argc, char *argv[] ) {
  argc =0;
  argv =0;
 
 
  componentParameters params;

 

  // Create a new window
  top = windowNew(multitaskerGetCurrentProcessId(), "Calc");
  if( !top )
    exit( ERR_NOTINITIALIZED );

  bzero(&params, sizeof(componentParameters));
  params.gridWidth = 4;
  params.gridHeight = 1;
  params.padLeft = 5;
  params.padRight = 5;
  params.padTop = 5;
  params.padBottom = 5;
  params.orientationX = orient_left;
  params.orientationY = orient_middle;
  params.useDefaultForeground = 1;
  params.useDefaultBackground = 1;
 
  answerLabel = windowNewTextLabel( top, "0", &params );

  params.gridWidth = 1;
  params.gridY = 1;
  params.orientationX = orient_center;

  btnCE = windowNewButton( top, "CE", NULL, &params );
  params.gridX++;
  btnC = windowNewButton( top, "C", NULL, &params );
  params.gridX++;
  btnBS = windowNewButton( top, "BS", NULL, &params );
  params.gridX++;
  btneq = windowNewButton( top, "=", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[7] = windowNewButton( top, "7", NULL, &params );
  params.gridX++;
  btnnum[6] = windowNewButton( top, "8", NULL, &params );
  params.gridX++;
  btnnum[5] = windowNewButton( top, "9", NULL, &params );
  params.gridX++;
  btndiv = windowNewButton( top, "/", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[4] = windowNewButton( top, "4", NULL, &params );
  params.gridX++;
  btnnum[5] = windowNewButton( top, "5", NULL, &params );
  params.gridX++;
  btnnum[6] = windowNewButton( top, "6", NULL, &params );
  params.gridX++;
  btnmul = windowNewButton( top, "*", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[1] = windowNewButton( top, "1", NULL, &params );
  params.gridX++;
  btnnum[2] = windowNewButton( top, "2", NULL, &params );
  params.gridX++;
  btnnum[3] = windowNewButton( top, "3", NULL, &params );
  params.gridX++;
  btnsub = windowNewButton( top, "-", NULL, &params );

  params.gridY++;
  params.gridX = 0;

  btnnum[0] = windowNewButton( top, "0", NULL, &params );
  params.gridX++;
  btnsgn = windowNewButton( top, "-+", NULL, &params );
  params.gridX++;
  btndot = windowNewButton( top, ".", NULL, &params );
  params.gridX++;
  btnadd = windowNewButton( top, "+", NULL, &params );

  windowRegisterEventHandler( top, &eventHandler);

  windowGuiRun();
  return 1;
}

 

by: heyhey84Posted on 2006-10-31 at 04:13:14ID: 17841295

Hi AronRubin ,

Are u able to solve the problem?

The code is already pasted above.

Thanks in advance

 

by: heyhey84Posted on 2006-11-06 at 04:34:59ID: 17880430

Hi AronRubin,

u r not around?

 

by: heyhey84Posted on 2006-11-14 at 06:12:33ID: 17938115

I have tried by hard code the variable

value = 7.045;
    sprintf( answer, "%f", value );
    windowComponentSetData( textArea, answer, strlen( answer ) );

Somehow i think the sprintf doesnt work

How?

 

by: AronRubinPosted on 2006-11-14 at 06:28:19ID: 17938256

Does the following work?

//sprintf( answer, "%f", value );
answer = "7.045";
windowComponentSetData( textArea, answer, strlen( answer ) );

 

by: heyhey84Posted on 2006-11-14 at 06:48:21ID: 17938432

At first there is a compile error
incompatible type
answer = "7.045" - > this line

then i tried strcpy(answer, "7.045");

strcpy(answer, "7.045");
windowComponentSetData( textArea, answer, strlen( answer ) );
This works.. the textArea will be replace

But
sprintf(answer, "%g", value);
windowComponentSetData( textArea, answer, strlen( answer ) );
doesnt work

Wat wrong with the sprintf?

 

by: AronRubinPosted on 2006-11-14 at 07:59:52ID: 17938962

maybe that sprintf is broken. try including this function and using it instead:
char *gcvt(double number, size_t ndigit, char *buf)
{
      int sign, decpt;
      register char *p1, *p2;
      register i;

      p1 = ecvt(number, ndigit, &decpt, &sign);
      p2 = buf;
      if (sign)
            *p2++ = '-';
      for (i=ndigit-1; i>0 && p1[i]=='0'; i--)
            ndigit--;
      if ((decpt >= 0 && decpt-ndigit > 4)
       || (decpt < 0 && decpt < -3)) { /* use E-style */
            decpt--;
            *p2++ = *p1++;
            *p2++ = '.';
            for (i=1; i<ndigit; i++)
                  *p2++ = *p1++;
            *p2++ = 'e';
            if (decpt<0) {
                  decpt = -decpt;
                  *p2++ = '-';
            } else
                  *p2++ = '+';
            if (decpt/100 > 0)
              *p2++ = decpt/100 + '0';
            if (decpt/10 > 0)
              *p2++ = (decpt%100)/10 + '0';
            *p2++ = decpt%10 + '0';
      } else {
            if (decpt<=0) {
                  if (*p1!='0')
                        *p2++ = '.';
                  while (decpt<0) {
                        decpt++;
                        *p2++ = '0';
                  }
            }
            for (i=1; i<=ndigit; i++) {
                  *p2++ = *p1++;
                  if (i==decpt)
                        *p2++ = '.';
            }
            if (ndigit<decpt) {
                  while (ndigit++<decpt)
                        *p2++ = '0';
                  *p2++ = '.';
            }
      }
      if (p2[-1]=='.')
            p2--;
      *p2 = '\0';
      return(buf);
}

 

by: AronRubinPosted on 2006-11-14 at 08:03:02ID: 17938986

Sorry you need this too:
/*
 * $Id: cvt.c,v 1.2 1995/06/01 01:19:05 chuck Exp $
 *
 * cvt.c - IEEE floating point formatting routines for FreeBSD
 * from GNU libc-4.6.27
 */

#include <stdlib.h>
#include <math.h>

#undef      IEEE

/*
 *      ecvt converts to decimal
 *      the number of digits is specified by ndigit
 *      decpt is set to the position of the decimal point
 *      sign is set to 0 for positive, 1 for negative
 */

#ifdef IEEE

static inline int __isspecial(double f, char *bp)
{
      union ieee754_double *ip = (union ieee754_double *) &f;

      if ((ip->ieee.exponent & 0x7ff) != 0x7ff)
            return(0);
      if (ip->ieee.mantissa0 || ip->ieee.mantissa1)
            strcpy(bp, "NaN");
      else if (ip->ieee.negative)
            strcpy(bp, "-Infinity");
      else
            strcpy(bp, "Infinity");
      return(1);
}

#define      NDIG      512
#else
#define      NDIG      80
#endif

static char *cvt(double arg, size_t ndigits, int *decpt, int *sign, int eflag) {
      register int r2;
      double fi, fj;
      register char *p, *p1;
      static char buf[NDIG];

#ifdef IEEE
      /* XXX */
      if (__isspecial(arg, buf))
            return(buf);
#endif
      if (ndigits>=NDIG-1)
            ndigits = NDIG-2;
      r2 = 0;
      *sign = 0;
      p = &buf[0];
      if (arg<0) {
            *sign = 1;
            arg = -arg;
      }
      arg = modf(arg, &fi);
      p1 = &buf[NDIG];
      /*
       * Do integer part
       */
      if (fi != 0) {
            p1 = &buf[NDIG];
            while (fi != 0) {
                  fj = modf(fi/10, &fi);
                  *--p1 = (int)((fj+.03)*10) + '0';
                  r2++;
            }
            while (p1 < &buf[NDIG])
                  *p++ = *p1++;
      } else if (arg > 0) {
            while ((fj = arg*10) < 1) {
                  arg = fj;
                  r2--;
            }
      }
      p1 = &buf[ndigits];
      if (eflag==0)
            p1 += r2;
      *decpt = r2;
      if (p1 < &buf[0]) {
            buf[0] = '\0';
            return(buf);
      }
      while (p<=p1 && p<&buf[NDIG]) {
            arg *= 10;
            arg = modf(arg, &fj);
            *p++ = (int)fj + '0';
      }
      if (p1 >= &buf[NDIG]) {
            buf[NDIG-1] = '\0';
            return(buf);
      }
      p = p1;
      *p1 += 5;
      while (*p1 > '9') {
            *p1 = '0';
            if (p1>buf)
                  ++*--p1;
            else {
                  *p1 = '1';
                  (*decpt)++;
                  if (eflag==0) {
                        if (p>buf)
                              *p = '0';
                        p++;
                  }
            }
      }
      *p = '\0';
      return(buf);
}

char *ecvt(double arg, size_t ndigits, int *decpt, int *sign) {
      return(cvt(arg, ndigits, decpt, sign, 1));
}

char *fcvt(double arg, size_t ndigits, int *decpt, int *sign) {
      return(cvt(arg, ndigits, decpt, sign, 0));
}

 

by: heyhey84Posted on 2006-11-14 at 18:38:35ID: 17943519

So i include all above code to my calc.c?

 

by: heyhey84Posted on 2006-11-14 at 18:47:54ID: 17943557

how to use the gcvt? can give me an example?

 

by: heyhey84Posted on 2006-11-14 at 21:21:02ID: 17944544

There is a error in function 'gcvt'

warning : comparison between signed and unsigned.

in visopsys i cannot ignore the warning, else i cannot compile.

 

by: heyhey84Posted on 2006-11-14 at 21:41:19ID: 17944616

This is the sprintf of visospsy. i think it dont support %g. I use %d and it can be display.

Can we edit the sorintf function to support %g?

// This is the standard "sprintf" function, as found in standard C libraries

#include <stdio.h>
#include <stdarg.h>
#include <sys/cdefs.h>


int sprintf(char *output, const char *format, ...)
{
  // This function will construct a single string out of the format
  // string and arguments that are passed.  Returns the number of
  // characters copied to the output string.

  va_list list;
  int outputLen = 0;

  // Initialize the argument list
  va_start(list, format);

  // Fill out the output line based on
  outputLen = _expandFormatString(output, format, list);

  va_end(list);

  // Return the number of characters we wrote to the string
  return (outputLen);
}

 

by: AronRubinPosted on 2006-11-19 at 16:31:33ID: 17976447

do the supplied functions not work?

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...