C and pointer (pointers on C) -- Chapter 4: Answers to the exercises of the statements (below)

Source: Internet
Author: User

Please see http://download.csdn.net/download/wangpegasus/5701765

In chapter 4

1,

#include "stdafx.h"double sqrt(double temp){double before, after;before = 1.0;after = 1.0;do{before = after;after = (before + temp/before)/2;} while (before != after);return after;}int _tmain(int argc, _TCHAR* argv[]){double temp,result;puts("input N:");scanf_s("%lf",&temp);result = sqrt(temp);printf("sqrt N = %lf \n",result);return 0;}
2,

#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]){int length = 100;puts("Prime(1~100):\n");for (int i = 1; i <= length; i++){int ALU = 0;for (int j =1; j <= i; j++){if (i%j == 0){ALU++;}}if (ALU == 2){printf("%d\t", i);}}return 0;}
3,

# Include "stdafx. H "int _ tmain (INT argc, _ tchar * argv []) {int A, B, C; puts (" input three-sided length (A, B, C ): \ n "); do {scanf_s (" % d, % d, % d ", & A, & B, & C) ;}while (A + B) <= c | (A + C) <= B | (B + C) <= A); If (A = B) & (B = C) {puts ("equals edge! ");} Else if (A = B) | (B = C) | (C = A) {puts ("! ");} Else {puts (" not equal! ");} Return 0 ;}

4. 2b method:

void copy_n( char dst[], char src[], int n ){int i,l_src;l_src = 0;for (i = 0; dst[i] == '\0'; i++){l_src++;}if (l_src < n){for (i = 0; i < l_src; i++){dst[i] = src[i];}for (i = l_src; i < n; i++){dst[i] = '\0';}}else{for (i = 0; i < n; i++){dst[i] = src[i];}}}

Correct practice:

void copy_n( char dst[], char src[], int n ){int dst_index, src_index;src_index = 0;for (dst_index = 0; dst_index < n; dst_index++){dst[dst_index] = src[src_index];if (src[src_index] != 0){src_index++;}}}
5. Too many strings are involved, slightly.
6,

void substr(char dst[], char src[], int start, int len){int dst_index, src_index, l_src;l_src =src_index = 0;do{l_src++;} while (src[l_src] != '\0' );if (start < 0 || len < 0 || start > l_src ++){dst[0] = '\0';}else{for ( dst_index = 0; dst_index < len; dst_index++){dst[dst_index] = src[src_index + start];if (src[src_index] != '\0'){src_index++;}}}}

7,

void deblank(char string[]){char copy_str[] = {'\0'};int str_index, copy_str_index, temp = 0;for ( str_index = 0; string[str_index] != '\0'; str_index++){if (string[str_index] == ' '){if (temp > 0){continue;}else{copy_str_index++;copy_str[copy_str_index] = string[str_index];temp++;}}else{copy_str_index++;copy_str[copy_str_index] = string[str_index];}}copy_str[copy_str_index++] = '\0';for ( copy_str_index = 0; copy_str[copy_str_index] != '\0'; copy_str_index++){string[copy_str_index]= copy_str[copy_str_index];}string[copy_str_index + 1] = '\0';}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.