Compare commits
2 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
2315ea7278 | 6 years ago |
|
|
95458cbb4d | 6 years ago |
@ -1,205 +0,0 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
|
||||
|
||||
typedef struct{ //定义数据类型
|
||||
int max, min;
|
||||
} Data;
|
||||
|
||||
int MIN; //全部变量
|
||||
|
||||
//通过函数传回最大值,通过全局变量传回最小值
|
||||
int fun1(int a[], int n){
|
||||
int i, max;
|
||||
max = MIN = a[0];
|
||||
for(i=0; i<n; i++){
|
||||
if(a[i]>max)
|
||||
max = a[i];
|
||||
if(a[i]<MIN)
|
||||
MIN = a[i];
|
||||
}
|
||||
return(max);
|
||||
}
|
||||
|
||||
//通过数组传回最大最小值
|
||||
int *fun2(int a[], int n){
|
||||
static int b[2];
|
||||
b[0] = b[1] = a[0];
|
||||
int i;
|
||||
for(i=1; i<n; i++){
|
||||
if(a[i] > b[0])
|
||||
b[0] = a[i];
|
||||
if(a[i] < b[1])
|
||||
b[1] = a[i];
|
||||
}
|
||||
return(b);
|
||||
}
|
||||
|
||||
//通过结构体指针传回最大最小值
|
||||
Data *fun3(int a[], int n){
|
||||
Data *p;
|
||||
int i;
|
||||
p = (Data *)malloc(sizeof(Data));
|
||||
p->max = p->min = a[0];
|
||||
for(i=1; i<n; i++){
|
||||
if(a[i] > p->max)
|
||||
p->max = a[i];
|
||||
if(a[i] < p->min)
|
||||
p->min = a[i];
|
||||
}
|
||||
return(p);
|
||||
}
|
||||
|
||||
//通过结构体传回最大最小值
|
||||
Data fun4(int a[], int n){
|
||||
Data p;
|
||||
int i;
|
||||
p.max = p.min = a[0];
|
||||
for(i=1; i<n; i++){
|
||||
if(a[i] > p.max)
|
||||
p.max = a[i];
|
||||
if(a[i] < p.min)
|
||||
p.min = a[i];
|
||||
}
|
||||
return (p);
|
||||
}
|
||||
|
||||
//通过指针传回最大最小值
|
||||
void fun5(int a[], int n, int *p, int *q){
|
||||
int i ;
|
||||
*p = *q = a[0];
|
||||
for(i=1; i<n; i++){
|
||||
if(*p < a[i])
|
||||
*p = a[i];
|
||||
if(*q > a[i])
|
||||
*q = a[i];
|
||||
}
|
||||
}
|
||||
|
||||
main(){
|
||||
int a[10] = {1,3,9,8,4,2,5,0,7,6}, max, *p;
|
||||
Data *q;
|
||||
Data z;
|
||||
int x, y;
|
||||
|
||||
max = fun1(a,10);
|
||||
printf("\n\n\t fun1: max == %d \t min == %d",max,MIN);
|
||||
|
||||
p = fun2(a,10);
|
||||
printf("\n\n\t fun2: max == %d \t min == %d",p[0],p[1]);
|
||||
|
||||
q = fun3(a,10);
|
||||
printf("\n\n\t fun3: max == %d \t min == %d",q->max,q->min);
|
||||
|
||||
z = fun4(a,10);
|
||||
printf("\n\n\t fun4: max == %d \t min == %d",z.max,z.min);
|
||||
|
||||
fun5(a,10,&x,&y);
|
||||
printf("\n\n\t fun5: max == %d \t min == %d",x,y);
|
||||
}
|
||||
|
||||
|
||||
////两个整数的值互换,局部有效
|
||||
//void swap1(int a, int b){
|
||||
// int c;
|
||||
// c = a;
|
||||
// a = b;
|
||||
// b = c;
|
||||
// printf("\n\n\t swap1: a = %d, b = %d",a,b);
|
||||
//}
|
||||
//
|
||||
////两个整数的地址互换,全局有效
|
||||
//void swap2(int *a, int *b){
|
||||
// int c;
|
||||
// c = *a;
|
||||
// *a = *b;
|
||||
// *b = c;
|
||||
//}
|
||||
//
|
||||
//main(){
|
||||
// int x = 100,y = 800;
|
||||
// swap1(x,y);
|
||||
// printf("\n\n\t swap1: x = %d, y = %d",x,y); //x,y的值不变
|
||||
// x = 100, y = 800;
|
||||
// swap2(&x,&y);
|
||||
// printf("\n\n\t swap2: x = %d, y = %d",x,y); //x,y的值改变
|
||||
//}
|
||||
|
||||
////定义复数的数据类型
|
||||
//typedef struct {
|
||||
// float realpart;
|
||||
// float imagpart;
|
||||
//}Complex;
|
||||
//
|
||||
////函数声明
|
||||
//Complex create(float x, float y); //功能:构建复数
|
||||
//Complex add(Complex z1, Complex z2); //功能:两个复数相加
|
||||
//
|
||||
////函数定义
|
||||
//Complex create(float x, float y){
|
||||
// Complex z;
|
||||
// z.realpart = x;
|
||||
// z.imagpart = y;
|
||||
// return(z);
|
||||
//}
|
||||
//
|
||||
//Complex add(Complex z1, Complex z2){
|
||||
// Complex sum;
|
||||
// sum.realpart = z1.realpart + z2.realpart;
|
||||
// sum.imagpart = z1.imagpart + z2.imagpart;
|
||||
// return(sum);
|
||||
//}
|
||||
//
|
||||
////主函数
|
||||
//main(){
|
||||
// float a,b;
|
||||
// Complex c1,c2,c3;
|
||||
// printf("\n\n\n\t Input realpart and imagpart:"); //要求输入第一个复数的实部和虚部,用空格隔开
|
||||
// scanf("%f %f",&a,&b);
|
||||
// c1 = create(a,b); //构建第一个复数
|
||||
// printf("\n\n\n\t Input realpart and imagpart:"); //要求输入第二个复数的实部和虚部,用空格隔开
|
||||
// scanf("%f %f",&a,&b);
|
||||
// c2 = create(a,b); //构建第二个复数
|
||||
// c3 = add(c1,c2); //两个复数相加
|
||||
// //输出结果
|
||||
// printf("\n\n\n\t c1 == %f + %fi",c1.realpart,c1.imagpart);
|
||||
// printf("\n\n\n\t c2 == %f + %fi",c2.realpart,c2.imagpart);
|
||||
// printf("\n\n\n\t c3 == c1 + c2 == %f + %fi",c3.realpart,c3.imagpart);
|
||||
//}
|
||||
|
||||
//int main(){
|
||||
// int sum=0, avg=0, i=0;
|
||||
// int t[10] = {80,85,77,56,68,83,90,92,80,98};
|
||||
// for(i=0; i<10; i++)
|
||||
// {
|
||||
// sum += t[i];
|
||||
// }
|
||||
// avg = sum/10;
|
||||
// printf("sum=%d\navg=%d\n",sum,avg);
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
//int main(){
|
||||
// int sum, avg;
|
||||
// int t1=80, t2=85, t3=77, t4=56, t5=68, t6=83, t7=90, t8=92, t9=80, t10=98;
|
||||
// sum = t1+t2+t3+t4+t5+t6+t7+t8+t9+t10;
|
||||
// avg = sum/10;
|
||||
// printf("sum=%d\navg=%d\n",sum,avg);
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
//int main(){
|
||||
// int n = 3;
|
||||
// float x, max = 0;
|
||||
// for(int i=1; i<=n; i++)
|
||||
// {
|
||||
// scanf("%f", &x);
|
||||
// if(x>max)
|
||||
// max = x;
|
||||
// }
|
||||
// printf("%f",max);
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue