Anonymní profil Total_Beginner – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Anonymní profil Total_Beginner – Programujte.comAnonymní profil Total_Beginner – Programujte.com

 

Příspěvky odeslané z IP adresy 213.81.220.–

C / C++ › Zaciatocnik v C a jeho lite…
15. 11. 2014   #196050

#3, nie #2

Total_Beginner
C / C++ › Zaciatocnik v C a jeho lite…
15. 11. 2014   #196049

Zdravim :)

Takze citanie fscanf(fr,"$%lf\n",&n) bolo spravne, chyba bola v tom, ze v beznej praci vo windowse pouzivam stredne tlacidlo (koliesko) mysi na pohyb po screene ale code::blocks ho pouziva na prilepenie kopirovaneho a pod, a takto som si do programu nakopiroval viac krat "fscanf(fr,"%lf",&n);" a preto mi to blblo. (Boli tam aj ine bagy ale tie s tou chybou nemali nic spolocne.).

Takto vyzera vysledne riesenie zadania:

"Citajte vsetky R cisla zo suboru CISLA.TXT a vypocitajte aritm.priemer. Koniec suboru testujte pomocou navratovej hodnoty fscanf(); 

#2 Funguje to aj tak, ako pises, akurat by som si tam asi musel vytvorit dalsiu nepotrebnu premennu a ulozit znak $ do nej...alebo sa to da aj inak riesit ?

Mam este taku otazku pre zaujimavost. Ked budem cisla citat bez konca na novom riadku - vo formate fscanf(fr,"$%lf",&n), tak mi kazde cislo cita dva krat. Preco prave dvakrat ?

Co by ste este urobili inak, ako ja ? (Ucim sa len podla knihy, tak este ak mate neaku radu, tip... ) 

Dakujem :) 

#include<stdio.h>
#include<conio.h>
#include<time.h>

void delay(unsigned int mseconds)           /*pre neskorsiu moznost pouzitia delay time*/
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}

int main()
{
    FILE *fr;                         /*Cisla.txt*/
    int count=0;                      /*Pocet cisiel, pociatocna hodnota 0*/
    double sum_total=0;               /*Sucet vsetkych cisel dokopy, pociatocna hodnota 0*/
    double n=0;                       /*Aktualne nacitane cislo*/
    double average=0;                 /*aritm. priemer vsetkych cisel*/

    openfile:
    if((fr=fopen("C:\\Users\\Maroš\\Documents\\Programming\\C\\Projects\\Exercises\\86.5\\CISLA.TXT","r")) == NULL){
      printf("\nERROR: Program can't open the file 'CISLA.TXT' for read. "
             "Press 'Enter' to try again or 'q' to close the program.");

      repair_open:
      switch(getchar()){
      case '\033':return 0;break;
      case '\015':goto openfile;
      default    :printf("Incorrect character, try again..");goto repair_open;
      }
    }
    else
      printf("\nFile is successfully opend.\n\n");

    while((fscanf(fr,"$%lf\n",&n))!=EOF){
      sum_total=sum_total+n;
      count++;
      printf("\nTemplorary sum total of numbers is %f"
             "\nTemplorary count of numbers is %d"
             "\nActual number is %lf\n\n",sum_total, count, n);

      delay(300);
    }

    average=sum_total/count;
    printf("\nThe average of numbers in 'CISLA.TXT' is %f",average);
    printf("\nPress any key to continue...");


    getch();
    fclose(fr);      /*kaslat na kontrolu, to uz ovladam a nechce sa mi to pisat :D*/
    return 0;
}
C / C++ › Zaciatocnik v C a jeho lite…
14. 11. 2014   #196038

Zdravim. Najskor asi opodstatnim nazov temy - nieje velmi presne popisane o co ide, lebo ja tych otazok budem mat asi dost vela casom a zakladat novy thread na kazdu by asi nebolo moc dobre, tak som to vyriesil takto. Ucim sa cca 3.-4. mesiac iba ako hobby popri full-time job (skoro az double full-time :D, v inom obore), takze este som uplne na zaciatku. Som tu novy, tak este velmi neviem ako to tu chodi - ak som nieco zle zaradil tak sa ospravedlnujem. 

Program ma precitat zo suboru vsetky realne cisla, (a potom dalsie teraz nepodstatne veci robit)  ktore su tam zapisane v tvare:

$3.140000
$6.280000
$9.420000
$12.560000
$15.700000
$18.840000
$21.980000
$25.120000
$28.260000
$31.400000
$34.540000
$37.680000
$40.820000
$43.960000
$47.100000
$50.240000
$53.380000
$56.520000
$59.660000
$62.800000

, ale vzdy mi precita iba prve a to stale opakuje dokola. 

Je mi jasne, ze je to pre vas uplna kravina ale neak neviem, preto to tak robi. Mal som to napisane v tvare prikazu for, do - while, while, ale stale to robi to iste. 

Je to vlastne zadanie 86/6 z knihy p.Herouta az na par detailov, ktore ale niesu moc podstatne. Je tam par chyb hlavne koli editacii pri tej chybe ale o tie vobec nejde, to su jednoduche veci.. 

Ak mi teda pomozete, budem vam velmi vdacny. Kludne ma len nasmerujte na nieco, co by som si mal prestudovat aby som prisiel na chybu - ucim sa rad. 

Pouzivam code::blocks

Vopred Dakujem :) 

#include<stdio.h>
#include<conio.h>
#include<time.h>

void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}

int main()
{
    FILE *fr;
    int count = 0.0000;             /*Pocet cisiel, pociatocna hodnota 0*/
    double sum_total = 0.0000;      /*Sucet vsetkych cisel dokopy, pociatocna hodnota 0*/
    double n;
    double average;
    double actual_sum_total;

    openfile:
    if((fr=fopen("C:\Users\Maroš\Documents\Programming\C\Projects\Exercises\86.5\CISLA.TXT","r")) == NULL){
      printf("\nERROR: Program can't open the file 'CISLA.TXT' for read. "
             "Press 'Enter' Exercises\to try Exercises\again or 'q' to close the program.");

      repair_open:
      switch(getchar()){
      case '�33':return 0;break;
      case '�15':goto openfile;
      default    :printf("Incorrect character, try again..");goto repair_open;
      }
    }
    else
    printf("\nFile is successfully opend.\n\n")
    ;

    do{
    fscanf(fr,"$%lf",&n);              /*fscanf(fr,"$%lf",&n)*//*VYMAZANE:fscanf(fr,"%lf",&n);*/
    actual_sum_total=sum_total+n;
    count++;
    printf("\nTemplorary sum total of numbers is %f"
           "\nTemplorary count of numbers is %d"
           "\nActual number is %lf\n\n", sum_total, count, n);

    delay(1000);
    }
    while ((fscanf(fr,"$%lf",&n))!=EOF);                                                              /*fscanf(fr,"%lf",&n);*/

    average=sum_total/count;fscanf(fr,"%lf",&n);
    fscanf(fr,"%lf",&n);
    printf("\nThe average of numbers in 'CISLA.TXT' is %f",average);
    printf("\nPress any key to continue...");
    getch();

    fclose(fr);

    return 0;


}

 

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý