The task is for the program to read acii signs, find among them the values ​​belonging to the Celsius scale and translate them into Fahrenheit values. For example, if 9237492572vcs 100C is msdaa aw 0C, then only 100 ° C should be considered and output 212F 32F. Values ​​to the input are randomized. It is forbidden to use arrays, pointers and explicit numeric codes of ascii-characters

#include <stdio.h> #include <stdlib.h> int c_f(int celsius) { return ((celsius * 9) / 5) + 32; } int in_alphabet(char c) { return (c >= '0' && c <= '9') || (c == 'C'); } int main(void) { char symbol; int fahrenheit; int state = 1; char celsius; while ((symbol = getchar()) != EOF) { switch(state) { case 1: if (in_alphabet(symbol)) { scanf("%d", &celsius); fahrenheit = c_f(celsius); state = 1; } else if (!in_alphabet(symbol)) { state = 1; } printf("%d%s", fahrenheit, "F"); break; } } return 0; } 

Now the loop goes through each character and outputs 32F instead. Instead of reading, celsius assigns it to 0. The cycle ignores zeros, at 100 ° C passes 1 and C. How can I fix this? How to make the cycle read the word completely, while not in the 16th number system? (Now 100C - perceives as 4108)

  • What is the deep meaning of state = 1 assignments, if the state from the very beginning, 1 t always and everywhere becomes only state = 1 ? - AnT

3 answers 3

You can count it this way (you can translate it yourself):

http://ideone.com/ThL4Zy

 #include <stdio.h> int main(void) { int x; char s[2]; while (1) { switch (scanf("%d%1[C]", &x, s)) { case -1: return 0; case 2: printf("%d\n", x); break; default: scanf("%*[^-+0-9]"); } } } 

    Somehow everything is complicated with you :) Two options with different approaches. The first works with the input string, so it is possible to use atoi() :

     #include <stdio.h> #include <ctype.h> static void c_f( int celsius ) { printf( "%4dC => %.2fF\n", celsius, ( ( ( double )celsius * 9.0 ) / 5.0 ) + 32 ); } int main( void ) { const char data[] = "2C9237492572vcs 100C msdaa aw 0C"; const char *ptr = data; while( *ptr ) { /* ищем цифру */ while( *ptr && !isdigit( *ptr ) ) { ptr++; } if( *ptr ) { /* запомнили позицию первой цифры */ const char *start = ptr; /* пропускаем всё до не-цифры */ while( isdigit( *ptr ) ) { ptr++; } /* после цифр стоит 'C' - нашли, выводим */ if( *ptr == 'C' ) { c_f( atoi( start ) ); } } } return 0; } 

    Conclusion:

      2C => 35.60F 100C => 212.00F 0C => 32.00F 

    The second works with a character-reading stream and generates a number ( celsius ) on the fly:

     int main( void ) { int c; while( ( c = getchar() ) != EOF ) { /* ищем цифру */ while( c != EOF && !isdigit( c ) ) { c = getchar(); } if( c != EOF ) { int celsius = 0; /* пропускаем всё до не-цифры */ /* параллельно формируем число */ while( 1 ) { celsius += ( c - '0' ); c = getchar(); if( isdigit( c ) ) { celsius *= 10; } else { break; } } /* после цифр стоит 'C' - нашли, выводим */ if( c == 'C' ) { c_f( celsius ); } } } return 0; } 
    • Difficult somehow ... - Qwertiy
    • @Qwertiy, are there options easier? In any case, nothing smarter (and at the same time more reliable) came to mind with the flow :) - PinkTux
    • I post it seems like the answer :) - Qwertiy
    • @Qwertiy, a :) I just have a thought of starting from a line to make a start. And how can I manage to do sscanf , so I already scored with the flow :) - PinkTux

    A simple solution.

     #include <string.h> #include <stdio.h> int c_to_f(int celsius) { return ((celsius * 9) / 5) + 32; } int main(int argc, char* argv[]) { char string[] = "9237492572vcs 100C msdaa aw 0C"; char seps[] = " "; char *token; /* выделям токены. разделител - ' ' */ token = strtok( string, seps ); while( token != NULL ) { /* выводим на экран чтобы убедиться что прочитано */ printf( "token: %s\n", token ); /* читаем число и после него символ C */ int t_in_c; if (sscanf(token, "%dC", &t_in_c) > 0) { /* если прочитано удачно то выводит результаты и преобразование */ printf( " %d => %d\n", t_in_c, c_to_f(t_in_c) ); } /* читаем следующий токен */ token = strtok( NULL, seps ); } return 0; } 
    • The code does not work correctly with your data. And in this line, he will not find anything: "ab123Cd" . - PinkTux
    • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky