Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Zahlenausgabe in C

Empfohlene Antworten

Hallo, bin neu hier im Forum und hab ein Problem mit folgender Aufgabenstellung. Ich hoffe ihr könnt mir weiterhelfen.

In der Programmiersprache C können Zahlen auf verschiedene Arten ausgegeben

werden.

printf(" %d\n", n);

printf(" %o\n", n);

printf(" %x\n", n);

printf(" %f\n", x);

printf(" %e\n", x);

Welche Datentypen können damit ausgegeben werden.

a) Bei der ersten Ausgabe werde die Zahl 368 ausgegeben. Was erhält man bei

den restlichen Ausgaben.

B) Bestimmen Sie die restlichen Ausgaben, wenn die zweite 761 ist.

c) Bestimmen Sie die restlichen Ausgaben, wenn die dritte _9 ist.

d) Bestimmen Sie die restlichen Ausgaben, wenn die vierte 1010.361 ist.

e) Bestimmen Sie die restlichen Ausgaben, wenn die fünfte -1.345E-10 ist.

Hinweis: Bei den restlichen Ausgaben in den einzelnen Fällen sind nur diejenigen zu bestimmen,

die für den entsprechenden Datentyp zuständig sind.

Ich weiß zwar was %d,..... bedeutet, aber wie die Ausgaben aussehen leider nicht.

Hallo,

oder schau dir die Manual-Page zu printf() an. Dort sind alle Ausgabeformate erklärt. Wobei ich die Aufgabe etwas seltsam formuliert finde (d.h. du sollst beispielsweise %o761 in die anderen Formate "umrechnen").

Nic

hab ich schon teilweise...

zu a)

wenn bei %d 368 ausgegeben wird dann

%o 560

%x 23

%f 23

%e 0,023e²

B)

%d 497

%o 761

%x 2f9

%f ???

%e ???

c) %d 585

%o 1089

%x ff9

%f

%e

d) %d 1106

%o 2122

%x 4112

%f 1010.361

%e

e) %d

%o

%x

%f

%e -1.345E-10

Mfg

Matthias

hat noch jemand eine Idee, wie die anderen Ausgabewerte aussehen?

printf(" %d\n", n);

printf(" %o\n", n);

printf(" %x\n", n);

printf(" %f\n", x);

printf(" %e\n", x);

Hat das nen Grund, dass die ersten drei Variablen n und die letzten zwei x heißen?

Also irgendwie mag sscanf die Verbindung %f und double nicht...

a)

368

560

170

368.000000

3.680000e+002

B)

497

761

1f1

497.000000

4.970000e+002

c)

9

11

9

9.000000

9.000000e+000

d)

1010

1762

3f2

1010.361023

1.010361e+003

e)

0

0

0

-0.000000

-1.345000e-010


#include "stdafx.h"

#include "stdlib.h"


void getInt(const char* format, const char*source, double* number);

void getFloat(const char* format, const char*source, double* number);

void formatNumber(double number);


int main(int argc, char* argv[])

{

	double number = 0.0;


	printf("a)\n");

	getInt("%d", "368", &number);

	formatNumber(number);

	printf("b)\n");

	getInt("%o", "761", &number);

	formatNumber(number);

	printf("c)\n");

	getInt("%x", "9", &number);

	formatNumber(number);

	printf("d)\n");

	getFloat("%f", "1010.361", &number);

	formatNumber(number);

	printf("e)\n");

	getFloat("%e", "-1.345E-10", &number);

	formatNumber(number);


	return 0;

}


void getInt(const char* format, const char*source, double* number)

{

	int result;

	sscanf(source, format, &result);

	*number = result;

}


void getFloat(const char* format, const char*source, double* number)

{

	float result;

	sscanf(source, format, &result);

	*number = result;

}


void formatNumber(double number)

{

	printf(" %d\n", (int)number);

	printf(" %o\n", (int)number);

	printf(" %x\n", (int)number);

	printf(" %f\n", (float)number);

	printf(" %e\n", number);

}

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.