String на си - C (СИ)
Формулировка задачи:
Напишите библиотеку и как вычислить длину этой строки
Решение задачи: «String на си»
textual
Листинг программы
#include<iostream> #include <conio.h> #include <windows.h> using namespace std; class CString{ private: char * str; public: CString(){ this->str=NULL; } CString(char * str){ this->str=new char[strlen(str)+1]; strcpy(this->str,str); } void Print()const{ cout<<"This string : "<<this->str<<endl; } ~CString(){ if(str!=NULL){ delete [] str; str=NULL; } } CString(const CString & S){ this->str=new char[strlen(S.str)+1]; strcpy(this->str,S.str); } CString operator=(CString S){ delete [] this->str; this->str= new char [strlen(S.str)+1]; strcpy(this->str,S.str); return *this; } CString operator+(CString S){ char * tmp=new char[strlen(this->str)+1+strlen(S.str)+1]; strcpy(tmp,this->str); strcat(tmp,S.str); return CString(tmp); } CString operator+(char * str){ char* tmp=new char[strlen(this->str)+1+strlen(str)+1]; strcpy(tmp,this->str); strcat(tmp,str); return CString(tmp); } CString operator=(char* str){ char * tmp=new char[strlen(this->str)+1]; strcpy(tmp,str); return CString(tmp); } bool operator<(CString S){ if(stricmp(this->str,S.str)<0) return true; return false; } bool operator>(CString S){ if(stricmp(this->str,S.str)>0) return true; return false; } bool operator!=(CString S){ if(stricmp(this->str,S.str)==0) return true; return false; } CString operator++(){//sufix for(int i=0;i<strlen(this->str);i++) { this->str[i]++; } return *this; } CString operator--(){//sufix for(int i=0;i<strlen(this->str);i++) { this->str[i]--; } return *this; } CString operator--(int){//postfix CString s(this->str); for(int i=0;i<strlen(this->str);i++){ this->str[i]--; } return s; } CString operator++(int){ CString s(this->str); int size=strlen(this->str); for(int i=0;i<size;i++){ this->str[i]++; } return s; } CString operator()(char * str){ strcpy(this->str,str); } }; void main(){ HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbInfo; GetConsoleScreenBufferInfo(hConsole, &csbInfo); WORD dwAttrConsole = csbInfo.wAttributes; char * str=new char[25]; char * str1=new char[25]; char * tmpchar=new char[25]; cout<<"Enter first string : "; cin.getline(str,25); cout<<"Enter second string : "; cin.getline(str1,25); CString c(str); CString c1(str1); CString c3; int option=1; char key; while(option!=10){ while(true){ system("cls"); cout<<"MENU"<<endl; if (option==1){ SetConsoleTextAttribute(hConsole,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_BLUE); cout<<"-->OPERATOR=(CSTRING)<--"<<endl; SetConsoleTextAttribute(hConsole, dwAttrConsole); } else cout<<"operator=(cstring)"<<endl; if(option==2){ SetConsoleTextAttribute(hConsole,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_BLUE); cout<<"-->OPERATOR+(CSTRING)<--"<<endl; SetConsoleTextAttribute(hConsole, dwAttrConsole); } else cout<<"operator+(cstring)"<<endl; if(option==3){ SetConsoleTextAttribute(hConsole,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_BLUE); cout<<"-->OPERATOR=(CHAR *)<--"<<endl; SetConsoleTextAttribute(hConsole, dwAttrConsole); } else cout<<"operator=(char *)"<<endl; if(option==4){ SetConsoleTextAttribute(hConsole,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_BLUE); cout<<"-->OPERATOR+(CHAR *)<--"<<endl; SetConsoleTextAttribute(hConsole, dwAttrConsole); } else cout<<"operator+(char *)"<<endl; if(option==5){ SetConsoleTextAttribute(hConsole,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_BLUE); cout<<"-->OPERATOR < > != (CSTRING)<--"<<endl; SetConsoleTextAttribute(hConsole, dwAttrConsole); } else cout<<"operator < > !=(cstring)"<<endl; if(option==6){ SetConsoleTextAttribute(hConsole,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_BLUE); cout<<"-->OPERATOR++(SECOND CSRTING)<--"<<endl; SetConsoleTextAttribute(hConsole, dwAttrConsole); } else cout<<"operator++(second cstring)"<<endl; if(option==7){ SetConsoleTextAttribute(hConsole,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_BLUE); cout<<"-->++OPERATOR(FIRST CSRTING)<--"<<endl; SetConsoleTextAttribute(hConsole, dwAttrConsole); } else cout<<"++operator(first cstring)"<<endl; if(option==8){ SetConsoleTextAttribute(hConsole,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_BLUE); cout<<"-->OPERATOR--(SECOND CSRTING)<--"<<endl; SetConsoleTextAttribute(hConsole, dwAttrConsole); } else cout<<"operator--(second cstring)"<<endl; if(option==9){ SetConsoleTextAttribute(hConsole,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_BLUE); cout<<"-->--OPERATOR(FIRST CSRTING)<--"<<endl; SetConsoleTextAttribute(hConsole, dwAttrConsole); } else cout<<"--operator(first cstring)"<<endl; if(option==10){ SetConsoleTextAttribute(hConsole,BACKGROUND_GREEN |BACKGROUND_BLUE |BACKGROUND_INTENSITY); cout<<"-->EXIT<--"<<endl; SetConsoleTextAttribute(hConsole, dwAttrConsole); } else cout<<"exit"<<endl; key=getch(); if (key==13) break; if (key==-32){ key=getch(); if((key==72)&&(option>1)){ option--; } if((key==80)&&(option<10)){ option++; } } } if (option==1){ c=c1; c.Print(); } if (option==2){ c3=c+c1; c3.Print(); } if (option==3){ cout<<"Enter char * string : "; cin.getline(tmpchar,25); c=tmpchar; c3.Print(); } if(option==4){ cout<<"Enter char * string : "; cin.getline(tmpchar,25); c3=c+tmpchar; c3.Print(); } if(option==5){ if(c>c1) cout<<"First string longer "<<endl; else if(c<c1) cout<<"Second string longer "<<endl; else if(c!=c1) cout<<"Strings are same "<<endl; } if(option==6){ c3=c1++; c3.Print(); } if(option==7){ c3=++c; c3.Print(); c.Print(); } if(option==8){ c3=c1--; c3.Print(); } if(option==9){ c3=--c; c3.Print(); c.Print(); } if(option==10){ cout<<"Good Bye!"<<endl; } system("pause"); } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д