עבור לתוכן

תוכנה פשוטה בשפת C - תרגילי חשבון

Featured Replies

פורסם

קיבלתי עבודה מהמורה שלי למחשבים לעשות תוכנה שתקלוט רמת קושי (מספר הספרות) ופעולה חשבונית (כפל/חיבור/חיסור) ותחשב ניקוד לפי רמת הקושי.

זו התכנית שכתבתי:


#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
void main()
{
int num1, num2, count=0, sum=0, dif, plus;
long sol, right;
char peula, finish;

printf("Do you wish to continue? Y / N ");
scanf(" %c", &finish);
while(finish!='Y' && finish!='N')
{
printf("ERORR! Press Again: ");
scanf(" %c", &finish);
}

srand(time(NULL));
while(finish=='Y')
{
printf("\nWhat peula do you want to use?\n");
printf("Addition: A\n");
printf("Subtraction: B\n");
printf("Multiplication: C\n");
scanf(" %c", &peula);
while(peula != 'A' && peula != 'B' && peula != 'C')
{
printf("\nERROR! Press Again: ");
scanf(" %c", &peula);
}

printf("\nWhat difficulty level?\n1 Digit: 1\n2 Digits: 2\n3 Digits: 3\n");
scanf("%d", &dif);
while(dif<1 || dif >3)
{
printf("\nERROR! Press Again: ");
scanf("%d", &dif);
}

switch(dif)
{
case 1: {
num1 = rand() % 10;
num2 = rand() % 10;
plus = 5;
}
break;
case 2: {
num1 = rand() % 90 + 10;
num2 = rand() % 90 + 10;
plus = 10;
}
break;
case 3: {
num1 = rand() % 900 + 100;
num2 = rand() % 900 + 100;
plus = 20;
}
break;
}

switch(peula)
{
case 'A': {
right = num1 + num2;
printf("%d + %d = ", num1, num2);
scanf("%ld", &sol);
if(right == sol)
{
printf("Correct!\n");
count++;
sum+=plus;
}
else
{ printf("Wrong!\n");
printf("The correct answer is %ld\n", right); }
}
break;
case 'B': {

right = num1 - num2;
printf("%d - %d = ", num1, num2);
scanf("%ld", &sol);
if(right == sol)
{
printf("Correct!\n");
count++;
sum+=plus;
}
else
{ printf("Wrong!\n");
printf("The correct answer is %ld\n", right); }
}
break;
case 'C': {
right = num1 * num2;
printf("%d * %d = ", num1, num2);
scanf("%ld", &sol);
if(right == sol)
{
printf("Correct!\n");
count++;
sum+=plus;
}
else
{ printf("Wrong!\n");
printf("The correct answer is %ld\n", right); }
}
break;
}

printf("\nDo you wish to continue? Y / N ");
scanf(" %c", &finish);
while(finish!='Y' && finish!='N')
{
printf("ERORR! Press Again: ");
scanf(" %c", &finish);
}
}

printf("\nNumber of correct answers: %d\n", count);
printf("Score: %d\n", sum);
getch();
}

התוכנה פועלת, אבל הבעיה זה שבקטע של קליטת הפעולה החשבונית, זה תמיד כותב ERROR ונכנס לתוך הלולאה. הוא בעצם לא נכנס לקליטת נתונים הראשונית.

אני משתמש ב-dev-cpp אבל בדקתי את זה גם ב- borland cpp 4.5 והבעיה התקיימה גם שם.

בתודה מראש.

פורסם

הבעיה היא שה-scanf של הפעולה מזהה את ה-Enter שלחצת אחרי שהכנסת את המספר בההתחלה

כל דיבגר היה מוצא לך את זה בשניה

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

פורסם

#include <stdio.h>

#include <conio.h>

#include <time.h>

#include <stdlib.h>

#include <iostream>

void main()

{

int num1, num2, count=0, sum=0, finish, dif, sol, plus;

char peula;

printf("Do you wish to continue? yes=1 no=0 ");

scanf("%d", &finish);

srand(time(NULL));

while(finish)

{

printf("What peula do you want to use?\nAddition: A\nSubtraction: B\nMultiplication: C\n");

// scanf("%c", &peula);

std::cin >> peula;

while((peula != 'A') & (peula != 'B') & (peula != 'C'))

{

printf("\nERROR! Press Again: ");

scanf("%c", &peula);

}

printf("What difficulty level?\n1 Digit: 1\n2 Digits: 2\n3 Digits: 3\n");

scanf("%d", &dif);

while(dif<1 || dif >3)

{

printf("\nERROR! Press Again: ");

scanf("%d", &dif);

}

switch(dif)

{

case 1: {

num1 = rand() % 10;

num2 = rand() % 10;

plus = 5;

}

break;

case 2: {

num1 = rand() % 90 + 10;

num2 = rand() % 90 + 10;

plus = 10;

}

break;

case 3: {

num1 = rand() % 900 + 100;

num2 = rand() % 900 + 100;

plus = 20;

}

break;

}

switch(peula)

{

case 'A': {

printf("%d + %d = ", num1, num2);

scanf("%d", &sol);

if(num1 + num2 == sol)

{

printf("Correct!\n");

count++;

sum+=plus;

}

else

printf("Wrong!\n");

}

break;

case 'B': {

printf("%d - %d = ", num1, num2);

scanf("%d", &sol);

if(num1 - num2 == sol)

{

printf("Correct!\n");

count++;

sum+=plus;

}

else

printf("Wrong!\n");

}

break;

case 'C': {

printf("%d * %d = ", num1, num2);

scanf("%d", &sol);

if(num1 * num2 == sol)

{

printf("Correct!\n");

count++;

sum+=plus;

}

else

printf("Wrong\n");

}

break;

}

printf("Do you wish to continue? yes=1 no=0 ");

scanf("%d", &finish);

}

printf("Number of correct answers: %d\n", count);

printf("Score: %d\n", sum);

getch();

}

תראה, ככה זה עובד, בגלל זה אני שונא את printf,scanf...

מטי.

פורסם

שונא שונא אבל cin שייך ל-C++ ולא עובד ב-C

חבל

פורסם

יש אנשים שעדיין עובדים על השפה העתיקה הזאת ?

טוב,,, אז ככה, יש את הדרך המסובכת והפשוטה... הפשוטה היא

srand(time(NULL));

while(finish)

{

printf("What peula do you want to use?\nAddition: A\nSubtraction: B\nMultiplication: C\n");

scanf("%c", &peula);

scanf("%c", &peula);

כן אני יודע, אותה שורה פעמיים.

מטי.

פורסם

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

בכל מקרה את מה שכתבת הצעתי למעלה, אבל יש דרים הרבה יותר טובות להתגבר על זה.

פורסם

תעשה קליטה של מחרוזת (%s)  ותשווה עם strcmp ל-"A" או שתנסה עם

getc,gets  ותנקה לפני ואחרי קליטה את הקלט ע"י  (fflush(stdin .

פורסם

יודע מה

הכי פשוט הרבה אנשים לא יודעים

מה שצריך לעשות זה פשוט רווח לפני ה"אחוז סי" בסקן אף

scanf(" %c", &peula);

ארכיון

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

דיונים חדשים