1 / 12

Programozás I.

Programozás I. Horváth Ernő. Elérhetőségek. Bauer Péter http://www.sze.hu/~bauer/ Horváth Ernő http://www.sze.hu/~herno/ Tanszéki honlap http://it.sze.hu. Feladat.

efuru
Download Presentation

Programozás I.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programozás I. Horváth Ernő

  2. Elérhetőségek Bauer Péter http://www.sze.hu/~bauer/ Horváth Ernő http://www.sze.hu/~herno/ Tanszéki honlap http://it.sze.hu

  3. Feladat • Készítsen int toi(char s[]) függvényt, mely tizenegynél kisebb alapú számrendszerbeli számjegyekből álló karakterláncból előállít egy egész számot! • A számrendszer alapja legyen fordítási időben változtatható!

  4. Algoritmus • Billentyűzetről karakterlánc beolvasása -getline • Karakterlánc ellenőrzése – megnézzük, számot adott-e be a felhasználó – karakterek ‘0’ és ‘9’ közé esnek (csak 10es számrendszer) • Karakterlánc átalakítása számmá

  5. toi inttoi(char s[]){ int i, eredmeny = 0; for(i=0; s[i]!=0; ++i) eredmeny = eredmeny * ALAP + s[i] - '0'; returneredmeny; }

  6. toi • printf("%d", '0'); • ASCII • 48 = '0' • 49 = '1' • 50 = '2' • ... • 65 = 'A'

  7. toi for(i=0; s[i]!=0; ++i) eredmeny = eredmeny * ALAP + s[i] - '0'; "1264" [0] >> 0 * 10 + 1 = 1 [1] >> 1 * 10 + 2 = 12 [2] >> 12* 10 + 6 = 126 [3] >> 126*10 + 4 = 1264

  8. szame //"Az s karakterlanc ALAP alapuszam-e?" kerdesre ad logikai valaszt a fuggveny. intszame(char s[]){ int i; // Ha 10-es szamrendszert kellene vizsgálni isdigit() for(i=strlen(s)-1; i && s[i]>='0' && s[i]<ALAP+'0'; --i); if(s[i]>='0' && s[i]<ALAP+'0') return 1; else return 0; }

  9. stlib.h

  10. atoi // string --> decimalis intatoi(charst[]){ intdecimal = 0; int i = 0; charneg=0; while(st[i]==' ' || st[i]=='\t' || st[i]=='\n') ++i; if(st[i]=='-') ++neg; if(st[i]=='-' || st[i]=='+') ++i; for(;st[i];++i) decimal = decimal*10+st[i]-'0'; if(neg) decimal = -decimal; returndecimal; }

  11. atoi charstring[]="-2147"; printf("Ez egy sima string: %s \n", string); printf("Ez decimalisszam : %d", atoi(string)); Ez egy sima string: -2147 Ez decimalisszam : -2147

  12. Köszönöm a figyelmet!

More Related