天天看點

讀書筆記-2019年03月05日

文章目錄

    • 今日見聞
    • 今日重要的三件事
    • 記錄
      • 科學上網:[Flutter: the good, the bad and the ugly – The ASOS Tech Blog – Medium](https://medium.com/asos-techblog/flutter-vs-react-native-for-ios-android-app-development-c41b4e038db9)
      • CodeList 4-1 at P
      • CodeList 4-2 at P
      • CodeList 4-3 at P
      • CodeList 4-4 at P
      • CodeList 4-5 at P
      • CodeList 4-6 at P
      • CodeList 4-7 at P
      • CodeList 4-8 at P
      • CodeList 4-9 at P
      • CodeList 4-10 at P
      • CodeList 4-11 at P
      • CodeList 4-12 at P
      • CodeList 4-13 at P76
      • CodeList 4-14 at P
      • CodeList 4-15 at P78
      • CodeList 4-16 at P
      • CodeList 4-17 at P

今日見聞

入門教程和文檔 | Visual Studio - Visual Studio

今日重要的三件事

  • 完成CPP第四章CodeList
  • 工作需求要完成
  • 實戰xcode開發App第三章

記錄

科學上網:Flutter: the good, the bad and the ugly – The ASOS Tech Blog – Medium

讀書筆記-2019年03月05日

CodeList 4-1 at P

/* talkback.c -- 一個能為您提供一些資訊的對話程式 */
#include <stdio.h>
#include <string.h>  // strlen()函數原型在這裡
#define DENSITY 62.4 //人體密度(機關: 磅/立方英尺)
int main(void)
{
    float weight, volume;
    int size, letters;
    char name[40]; // name是一個有40個字元的數組.
    printf("Hi! What`s your first name?\n");
    scanf("%s", name);
    printf("%s, what`s your weight in pounds?\n", name);
    scanf("%f", &weight);
    size = sizeof name;
    letters = strlen(name);
    volume = weight / DENSITY;
    printf("Well! %s, your volume is %2.2f cubic feet.\n", name, volume);
    printf("Also, your first name has %d letters ", letters);
    printf("and we have %d bytes to stores it.\n", size);
    return 0;
}
           
Panda-MBP:CodeList4-1 panda8z$ gcc -o talkback.out talkback.c 
Panda-MBP:CodeList4-1 panda8z$ ./talkback.out 
Hi! What`s your first name?
panda
panda, what`s your weight in pounds?
40
Well! panda, your volume is 0.64 cubic feet.
Also, your first name has 5 letters and we have 40 bytes to stores it.
Panda-MBP:CodeList4-1 panda8z$ 
           

CodeList 4-2 at P

讀書筆記-2019年03月05日
/* praise1.c -- 使用不同類别的字元串 */
#include <stdio.h>
#define PRAISE "What a super marvelous name!"
int main(void)
{
    char name[40];
    printf("What`s your name?\n");
    scanf("%s", name);
    printf("Hello %s,  %s\n", name, PRAISE);
    return 0;
}
           
Panda-MBP:CodeList4-2 panda8z$ gcc -o praise1.out praise1.c 
Panda-MBP:CodeList4-2 panda8z$ ./praise1.out 
What`s your name?
panda
Hello panda,  What a super marvelous name!
Panda-MBP:CodeList4-2 panda8z$ 

           

CodeList 4-3 at P

/* praise2.c  */
#include <stdio.h>
#include <string.h> // strlen() is here
#define PRAISE "What a super marvelous name!"
int main(void)
{
    char name[40];

    printf("What`s your name?\n");
    scanf("%s", name);
    printf("Hello, %s. %s\n", name, PRAISE);
    printf("Your name of %d letters occupies %d memory cells.\n",strlen(name), sizeof name);
    printf("The phrase of prasie has %d letters",strlen(PRAISE));
    printf("and occupies %d memory cells.\n", sizeof(PRAISE));
}
           
Panda-MBP:CodeList4-3 panda8z$ gcc -o praise2.out praise2.c 
praise2.c:12:66: warning: format specifies type 'int' but the
Panda-MBP:CodeList4-3 panda8z$ gcc -o praise2.out praise2.c 
praise2.c:12:66: warning: format specifies type 'int' but the
      argument has type 'unsigned long' [-Wformat]
  ...%d letters occupies %d memory cells.\n",strlen(name)...
     ~~                                      ^~~~~~~~~~~~
     %lu
