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.
Main Topics
Browse All TopicsHi 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.
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.
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.
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.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
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.
The source for visopsys is here:
http://visopsys.org/files/
The sketchy window system documentation is here:
http://visopsys.org/develo
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.
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.plu
Regards
Friedrich
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
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(multitaskerGetCu
if( !top )
exit( ERR_NOTINITIALIZED );
bzero(¶ms, 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", ¶ms );
params.gridWidth = 1;
params.gridY = 1;
params.orientationX = orient_center;
btnCE = windowNewButton( top, "CE", NULL, ¶ms );
params.gridX++;
btnC = windowNewButton( top, "C", NULL, ¶ms );
params.gridX++;
btnBS = windowNewButton( top, "BS", NULL, ¶ms );
params.gridX++;
btneq = windowNewButton( top, "=", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[7] = windowNewButton( top, "7", NULL, ¶ms );
params.gridX++;
btnnum[6] = windowNewButton( top, "8", NULL, ¶ms );
params.gridX++;
btnnum[5] = windowNewButton( top, "9", NULL, ¶ms );
params.gridX++;
btndiv = windowNewButton( top, "/", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[4] = windowNewButton( top, "4", NULL, ¶ms );
params.gridX++;
btnnum[5] = windowNewButton( top, "5", NULL, ¶ms );
params.gridX++;
btnnum[6] = windowNewButton( top, "6", NULL, ¶ms );
params.gridX++;
btnmul = windowNewButton( top, "*", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[1] = windowNewButton( top, "1", NULL, ¶ms );
params.gridX++;
btnnum[2] = windowNewButton( top, "2", NULL, ¶ms );
params.gridX++;
btnnum[3] = windowNewButton( top, "3", NULL, ¶ms );
params.gridX++;
btnsub = windowNewButton( top, "-", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[0] = windowNewButton( top, "0", NULL, ¶ms );
params.gridX++;
btnsgn = windowNewButton( top, "-+", NULL, ¶ms );
params.gridX++;
btndot = windowNewButton( top, ".", NULL, ¶ms );
params.gridX++;
btnadd = windowNewButton( top, "+", NULL, ¶ms );
windowRegisterEventHandler
windowGuiRun();
}
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[] )
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.
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[] )
Hi i manage to get it into Visopsys
However some problem with the program. pls refer to the image link
http://img131.imageshack.u
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(multitaskerGetCu
bzero(¶ms, 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", ¶ms );
params.gridWidth = 1;
params.gridY = 1;
params.orientationX = orient_center;
btnCE = windowNewButton( top, "CE", NULL, ¶ms );
params.gridX++;
btnC = windowNewButton( top, "C", NULL, ¶ms );
params.gridX++;
btnBS = windowNewButton( top, "BS", NULL, ¶ms );
params.gridX++;
btneq = windowNewButton( top, "=", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[7] = windowNewButton( top, "7", NULL, ¶ms );
params.gridX++;
btnnum[6] = windowNewButton( top, "8", NULL, ¶ms );
params.gridX++;
btnnum[5] = windowNewButton( top, "9", NULL, ¶ms );
params.gridX++;
btndiv = windowNewButton( top, "/", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[4] = windowNewButton( top, "4", NULL, ¶ms );
params.gridX++;
btnnum[5] = windowNewButton( top, "5", NULL, ¶ms );
params.gridX++;
btnnum[6] = windowNewButton( top, "6", NULL, ¶ms );
params.gridX++;
btnmul = windowNewButton( top, "*", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[1] = windowNewButton( top, "1", NULL, ¶ms );
params.gridX++;
btnnum[2] = windowNewButton( top, "2", NULL, ¶ms );
params.gridX++;
btnnum[3] = windowNewButton( top, "3", NULL, ¶ms );
params.gridX++;
btnsub = windowNewButton( top, "-", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[0] = windowNewButton( top, "0", NULL, ¶ms );
params.gridX++;
btnsgn = windowNewButton( top, "-+", NULL, ¶ms );
params.gridX++;
btndot = windowNewButton( top, ".", NULL, ¶ms );
params.gridX++;
btnadd = windowNewButton( top, "+", NULL, ¶ms );
windowRegisterEventHandler
windowGuiRun();
}
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(multitaskerGetCu
if( !top )
exit( ERR_NOTINITIALIZED );
bzero(¶ms, 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.useDefaultForegroun
params.useDefaultBackgroun
answerLabel = windowNewTextLabel( top, "0", ¶ms );
params.gridWidth = 1;
params.gridY = 1;
params.orientationX = orient_center;
btnCE = windowNewButton( top, "CE", NULL, ¶ms );
params.gridX++;
btnC = windowNewButton( top, "C", NULL, ¶ms );
params.gridX++;
btnBS = windowNewButton( top, "BS", NULL, ¶ms );
params.gridX++;
btneq = windowNewButton( top, "=", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[7] = windowNewButton( top, "7", NULL, ¶ms );
params.gridX++;
btnnum[6] = windowNewButton( top, "8", NULL, ¶ms );
params.gridX++;
btnnum[5] = windowNewButton( top, "9", NULL, ¶ms );
params.gridX++;
btndiv = windowNewButton( top, "/", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[4] = windowNewButton( top, "4", NULL, ¶ms );
params.gridX++;
btnnum[5] = windowNewButton( top, "5", NULL, ¶ms );
params.gridX++;
btnnum[6] = windowNewButton( top, "6", NULL, ¶ms );
params.gridX++;
btnmul = windowNewButton( top, "*", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[1] = windowNewButton( top, "1", NULL, ¶ms );
params.gridX++;
btnnum[2] = windowNewButton( top, "2", NULL, ¶ms );
params.gridX++;
btnnum[3] = windowNewButton( top, "3", NULL, ¶ms );
params.gridX++;
btnsub = windowNewButton( top, "-", NULL, ¶ms );
params.gridY++;
params.gridX = 0;
btnnum[0] = windowNewButton( top, "0", NULL, ¶ms );
params.gridX++;
btnsgn = windowNewButton( top, "-+", NULL, ¶ms );
params.gridX++;
btndot = windowNewButton( top, ".", NULL, ¶ms );
params.gridX++;
btnadd = windowNewButton( top, "+", NULL, ¶ms );
windowRegisterEventHandler
windowGuiRun();
return 1;
}
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?
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);
}
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));
}
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
va_end(list);
// Return the number of characters we wrote to the string
return (outputLen);
}
Business Accounts
Answer for Membership
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/tut orial/