當前位置:編程學習大全網 - 編程語言 - 求兩道C++編程題

求兩道C++編程題

//

//C++麽?

//

#include?<iostream>

#include?<string>

using?namespace?std;

//------------------------------------

//名稱:?MaxIndex

//功能:求數據最大值及其索引

//參數:a[],數組;nSize,數組長度;

//?pIndex,存放最大值的索引

//返回:最大值

//------------------------------------

int?MaxIndex(int?a[],int?nSize,int?*?pIndex)

{

int?nMax?;

nMax?=?a[0];

*pIndex?=?0;

for?(int?i?=?1;i?<?nSize;i++)

{

if?(a[i]?>?nMax)

{

*pIndex?=?i;

nMax?=?a[i];

}

}

return?nMax;

}

//------------------------------------

//名稱:?UpperCase

//功能:將字符轉化成大寫

//參數:字符

//返回:轉化好的字符

//------------------------------------

char?UpperCase(char?ch)

{

if?(ch?<=?'z'?&&?ch?>=?'a'?)

{

ch?-=?'a'?-?'A';

}

return?ch;

}

//------------------------------------

//名稱:StrInputFormat

//參數:

//這個用到的是串類型

//------------------------------------

int?StrInputFormat(string?s)

{

cout?<<?"請輸入字串並以回車結束:\n";

cin?>>?s;

for?(int?i?=?0;i?<?s.size();i++)

{

s[i]?=?UpperCase(s[i]);

}

cout?<<?s?<<?endl;

return?0;

}

//------------------------------------

//名稱:StrInputFormatChar

//參數:

//這個用到的是串數組

//------------------------------------

void?StrInputFormatChar(char?*?&?szFormat)

{

char?ch?=?'a';

int?nSize?=?100;//每次的增量

int?nBufferSize?=?0;?//緩沖大小

int?nCurLen?=?0;//當前串長

char?*?p?=?szFormat;

int?i?=?0;

printf("請輸入字串並以回車結束:\n");

fflush(stdin);//刷新緩存

while((ch?=?fgetc(stdin))?!=?'\n'?)?//回車結束

{

if?(nCurLen?>=?nBufferSize)

{//緩存不夠就自動增加

p?=?(char?*)malloc(nBufferSize?+?nSize);

memset(p?+?nBufferSize,0,nSize);

memcpy(p,szFormat,nBufferSize);

nBufferSize?+=?nSize;

free(szFormat);

szFormat?=?p;

}

szFormat[nCurLen++]?=?UpperCase(ch);

}

printf("%s",szFormat);

}

//主函數

int?main()

{

int?arr[]?=?{17,85,67,83,65,49,26,92,38,42};

string?s;

char?*?szFormat?=?0;

int?nMaxIndex;

//第壹題?

MaxIndex(arr,sizeof(arr)/sizeof(arr[0]),?&nMaxIndex);

printf("數組的最大值為:%d其索引為%d\n",nMaxIndex,arr[nMaxIndex]);

//第二題的C++方法?

StrInputFormat(s);

//第二題的C方法

StrInputFormatChar(szFormat);

return?0;

}

  • 上一篇:VB高手指點壹下...常用的編程代碼詞組及翻譯是?
  • 下一篇:如何評價美國教授開除中國研究生?
  • copyright 2024編程學習大全網