praise2.c:12:80: warning: format specifies type 'int' but the
      argument has type 'unsigned long' [-Wformat]
  ...%d memory cells.\n",strlen(name), sizeof name);
     ~~                                ^~~~~~~~~~~
     %lu
praise2.c:13:50: warning: format specifies type 'int' but the
      argument has type 'unsigned long' [-Wformat]
  ...phrase of prasie has %d letters",strlen(PRAISE));
                          ~~          ^~~~~~~~~~~~~~
                          %lu
praise2.c:14:47: warning: format specifies type 'int' but the
      argument has type 'unsigned long' [-Wformat]
  ...occupies %d memory cells.\n", sizeof(PRAISE));
              ~~                   ^~~~~~~~~~~~~~
              %lu
4 warnings generated.
Panda-MBP:CodeList4-3 panda8z$ ./praise2.out 
What`s your name?
panda panda
Hello, panda. What a super marvelous name!
Your name of 5 letters occupies 40 memory cells.
The phrase of prasie has 28 lettersand occupies 29 memory cells.
Panda-MBP:CodeList4-3 panda8z$ 

           

CodeList 4-4 at P

/* pizza.c -- 在這個匹薩餅的例子中使用定義常量 */
#include <stdio.h>
#define PI 3.14159
int main(void)
{
    float area, circum, radius;

    printf("What is the radius of your pizza?\n");
    scanf("%f", &radius);
    area = PI * radius * radius;
    circum = 2.0 * PI * radius;
    printf("Your basic pizza parameters are as follows: \n");
    printf("circumference = %1.2f, area = %1.2f\n", circum, area);
    return 0;
}
           

這個編譯說錯誤是因為我的printf語句不正常,确實寫錯了

Panda-MBP:CodeList4-4 panda8z$ gcc -o pizza.out pizza.c 
pizza.c:13:5: warning: implicit declaration of function
      'printd' is invalid in C99
      [-Wimplicit-function-declaration]
    printd("circumference = %1.2f, area = %1.2f\n", c...
    ^
1 warning generated.
Undefined symbols for architecture x86_64:
  "_printd", referenced from:
      _main in pizza-e528df.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v 
to see invocation)

           

正确的輸出

Panda-MBP:CodeList4-4 panda8z$ gcc -o pizza.out pizza.c 
Panda-MBP:CodeList4-4 panda8z$ ./pizza.out 
What is the radius of your pizza?
12
Your basic pizza parameters are as follows: 
circumference = 75.40, area = 452.39
Panda-MBP:CodeList4-4 panda8z$ 
           

CodeList 4-5 at P

/* defines.c -- 使用limit.h和floath中定義的常量 */
#include <stdio.h>
#include <limits.h> //整數限制
#include <float.h>  //浮點數限制
int main(void)
{
    printf("Some number limits for this system: \n");
    printf("Biggest int: %d\n", INT_MAX);
    printf("Smallest unsigned long: %lld\n", LLONG_MIN);
    printf("One byte = %d bits on this system.\n", CHAR_BIT);
    printf("Largest double: %e.\n", DBL_MAX);
    printf("Smallest normal float: %e\n", FLT_MIN);
    printf("float pricision = %d digits\n", FLT_DIG);
    printf("float epsilon = %e\n", FLT_EPSILON);
    return 0;
}
           
Panda-MBP:CodeList4-5 panda8z$ gcc -o defines.out defines.c 
Panda-MBP:CodeList4-5 panda8z$ ./defines.out 
Some number limits for this system: 
Biggest int: 2147483647
Smallest unsigned long: -9223372036854775808
One byte = 8 bits on this system.
Largest double: 1.797693e+308.
Smallest normal float: 1.175494e-38
float pricision = 6 digits
float epsilon = 1.192093e-07
Panda-MBP:CodeList4-5 panda8z$ 

           

CodeList 4-6 at P

/* printout.c -- 使用轉換說明符 */
#include <stdio.h>
#define PI 3.141593
int main(void)
{
    int number = 5;
    float expresso = 13.5;
    int cost = 3100;
    printf("The %d CEOs drank %f cups of expresso.\n", number, expresso);
    printf("The value of pi is %f.\n", PI);
    printf("FarewellA thou art too dear for my prossessing, \n");
    printf("%c%d\n",'$', 2 * cost);
    return 0;
}
           
Panda-MBP:CodeList4-6 panda8z$ gcc -o printout.out printout.c 
Panda-MBP:CodeList4-6 panda8z$ ./printout.out 
The 5 CEOs drank 13.500000 cups of expresso.
The value of pi is 3.141593.
FarewellA thou art too dear for my prossessing, 
$6200
Panda-MBP:CodeList4-6 panda8z$ 

           

CodeList 4-7 at P

/* width.c -- 字段寬度 */
#include <stdio.h>
#define PAGES 931
int main(void)
{
    printf("*%d*\n",PAGES);
    printf("*%2d*\n",PAGES);
    printf("*%10d*\n",PAGES);
    printf("*%-10d*\n",PAGES);
    return 0;
}
           
Panda-MBP:CodeList4-7 panda8z$ gcc -o width.out width.c 
Panda-MBP:CodeList4-7 panda8z$ ./width.out 
*931*
*931*
*       931*
*931       *
Panda-MBP:CodeList4-7 panda8z$ 

           

CodeList 4-8 at P

/* float.c -- 一些浮點數的組合 */
#include <stdio.h>
int main(void)
{
    const double RENT = 3852.99; //以const方法定義常量

    printf("*%f*\n",RENT);
    printf("*%e*\n",RENT);
    printf("*%4.2f*\n",RENT);
    printf("*%3.1f*\n",RENT);
    printf("*%10.3ff*\n",RENT);
    printf("*%10.3e*\n",RENT);
    printf("*%+4.2f*\n",RENT);
    printf("*%010.2f*\n",RENT);
    return 0;
}
           
Panda-MBP:CodeList4-8 panda8z$ gcc -o float.out float.c 
Panda-MBP:CodeList4-8 panda8z$ ./float.out 
*3852.990000*
*3.852990e+03*
*3852.99*
*3853.0*
*  3852.990f*
* 3.853e+03*
*+3852.99*
*0003852.99*
Panda-MBP:CodeList4-8 panda8z$
           

CodeList 4-9 at P

/* flags.c -- 一些标志的使用示例 */
#include <stdio.h>
int main(void)
{
    printf("%x, %X, %#x\n", 31, 31, 31);
    printf("**%d**% d**% d**\n", 42, 42, -42);
    printf("**%5d**%5.3d**%05d**%05.3d**\n", 6, 6, 6, 6);
    return 0;
}
           
Panda-MBP:CodeList4-9 panda8z$ gcc -o flags.out flags.c 
Panda-MBP:CodeList4-9 panda8z$ ./flags.out 
1f, 1F, 0x1f
**42** 42**-42**
**    6**  006**00006**  006**
Panda-MBP:CodeList4-9 panda8z$ 
           

CodeList 4-10 at P

/* string.c -- 字元串格式化 */
#include <stdio.h>
#define BLURB "Authentic imitation!"
int main(void)
{
    printf("/%2s/\n", BLURB);
    printf("/%24s/\n", BLURB);
    printf("/%24.5s/\n", BLURB);
    printf("/%-24.5s/\n", BLURB);
    return 0;
}
           
Panda-MBP:CodeList4-10 panda8z$ gcc -o string.out string.c 
Panda-MBP:CodeList4-10 panda8z$ ./string.out 
/Authentic imitation!/
/    Authentic imitation!/
/                   Authe/
/Authe                   /
Panda-MBP:CodeList4-10 panda8z$ 

           

CodeList 4-11 at P

/* intconv.c -- 一些不比對的整數轉換 */
#include <stdio.h>
#define PAGES 336
#define WORDS 65618
int main(void)
{
    short num = PAGES;
    short mnum = -PAGES;

    printf("num as short and unsigned short: %hd %hu\n", num, num);
    printf("-num as short and unsinged short: %hd %hu\n", mnum, mnum);
    printf("num as int and char: %d %c\n", num, num);
    printf("WORDS as int, short, and char: %d %hd %c\n", WORDS, WORDS, WORDS);
    return 0;
}
           
Panda-MBP:CodeList4-11 panda8z$ gcc -o intconv.out intconv.c 
intconv.c:13:65: warning: format specifies type 'short' but the argument has type 'int' [-Wformat]
    printf("WORDS as int, short, and char: %d %hd %c\n", WORDS, WORDS, WORDS);
                                              ~~~               ^~~~~
                                              %d
intconv.c:4:15: note: expanded from macro 'WORDS'
#define WORDS 65618
              ^~~~~
1 warning generated.
Panda-MBP:CodeList4-11 panda8z$ ./intconv.out 
num as short and unsigned short: 336 336
-num as short and unsinged short: -336 65200
num as int and char: 336 P
WORDS as int, short, and char: 65618 82 R
Panda-MBP:CodeList4-11 panda8z$ 

           

CodeList 4-12 at P

/* floatcnv.c -- 不比對的浮點數轉換 */
#include <stdio.h>
int main (void)
{
    float n1 = 3.0;
    double n2 = 3.0;
    long n3 = 2000000000;
    long n4 = 1234567890;

    printf("%.1e %.1e %.1e %.1e\n", n1, n2, n3, n4);
    printf("%1d %1d\n", n3, n4);
    printf("%1d %1d %1d %1d\n", n1, n2, n3, n4);
    return 0;
}
           
Panda-MBP:CodeList4-12 panda8z$ gcc -o floatcnv.out floatcnv.c 
floatcnv.c:10:45: warning: format specifies type 'double' but the argument has type 'long' [-Wformat]
    printf("%.1e %.1e %.1e %.1e\n", n1, n2, n3, n4);
                      ~~~~                  ^~
                      %.1ld
floatcnv.c:10:49: warning: format specifies type 'double' but the argument has type 'long' [-Wformat]
    printf("%.1e %.1e %.1e %.1e\n", n1, n2, n3, n4);
                           ~~~~                 ^~
                           %.1ld
floatcnv.c:11:25: warning: format specifies type 'int' but the argument has type 'long' [-Wformat]
    printf("%1d %1d\n", n3, n4);
            ~~~         ^~
            %1ld
floatcnv.c:11:29: warning: format specifies type 'int' but the argument has type 'long' [-Wformat]
    printf("%1d %1d\n", n3, n4);
                ~~~         ^~
                %1ld
floatcnv.c:12:33: warning: format specifies type 'int' but the argument has type 'float' [-Wformat]
    printf("%1d %1d %1d %1d\n", n1, n2, n3, n4);
            ~~~                 ^~
            %1f
floatcnv.c:12:37: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
    printf("%1d %1d %1d %1d\n", n1, n2, n3, n4);
                ~~~                 ^~
                %1f
floatcnv.c:12:41: warning: format specifies type 'int' but the argument has type 'long' [-Wformat]
    printf("%1d %1d %1d %1d\n", n1, n2, n3, n4);
                    ~~~                 ^~
                    %1ld
floatcnv.c:12:45: warning: format specifies type 'int' but the argument has type 'long' [-Wformat]
    printf("%1d %1d %1d %1d\n", n1, n2, n3, n4);
                        ~~~                 ^~
                        %1ld
8 warnings generated.
Panda-MBP:CodeList4-12 panda8z$ ./floatcnv.out 
3.0e+00 3.0e+00 0.0e+00 0.0e+00
2000000000 1234567890
2000000000 1234567890 -795279276 77992
Panda-MBP:CodeList4-12 panda8z$ 
           

CodeList 4-13 at P76

/* prntval.c -- 發現printf()函數的傳回值 */
#include <stdio.h>
int main (void)
{
    int bph2o = 212;
    int rv;

    rv = printf("%d F is water`s boiling point.\n", bph2o);
    printf("The printf() function printed %d characters.\n", rv);
    return 0;
}
           
