ALBUL
May 6th, 2012, 04:13 PM
This is my calculator:
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include "myconio.h"
using namespace std;
main()
{
float x, y, c;
int z,i;
printf("CEL MAI TARE CALCULATOR!!!!\n");
start:
printf("Primul numar: ");
if(scanf("%f", &x)!=1){
printf("Nu ati introdus un numar sau o cifra!");
getch();
fflush(stdin);
clrscr();
goto start;
}
printf("Al doilea numar: ");
if(scanf("%f", &y)!=1){
printf("Nu ati introdus un numar sau o cifra!");
getch();
clrscr();
fflush(stdin);
goto start;
}
printf("Apasati: 1.Adunare 2.Scadere 3.Inmultire 4.Impartire: ");
if(scanf("%d", &z)!=1){
printf("Nu ati introdus o cifra!");
getch();
clrscr();
fflush(stdin);
goto start;
}
if (z == 1)
{
c = x+y;
printf("Rezultatul este: %.2f\n", c);
getch();
clrscr();
goto fin;
}
if (z == 2)
{
c = x-y;
printf("Rezultatul este: %.2f\n", c);
getch();
clrscr();
goto fin;
}
if (z == 3)
{
c = x*y;
printf("Rezultatul este: %.2f\n", c);
getch();
clrscr();
goto fin;
}
if (z == 4)
{
if (y == 0) {
printf("Nu se poate imparti la zero!!!!!!!");
getch();
clrscr();
goto start;
} else {
c = x/y;
printf("Rezultatul este: %.2f\n", c);
getch();
clrscr();
goto fin;
}
}
if (z != 1 || 2 || 3 || 4)
{
printf ("Ati apasat un buton gresit... Va vom duce inapoi la inceput!");
getch();
clrscr();
fflush(stdin);
goto start;
}
fin:
printf ("Apasati: 1.Continuare 2.Iesire: ");
if(scanf("%d", &i)!=1){
printf("Nu ati apasat pe o cifra! Programul se va inchide!");
getch();
goto end;
}
if (i == 1)
{
clrscr();
goto start;
}
if (i == 2)
{
puts ("La revedere!");
getch();
goto end;
}
if (i != 1 || 2)
{
printf ("Raspunsul aleatoriu se va lua drept iesire. La revedere!");
getch();
}
end:
;}
And myconio.h
#include <termios.h>
#include <unistd.h>
int getch(void)
{
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
int clrscr(void)
{
printf ("\x1B[2J");
printf ("\x1B[0;0H");
}
I use a modified getch() from myconio.h. It does well until (the code under) where it must add x to y if was selected first option (adding). At this part of code it doesn`t want to pause and goes directly at fin.
if (z == 1)
{
c = x+y;
printf("Rezultatul este: %.2f\n", c);
getch();
clrscr();
goto fin;
}
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include "myconio.h"
using namespace std;
main()
{
float x, y, c;
int z,i;
printf("CEL MAI TARE CALCULATOR!!!!\n");
start:
printf("Primul numar: ");
if(scanf("%f", &x)!=1){
printf("Nu ati introdus un numar sau o cifra!");
getch();
fflush(stdin);
clrscr();
goto start;
}
printf("Al doilea numar: ");
if(scanf("%f", &y)!=1){
printf("Nu ati introdus un numar sau o cifra!");
getch();
clrscr();
fflush(stdin);
goto start;
}
printf("Apasati: 1.Adunare 2.Scadere 3.Inmultire 4.Impartire: ");
if(scanf("%d", &z)!=1){
printf("Nu ati introdus o cifra!");
getch();
clrscr();
fflush(stdin);
goto start;
}
if (z == 1)
{
c = x+y;
printf("Rezultatul este: %.2f\n", c);
getch();
clrscr();
goto fin;
}
if (z == 2)
{
c = x-y;
printf("Rezultatul este: %.2f\n", c);
getch();
clrscr();
goto fin;
}
if (z == 3)
{
c = x*y;
printf("Rezultatul este: %.2f\n", c);
getch();
clrscr();
goto fin;
}
if (z == 4)
{
if (y == 0) {
printf("Nu se poate imparti la zero!!!!!!!");
getch();
clrscr();
goto start;
} else {
c = x/y;
printf("Rezultatul este: %.2f\n", c);
getch();
clrscr();
goto fin;
}
}
if (z != 1 || 2 || 3 || 4)
{
printf ("Ati apasat un buton gresit... Va vom duce inapoi la inceput!");
getch();
clrscr();
fflush(stdin);
goto start;
}
fin:
printf ("Apasati: 1.Continuare 2.Iesire: ");
if(scanf("%d", &i)!=1){
printf("Nu ati apasat pe o cifra! Programul se va inchide!");
getch();
goto end;
}
if (i == 1)
{
clrscr();
goto start;
}
if (i == 2)
{
puts ("La revedere!");
getch();
goto end;
}
if (i != 1 || 2)
{
printf ("Raspunsul aleatoriu se va lua drept iesire. La revedere!");
getch();
}
end:
;}
And myconio.h
#include <termios.h>
#include <unistd.h>
int getch(void)
{
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
int clrscr(void)
{
printf ("\x1B[2J");
printf ("\x1B[0;0H");
}
I use a modified getch() from myconio.h. It does well until (the code under) where it must add x to y if was selected first option (adding). At this part of code it doesn`t want to pause and goes directly at fin.
if (z == 1)
{
c = x+y;
printf("Rezultatul este: %.2f\n", c);
getch();
clrscr();
goto fin;
}