זו הבעיה הגדולה יותר, כנראה שזה לא היה הcin.
מה לא בסדר בתוכנית הבאה?
היא אמורה לקלוט מחרוזת באורך מוגדר ע"י המשתמש ולהחזיר מחרוזת
zipped
או
unzipped
בהתאםלמשל, אם היא מקבלת
aaabbc
היא תחזיר
3a2b1c
ואם היא תקבל
2d1g4a
היא תחזיר
ddgaaaa
הקימפול מצליח והתוכנית עובדת אבל לא מדפיסה כלום
ואם אני חוזר לעמוד של האאוטפוט רשום לי
null pointer assignment
מישהו יודע במקרה מה ואיפה הבעיה? זה חשוב לאללה
אני צריך להגיש את זה ביום שלישי
התוכנית:
#include <iostream.h> #include <conio.h> #include <stdio.h> void ZipStr (char *Str1, char *Str2); int FindUnZipLen (char *Str1); void UnZipStr (char *Str1, char *Str2); int FindZipLen (char *Str1); void main () { clrscr(); char *Str1,*Str2,*p1,*p2; int i,length; cout<<"Enter your string's length"<<'\n'; cin>>length; Str1=new char ; // adding NULL cout<<"Enter your string."<<'\n'; cout<<"The program will analyze whether you want to Zip or UnZip"<<'\n'; for (i=0; i<length; i++) cin>>*(Str1+i); *(Str1+length)='\0'; p1=Str1; p2=Str2; //Saving the original pointing of Str1,Str2 if ((*Str1<='9') && (*Str1>'0')) { Str2=new char ; //giving Str2 its exact length Str1=p1; UnZipStr(Str1,Str2); } else { Str2=new char ; //giving Str2 its exact length Str1=p1; ZipStr(Str1,Str2); } Str2=p2; while (*Str2!='\0') { cout<<*Str2; Str2++; } delete Str1; delete Str2; getch(); } void ZipStr (char *Str1, char *Str2) { char chr; int mone; while (*Str1!='\0') { mone=0; chr=*Str1; while (*Str1==chr) { Str1++; mone++; } *Str2=mone+48; Str2++; *Str2=chr; Str2++; } *Str2='\0'; } int FindUnZipLen (char *Str1) { int mone=0; while (*Str1!='\0') { if (*Str1!=*(Str1++)) mone+=2; Str1++; } return(mone+1); //one for NULL } void UnZipStr (char *Str1, char *Str2) { int i; while (*Str1!='\0') { for (i=0; i<(*Str1-48); i++) *(Str2+i)=*(Str1++); Str2+=*Str1-48; Str1+=2; } *Str2='\0'; } int FindZipLen (char *Str1) { int mone=0; while (*Str1!='\0') { mone+=(*Str1-48)+1; //+1 for the char Str1+=2; } return(mone+1); //+1 for NULL }
|