Panda-MBP:CodeList4-13 panda8z$ gcc -o prntval.out prntval.c 
Panda-MBP:CodeList4-13 panda8z$ ./prntval.out 
212 F is water`s boiling point.
The printf() function printed 32 characters.
Panda-MBP:CodeList4-13 panda8z$ 

           

CodeList 4-14 at P

/* longstrg.c -- 列印較長的字元串 */
#include <stdio.h>
int main (void)
{
    printf("Here is one way to print a ");
    printf("long string.\n");
    printf("Here is another way to print a \
    long string.\n");
    printf("Here`s the newest way to print a"
    "long string.\n");
    return 0;
}
           
Panda-MBP:CodeList4-14 panda8z$ gcc -o longstrg.out longstrg.c 
Panda-MBP:CodeList4-14 panda8z$ ./longstrg.out 
Here is one way to print a long string.
Here is another way to print a     long string.
Here`s the newest way to print along string.
Panda-MBP:CodeList4-14 panda8z$ 
           

CodeList 4-15 at P78

/* input.c -- 什麼情況下使用& */
#include <stdio.h>
int main(void)
{
    int age;
    float assets;
    char pet[30];

    printf("Enter your age, assets, and favorite pet.\n");
    scanf("%d %f", &age, &assets);
    scanf("%s",pet);
    printf("%d $%.2f %s\n", age, assets, pet);
    return 0;
}
           
Panda-MBP:CodeList4-15 panda8z$ gcc -o input.out input.c 
Panda-MBP:CodeList4-15 panda8z$ ./input.out 
Enter your age, assets, and favorite pet.
27 cat
27 $0.00 cat
Panda-MBP:CodeList4-15 panda8z$ ./input.out 
Enter your age, assets, and favorite pet.
27
hoby
27 $0.00 hoby
Panda-MBP:CodeList4-15 panda8z$ ./input.out 
Enter your age, assets, and favorite pet.
27 
234234
cat
27 $234234.00 cat
Panda-MBP:CodeList4-15 panda8z$ ./input.out 
Enter your age, assets, and favorite pet.
sdf
0 $0.00 sdf
Panda-MBP:CodeList4-15 panda8z$ 
           

CodeList 4-16 at P

/* varwid.c -- 使用可變寬度的輸出字段 */
#include <stdio.h>
int main(void)
{
    unsigned width, precision;
    int number = 256;
    double weight = 242.5;

    printf("What field width?\n");
    scanf("%d", &width);
    printf("The number is: %*d: \n",width, number);
    printf("Now enter a width and a precision:\n");
    scanf("%d %d", &width, &precision);
    printf("Weight = %*.*f\n", width, precision, weight);
    return 0;
}
           
Panda-MBP:CodeList4-16 panda8z$ gcc -o varwid.out varwid.c 
Panda-MBP:CodeList4-16 panda8z$ ./varwid.out 
What field width?
2
The number is: 256: 
Now enter a width and a precision:
33
44
Weight = 242.50000000000000000000000000000000000000000000
Panda-MBP:CodeList4-16 panda8z$
           

CodeList 4-17 at P

/* skip2.c -- 跳過輸入的頭兩個整數 */
#include <stdio.h>
int main(void)
{
    int n;

    printf("Please enter three integers: \n");
    scanf("%*d %*d %d", &n);
    printf("The last integer was %d\n", n);
    return 0;
}
           
Panda-MBP:CodeList4-17 panda8z$ gcc -o skip2.out skip2.c 
skip2.c:8:16: warning: more '%' conversions than data arguments [-Wformat]
    scanf("%d %d %d", &n);
              ~^
1 warning generated.
Panda-MBP:CodeList4-17 panda8z$ ./skip2.out 
Please enter three integers: 
5 6 8
Segmentation fault: 11
Panda-MBP:CodeList4-17 panda8z$ gcc -o skip2.out skip2.c 
skip2.c:8:20: warning: more '%' conversions than data arguments [-Wformat]
    scanf("%d %*d %d", &n);
                  ~^
1 warning generated.
Panda-MBP:CodeList4-17 panda8z$ gcc -o skip2.out skip2.c 
Panda-MBP:CodeList4-17 panda8z$ ./skip2.out 
Please enter three integers: 
3 4 5
The last integer was 5
Panda-MBP:CodeList4-17 panda8z$ 
           

繼續閱讀