CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

+ - * / two ints and output the result?? But how???

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
j_o_k_e_r



Joined: 10 Dec 2007
Posts: 3

View user's profile Send private message

+ - * / two ints and output the result?? But how???
PostPosted: Mon Dec 10, 2007 5:26 am     Reply with quote

Hi,

I'm quite new to the compiler and that is why if possible, I need a very quick and explanatory response to solve an urgent, but of course for some gurus a very silly problem. I need to program a 16F877A to take two numbers from the computer and make arithmetic operations (+ * / -) as if it is a calculator and output the result to the computer. More detaily, I will give two 1 byte (or 2 byte) integers and an operator (like: 3+4) from the keyboard and I want to get the result. I tried to use getc() and putc() for this purpose, and also I tried to assign the first and the third element as byte, char, int, etc. but nevertheless I couldn't get the result of such a silly operation. For example somehow I get 'e' for the result of 2+3 (decimal byte value of 2 is 50 and 3 is 51 and e is 101, definetly correct 50+51=101 but I want just 5 nothing more!) What kind of commands I should use to take expression (getc() or something else), what types of variables should I assign them (int, byte, char or what) and how should I output the result (is it putc() or what it should be)??? Please help me!

Thanks a lot.
Kind regards.
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Mon Dec 10, 2007 6:41 am     Reply with quote

first thing I have to say is that int, byte and char are the same in CCS. They are all unsigned 8 bits variables.

To answer your question of how to do it:
You could read in the integers as you are doing now, and then substract '0' of those integers.

something like this:
Code:
int8 integer1, integer2, operator, result = 0;
integer1= getc() - '0';
operator = getc();
integer2 = getc() - '0';

switch (operator)
{
case '+':
   result = integer1 + integer2;
break;
case '-':
   result = integer1 - integer2;
break;
case '*':
   result = integer1 * integer2;
break;
case '/':
   result = integer1 / integer2;
break;
}
printf("%u", result);


I have not tested the above code, so it might contain errors.

I hope this helps
j_o_k_e_r



Joined: 10 Dec 2007
Posts: 3

View user's profile Send private message

PostPosted: Mon Dec 10, 2007 7:54 am     Reply with quote

Foppie, thanks a lot for your interest, but the code doesn't work. Could you explain what might be the reason or what else should I do? (Meanwhile, what is the meaning of subtracting '0')
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Mon Dec 10, 2007 8:13 am     Reply with quote

Substracting '0' is the same as substracting 48. So if a '2' is received as character, 50 is received as integer and then when '0' is substracted, you will get the integer value (and not the ascii value) in your variable.

I don't know why the code isn't working. Can you maybe describe what the error is that occures?
I know that the code will give results that you might think of as errors when overflows occur. You must remember that an int8 only can hold values between 0 and 255.

I hope this helps.
j_o_k_e_r



Joined: 10 Dec 2007
Posts: 3

View user's profile Send private message

PostPosted: Mon Dec 10, 2007 3:35 pm     Reply with quote

I could finally find some way to handle the problem. Thanks foppie for your help. Kind regards.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group