读书人

讨论 几个关于控制台的有关问题来!

发布时间: 2012-02-16 21:30:36 作者: rapoo

讨论 几个关于控制台的问题,高手进来!!!
1. 编写一个控制台程序,完成以下功能,

首先要求程序的使用者输入一个字符串(长度不能有限制),以下称为字符串A

再要求用户输入一个字符串,以下称为字符串B,

如果B是A的子串,则得到并输出以B本身为分隔符将A分成的一个或多个字符串(不含B的),

如果不是,提示用户.

例如:A: "adsfg2334adfgj ", B: "ad " 则输出字符串为2个: "sfg2334 "和 "fgj ";

A: "123asd23wer234fff ", B: "f " 则输出字符串为1个: "123asd23wer234 ";



2. 编写一个控制台程序,完成以下功能,

要求程序的使用者输入一个正整数, 程序根据该数目大小随机生成一个不重复的整数序列,

对此序列排序,排序方法不限,将排序前的序列和排序后的序列输出给用户;

例如: 输入 20, 则产生大小为20的不重复整数序列,并对其排序



3.编写2个控制台程序, 实现一个Client和Server的TCP通信,
要求Server端程序启动后保持监听,

Client端启动后连接Server端(用户启动时手动指定IP和端口),连接上以后显示相应消息,

在Client端输入一个字符串,Server端可以显示相应的字符串,并将该字符串倒序发还给Client,

Client将此字符串显示出来.
这是我在看书过程中困扰我的几个典型问题,
我自己试着写的程序
可是都无法通过
希望高手能帮我写出程序的算法,参酌一下,谢谢,


[解决办法]
第二题
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <time.h>
using namespace std;

bool _less(const int& a,const int& b)
{
return a <b;
}
int main()
{
int count=0;
cout < < "input the number: ";
cin> > count;
vector <int> num;
int i=0;
srand(time(NULL));
while (i <count)
{
int a = rand();
vector <int> ::const_iterator it=num.begin();
for (;it!=num.end();++it)
if (*it==a)
break;
if (it==num.end())
{
num.push_back(a);
i++;
}
}
vector <int> ::const_iterator it=num.begin();
for (;it!=num.end();++it)
cout < <(*it) < <endl;
cout < <endl < < "sort: " < <endl;
sort(num.begin(),num.end(),_less);
it=num.begin();
for (;it!=num.end();++it)
cout < <(*it) < <endl;
return 0;
}

读书人网 >C++

热点推荐