c++课设 不使用<string.h是什么头文件>中的函数 大一初学者,帮帮忙吧,急用

紧急求助,明早考试C++试题有三道,帮帮忙!(今晚必须解决)_百度知道
紧急求助,明早考试C++试题有三道,帮帮忙!(今晚必须解决)
第一题:写一个函数模板,求数组中的最大元素。使得函数调用时,数组的类型和返回类型可以是整数也可以是双精度类型。第二题:写一个函数摸板,使用冒泡排序将数组内容由小到大排列。使得函数调用时,数组的类型可以是整数也可以是双精度类型。第三题:建立类型模板,input,在调用构造函数时,完成以下工作:(1)提示用户输入。(2)让用户输入数据。(3)如果数据不在预定范围内,重新提示输入。input型的对象应当按以下形式定义:input ob(&promput message&,min_value,max_value)其中,promput message 是提示输入的信息。可接受的最小值和最大值分别有min_value与max_value指定。
麻烦能否写清楚点,能直接运行的
提问者采纳
再补充:我承认我说得有点过火了……你把下面代码全部复制,然后中文全部去掉,再把几个main函数并成一个main函数就可以了。别忘了在最开始包含这些头文件:#include &iostream&#include &iomanip&完整的代码我等下发到我baidu空间里。偷懒的话直接去复制好了……--------------------------------补充:写成这样还要再详细,我就没话说了每一题都告诉你怎么做,给你举例子,给你测试,还想怎么样,直接写好cpp文件发到你邮箱?楼主干脆花钱雇个人给你当保姆吧……_______________________________________注意:(1) 代码中部分注释是没有意义的。(2) 示例程序中仅写出主函数,包含文件等其他部分请自行加入(3) 代码调试环境:VC6Pro SP6 + WinXP第一题:写一个函数模板,求数组中的最大元素。使得函数调用时,数组的类型和返回类型可以是整数也可以是双精度类型。 template & class Type &Type getArrMax( const Type* pArr, const int len ) // 抄袭可耻!{ // 这里验证数组基地址及数组长度len的合法性,代码省略
Type tmp = *pA for( int i=0; i& i++ )
tmp = *(pArr+i)&tmp ? *(pArr+i) :}/**
示例程序void main(){ int ia[] = { 5,2,6,4,1,3 }; double da[3]; // 抄袭可耻! da[0] = 1.0123456; da[1] = 98.; da[2] = 3.1415927; // 测试getArrMax cout && getArrMax(ia,6) && cout && setprecision(16) && getArrMax(da,3) &&}**/输出结果:698.Press any key to continue第二题:写一个函数摸板,使用冒泡排序将数组内容由小到大排列。使得函数调用时,数组的类型可以是整数也可以是双精度类型。 template & class Type &void bSort( Type* pArr, const int len ){ // 这里验证数组基地址及数组长度len的合法性,代码省略 T for( int i=0; i& i++ )
for( int j=0; j&i; j++ )
if( *(pArr+j)&*(pArr+i) )
{ // 抄袭可耻!
tmp = *(pArr+i);
*(pArr+i) = *(pArr+j);
*(pArr+j) =
示例程序void main(){ int ia[] = { 5,2,6,4,1,3 }; double da[3]; // 抄袭可耻! da[0] = 1.0123456; da[1] = 98.; da[2] = 3.1415927; // 测试bSort bSort(ia,6); for(i=0;i&6;i++) cout && ia[i] && & &; cout && bSort(da,3); for(i=0;i&3;i++) cout && setprecision(16) && da[i] && & &; cout &&}**/输出结果:1 2 3 4 5 61...Press any key to continue第三题:建立类型模板,input,在调用构造函数时,完成以下工作: (1)提示用户输入。 (2)让用户输入数据。 (3)如果数据不在预定范围内,重新提示输入。 input型的对象应当按以下形式定义:input ob(&promput message&,min_value,max_value) 其中,promput message 是提示输入的信息。可接受的最小值和最大值分别有min_value与max_value指定。template & class Type & class EInput // 为防止命名冲突,不用input而改用EInput{ protected:
EInput( const char* prompt_msg, const Type min_val, const Type max_val, std::istream& is = std::cin, std::ostream& os = std::cout )
// 这里验证数字符串指针prompt_msg的合法性,代码省略
os && prompt_ is &&
while( tmp & max_val || tmp & min_val )
os && &error init.& && std::
os && prompt_ is &&
} // 抄袭可耻!
Type GetVal() { return m_ };};/**
示例程序void main(){ // 测试类EInput EInput&int& ob(&please enter some int(30-50): &,30,50); cout && &value got from class template: & && ob.GetVal() &&}**/输出结果:please enter some int(30-50): 1error init.please enter some int(30-50): 15error init.please enter some int(30-50): 99error init.please enter some int(30-50): 37value got from class template: 37Press any key to continue
为您推荐:
其他3条回答
我是初学者,进来学习
#include &iostream&#include &cassert&template &class T&T FindMax(T* array,int len){ assert(array != NULL); T max=array[0]; for (int i=1; i& i++) {
if(array[i]&max)
max=array[i];
} }}template &class T&void BubbleSort(T *array,int len){ int i,j;
for (i=0; i& i++) {
exchange =
for (j=len-1; j&i; j--)
if (array[j]&array[j-1])
T temp=array[j];array[j]=array[j-1];array[j-1]=
exchange =
if (!exchange)
#include &stdio.h&
#include &iostream&
#include &string&
template&class T& T Max( T a[], int length )
for ( int i=1; i & i++ )
if ( a[i] & t )
template &class T& void BubbleSort( T a[], int length )
bool bFinished =
while ( !bFinished )
bFinished =
for ( int j=0; j & length-1-i; j++ )
if ( a[j] & a[j+1] )
a[j]=a[j+1];
bFinished =
template &class T& class Input
Input ( string msg , T minValue, T maxValu...
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁C++如何对一个string数组按字典序排序_百度知道
C++如何对一个string数组按字典序排序
1.#include &vector&#include &string&#include &algorithm&#include &iostream&#include &iterator&#include &map&int _tmain(int argc, _TCHAR* argv[]){vector&string&vec.push_back(&This&);vec.push_back(&is&);vec.push_back(&a&);vec.push_back(&key&);vec.push_back(&this&);vec.push_back(&key&);vec.push_back(&A&);vec.push_back(&Thas&);vec.push_back(&kEy&);map&string,int&vector&string&::iterator it = vec.begin();for( it != vec.end(); ++it){if(words.find(*it) != words.end() ){int i = words[*it];++i;words[*it] =}else{words.insert(map&string,int&::value_type(*it,1));}}map&string,int&::iterator mapit = words.begin();for(mapit != words.end();++mapit){cout&& mapit-&first&&&
&&&mapit-&second&&}return 0;}#include &vector&#include &string&#include &algorithm&#include &iostream&#include &iterator&#include &map&#include &fstream&const int MAX_SIZE = 40;void GetWords(vector&string& &vec){char str[ MAX_SIZE];ifstream InFile(&E:\\test.txt&);while(!InFile.eof()){InFile.getline(str,MAX_SIZE);const char * split = &,* ;&; char * p = strtok (str,split); while(p!=NULL){ string str =vec.push_back(str);p = strtok(NULL,split); } }copy (vec.begin(),vec.end(),ostream_iterator&string&(cout,&
&));}void SortWords( vector&string& &vec,map&string,int& &words){vector&string&::iterator it = vec.begin();for( it != vec.end(); ++it){if(words.find(*it) != words.end() ){int i = words[*it];++i;words[*it] =}else{words.insert(map&string,int&::value_type(*it,1));}}}int _tmain(int argc, _TCHAR* argv[]){vector&string&map&string,int&GetWords(vec);SortWords( vec,words);cout&&endl&&&====After sort====&&&map&string,int&::iterator mapit = words.begin();for(mapit != words.end();++mapit){cout&& mapit-&first&&&
&&&mapit-&second&&}return 0;}2.#include &algorithm&std::sort(dict, dict+1000)
其他类似问题
为您推荐:
提问者采纳
#include &algorithm&std::sort(dict, dict+1000)就是把dict[0]到dict[1000-1]排序
来自团队:
其他6条回答
直接比较就可以了,起泡法。可以自定义n的值#include&iostream&#include&string&#include&iomanip&int main(){ int i,j,n=5; string dict[10],e;//先用5个意思一下 for(i=0;i&n;i++)
cin&&dict[i];//输入单词 for(j=0;j&n-1;j++)
for(i=0;i&n-j-1;i++)
if(dict[i]&dict[i+1])
e=dict[i];
dict[i]=dict[i+1];
dict[i+1]=e;//交换相邻的
} for(i=0;i&n;i++)
cout&&setw(6)&&dict[i]&&& &; cout&& return 0;}
一楼正解,这才是c++,其他都是浮云。 标准库的sort效率绝对比单纯的冒泡优秀,而且还是智能的,大数据量情况下是快速排序,反之则退化成冒泡排序。 楼上说的也好,其实还有更简单的,直接用std::set&string&就完事了,自动排序。
按字典序排序不就是按ABCD排序吗?String中每一个元素都是一个char型的变量,所以进行排序的话,就相当于对char型变量进行排序。而ABCD的char型值对应的是A&B&C&D&...&X&Y&Z,所以只需要对元素进行升序或者降序排列就可以了的~
用冒泡法排序,不断遍历至稳定。
冒泡排序的话,不管是时效还是空效都会很差得,
#include &iostream&
#include &cstring&
double replace(char ch)
int diff=ch-'a';
if (diff&=0)
return 97+
return 32+diff+96.5;
int compare(char a[],char b[])
//对数组做比较
int length_a=strlen(a);
int length_b=strlen(b);
if (length_a&length_b)
return length_a;
return length_b;
int main()
int count=0;//计算用户输入了多少个字符串;
int count1=0;//看比较数组大小时一组里面有多少是一样的;
int count2=0;//计算输出了多少个字符串;
char ch[100][100];
bool exist[100];
double replacement[100][100];
cout&&&请输入要排序的字符串,以#...
汗~不是说C++程序吗?怎么除了输入输出以外全是C风格的?
先说你几句,你这程序写得还真是把看官折腾得够呛,我反正是费了些工夫才弄明白你在干什么。
而且你这并不是排序,只是按字典序输出而已,序其实没变,对吧。。
先是一个语法错误:
else if (replacement[m][n]=temp[n]) //“==”
然后是逻辑问题:
for (int n=0;n&compare(ch[m],ch[J]);n++)
if (replacement[m][n]&temp[n])
{ for (int l=0;l&strlen(ch[m]);l++)
temp[l]=replacement[m][l];
else if (replacement[m][n]=temp[n])
这段判断并没有实现字典序的比较,比如adjk,Bdkj作为输入时,判断并没有在a,B的差异上打住,而是继续判断到j,k。
只要再加一句
字典序的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁C++课设报告(关于电话簿管理的,是自己调试出来的)_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
C++课设报告(关于电话簿管理的,是自己调试出来的)
编​写​一​个​电​话​簿​管​理​程​序​,​要​求​通​讯​录​中​包​括​姓​名​、​序​号​和​联​系​电​话​,​程​序​中​可​以​对​电​话​簿​人​员​进​行​插​入​、​删​除​、​显​示​、​查​找​、​修​改​等​操​作​,​并​可​将​结​构​保​存​到​文​件​中​,​可​以​人​为​的​对​系​统​内​的​数​据​进​行​修​改​而​且​可​以​保​存​你​的​所​有​的​修​改​。​从​而​达​到​对​电​话​簿​的​管​理​。​是​自​己​认​真​写​的​,​调​试​出​来​的​,
阅读已结束,如果下载本文需要使用
想免费下载本文?
你可能喜欢标准c++中string类函数介绍_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
标准c++中string类函数介绍
在​C​+​+​中​s​t​r​i​n​g​流​是​很​重​要​的​一​个​操​作​,​本​文​档​对​该​操​作​进​行​了​详​细​的​解​释​,​对​初​学​者​大​有​裨​益​…​…
阅读已结束,如果下载本文需要使用
想免费下载本文?
你可能喜欢小弟是C/C++的初学者,有两个问题想请教一下,急急急~~_百度知道
小弟是C/C++的初学者,有两个问题想请教一下,急急急~~
1.编写函数void reverse_string(char *str),不借助任何库函数,实现字符串反转功能,例如&abcd&变为&dcba&2.编写函数int pick_letter(char* file_a, char* file_b),将文件file_a中所有的英文字符写入文件file_b,返回写入字符个数。求解~~
提问者采纳
zhykzhykzhyk 见谅,小修改一下void reverse_string(char *str){
if( str == NULL ||
str[0] == '\0' ) char *end = while (*end++); while (str & end) {
char t = *--
*str++ = }}#include &stdio.h&#include &ctype.h&int pick_letter(char *file_a, char *file_b){ FILE *fin = fopen(file_a, &r&); FILE *fout = fopen(file_b, &w&); int c, count = 0;
if (fin == NULL || fout == NULL)
return 0; while ((c = fgetc(fin)) != EOF)
if (isalpha(c)) fputc(c, fout), ++ fclose(fin); fclose(fout); }
提问者评价
其他类似问题
为您推荐:
其他2条回答
void reverse_string(char *str){ char *end = while (*end++); while (str & end) {
char t = *--
*str++ = }}#include &stdio.h&#include &ctype.h&int pick_letter(char *file_a, char *file_b){ FILE *fin = fopen(file_a, &r&); FILE *fout = fopen(file_b, &w&); int c, count = 0; while ((c = fgetc(fin)) != EOF)
if (isalpha(c)) fputc(c, fout), ++ fclose(fin); fclose(fout); }
What question do you want ask?你要提什么问啊
我已经追问了啊。就是这两题。1.编写函数void reverse_string(char *str),不借助任何库函数,实现字符串反转功能,例如&abcd&变为&dcba&2.编写函数int pick_letter(char* file_a, char* file_b),将文件file_a中所有的英文字符写入文件file_b,返回写入字符个数。
初学者的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 c string.h 的文章

 

随机推荐