声明:本文章公开旨意在为解题提供思路,若有要上交的作业,严禁复制本文中代码直接上传(老师有办法和习惯查重复作业)
2018.6.28 开了C语言的课,将作业代码发在这里,以便上课打的代码方便记录,下课打的代码上课能看到
7.4, lec 1
EX1
// 比较三个数字大小然后按正序和倒序输出
//by Gallien
#include <stdio.h>
void swap(float *m,float *n); //交换数字的函数
int main(void)
{
float a[3];
printf("Please enter 3 numbers:\n");
scanf("%f",&a[0]);
scanf("%f",&a[1]);
scanf("%f",&a[2]);
if (a[2]<a[1])
swap(&a[2],&a[1]);
if (a[1]<a[0])
swap(&a[1],&a[0]);
if (a[2]<a[1])
swap(&a[2],&a[1]);
printf("Order from small to big:%g %g %g\n",a[0],a[1],a[2]);
printf("Order from big to small:%g %g %g\n",a[2],a[1],a[0]);//输出排序后的数字
return 0;
}
void swap(float *m,float *n)2
3
{
float c;
c = *m;
*m = *n;
*n = c;
}
EX2
#include <stdio.h>
int main(void)
{
double A,B,C;
printf("We will swap two numbers A and B after you enter them\n\n");
printf("Please enter the number of A:\n");
scanf("%lf",&A);
printf("Please enter the number of B:\n");
scanf("%lf",&B); //数字录入
printf("Before swap:\nA=%g\nB=%g\n",A,B);
C=A;
A=B;
B=C; //交换数字
printf("After swap:\nA=%g\nB=%g\n",A,B);
return 0;
}
EX3
#include <stdio.h>
//定义三维空间点以及计算两点之间距离
double kaifang(double A);
//开平方函数
double abso(double A);
//绝对值函数
int main(void)
{
double x1,y1,z1,x2,y2,z2;
struct P_3D
{
double x;
double y;
double z;
};
printf("请输入第一个点的坐标:");
scanf("%lf %lf %lf",&x1,&y1,&z1);
printf("请输入第二个点的坐标:");
scanf("%lf %lf %lf",&x2,&y2,&z2);
struct P_3D A= {x1,y1,z1};
struct P_3D B= {x2,y2,z2};
printf("两个点坐标分别为: \nA:(%g,%g,%g) \nB:(%g,%g,%g)\n",A.x,A.y,A.z,B.x,B.y,B.z);
double Dfang =(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2);
double distance =kaifang(Dfang);
printf("两点之间的距离为:%f\n",distance);
return 0;
}
double kaifang(double A)
{
double x=0x5f375a86*A;
while(abso(x*x-A)>0.00000000001) x=(x+A/x)/2;
return x;
}
double abso(double A)
{
if(A<0) A=-A;
return A;
}
===============================================================================================
7.5, lec 2
EX1
while
#include <stdio.h>
//用户输入正整数n,计算从1到n中所有奇数的和,while
int main(void)
{
int n,i=1,sum=0;
printf("在该程序中,我们将计算从1到n中所有奇数之和并输出\n请输入你想计算的数对应的正整数n:\n");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i=i+2;
}
printf("\n1到n中所有奇数的和为%d\n",sum);
return 0;
}
for
#include <stdio.h>
//用户输入正整数n,计算从1到n中所有奇数的和,for
int main(void)
{
int n,i=1,sum=0;
printf("在该程序中,我们将计算从1到n中所有奇数之和并输出\n请输入你想计算的数对应的正整数n:\n");
scanf("%d",&n);
for(i=1;i<=n;i=i+2)
{
sum=sum+i;
}
printf("1到n中所有奇数的和为%d\n",sum);
return 0;
}
dowhile
#include <stdio.h>
//用户输入正整数n,计算从1到n中所有奇数的和,dowhile
int main(void)
{
int n,i=-1,sum=1; //设置的初始值使得第一次循环后i=1,sum=0
printf("在该程序中,我们将计算从1到n中所有奇数之和并输出\n请输入你想计算的数对应的正整数n:\n");
scanf("%d",&n);
do
{
sum=sum+i;
i=i+2;
}while (i<=n);
printf("\n1到n中所有奇数的和为%d\n",sum);
return 0;
}
EX2
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//利用随机数计算pi
int main(void)
{
int n=0,num=0,i=0;
double x,y,d;
printf("本程序将利用随机数计算pi的值\n请输入您想进行的实验的次数:");
scanf("%d",&n);
double pi;
while(++i<=n)
{
x=(double)rand()/RAND_MAX;
y=(double)rand()/RAND_MAX;
d=sqrt(x*x+y*y);
num=num+(d<=1);
}
pi=4*(double)num/n;
printf("本次运行程序计算出的pi的值为:%lf\n",pi);
}
===============================================================================================
7.12, lec 3
EX1
判断一个数是否为素数
//判断一个整数是否为素数
//程序有缺陷,会把1当成素数,懒得改了
//by Gallien
#include <stdio.h>
int IsPrime(int n);
int main(void)
{
int a;
printf("请输入您想判断是否为素数的整数:\n");
while(scanf("%d",&a)==1)
{
if(IsPrime(a))
printf("您输入的整数是素数\n请再输入一个您想判断的整数,或者输入q退出程序:\n");
else
printf("您输入的整数不是素数\n请再输入一个您想判断的整数,或者输入q退出程序:\n");
}
return 0;
}
int IsPrime(int n)
{
int index;
for(index=2;index*index<=n;index++)
if (n%index==0)
return 0;
return 1;
}
输出一个数的所有约数
//输出一个整数的所有约数
//by Gallien
#include <stdio.h>
int main(void)
{
int a,index;
printf("本程序可获得一个数的所有约数,请输入您的整数:");
scanf("%d",&a);
printf("%d的所有约数为:",a);
for(index=1;index<=a;index++)
if (a%index==0) printf("%d ",index);
printf("\n");
return 0;
}
求两个数的最大公约数
//求两个数的最大公约数
//by Gallien
#include <stdio.h>
int BigCommonDivisor(int m,int n); //求最大公约数的函数
int main(void)
{
int m,n;
printf("本程序可以计算两个正整数的最大公约数,请输入您的两个整数:");
scanf("%d",&m);
scanf("%d",&n);
int bcd=BigCommonDivisor(m,n);
printf("这两个整数的最大公约数为%d\n",bcd);
return 0;
}
int BigCommonDivisor(int m,int n) //求最大公约数的函数
{
int big,small;
if(m>n)
{
big = m;
small = n;
}
else
{
big = n;
small = m;
}
int yu = big%small; //余项
while(big%small!=0)
{
big=small;
small = yu;
yu = big-small;
}
return small;
}
EX2
计算并输出两个浮点数的和差积商
//计算并输出两个浮点数的和差积商
//by Gallien
#include <stdio.h>
float compute(float x,float y,char operator); //进行浮点数四则运算的函数
int main(int argc,char*argv[])
{
float a,b;
char oper;
scanf("%f%c%f",&a,&oper,&b);
printf("=%f\n",compute(a,b,oper));
return 0;
}
float compute(float x,float y,char operator) //进行浮点数四则运算的函数
{
if (operator==43) return (x+y);
else
if(operator==45) return (x-y);
else
if(operator==42) return (x*y);
else return (x/y);
}
EX3
递归算法求n阶Legendre多项式的值
//递归算法求n阶Legendre多项式的值
//by Gallien
#include <stdio.h>
float Legendre(int n,float x); //求n阶Legendre多项式的值的函数
int main(void)
{
int n;
float x;
printf("本程序可求n阶Legendre多项式的值P(x),请输入您要求的阶数和x的值:");
scanf("%d",&n);
scanf("%f",&x);
float resultat=Legendre(n,x);
printf("%d阶的x值为%g的Legendre多项式的值为:%g\n",n,x,resultat);
return 0;
}
float Legendre(int n,float x) //求n阶Legendre多项式的值的函数
{
if (n==0) return 1;
if (n==1) return x;
if (n>1)
{
float legendre[n]; //这个语句使用一个变量为 数组legendre定义大小,可能有些编译器不支持这种定义,若如此,按下面几行操作
//在最前声明头文件
//#include <stdlib.h> //调用给数组分配内存的malloc函数
//然后将float legendre[n];这一行替换为下面两行
//float *legendre; //声明一个指针,或者叫动态数组?我也不太懂
//legendre= malloc(sizeof(float) * n); //为legendre分配n个float值那么大的内存
legendre[0]=1;
legendre[1]=x; //为前两项赋值
int index; //声明一个循环计数器
for (index=2;index<=n;index++)
{
legendre[index]=((2*index-1)*x*legendre[index-1]-(index-1)*legendre[index-2])/index;
}
return legendre[n];
}
return 1000; //这句没用,但是不写他会提示我以上循环可能没有一个返回值,所以如果真的出现了1000,基本上是因为用户没按要求输入n与x,比如输了个负的n
}
求特殊的九位数
//求特殊的九位数,前n位能被n整除
//暴力法,有点蠢
//by Gallien
#include <stdio.h>
int main(void)
{
int wanted;
for (wanted=100000000;wanted<=999999999;wanted++)
if ((wanted/10000000)%2==0)
if ((wanted/1000000)%3==0)
if ((wanted/100000)%4==0)
if ((wanted/10000)%5==0)
if ((wanted/1000)%6==0)
if ((wanted/100)%7==0)
if ((wanted/10)%8==0)
if ((wanted/1)%9==0) printf("%d\n",wanted);
return 0;
}
7.13, Lec 4
//计算物理题,制表,貌似不用先创建一个data.txt
//by Gallien
#include <stdio.h>
#define LENGTH 1.
#define TIME 10.
#define DELTA_X 0.33
#define DELTA_T 0.1
#define K 1.0 //常数都设出来了,但是写完程序发现有些没用到,有点蠢
#define C 2.0
#define RHO 5.0
#define R 0.092 //一个常数,值为(K*DELTA_T)/(RHO*C*(DELTA_T^2),此处取0.092
#define TABLE_HENG ((int)(LENGTH/DELTA_X)+1) //表格横长
#define TABLE_SHU ((int)(TIME/DELTA_T)+1) //表格竖长
int main(void)
{
FILE *fp; //打开文件写入
fp= fopen("data.txt","w+");
float T[TABLE_HENG][TABLE_SHU];
int x,y; //用于定位数组(表格)
for (x=0;x<TABLE_HENG;x++)
T[x][0]=0.;
for (y=0;y<TABLE_SHU;y++)
{
T[0][y]=0.;
T[TABLE_HENG-1][y]=100.;
} //初值
//by Gallien
for(y=1;y<TABLE_SHU;y++) //一个循环计算一行的值
for (x=1;x<TABLE_HENG-1;x++) //整个循环计算某固定行从左到右的值
T[x][y]=R*(T[x-1][y-1]+T[x+1][y-1]-2*T[x][y-1])+T[x][y-1];
fprintf(fp,"time "); //打印12个空格
for(x=0;x<TABLE_HENG;x++)
fprintf(fp,"%12d",x);
for(y=0;y<TABLE_SHU;y++) //一个循环打印一行数据
{
fprintf(fp,"\n");
fprintf(fp,"t=%-10.1f",0.1*y);
for(x=0;x<TABLE_HENG;x++) //一个循环打印一个值,从左打到右
fprintf(fp,"%12.4f",T[x][y]);
}
fclose(fp); //关闭文件写入
printf("Ok!");
return 0;
}
7.16 Lec 5
//链表排序输出输入的整数
//by Gallien
#include <stdio.h>
#include <stdlib.h>
struct node
{
int n;
struct node * pNext;
};
int main(void)
{
struct node *pHead = NULL, *pEnd = NULL, *pNode = NULL, *Judge1 = NULL, *Judge2 = NULL, *oldHead=NULL;
int i = 1;
printf("Please input a integer:\n");
printf("en by inputing 0:");
p1:
do
{
scanf("%d",&i);
if(0!=i)
{
pNode = (struct node *)malloc(sizeof(struct node));
if(NULL!=pNode)
{
pNode->n=i;
pNode->pNext=NULL;
if(NULL == pHead)
{
pHead = pNode;
pEnd = pNode;
}
else
{ //要改的地方,老师的程序的这里是把新输入的数放到结尾,应改为判断位置然后插入
Judge1 = pHead; //利用两个指针Judge1,Judge2,byGallien来判断应该插入在哪里
if (pHead->n<i)
{
if (pHead==pEnd) //如果pHead=pEnd,那么链表中此时只有一个数,且前置条件为链表中的那个数小于pNode中存的数
{
pHead->pNext=pNode; //故这里操作为把pNode加在pHead后面
pEnd=pNode;
continue;
}
else
Judge2=pHead->pNext;
}
else
{
oldHead = pHead; //若pNode中的数比pHead中的还小,只需加在pHead前就行
pHead = pNode;
pHead->pNext = oldHead;
continue;
}
while (Judge2->n<i) //此循环用于定位到底该插在哪里,当结构中的n有如下关系时停止:Judge1<pNode<=Judge2
{
if (Judge2 == pEnd) //特殊情况,若pNode比链表中所有数都大,我们就把它接在链表后面
{
pEnd->pNext = pNode;
pEnd = pNode;
goto p1; //因为符合两个Judge中间的插入操作接在循环后面,故特殊情况接在结尾后应跳转至总循环前,故用goto
}
else
{
Judge1 = Judge2; //把Judge1,Judge2往后推一位的操作
Judge2 = Judge2->pNext;
}
}
Judge1->pNext = pNode; //当循环结束的时候,若到了这里,说明已经找到了插入的地方,插入即可
pNode->pNext = Judge2;
} //修改结束
}
}
}while(i!=0);
pNode = pHead;
while(pNode!=NULL)
{
printf("%d\t",pNode->n);
pHead = pNode;
pNode = pNode->pNext;
free(pHead);
}
printf("\n");
return 0;
}