C和指標第四章編程練習

來源:互聯網
上載者:User

標籤:c和指標第四章編程練習

1>正數n的平方根可以通過計算一系列近似值來獲得,每個近似值都比前一個更加接近準確值。第一個近似值是1,接下來的近似值則通過下面的公式來獲得。編寫一個程式,讀入一個值,計算並列印它的平方根。

int main(){     float a = 1 ;     float b = 0;     float n = 0;     printf("請輸入:\n");     scanf("%f", &n);     if (n <= 0)     {        printf("輸入數字有誤!");     }     else     {         do         {           b = a;           a = (b + n / b) / 2;           printf("%f\n", a);         } while (b != a);         printf("平方根為:%f\n", a);     }     system("pause");     return 0;}

2.一個整數如果只能被它本身和1整除,它就被稱為質數,請編寫一個程式,列印出1-100之間的所有質數

int main(){     int i = 0;     int j = 0;       int k = 0;     for (i = 3; i <= 100; i+=2)//偶數不可能為質數     {        k = (int)sqrt(i);         for (j = 2; j <= k; j++)//注意j的範圍,可以提高計算效率        {             if (i%j == 0)               break;         }        if (i%j != 0)             printf("%d\n", i);   }    system("pause");   return 0;}

3.等邊三角形的三條邊長度都相等,但等腰三角形只有兩條邊長度是相等的。如果三角形的三條邊長度都不相等,就稱為不等邊三角形
請編寫一個程式,提示使用者輸入三個數,分別表示三角形三條邊的長度,然後由程式判斷它是什麼類型的三角形。
提示:除了邊的長度是否相等外,程式是否還應考慮一些其他東西?

int main(){    int a = 0;    int b = 0;    int c = 0;    printf("請輸入:\n");    scanf("%d%d%d", &a, &b, &c);    if ((a + b > c&&a - b<c) || (a + c>b&&a - c<b) || (b + c>a&&b - c < a))   {         if ((a == b) || (a == c) || (b == c))             printf("這是一個等腰三角形\n");         if ((a == b) && (b == c))             printf("這是一個等邊三角形\n");         else             printf("這是一個不等邊三角形\n");   }   else   {      printf("這三條邊不能構成三角形!\n");   }  system("pause");  return 0;}

4.編寫函數copy_n,它的函數原型如下:void copy_n(char dst[],char src[],int n);
這個函數用於把一個字串從數組src複製到dst,但有如下要求:必須正好複製n個字元到dst,不能多,也不能少。如果src字串的長度小於n,你必須在複製後的
字串尾部補充足夠的NUL字元,使它的長度正好為n。如果src的長度長於或等於n,那麼你在dst中儲存了n個字元便可停止。此時,數組dst將不是以NUL結尾。
注意調用copy_n時,它應該在dst[0]至dst[n-1]的空間中儲存一些東西,但也只限於那些位置,這與src的長度無關。

void copy_n(char dst[], char src[], int n){    int n_copyed = 0;//已經複製的位元組數    while (*src != ‘\0‘)//注意*src    {        if (n_copyed == n)//n<sizeof(src)        {            break;        }        *dst++ = *src++;        n_copyed++;    }    if (n_copyed < n)//n>sizeof(src)    {        int i = 0;        for (i = 0; i < (n - n_copyed); i++)        {            *dst = ‘\0‘;            dst++;        }    }}int main(){    char dst[10] = { 0 };    char src[10] = { "asdf" };    int n = 3;    copy_n(dst, src, n);    printf(dst);    system("pause");    return 0;}

5.編寫一個程式,從標準輸入一行一行地讀取文本,並完成如下任務:如果檔案中有兩行或更多行相鄰的常值內容相同,那麼就列印出其中一行,其餘的行不列印,
你可以假設檔案中的文本行在長度上不會超過128個字元。
提示:使用gets函數讀取輸入行,使用strcpy來複製它們,strcmp對其進行比較,相等函數返回0,不相等返回非0值

This is the first lineAnother lineAnd anotherAnd anotherAnd anotherAnd anotherStill moreAlmost done now--Almost done now--Another lineStill moreFinished
#define MAX_LINE 20 //最大處理行數#define MAX_COLS 128//最大處理列數//處理輸入行資料void range(char input[MAX_LINE][MAX_COLS], char output[MAX_LINE], int i){      int flag =0;                   while (flag <= i)      {            if (strcmp(input[flag], input[flag + 1]) == 0)            {                strcpy(output, input[flag]);                                  if (strcmp(output, input[flag + 2]) != 0)                     printf("%s\n", output);           }           flag++;      }}int main(){      char input[MAX_LINE][MAX_COLS] = { 0 };         //存放輸入行的       char output[MAX_LINE] = { 0 };                  //存放輸出行      int i=0;      //把輸入行依次存入input數組中用ctrl+z結束輸入      while (gets(input[i]) != NULL)      {           i++;      }      range(input, output, i);//調用了range函數      system("pause");      return 0;}

6.請編寫一個函數,它從一個字串中提取一個字串。函數的原型應該如下:

int substr(char dst[],char src[],int start,int len);

函數的任務是從src數組起始位置向後位移start個字元的位置開始,最多複製len個非NULL字元到dst數組。複製完畢後,dst數組必須以NULL位元組結尾
函數的傳回值是儲存於dst數組中的字串長度。
如果,start所指定的位置越過了src數組的尾部,或者start或len的值為負,那麼複製到dst數組的是個Null 字元串。

 #include <assert.h>int substr(char dst[], char src[], int start, int len){     assert(dst);     assert(src);     char *p = src + start;//指標變數指向要提取字串的地址     int i = 0;     int n = strlen(p);//start後面字串的長度     if (n < len)     len = n;     while (len)     {        dst[i] = src[start-1];        i++;        len--;        start++;     }     dst[i + 1] = ‘\0‘;     i = i + 1;     return i;}int main(){     char src[20] = "heloworld";     char dst[20] = { 0 };     int len = 6;     int start = 1;     substr(dst, src, start, len);     printf("%s", dst);     system("pause");     return 0;}

7.編寫一個函數,從一個字串中除去多餘的空格。函數原型:

void deblank(char string[])

當函數發現字串中如果有一個地方由一個或者多個連續的空格組成,就把它們改成單個空白字元。注意當你遍曆整個字串是要確保它以NUL字元結尾

void deblank(char string[]){     int i = 0;     int len = 0;     while (1)     {         string[i] = getchar();         if (string[i] == ‘\n‘)            break;         i++;     }     len = i;     for (i = 0; i < len; i++)     {         if (string[i] == ‘ ‘ && string[i - 1] == ‘ ‘)             continue;         putchar(string[i]);     }}int  main(){     char c[120];     printf("請輸入:\n");     deblank(c);     system("pause");     return 0;}

 

本文出自 “Sean” 部落格,請務必保留此出處http://seann.blog.51cto.com/11126490/1740215

C和指標第四章編程練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.