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 line Another line And another And another And another And another Still more Almost done now-- Almost done now-- Another line Still more Finished
#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數組的是個空字符串。
#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; }