Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Napisz program do zamiany liczby w systemie (2,8,10) na system (2,8,10)
- Liczby pobierane bฤdฤ z pliku tekstowego (wejscie.txt) i zapisywane do pliku(wyjscie.txt)
- Plik wejscie.txt ma mieฤ format:
- 2:10:00101010
- gdzie:
- 2: - na wejsciu system dwojkowy
- 10: - na wyjsciu system dziesietny
- 00101010 - liczba do zamiany
- Zawartosc pliku wejscie.txt
- 2:10:10111010
- 8:10:35703
- 10:2:192832
- 10:8:13022
- 2:8:1010011
- 8:2:645213*/
- /*#include <iostream>
- #include <fstream>
- using namespace std;
- string nazwaZag="wejscie.txt";
- string nazwaWyn="wyjscie.txt";
- int main(){
- system("pause >> null");
- return 0;
- }*/
- #include<iostream>
- #include<cstdio>
- #include<ctime>
- #include<fstream>
- #include<sstream>
- #include<string>
- #include<cstdlib>
- using namespace std;
- struct podz{
- string pocz;
- string poczUchw;
- char systemP,systemN;
- };
- podz tab[100];
- // zamiana ze string na int i int na string {
- int stringNaInt(string &warStr){
- int zwInt;
- istringstream iss(warStr);
- iss >> zwInt;
- return zwInt;
- }
- string intNaString(int &warInt){
- ostringstream ss;
- ss << warInt;
- string str = ss.str();
- return str;
- }
- // }
- void odczyt(){
- ifstream uchw("wejscie.txt");
- if(uchw.good()){
- int i=0,n;
- string kol;
- while(!uchw.eof()){
- getline(uchw,tab[i].pocz);
- if(tab[i].pocz[0]=='1') tab[i].systemP='d';
- if(tab[i].pocz[0]=='2') tab[i].systemP='b';
- if(tab[i].pocz[0]=='8') tab[i].systemP='o';
- if(tab[i].pocz[1]==':'){
- if(tab[i].pocz[2]=='1') tab[i].systemN='d';
- if(tab[i].pocz[2]=='2') tab[i].systemN='b';
- if(tab[i].pocz[2]=='8') tab[i].systemN='o';
- }
- if(tab[i].pocz[2]==':'){
- if(tab[i].pocz[3]=='1') tab[i].systemN='d';
- if(tab[i].pocz[3]=='2') tab[i].systemN='b';
- if(tab[i].pocz[3]=='8') tab[i].systemN='o';
- }
- n=0;kol="";
- for(int j=0;j<tab[i].pocz.length();j++){
- if(n>1) kol+=tab[i].pocz[j];
- if(tab[i].pocz[j]==':') n++;
- }
- tab[i].poczUchw=kol;
- cout<<kol<<endl;
- i++;
- }
- }
- uchw.close();
- }
- int dlZapTab(int &rIle){ // zwraca ile tab ma zapisanych wartosci !=""
- for(int i=0;i<100;i++){
- if(tab[i].pocz!="") rIle++;
- }
- return rIle;
- }
- string przekrec(string &co){
- string gotowe="";
- int y1=co.length();
- for(int y=y1;y>=0;y--){
- gotowe+=co[y];
- }
- return gotowe;
- }
- string binarnie(int &a){
- string kol="";
- int bierz=a;
- do{
- if(bierz%2==1) kol+='1'; else kol+='0';
- bierz/=2;
- }while(bierz>=1);
- return przekrec(kol);
- }
- string osemkowo(int &a){
- string kol="";
- int bierz=a,op;
- do{
- op=bierz%8;
- kol+=intNaString(op);
- bierz/=8;
- }while(bierz>=1);
- return przekrec(kol);
- }
- int potR(int x,int y){ // potegowanie rekurencyjne
- if (y==0) return 1; else return potR(x,y-1)*x;
- }
- string naDzies(string &a,char &zCzego){
- int dlEl=a.length(),ktIndex=dlEl-1,sumator=0,nowyInt,ktSys=0;
- string nowyString;
- if(zCzego=='b') ktSys=2;
- if(zCzego=='o') ktSys=8;
- for(int z=0;z<dlEl;z++){
- nowyString="";
- nowyString+=a[z];
- nowyInt=stringNaInt(nowyString);
- sumator+=potR(ktSys,ktIndex)*nowyInt;
- ktIndex--;
- }
- nowyString=intNaString(sumator);
- return nowyString;
- }
- string polszczyzna(char &zNa){
- string ktSysNap="";
- switch(zNa){
- case 'd': ktSysNap="10";
- break;
- case 'b': ktSysNap="2";
- break;
- case 'o': ktSysNap="8";
- break;
- }
- return ktSysNap;
- }
- void konwersja(){
- int ile=0,pomInt,warModyf;
- string pomString,poKon;
- ile=dlZapTab(ile);
- ofstream uchw("wyjscie.txt");
- for(int i=0;i<ile;i++){
- poKon="";
- if(tab[i].poczUchw=="") continue;
- switch(tab[i].systemP){
- case 'd':{
- switch(tab[i].systemN){
- case 'b':{
- pomInt=stringNaInt(tab[i].poczUchw);
- poKon=binarnie(pomInt);
- break;}
- case 'o':{
- pomInt=stringNaInt(tab[i].poczUchw);
- poKon=osemkowo(pomInt);
- break;}
- }
- break;}
- case 'b':{
- poKon=naDzies(tab[i].poczUchw,tab[i].systemP);
- if(tab[i].systemN=='o'){
- pomInt=stringNaInt(poKon);
- poKon=osemkowo(pomInt);
- }
- break;}
- case 'o':{
- poKon=naDzies(tab[i].poczUchw,tab[i].systemP);
- if(tab[i].systemN=='b'){
- pomInt=stringNaInt(poKon);
- poKon=binarnie(pomInt);
- }
- break;}
- }
- tab[i].poczUchw+="("+polszczyzna(tab[i].systemP)+")";
- warModyf=tab[i].poczUchw.length();
- uchw<<tab[i].poczUchw;
- uchw.seekp(+25-warModyf,ios_base::cur);
- uchw<<"-";
- if(polszczyzna(tab[i].systemN)=="10") uchw.seekp(+11,ios_base::cur);else uchw.seekp(+10,ios_base::cur);
- uchw<<poKon<<"("<<polszczyzna(tab[i].systemN)<<")";
- if(i<ile-1) uchw<<endl;
- }
- uchw.close();
- }
- int main(){
- odczyt();
- konwersja();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment