lady_krizzie
- Aug 2, 2012
- 30
- Joined
- Aug 2, 2012
- Messages
- 30
Hi... I'm trying to make a calculator using Hyperterminal, AT90USB647, RS232 and Atmel Studio 6. I want it to display the decimal places (i.e. 5.1, 2.332 , and so on..). ... At first I make all answer as float, so all output answer became '?'. So I separate the '/' operation.I put float and double but its output answer is still '?',and other arithmetic operations functions correctly. How can I make the calculation right?
Code:
#include <avr/io.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdlib.h>
#define F_CPU 8000000UL
/* UART library included here*/
char string [16];
char num1[16] = {};
char num2[16] = {};
int number1 = 0;
int number2 = 0;
char operand;
int answer = 0;
float fans = 0;
char foo;
int main(void)
{
//hw_lcd_init();
USART_Init(51);
stdout = &uart_output;
stdin = &uart_input;
int i = 0, j=0, k=0;
int c;
do
{
printf("\n\n\tHello\n\n\n");
printf("Enter operands: \n");
scanf("%s", string);
//lcd_gotoxy(0,0);
//lcd_puts(foo);
printf("Equation: %s\n", string);
{
for (i=0; i<strlen(string); i++)
{
if (isdigit(string[i]))
num1[i] = string[i];
else
break;
}
number1 = atoi(num1);
operand = string[i];
i++;
k = 0;
for (j=i; j<strlen(string); j++)
{
if (isdigit(string[j]))
num2[k] = string[j];
else
break;
k++;
}
number2 = atoi(num2);
}
switch (operand)
{
case '+':
answer = number1+number2;
break;
case '-':
answer = number1-number2;
break;
case '*':
answer = number1*number2;
break;
case '/':
if (number2==0)
{
answer= number2;
}
else
fans = (float)number1/ (float)number2;
break;
}
printf("number1: %d\n", number1);
printf("number2: %d\n", number2);
if (operand == '/')
printf ("\nAnswer is: %f\n", fans);
else
printf("\nAnswer is: %d\n", answer);
printf("\nNew Operand, enter an expression twice");
scanf("%d" , &c);
}
while( (getchar()!='n') && (getchar()!='N') );
return 0;
}