C++程序设计实践指导1.14字符串交叉插入改写要求实现

#include <cstdlib>
#include <iostream>
#include <cstring>

using namespace std;
class STRING
{
char *str1;
char *str2;
public:
STRING(char* s1,char* s2)
{
str1=new char[strlen(s1)];
str2=new char[strlen(s2)];
strcpy(str1,s1);
strcpy(str2,s2);

}
void InsertInOrder();
void process();
void print()
{
cout<<str1<<endl;
}
};

void STRING::process()
{
int len1,len2,count=1;
char str[80];
int i,j=0,k=0;
len1=strlen(str1);
len2=strlen(str2);
int n=len2/len1;
for(i=0;i<len1+len2;i++)
{
if(count%(n+1)==0)
{
str[i]=str1[j++];
count++;
}else{
str[i]=str2[k++];
count++;
}

}
str[i]=‘\0‘;
cout<<str<<endl;
strcpy(str1,str);
}

void STRING::InsertInOrder()
{
char temp;
int i,j,k;
int len1=strlen(str1);
int len2=strlen(str2);
for(i=0;i<len2-1;i++)
{
k=i;
for(j=i+1;j<len2;j++)
if(str2[k]<str2[j])
{
k=j;
temp=str2[i];
str2[i]=str2[k];
str2[k]=temp;
}
}
cout<<str2<<endl;
for(i=0;i<len1;i++)
{
for(j=len2-1+i;j>=0;j--)
{
if(str1[i]>str2[j])
str2[j+1]=str2[j];
else
break;
}
str2[j+1]=str1[i];
}
cout<<str2<<endl;
}

int main(int argc, char *argv[])
{
STRING test("abcd","ABCDEFG");
// test.InsertInOrder();
test.process();
test.print();
system("PAUSE");
return EXIT_SUCCESS;
}

改写要求2和3要分开运行即注释掉改写InsertInOrder()来测试改写要求2 否则会出现错误 这是由于这里是直接在str1和str2上修改

时间: 2024-08-06 15:55:36

C++程序设计实践指导1.14字符串交叉插入改写要求实现的相关文章

C++程序设计实践指导1.9统计与替换字符串中的关键字改写要求实现

改写要求1:将字符数组str改为字符指针p,动态开辟存储空间 改写要求2:增加统计关键字个数的函数void CountKeyWords() 改写要求3: 增加替换函数void FindKeyWords() #include <cstdlib> #include <iostream> #include <string> using namespace std; class WORDNUM { char *p; double c; public: WORDNUM(char

C++程序设计实践指导1.4正整数转换为字符串改写要求实现

改写要求1:改为适合处理超长整数 #include <cstdlib> #include <iostream> #include <string> using namespace std; struct LinkNode { short int data; LinkNode *next; }; class STR { string str; public: STR(string x) { str=x; } struct LinkNode* itoa(); struct

C++程序设计实践指导1.13自然数集中找合数改写要求实现

改写要求1:用单链表实现 改写要求2:析构函数中依次将链表结点删除 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode { int data; LinkNode* next; }; class NOPRIME { friend struct LinkNode; LinkNode* Head; int n; public: NOPRIME(int n1) { n=n1; } void

C++程序设计实践指导1.10二维数组元素换位改写要求实现

改写要求1:改写为以单链表和双向链表存储二维数组 改写要求2:添加函数SingleLinkProcess()实现互换单链表中最大结点和头结点位置,最小结点和尾结点位置 改写要求3:添加函数DoubleLinkProcess()实现互换双向链表中最大结点和头结点位置,最小结点和尾结点位置 #include <cstdlib> #include <iostream> using namespace std; #define M 3 #define N 4 struct SingleLi

C++程序设计实践指导1.12数组中数据线性变换改写要求实现

改写要求1:分别用指针pa.pb代替数组 改写要求2:从键盘输入data元素 元素个数任意,输入0结束 #include <cstdlib> #include <iostream> using namespace std; class DATA { double *pa,*pb; double max,min; double new_max,new_min; int length; public: DATA(double a1[],double x,double y,int len

C++程序设计实践指导1.15找出回文数改写要求实现

改写要求1:用单链表实现 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode { int data; LinkNode *next; }; class PALINDROME { int low,up; int a[100]; int count; public: PALINDROME(int t1,int t2); int IsPalin(int x); LinkNode* IsPa

C++程序设计实践指导1.6超长数列中n个数排序改写要求实现

改写要求1:将以上程序改写为适合超长整数 改写要求2:将以上程序改写为适合超长数列 改写要求3:将数列中指定位置m开始的n个结点重新按降序排序 改写要求4:输出指定位置m开始的n个结点的超长整数 #include <cstdlib> #include <iostream> #include <string> using namespace std; struct LongIntegerNode { short int data; LongIntegerNode *nex

C++程序设计实践指导1.3求任意整数降序数改写要求实现

改写要求1:动态生成单链表存储 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode { int data; struct LinkNode *next; }; class NUM { int n; public: NUM(int x) { n=x; } struct LinkNode * descrease(); void show(LinkNode* pHead) { LinkNo

C++程序设计实践指导1.6分数运算改写要求实现

改写要求:重载>>和<<实现分数类对象的直接输入输出,重载+完成多个分数对象加法 #include <cstdlib> #include <iostream> using namespace std; class Fract { int num,den; public: friend ostream& operator << (ostream& output,Fract& temp); friend istream&