int ggt (int a, int b) { if (b==0) return a; else return ggt(b, a%b); }
unsigned long ggt(unsigned long a, unsigned long b){ if(b>0){ ggt(b, a%b); } if(b<=0){ return a; } }
int main(void) { unsigned long a, b; printf("ggt = größter gemeinsamer Teiler(mit 0 beenden)\n"); printf("Zahl> "); scanf("%lu", &a); printf("Zahl> "); scanf("%lu", &b); a=ggt(a, b); while(1) { printf("Zahl> "); scanf("%lu", &b); if(b==0) break; a=ggt(a, b); } printf("-------->ggt = %lu\n", a); return EXIT_SUCCESS; }
/* kuerzen() */ void KBruch::kuerzen () { // falls kürzbar, Zähler und Nenner durch GGT teilen if (kuerzbar) { int teiler = ggt(); zaehler /= teiler; nenner /= teiler; kuerzbar = false; // damit nicht mehr kürzbar } }
/* Operator *= * - zum Überschreiben mit Typen der Basisklasse neu implementiert */ const KBruch& KBruch::operator*= (const Bruch& b) { /* Implementierung der Basisklasse aufrufen * - auf nichtöffentliche Komponenten von b besteht kein Zugriff */ Bruch::operator*= (b); // weiterhin gekürzt ? if (!kuerzbar) { kuerzbar = (ggt() > 1); } return *this; }
int main(int argc, char *argv[]) { int sockfd; int serverPort; int new_fd; struct sockaddr_in serv_addr, cli_addr; int clilen; printf("TCP server example\n\n"); if (argc != 2) { fprintf(stderr,"Usage: tcpServe serverPort \n"); exit(1); } serverPort = atoi(argv[1]); printf("port = %i\n", serverPort); memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(serverPort); sockfd = socket(AF_INET, SOCK_STREAM, 0); if(bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) { printf("could not bind\n"); } listen(sockfd, 10); clilen = sizeof cli_addr; while(1) { new_fd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen); unsigned char buffer[4]; int size = read(new_fd, buffer, 4); if(size > 0) { unsigned int a,b; printf("received: %x %x %x %x\n", buffer[0],buffer[1],buffer[2],buffer[3]); unpackData(buffer, &a, &b); printf("ggt of %i %i is %i\n", a ,b, ggt(a,b)); } } close(sockfd); return 0; }
int main(int argc, char *argv[]) { long int n, m; if (argc == 3) { if (sscanf(argv[1], "%ld", &n)) { if (sscanf(argv[2], "%ld", &m)) { fprintf(stdout, "ggt von %ld and %ld ist %ld \n", n, m, ggt(n, m)); return EXIT_SUCCESS; } } } fprintf(stderr, "Geben sie bitte zwei Zahlen ein...\n"); return EXIT_FAILURE; }
int main(int argc, char *argv[]) { struct sockaddr_storage their_addr; socklen_t addr_size; int sockfd; struct addrinfo hints, *res; unsigned char buffer[4]; unsigned int a=0; unsigned int b=0; int ergebnis; printf("TCP/UDP server for TechGI 4\n\n"); if (argc != 2) { fprintf(stderr,"Usage: server udport \n"); exit(1); } // get serverport const char* udpPort =argv[1]; //load address memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_PASSIVE; getaddrinfo(NULL, udpPort, &hints, &res); printf("udpPort: %s\n",udpPort); //create socket sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //bind socket bind(sockfd, res->ai_addr, res->ai_addrlen); addr_size = sizeof their_addr; //recieve packet and test int recieve = recvfrom(sockfd, &buffer, 20, 0, (struct sockaddr *)&their_addr, &addr_size); printf("Recieved %d Byte\n", recieve); //unpack buffer unpackData(buffer, &a, &b); //calculate result and return ergebnis = ggt(a, b); printf("Zahl 1: %d Zahl 2: %d GGT: %d \n", a, b, ergebnis); }
unsigned long ggt(unsigned long a, unsigned long b) { if(b == 0) return a; return ggt(b, a % b); }
int ggt (int i){ if (i <= 0) return 0; if (g[i])return g[i]; return g[i] = ggt(i - 1) * 2 + i + i - 2; }
int get (int n){ if (n == 1) return 0; int i = log(n+.0)/log(2.), k = n - (1 << i); return i - 1 + ggt(i - 1) + ( k ? get(k) + i - 1 : 0); }
/* scanFrom() */ void KBruch::scanFrom (std::istream& strm) { Bruch::scanFrom (strm); // scanFrom() der Basisklasse aufrufen kuerzbar = (ggt() > 1); // Kürzbarkeit testen }
int main(void){ printf("%lu", ggt(12, 14)); return 0; }