命令提示符在什么地方:C程序救救...

来源:百度文库 编辑:中科新闻网 时间:2024/05/08 04:44:46
下列各题请用C语言编辑:
1.求方程ax2+bx+c=0(2为上标,即a*x*x)根,用3个函数分别求当b*b-4ac大于0、等于0和小于0时的根并输出结果。从主函数输入a、b、c的值。

2.写一函数,使给定的一个二维整形数组(3×3)转置,即行列互换。

3.编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果。

3个题的答案在一个程序里,次序是3,2,1.

题1的函数是 f1(),f2(),f3()
题2的函数是 trans()
题3的函数是 stat1()

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double b4ac;
void f1(double a,double b,double c){
double r1,r2,d;
d = sqrt(b4ac);
r1 = (-b + d ) / 2.0 / a;
r2 = (-b - d) / 2.0 / a;
printf("r1 = %lf, r2 = %lf\n", r1,r2);
};

void f2(double a,double b,double c){
double r1,r2;
r1 = (-b ) / 2.0 / a;
printf("r1 = %lf, r2 = %lf\n", r1,r1);
};

void f3(double a,double b,double c){
double r1,r2,d;
d = sqrt(fabs(b4ac));
r1 = -b / 2.0 / a;
r2 = d / 2.0 / a;
printf("r1 = %lf + i %lf, r2 = %lf - i %lf\n", r1,r2,r1,r2);
};

void trans( int a[3][3], int b[3][3]){
int i,j;
int t;
for (i=0;i<3;i++){
for (j=0;j<3;j++){
b[i][j] = a[j][i];
}}
}

void stat1(char *str,int *l, int *n, int *sp, int *others){
int i,L;
int c;
L = strlen(str);
*l = 0; *n = 0; *sp = 0; * others = 0;
for (i=0;i<L;i++){
sscanf(str+i,"%c",&c);
c = c & 0xff;
if (c >= '0' && c <= '9') {*n = *n + 1;}
else if ( (c>= 'a' && c <= 'z') || (c>= 'A' && c <= 'Z')){
*l = *l + 1;
} else if ( c == ' '){
*sp = *sp + 1;
} else {
*others = *others + 1;
}
}
}

main()
{
int i,j;
double a,b,c;
int aa[3][3] = {1,2,3,4,5,6,7,8,9};
int bb[3][3];
char str[80];
int l,n,sp,others;

// 33333333333
printf("Enter your string\n");
gets(str);
(void) stat1(str,&l, &n, &sp, &others);
printf("Your string: %s\n",str);
printf("Letters: %d\n",l);
printf("Numbers: %d\n",n);
printf("Spaces: %d\n",sp);
printf("Others: %d\n",others);

// 222222222
printf("before\n");
(void) trans(aa,bb);
for (j=0;j<3;j++){
for (i=0;i<3;i++){
printf("%d ",aa[i][j]);
};
printf("\n");
};
printf("after\n");
for (j=0;j<3;j++){
for (i=0;i<3;i++){
printf("%d ",bb[i][j]);
};
printf("\n");
};

// 111111111
Lab1:
printf("Enter a,b,c please:\n");
scanf("%lf %lf %lf",&a,&b,&c);

if (a == 0.0){
printf("\007a must != 0.0\n");
goto Lab1;
}

b4ac = b*b - 4.0 * a * c;
if (b4ac > 0.0){
(void) f1(a,b,c);
} else if (b4ac < 0.0){
(void) f3(a,b,c);
} else {
(void) f2(a,b,c);
};
exit(0);
}