Telaryon

Benzinár számítás (egydimenziós tömb alapok)

Mar 5th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void beolvas(float * t, int meret);
  5. void kiir(float t[], int meret);
  6. void speckiir(float t[], int meret, float alap);
  7. void relativkiir(float t[], int meret);
  8. float atlag(float t[], int meret);
  9. //int mini(float t[], int meret);
  10. //int maxi(float t[], int meret);
  11. int szelsoertek(float * t, int meret, char ch);
  12. int monoton_e(float * t, int meret, char ch);
  13. int szamol(float * t, int meret, float hatar);
  14. int monotonvizsgalo(float * t, int meret);
  15.  
  16. int main(void){
  17.  
  18.     float benzinar[4];
  19.     int tmeret = 4;
  20.     int minhet;
  21.     int maxhet;
  22.     int monoton;
  23.     float hatarertek;
  24.     float atl;
  25.  
  26.     beolvas(benzinar, tmeret);
  27.     //kiir(benzinar, tmeret);
  28.     minhet = szelsoertek(benzinar, tmeret, '-');
  29.     maxhet = szelsoertek(benzinar, tmeret, '+');
  30.     printf("%d. heten volt a legalacsonyabb, %d. heten volt a legmagasabb a benzinar. \n", minhet+1, maxhet+1);
  31.  
  32.     atl = atlag(benzinar, tmeret);
  33.     printf("A tombelemek atlaga %.2f\n", atl);
  34.     speckiir(benzinar, tmeret, atl);
  35.     speckiir(benzinar, tmeret, benzinar[minhet]);
  36.     relativkiir(benzinar, tmeret);
  37. /*
  38.     monoton= monoton_e(benzinar, tmeret, '-');
  39.     printf("%s\n", monoton?"monoton csokkent":"nem monoton csokkeno");
  40.     monoton= monoton_e(benzinar, tmeret, '+');
  41.     printf("%s\n", monoton?"monoton nott":"nem monoton novo");*/
  42.     monoton = monotonvizsgalo(benzinar, tmeret);
  43.     switch(monoton) {
  44.         case 0 : printf("Nem monoton sorozat. \n"); break;
  45.         case 1 : printf("Monoton csokken. \n"); break;
  46.         case 2 : printf("Montonon no. \n"); break;
  47.     }
  48.     hatarertek = 300;
  49.     printf("%dx volt %.2f Ft alatt a benzinar.\n", szamol(benzinar, tmeret, hatarertek), hatarertek);
  50.     return 0;
  51. }
  52.  
  53. void beolvas(float * t, int meret){
  54.  
  55.     int i;
  56.     int j;
  57.     int jo;
  58.     int buffermeret = 100;
  59.     char inputbuffer[100];
  60.  
  61.     for(i = 0; i < meret; i++){
  62.         jo = 0;
  63.         do{
  64.             printf("%d. tombelem: ", i+1);
  65.             scanf("%s", inputbuffer);
  66.             if(sscanf(inputbuffer, "%f", &t[i])==1){
  67.                 jo = 1;
  68.             }
  69.             else{
  70.                 printf("Hibas adat\n");
  71.                 for(j = 0; j < buffermeret; j++ ) inputbuffer[i] = ' ';
  72.             }
  73.         }while(!jo);
  74.     }
  75.  
  76.     return ;
  77. }
  78.  
  79. void kiir(float t[], int meret){
  80.  
  81.     int i;
  82.  
  83.     for(i = 0; i < meret; i++){
  84.         printf("%d. tombelem: %.2f\n", i+1, t[i]);
  85.     }
  86.  
  87.     return ;
  88. }
  89. void relativkiir(float t[], int meret){
  90.  
  91.     int i;
  92.  
  93.     for(i = 0; i < meret; i++){
  94.         if(i == 0) {
  95.             printf("%.2f, ", t[i]);
  96.         }
  97.         else
  98.             printf("%.2f, ", t[i] - t[i-1]);
  99.     }
  100.     printf("\n");
  101.     return ;
  102. }
  103. void speckiir(float t[], int meret, float alap){
  104.  
  105.     int i;
  106.  
  107.     printf("Sorszam \t Ertek \t Viszonyitasi alap \t Elteres \t");
  108.     for(i = 0; i < meret; i++){
  109.         printf("%d. \t %.2f \t %.2f \t %.2f \t \n", i+1, t[i], alap, t[i]-alap);
  110.     }
  111.  
  112.     return ;
  113. }
  114. float atlag(float t[], int meret){
  115.  
  116.     int i;
  117.     float sum = 0;
  118.  
  119.     for(i=0; i<meret; i++){
  120.         sum += t[i];
  121.     }
  122.     return sum/meret;
  123. }
  124. int mini(float t[], int meret){
  125.  
  126.     int index;
  127.     int i;
  128.  
  129.     index = 0;
  130.     for(i = 0; i < meret; i++){
  131.         if(t[index] > t[i]) index=i;
  132.     }
  133.     return index+1;
  134. }
  135. int maxi(float t[], int meret){
  136.  
  137.     int index;
  138.     int i;
  139.  
  140.     index = 0;
  141.     for(i = 0; i < meret; i++){
  142.         if(t[index] < t[i]) index=i;
  143.     }
  144.     return index+1;
  145. }
  146.  
  147. int szelsoertek(float * t, int meret, char ch){
  148.  
  149.     int index;
  150.     int i;
  151.  
  152.     index = 0;
  153.     if(ch=='+') {
  154.         for(i=0; i<meret; i++){
  155.             if(t[index] < t[i]) index=i;
  156.         }
  157.     }
  158.     else{
  159.         index = 0;
  160.         for(i = 0; i < meret; i++){
  161.             if(t[index] > t[i]) index=i;
  162.         }
  163.     }
  164.     return index+1;
  165. }
  166.  
  167. /*int monoton_e(float * t, int meret, char ch){
  168.     int i;
  169.     int monoton=1;
  170.  
  171.     if(ch=='-') {
  172.         for(i=0; i<meret-1; i++){
  173.             if (t[i] < t[i+1]) monoton=0;
  174.         }
  175.     }
  176.     else{
  177.         for(i=0; i<meret-1; i++){
  178.             if (t[i] > t[i+1]) monoton=0;
  179.         }
  180.     }
  181.     return monoton;
  182. }*/
  183.  
  184. int monotonvizsgalo(float * t, int meret){
  185.     int i;
  186.     int monoton;
  187.  
  188.     monoton = 1;
  189.     for(i = 0; i < meret-1; i++){
  190.         if (t[i] < t[i+1]) monoton=0;
  191.     }
  192.     if(monoton) return 1; //monoton csokken.
  193.  
  194.     monoton = 1;
  195.     for(i = 0; i < meret-1; i++){
  196.         if (t[i] > t[i+1]) monoton=0;
  197.     }
  198.     if (monoton) return 2; // monoton novekszi
  199.     return 0; // nem monoton sorozat
  200. }
  201.  
  202. int szamol(float * t, int meret, float hatar){
  203.     int i;
  204.     int db = 0;
  205.     int limit;
  206.     for(i = 0; i < meret; i++){
  207.         if(t[i] < limit) db++;
  208.     }
  209.     return db;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment