Boshlang'ich S mashqlari [sscanf - fgets - if else]

Men boshlang'ich S mashqlari bilan shug'ullanyapman, men birinchi qismni juda yaxshi bajarganman deb o'ylayman (hech qanday oqlangan emas); Agar bal kiritilgan bo'lsa, to'g'ri belgini chiqaradi. Lekin, albatta, jismoniy mashqlar oxirgi qismida muvaffaqiyat qozona olmaysiz (ortiqcha yoki minus qo'shing) va shuni tushunib etolmayman. line [1] nima uchun kutilganidek ishlamayotganini tushunmoqchiman.

Jismoniy mashqlar matni: Professor quyidagi jadval yordamida harflar sinflarini hosil qiladi:

0–60  -> F
61–70 -> D
71–80 -> C
81–90 -> B
91–100 -> A

Raqamlar sonini hisobga olgan holda, xatni chop eting. Endi, oldingi dasturni + yoki - natija qog'ozidan so'ng, ballning so'nggi raqamiga asoslanib o'zgartiring. Oxirgi raqam o'zgartirish

1–3 -> "–"
4–7 -> 
8–0 -> "+"

Masalan, 81 = B-, 94 = A va 68 = D +. Eslatma: F - faqat F. F + yoki F - yo'q.

Nima qildim:

    #include 

char line[20];              //prepare the input from keyboard
int score;
char plusminus;

int main() {
    printf("insert your score: ");     //ask for the score
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &score);

// check for conditions
    if (score <= 60) {
        printf("F");
    }
    if (score <= 70 && score >60) {
        printf("D");
    }
    if (score <= 80 && score >70) {
        printf("C");
    }
    if (score <= 90 && score >80) {
        printf("B");
    }
    if (score <= 100 && score >90) {
        printf("A");
    }

// plus and minus to the mark, but didn't succed :(
    plusminus = line[1];
    if (plusminus < "3") {
        printf("-");
    }
    if (plusminus > "8") {
        printf("+");
    }
}

Barchaga rahmat!

0
if (plusminus <"3") { derivativ ogohlantirishini yaratmaydi? Aks holda, barcha kompilyator ogohlantirishlarini yoqing - vaqtni tejaydi.
qo'shib qo'ydi muallif chux, manba
Ikkita tirnoq - "3" - 3 \ 0 o'z ichiga olgan mag'lubiyatga ega bo'ladi (bu \ 0 ) satr oxirida). Bitta tirnoq - '3' - belgi qiymatini xotiraga joylashtiradi - 51.
qo'shib qo'ydi muallif Attie, manba
Bu erda faqat umumiy taklif: kirish/chiqishdan alohida mantiq. Buni qanday qilish kerak? Misol uchun, "translateScoreToGrade", masalan, kirish sifatida tamsayıli va "struct grade" ning chiqishi (char-navi, char-modifikator) kabi yangi fojialar yarating. Bu juda ham oqlangan, va sinovdan o'tishi juda toza, va sizning professoringiz sizga yuqori ball beradi :)
qo'shib qo'ydi muallif charlie_pl, manba
Bundan tashqari, bu ifllarni almashtirish uchun -> (100 - pupp)/10, hol 0 -> case 1> B ... default-> F
qo'shib qo'ydi muallif charlie_pl, manba

7 javoblar

Agar sizda ballni int sifatida olgan bo'lsangiz, hisobning qolgan qismini 10ga bo'linib olish uchun modul operatorini % dan foydalanishingiz mumkin. oxirgi raqam.

Misol uchun:

if((score % 10) <= 3)&&((score % 10) >= 1))
  {
  printf("-");
  }
1
qo'shib qo'ydi

Birinchi ifs zanjirini soddalashtirishingiz mumkin:

// check for conditions
if (score <= 60) {
    printf("F");
} else if (score <= 70) {
    printf("D");
} else if (score <= 80) {
    printf("C");
} else if (score <= 90) {
    printf("B");
} else {
    printf("A");
}

Va +/- shunday qiling:

if ((score - 1) % 10 < 2) {
    printf("-");
} else if ((score - 1) % 10 > 7) {
    printf("+");
}
1
qo'shib qo'ydi

Siz string ni bir belgi bilan taqqoslaysiz. Bir mag'lubiyat literal xotiradagi haqiqiy string majmuasi uchun markerdir. Bir belgi kichik bir tamsayıdır.

Buning o'rniga, bitta </​​em> tirnoqlari, masalan, belgilar dan foydalaning. '3' .

0
qo'shib qo'ydi
   plusminus = line[1];   
if (plusminus < '3'&&plusminus!='0') {
    printf("-");
}
if (plusminus > '8'||plusminus=='0') {
    printf("+");
}

you also need to check for '0' because it is < 3 but it output should be '+' according to the rule .

0
qo'shib qo'ydi
#include 

char * scoreToGrade(int);

int main(){
    int score;
    printf("Enter your score:");
    scanf("%d",&score);

    char * grade = scoreToGrade(score);

    printf("Grade: %s\n", grade);
}

char * scoreToGrade(int score){
    /*
     * 1 character for the letter
     * 1 character for the +/- (if it's there, otherwise a terminating
     * character, and 1 last character if there was a +/- 
     */
    char * result = malloc(sizeof(char) * 3);

    if(score <= 60){
        result[0] = 'F';
    } else if(score <= 70){
        result[0] = 'D';
    } else if(score <= 80){
        result[0] = 'C';
    } else if(score <= 90){
        result[0] = 'B';
    } else {
        result[0] = 'A';
    }

    /* Handle plusses and minuses if the grade is not an F */
    if(result[0] != 'F'){
        int subscore = (score - 1) % 10;
        if(subscore >= 7){
            result[1] = '+';
        } else if(subscore <= 2){
            result[1] = '-';
        } else {
            result[1] = '\0'; /* Add a string terminator, making the string one character long */
        }
    } else {
        result[1] = '\0'; /* Add a string terminator, making the string one character long. This is separate because the 'F' forces a one-character grade */
    }
    /* This is stuck on so that if there is a '+' or '-', the string does not go un-terminated. */
    result[2] = '\0';

    return result;
}
0
qo'shib qo'ydi

Agar 10-sonli bazaning oxirgi raqamini olishni istasangiz, i% 10 dan foydalaning. So'ngra, siz bu raqamga taqqoslashni amalga oshirishingiz mumkin bo'lgan belgilar o'rniga tamsayılar yordamida taqqoslashingiz mumkin.

0
qo'shib qo'ydi
#include 

int main (){
    int grade=0;
    printf("Enter your grade\n");
    scanf("%d",&grade);

    if (grade<=60) {
        printf("F\n");
    }
    if (grade>=61^grade>=70) {
        printf("D\n");
    }
    if (grade>=71^grade>=80) {
        printf("C\n");
    }
    if (grade>=81^grade>=90) {
        printf("B\n");
    }
    if (grade>=91^grade>=100) {
        printf("A\n");
    }
    return 0;
}
0
qo'shib qo'ydi