C語言作業

聲明:本文章公開旨意在爲解題提供思路,若有要上交的作業,嚴禁複製本文中代碼直接上傳(老師有辦法和習慣查重複作業)

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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章