c|מבקש הסבר על קוד - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

c|מבקש הסבר על קוד


eliom

Recommended Posts

הקוד הזה לא כל כך

מובן לי.ומזה אומר הוראת ה-* Define

ו-const .

אם אפשר הסבר על השורות בקוד.

כדי שזה יחדד לי את ההבנה.

תודה.

[CODE /* Demonstartes variables and constants */
#include <stdio.h>
/* Define a Constant to convert from punds to grams */
<M>#define<M> GRAMS_PER_POUND 454
/* Define a constant for the start of the next century */
const int NEXT_CENTURY = 2000;
/* Define the needed variables */
int weight_in_grams, weight_in_pounds;
int year_of_birth, int age_in_2000;

main()
{
/* Input data from user */

printf("Enter your Weight in pounds: ");
scanf("%d", &weight_in_pounds);
printf("Enter your year of birth: ");
scanf("%d", &year_of_birth);

/* Perform conversions */

weight_in_grams = weight_in_pounds *
GRAMS_PER_POUNDS;
age_in_2000 = NEXT_CENTURY - year_of_birth;

/* Display results on the screen */

printf("\nYour weoght in grams = %d", weight_in_grams);
printf("\nIn 2000 you will be %d years old", age_in_2000);
}]

קישור לתוכן
שתף באתרים אחרים

הפקודה Define אומרת לקומפיילר להחליף מילה אחת באחרת לדוגמה:

#define zero 0

הפקודה אומרת שכל פעם שהקומפיילר נתקל במילה zero, הוא יחליף אותה ב-0 (הרי zero זו מילה, אין לה ייצוג בשפת c, אבל ל-0 יש משמעות).

const זה פשוט קבוע, זו צורת כתיב (אם תנסה במהלך התוכנית לשנות את המשתנה הזה, תקבל שגיאת קומפילציה כי לא היית אמור לשנות אותו וכנראה עשית טעות), בעיקרון יכולת להגדיר אותה כמשתנה גלובאלי int רגיל, ההבדל הוא ש-const לא אמור להשתנות.

בקוד שלך 454, זה מספר שאין סיכוי שישתנה... מדובר בכמות הגרמים בפאונד.

הוראת * אומרת שזה מצביע למשתנה כלשהו.

קישור לתוכן
שתף באתרים אחרים

תנסה לתקן אותו שיופיע משמאל לימין ..

- - - תגובה אוחדה: - - -

ותבדיל בין הערות לבין הקוד עצמו ..

איך עושים משמאל לימין?

ומה זאת תבדיל בין הערות לקוד עצמו?

קישור לתוכן
שתף באתרים אחרים

יש לי שגיאת קופילציה בקוד הזה.

מהי השגיאה?.

error C2014: preprocessor command must start as first nonwhite space

/* Demonstartes variables and constants */      #include <stdio.h>
/* Define a Constant to convert from punds to grams */
<M>#define<M> GRAMS_PER_POUND 454
/* Define a constant for the start of the next century */
const int NEXT_CENTURY = 2000;
/* Define the needed variables */
int weight_in_grams, weight_in_pounds;
int year_of_birth, int age_in_2000;


int main()
{
/* Input data from user */


printf("Enter your Weight in pounds: ");
scanf("%d", &weight_in_pounds);
printf("Enter your year of birth: ");
scanf("%d", &year_of_birth);


/* Perform conversions */


weight_in_grams = weight_in_pounds *
GRAMS_PER_POUNDS;
age_in_2000 = NEXT_CENTURY - year_of_birth;


/* Display results on the screen */


printf("\nYour weoght in grams = %d", weight_in_grams);
printf("\nIn 2000 you will be %d years old", age_in_2000);
}

- - - תגובה אוחדה: - - -

קישור לתוכן
שתף באתרים אחרים

סדרתי קצת את הקוד:


#include <stdio.h>
#define GRAMS_PER_POUNDS 454
const int NEXT_CENTURY = 2000;
int weight_in_grams, weight_in_pounds;
int year_of_birth, age_in_2000;




int main()
{
/* Input data from user */




printf("Enter your Weight in pounds: ");
scanf_s("%d", &weight_in_pounds);
printf("Enter your year of birth: ");
scanf_s("%d", &year_of_birth);




/* Perform conversions */




weight_in_grams = weight_in_pounds *
GRAMS_PER_POUNDS;
age_in_2000 = NEXT_CENTURY - year_of_birth;




/* Display results on the screen */




printf("\nYour weoght in grams = %d", weight_in_grams);
printf("\nIn 2000 you will be %d years old", age_in_2000);
return 0;
}

*אם פונקציית main מחזירה משהו אז תרשום ביטוי החזרה בסוף

*הייתה אי תאימות בין שם הגדרת המקרו לבין שם המקרו שניכתב בקוד

*הגדרת את השורה הזאת לא נכון :


int year_of_birth, int age_in_2000;

תוקן ל:


int year_of_birth, age_in_2000;

במידה ואתה לא משתמש בVS תשאיר את scanf במקום scanf_s(משום מה לא נתן לי לקמפל ולהריץ בלי לשנות)

*תקנו אותי במידת הצורך,אני לא מתכנת בC*

קישור לתוכן
שתף באתרים אחרים

בסדר תיקנתי את הקוד.

לפי מה שאני יודע התוכנית ממירה את המשקל שלי בקילוגרם לגרמים.

אני הכנסתי 100 ק"ג וגם בגרמים יצא לי 100 איך זה יכול להיות.?

ומזה אומר השורה הזאת.

#define
GRAMS_PER_POUND (454)

בקוד הזה.

/*Credits to EmuleDanielFXP */
/* Demonstartes variables and constants */
#include<stdio.h>
/* Define a Constant to convert from punds to grams */
#define GRAMS_PER_POUND (454)

/* Define a constant for the start of the next century */

const int NEXT_CENTURY = 2000;

/* Define the needed variables */
int weight_in_grams, weight_in_pounds;
int year_of_birth, age_in_2000;

int main()
{

/* Input data from user */
printf("Enter your Weight in pounds: ");
scanf("%d", &weight_in_pounds);
printf("Enter your year of birth: ");
scanf("%d", &year_of_birth);

/* Perform conversions */
weight_in_grams = weight_in_pounds;
age_in_2000 = NEXT_CENTURY - year_of_birth;

/* Display results on the screen */
printf("\nYour weoght in grams = %d", weight_in_grams);
printf("\nIn 2000 you will be %d years old", age_in_2000);

return 0;
}


קישור לתוכן
שתף באתרים אחרים

שוב, define# מגדיר לקומפיילר, לפני שהוא עושה קומפילציה לקוד, לעבור על כל הטקסט ולעשות replace לכל מופע של GRAMS_PER_POUND במקרה שלך למספר 454. תחשוב שאתה יכול לעשות את זה ידנית - עם שימוש ב-define הרבה יותר קריא לדעת שזה GRAMS_PER_POUND ולא איזה מספר "קסם" כמו 454 שמישהו אחר לא ידע מה המספר הזה בכלל מייצג.

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...