\
}
cin>>b;
cout<<\请输入三角形的边c:\cin>>c;
while(!triangle(a,b,c)) { }
cout<<\该三角形的面积为:\
cout<<\不满足三角形的定义cout<<\请输入三角形的边a:\cin>>a;
cout<<\请输入三角形的边b:\cin>>b;
cout<<\请输入三角形的边c:\cin>>c;
double area(float a,float b,float c)//面积公式 { }
bool triangle(float a,float b,float c)//是否是三角形 { }
if(a<=0 || b<=0 || c<=0)return false; if(a+b<=c || a+c<=b || b+c<=a)return false; return true; double area; float s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c)); return area;
第六单元 数组
根据题目要求,编写完整程序加以验证,可调用已有函数,也可添加其它函数
(1)编写一个函数:bool isSorted(const int a[], int n),判断数组a中的元素是否按升序排列。
#include
bool isSorted(const int a[],int n) { }
for(int i=1;i<=n-1;i++) { } return true;
if(a[i]
return false;
void main() { }
(2)编写一个函数:void getRandom(int a[], int n),生成0到100之间的随机整数作为数组元素。再编写一个函数int getMax(const int a[], int n),在数组a中找出最大值并返回下标。再编写一个排序函数,将int数组进行升序排序,运行程序并验证正确性。
#include
void getRandom(int a[],int n); int getMax(const int a[],int n); void sort(int a[],int n);
const int n = 100; //the length of the array void main() {
int a[n],counter = 0;
getRandom(a,n); //create random array cout<<\ //show original array for(int i = 0;i < n;i++) { }
int max_counter = 1;
for(i = 0;i < n;i++) //show max numbers in array { }
sort(a,n); //sort the array cout<<\ //show the sorted array for(i = 0;i < n;i++) {
cout<
if(a[i] == a[getMax(a,n)])
cout<<\\max num in array is \+ cout<
if((counter % 10) == 0)
cout< int a[10]={1,2,2,4,5,6,7,8,9,10}; if(isSorted(a,10)==true) else cout<<\非升序排列\\n\cout<<\按升序排列\\n\ 1<<\ cout<<\ } } if((counter % 10) == 0) cout< void getRandom(int a[],int n) { time } int getMax(const int a[],int n) { } void sort(int a[],int n) { } (3)编写一个函数:bool insertSort(int a[], int maxnum, int n, int x),形参a是一个数组,形参maxnum是数组a的大小,即能容纳最多元素个数,n是当前已插入的元素个数,形参x是要插入的一个值。该函数尝试将x插入到数组a中的合适位置,使所有元素保持升序排列。如果成功插入,返回true。如果没有位置插入(即已满),就返回false。验证该函数的正确性。 #include bool insertSort(int a[], int maxnum, int n, int x) { if (n==maxnum) return false; int i, j, temp; for(i = 0; i < n-1; i++) if(a[j] > a[j+1]) { }; temp =a[j]; a[j] = a[j+1]; a[j+1] = temp; //the bubblesort for(j = 0; j < n-1-i; j++) int max_subscript = 0; for(int i = 1;i < n;i++) { } return(max_subscript); if(a[max_subscript] < a[i]) max_subscript = i; for(int i = 0;i < n;i++) a[i] = rand() % 101; srand((unsigned)time(NULL)); //the seed is the current } for (int i=0;i if (x for(int j=n;j>i;j--)//i及i后的所有值往后移一个位置 a[j]=a[j-1]; a[i]=x; return true; void print(int a[],int maxnum) { } void main() { } (4)编写一个函数:void trim(char a[]),将字符串a的前后的所有空格过滤掉,注意中间的空格要保留。例如,字符串\的过滤结果是\。 字符串法 #include int a[10];//数组定义并初始化为0 int maxnum=10; int n=0;//当前已插入个数为0 bool result; int x = 0; srand((unsigned int)time(NULL)); cout<<\生成随机数:\\n\do{ x=rand()0; cout< result=insertSort(a,maxnum,n,x); n++; cout<<\数组打印:\\n\for(int i=0;i cout< cout< }while(result); cout< #include void main() { } 指针法 #include char * trim(char *str); char * trim(char * s) { if (s == NULL) return NULL; int i=0; int j=0; int k=0; while(*(str+i)==' ')i++; j=strlen(str); cout< while(str[j-1]==' '&&j>0)j--; for(;i<=j;i++) { *(str+k)=str[i]; char str[20]; cin.getline(str, 20); trim(str); cout< while(str[i]==' ')i++; j=strlen(str); while(str[j-1]==' ')j--; for(;i str[k]='\\0'; str[k]=str[i]; k++;