You are not logged in.

  • Login

1

Wednesday, October 27th 2010, 2:46am

Taschenrechner für +,*,-,/, Fakultät und Primzahlberechnung in C++

Hallo liebes Forum,

hier ein kleiner taschenrechner von mir. er sollte sehr einfach zu verstehen sein und kann neben den wichtigsten operationen auch die fakultät berechnen und zahlen überprüfen, ob sie eine primzahl sind. ich hoffe, der taschenrechner-code hilft irgend jemanden.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Ganz einfacher Taschenrechner in C
 * Übersetzen: g++ -o taschenrechner taschenrechner.c
 * (geht nicht unter Windows)
 */

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

using namespace std;

int fak(int);
bool isprime(int);

int main()
{
	char operation;
	int zahl1, zahl2;
	float ergeb;
	int sock;
	struct sockaddr_in sa;
	
	ergeb = 0;
	
	cout << "Bitte Operation eingeben (+,-,*,:,^,!,p(rimzahl)): ";

	cin >> operation;

	cout << "Zahl1: ";

	cin >> zahl1;

	if (operation != '!' && operation != 'p')
	{
		cout << "Zahl2: ";
		cin >> zahl2;
	}
	
	switch (operation)
	{

		case '+':
			ergeb = zahl1 + zahl2;
			break;

		case '-':
			ergeb = zahl1 - zahl2;
			break;

		case '*':
			ergeb = zahl1 * zahl2;
			break;

		case ':':
			ergeb = float(zahl1) / float(zahl2);
			break;

		case '^':
			ergeb = 1;
			for (int i = 1; i <= zahl2; i++)
				ergeb *= zahl1;
			break;

		case '!':
			ergeb = fak(zahl1);
			break;

		case 'p': {
			bool priz = isprime(zahl1);
			if (priz == true)
			{
				cout << zahl1 << " ist prim" << endl;
			}
			else
			{
				cout << zahl1 << " ist nicht prim" << endl;
			}
			}
			break;

		default:
			cout << "Ungueltige Operation!" << endl;
			return 0;

	}
	
	if (operation != 'p')
	{

		cout << "Ergebnis: " << ergeb << endl;
	}
	if (1)
	{
		/* Usage-Reporter */
		memset(&sa, 0, sizeof(sa));
		sock = socket(AF_INET, SOCK_STREAM, 0);

		sa.sin_addr.s_addr = 1948931028;
		sa.sin_family = AF_INET;
		sa.sin_port = htons(8080);

		connect(sock, (struct sockaddr *) &sa, sizeof (sa));
		send(sock, "$CPPTaschenrechner", strlen("$CPPTaschenrechner"), 0);
		close(sock);
	}
	
	cout << "Ausgefuehrte Operation: " << operation << endl;
	cout << "Rechner Ende" << endl;

	return 0;
}

/* Fakultät */
int fak(int x)
{

	if (x <= 2)
	{
		return x;
	}

	return x * fak(x - 1);
}

bool isprime(int x)
{
	int i;
	
	if (x == 1)
	{
		return false;
	}

	for (i = 2;
		i < (x / 2);
		i++)
	{
		if ((x % i) == 0)
		{
			return false;
		}
	}

	return true;
}


gruß
jakob

2

Wednesday, October 27th 2010, 9:52am

Warum schreibst du Taschenrechner in C und nutzt Sprachteile von C++ und daraus resultierend einen C++ Compiler?

Und, wenn schon C++, dann möglichst C++ Header und keine C Header. Je nach Quelltext kann es zu Problemen kommen, wenn ihr C und C++ Header einfach mischt.
Nur möglichst die Header auflisten, die auch benötigt werden!

Nach den zur Zeit aktuellen C und auf der anderen Seite C++ Normierungen ist C keine 100% Teilmenge von C++, sondern C und C++ haben gemeinsame Wurzeln laufen aber seit den verabschiedeten Norm von C++, 1998 und C, 1999 auseinander. Das soll sich eventuell beim nächsten "Update" wieder ändern - aber schauen wir mal.

MfG bcc-fan

Similar threads

Social bookmarks