Guten Tag liebes Forum,
ich möchte gerade folgende Aufgabe lösen:
Your job is to write a program that shows, in human-readable form (see below for specifics), how much memory a set of variables of a certain type will use. Your program should read a character that identifies the data type ('i' for int, 's' for short, 'c' for char, 'd' for double). Your program should then calculate the amount of memory required to store the given variables.
Your program needs to be written in such a way that it would also perform correctly on other computers. In other words, rather than hard-coding specific sizes for the different variable types, your program needs to use the "sizeof()" function to determine how much memory an individual variable of a given type needs.
Finally, you need to output the amount of space required by your variables to the screen. You need to make sure you provide this output in a form that is easy to read for humans. The following examples illustrate what this means:
Examples
If the user input was:
i 36794
then the amount of space needed (if we assume that an integer uses 4 bytes in memory) would be 4*36794 = 147176 bytes. This corresponds to 147 kilobytes and 176 bytes, so the output should be:
147 KB and 176 B
Aber wenn ich meine Lösung kompiliere, dann bekomme ich komische Resultate, d.h. hinten hängt dort dann einfach noch einmal eine Zahl dran.
Ich weiß nicht woher die kommt:
i 36794
147 KB and 176 B 102
Vielleicht sieht hier jemand woher der Fehler kommt?
#include <stdio.h>
int SizeOfCall(char type, int input);
int SizeForHumans(int num);
int main(void) {
int input;
char type;
scanf("%c %d",&type, &input);
printf("%d",SizeOfCall(type, input));
return 0;
}
int size;
int SizeOfCall(char type, int input){
if (type == 'i'){
size = input*sizeof(int);
printf("%d", SizeForHumans(size));
}else if (type == 'c'){
size = input*sizeof(char);
printf("%d", SizeForHumans(size));
}else if (type == 'd'){
size = input*sizeof(double);
printf("%d", SizeForHumans(size));
}else{
printf("Invalid Type");
}
}
int size;
int GB, MB, KB, B = 0;
int SizeForHumans(int size){
if (size<1000){
B = size;
printf("and %d B\n",B);
}else if (size<1000000){
KB = size/1000;
printf("%d KB ",KB);
return SizeForHumans(size%1000);
}else if (size<1000000000){
MB = size/1000000;
printf("%d MB ",MB);
return SizeForHumans(size%1000000);
}else if (size<1000000000000){
GB = size/1000000000;
printf("%d GB ",KB);
return SizeForHumans(size%1000000000);
}
}
Frage
Gast Schnuggenfuggler
Your job is to write a program that shows, in human-readable form (see below for specifics), how much memory a set of variables of a certain type will use. Your program should read a character that identifies the data type ('i' for int, 's' for short, 'c' for char, 'd' for double). Your program should then calculate the amount of memory required to store the given variables.
Examples
If the user input was:
then the amount of space needed (if we assume that an integer uses 4 bytes in memory) would be 4*36794 = 147176 bytes. This corresponds to 147 kilobytes and 176 bytes, so the output should be:
10 Antworten auf diese Frage
Empfohlene Beiträge
Erstelle ein Benutzerkonto oder melde Dich an, um zu kommentieren
Du musst ein Benutzerkonto haben, um einen Kommentar verfassen zu können
Benutzerkonto erstellen
Neues Benutzerkonto für unsere Community erstellen. Es ist einfach!
Neues Benutzerkonto erstellenAnmelden
Du hast bereits ein Benutzerkonto? Melde Dich hier an.
Jetzt anmelden