《實驗四》

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

void jfc(double a,double b,double c);
int main()
{
double a,b,c;
printf("請輸入一元二次方程組的系數(shù)a,b,c: ");
while(scanf("%lf%lf%lf",&a,&b,&c))
{
jfc(a,b,c);
printf("\n請繼續(xù)輸入三個系數(shù),我將繼續(xù)解方程:");
}
system("pause");
return 0;
}
void jfc(double a,double b,double c)
{
double x1,x2,det,d;
d=b*b-4*a*c;
if(d>=0)
{
det=sqrt(b*b-4*a*c);
x1=(-b+det)/2*a;
x2=(-b-det)/2*a;
if(det==0)
printf("此方程有兩個相同的實根x1=x2=%lf",x1);
else
printf("此方程的兩個實根是:x1=%lf,x2=%lf",x1,x2);
}
else
printf("這個方程沒有實根");
}

 

 《打印坐標》

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
void pri(int x,int y);
int main()
{
system("color 02");
int x,y;
printf("請輸入坐標:");
while(scanf("%d%d",&x,&y))
{
pri(x,y);
Sleep(3000);
system("cls");
printf("請繼續(xù)輸入坐標:");
}
system("pause");
return 0;
}
void pri(int x,int y)
{
int i;
for(i=1;i<=y-1;i++)
printf(".\n");
for(i=1;i<=x-1;i++)
printf(". ");
printf("○");
}

 

 

 

《素數(shù)》

#include<stdio.h>
#include<stdlib.h>
#define N 1000
int find(int x,int y,int bb[N])
{
int i,j,k=0,flag;
for(j=x;j<=y;j++)
{
flag=1;
for(i=2;i<j;i++)
if(j%i==0)
{
flag=0;
break;
}
if(flag==1)
bb[k++]=j;
}
return k;
}
int main()
{
int n,m,i,k,bb[N];

printf("請輸入兩個整數(shù):");
scanf("%d%d",&n,&m);

for(i=0;i<m-n;i++)
bb[i]=0;
k=find(n,m,bb);

for(i=0;i<k;i++)
printf("%4d",bb[i]);

system("pause");
return 0;
}

 

 

《矩陣》

#include<stdio.h>
#include<stdlib.h>
#define N 100
void fun(int x[N][N],int n) {
int i,j;

for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i>=j)
x[i][j]=i;
else
x[i][j]=j ;
}

int main() {
int n,i,j,a[N][N];

scanf("%d",&n);

fun(a,n);

for(i=1;i<=n;i++) {
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}

system("pause");
return 0;
}

 

 

 

《整除求偶》

#include <stdio.h>
#include <stdlib.h>
void fun(int k,int bb[]) {
int i;
int j=0;

for(i=1;i<=k;i++) {
if(k%i==0&&i%2==0)
bb[j++]=i;
}

for(i=j-1;i>=0;i--)
printf("%d ",bb[i]);
}

int main() {
int x, *t;

scanf("%d", &x);
t = (int *) malloc(sizeof(int)*x);

fun(x,t);

system("pause");
return 0;
}

 

 《冒泡排序》

#include <stdio.h>
#include <stdlib.h>
const int N=4;
void output(char x[], int n);
void paixu(char x[]);
int main() {
char string[N] = {'2','0','1','9'};
int i;

printf("排序前: \n");
output(string, N);

// 調(diào)用排序函數(shù)對字符數(shù)組中的字符由大到小排序
// 補足代碼2
// 。。。

printf("\n排序后: \n");
paixu(string);
output(string, N);

printf("\n");

system("pause");
return 0;
}
void output(char x[], int n) {
int i;

for(i=0; i<N; i++)
printf("%c", x[i]);
}
void paixu(char x[])
{
int i,j,t;
for(i=1;i<N;i++)
{
for(j=0;j<N-1;j++)
{
if(x[j]<=x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
}
}