1 / 62

Beginning Visual C++

Beginning Visual C++. Park, KyungMi kmpark@bulsai.kaist.ac.kr. Kwon,JeongMin X5577 jmkwon@islab.kaist.ac.kr. Contents. Using Visual C++ Basic C Programming. USING VISUAL C++. Start Page. Create Project (1/3). Create Project (2/3). Create Project (3/3). Create Files. Programming.

rafe
Download Presentation

Beginning Visual C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Beginning Visual C++ Park, KyungMi kmpark@bulsai.kaist.ac.kr Kwon,JeongMin X5577 jmkwon@islab.kaist.ac.kr

  2. Contents • Using Visual C++ • Basic C Programming

  3. USING VISUAL C++

  4. Start Page

  5. Create Project (1/3)

  6. Create Project (2/3)

  7. Create Project (3/3)

  8. Create Files

  9. Programming

  10. Compile, Build, Execute

  11. Results

  12. BASIC C PROGRAMMING

  13. Program Design • Five phases in developing a program • Specify the problem • Analyze the problem • Design a method of solution (algorithm) • Coding (using programming lang.) • Test the program

  14. C History • 1970 : K Tompson wrote B for the first UNIX system on the PDP-7 • 1972 : C was designed as an extension of B • 1973 : UNIX OS was written in C over 90% • 1976 - 1977 : UNIX was ported to VAX • BCPL -> B -> C • S/Wused in UNIX is almost written in C

  15. Genealogy of High-Level Language FORTRAN I FLOW-MATIC ALGOL 58 COMTRAN 1957 1960 1965 1970 1975 1980 1885 1990 FORTRAN II ALGOL 60 FORTRAN IV COBOL LISP BASIC CPL ALGOL-W PL/I BCPL ALGOL 68 B Pascal Prolog C FORTRAN 77 Smalltalk 80 Ada C++ ANSI C FORTRAN 90 Ada 95

  16. From Source Code to Executable File Source Code Source File Text Editor Compiler Library Object File Executable File Linker

  17. Sample Program #include <stdio.h> #define SCOPE 10 void main() { int x, y; /* Variables */ int sum; x = 10; y = 200; sum = SCOPE * x + y; printf(“Sum = %d\n”, sum); /* print function */ } Sum = 300

  18. Name, Variables, Declarations • "identifier" : variable name - letters, digits, _, • First character : letter or _. • Long name with _ : help readability of program. (up to 31 characters) • Upper and lower case letters are distinct. • Function name : up to 6 characters with single case • Variable name : lower case • Symbolic constant : upper case • Key words are reserved. (lower case only)

  19. Data Types and Sizes • Char : a single byte, one character • int : integer ( 32 bit long in general ) • float : single-precision floating point precision: 6 digits, range: 10^(-38~+38) • Double: double-precision floating point precision: 12 digits, range: 10^(-308~+308) • short, long: specifying the size of integer short int sint; short sint; long int lint; long counter; int cntr;

  20. Unsigned vs. Signed • unsigned int vs. signed int : range of value • unsigned char vs. signed char • Signed integer : 1 bit for the sign of the number • the rest for the magnitude of the number • Unsigned integer use all the bits for the magnitude, non-negative. unsigned short int x; /* 0 ~ 2**16-1 */ signed int y; /* -2**31 ~ 2**31-1 */ signed char zz; /* -128 ~ +127 */

  21. Data Types and Values • Numeric data types : int, float, double ...... • Single ASCII character : char (also integer) Ex) 'A' '7' '+' 'a' '‘ • int • short int e.g. 1234 938 -392 • long int : 1234567890123L 29438l /* L or l */ • unsigned int: 2132U 332u /* u or U */ • octal representation of integer: begin with 0 Ex) 0177 034 02222222222L 09932U • hexadecimal representation of ingeter: 0X or 0x Ex) 0X3ff 0x23 0X2B 0XFUL

  22. Data Types and Values II • float : either an int, a decimal fraction, an exponent part or their combination (but not integer) Ex) -5.3e-3 1.0 1. .023 3.14159 314.159e-2 314158e-5 0.00314158e+3 0.00314158e3 0.00314158e003 0.0 0e0 .0e0 • Special notation for characters : '\ooo' or 'xhh' or '\n' Ex) '\013' /* ascii vertical tab */ '\007' /* ascii bell character */ '\xb' /* ascii vertical tab */ '\0' /* null character */

  23. Escape Sequence

  24. String Constant (1/2) • String constant or string literal • A sequence of zero or more characters surrounded by “” Ex) "This is string" "Thisisstring" "This is\talso string" "This is string\n" "" /* empty string */

  25. String Constant (2/2) • String constants can be concatenated at compile time Ex) "hello," " world" --- "hello, world" • String constant is an array of character terminated with • Null character.

  26. Class declaration • Variables must be declared before use • class (storage class) • automatic (default within functions) • extern (global) • Static • register • variable type • short, long, int, float, unsigned char, etc. • identifier_list • a list of variable names, separated by commas whose values are to be of the designed type and class. class type identifier_list;

  27. Declaration: examples float x, sum, av; int n; static unsigned long int real_long, rl; • Initialization of variables at the DECLARATION TIME (must be a constant expression) • qualifier const #define MAXLINE 1000 char esc = '\\'; int i = 0; int limit = MAXLINE+1 /* constant expression*/ float eps = 1.0e-5 const double e = 2.71828182845905; const char msg[] = "warning: "; int strlen(const char[]);

  28. Size of Data Types: program #include <stdio.h> /* compute the size of the fundamental types */ main() { printf("\n char: %d bytes", sizeof( char)); printf("\n short: %d bytes", sizeof( short)); printf("\n int: %d bytes", sizeof( int)); printf("\n long: %d bytes", sizeof( long)); printf("\nunsigned: %d bytes", sizeof(unsigned)); printf("\n float: %d bytes", sizeof( float)); printf("\n double: %d bytes", sizeof( double)); printf("\n\n"); }

  29. Arithmetic Operators • binary arithmetic operators:+ - * / % (modulus operator) • relational and logical operators: > >= < <= == != int year; if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) printf(%d is a leap year\n", year); else printf(%d is not a leap year\n", year); for (i=0; i<lim-1 && (c!='\n' && c!=EOF; i++) s[i] = c;

  30. Arithmetic Conversion Rules double float long unsigned int char, short

  31. Conversion Rule: example char c; double d; float f; int i; long l; short s; unsigned u;

  32. Increment and Decrement • ++ (increment: add 1) • -- (decrement: substract 1) • ++n -> increment n before using its value • n++ -> increment n after its value has been used • only applied to variables (not to expresson or const)

  33. Assignment Operators • compress form i = i + 2; i += 2; /* assignment operator */ • expr1 op= expr2 expr1 = (expr1) op (expr2) • op : + - * / % << >> & ^ | // x *= y + 1 means x = x * (y+1) rather than x = x*y + 1 /* bitcount: count 1 bits in x */ int bitcount(unsigned x) { int b; for (b = 0; x != 0; x >>=1) if (x &01) b++; return b; }

  34. Statement and Block • Expression: combination of var, const and operators • statement : expression + semicolon • Block: { series of statement} if (x == 0) x = 0; /* expression & statement */ x = 0; i++; printf("%d %d\n", i,j); /*statement */ { x = 0; i++; printf("%d %d\n", i,j); } /* block */

  35. Ambiguity of If-Then-Else • Part of if statement is optional : problem with nested if statements if (n > 0) { if (a > b) z = a; } else z = b; if (n >= 0) for ( i = 0; i < n; i++) if (s[i] > 0) { printf("..."); return i; } else /* WRONG */ printf("error --n is negative-n");

  36. Else-If : multiple choice /* count blanks, digits, letters, newlines, and others */ #include <stdio.h> main() { int c, blank_cnt, digit_cnt, letter_cnt, nl_cnt, other_cnt; blank_cnt=digit_cnt=letter_cnt=nl_cnt=other_cnt = 0; while ((c = getchar()) != EOF) /* brace not necessary */ if (c == ' ') ++blank_cnt; else if ('0' <= c && c <= '9') ++digit_cnt; else if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') ++letter_cnt; else if ( c == '\n') ++nl_cnt; else ++other_cnt; }

  37. Binary Search : example /* binsearch: find x in v[0]<=v[1]<=....<=v[n-1] */ int binsearch(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low+high) / 2; if (x < v[mid]) high = mid - 1; else if (x > v[mid]) low = mid + 1; else /* found match */ return mid; } return -1; /* no match */ }

  38. While • While ( expression ) : While expression is evaluated true(non zero), repeat statement. while (i++ < n) factorial = factorial * i; while ((c = getchar()) != EOF) { if ('a' <= c && c <= 'z') ++lower_case_letter_cnt; ++total_cnt; } while (++i < LIMIT) { j = 2 * i + 3; printf("\n%d", j); }

  39. While : continued • repeat statement zero or more times. • Control is passed to the next statement of while when expression is false (zero). • If expression is zero at first, skip while loop. int n; printf("\ninput an integer: "); scanf("%d", &n); while (--n) { ........ /* do something */ } /* for positive integer OK, but for negative ....*/ while (--n > 0) { /* do something */ }

  40. FOR for ( init_expr; test_expr; update_expr) statement next_statement ... int i, sum; sum = 0; for (i = 1; i <= 10; ++i) /* say evaluation seq */ sum += i;

  41. FOR : examples for (i = 1; i <= n; ++i) factorial *= i; for (j = 2; k % j == 0; ++j) { printf("\n%d is a divisor of %d", j, k); sum += j; } i = 0; sum = 0; for ( ; i <= 10; ++i) sum += i; i = 1; sum = 0; for ( ; i <= 10 ; ) sum += i++; for ( ; ; ) ; /* loop forever */

  42. COMMA operator • expression1 , expression2 • evaluate expression1, and then expression2, left to right, value of operation is value of expression2 • comma operator : extends the flexibility of for loop • allowing you to include initialize, test or update expression in a for loop sum = 0, i = 1 for ( sum = 0, i = 1; i <= n; ++i) sum += i; for ( sum = 0, i = 1; i <= n; sum += i, ++i) ; /* empty statement */

  43. COMMA and FOR #include <stdio.h> main() { int even_sum, odd_sum; int cnt, j, k, n; scanf("%d", &n); /* get the number */ even_sum = odd_sum = 0; for (cnt = 0, j = 2, k = 1; cnt < n; ++cnt, j +=2, k += 2) { even_sum += j; odd_sum += k; } printf("%7d%7d\n", even_sum, odd_sum); }

  44. Do while statement : exit condition loop • do statement while (expression); • evaluate statement first and then evaluate expression • If expression is true (nonzero) , repeat statement. • If expression is false (zero), control passes to next statement. do { printf("\n\ninput a positive integer: "); scanf("%d", &n); } while (n <= 0);

  45. TIP to Program • Relational expression ( < <= == >= > ) : appropriate, rather than an equality expression. (for boolean part of if-else, while, for, ....) /* test may fail */ main() { int cnt = 0; double sum = 0.0, x; for (x = 0.0; x != 9.9; x += 0.1) { /* test not robust */ sum += x; printf("\ncnt =%5d", ++cnt); } printf("\nsum = %f", sum); }

  46. Useful Example #include <string.h> /* reverse: reverse string s in place */ void reverse(char s[]) { int c, i, j; for (i = 0, j = strlen(s)-1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; } }

  47. Fibonacci Number: example #include <stdio.h> /* print fibonacci number and quotients */ #define LIMIT 46 main() { int f0 = 1, f1 = 1, n, temp; printf("\n%7d%19d", 0, 1); /* fi(0) = 1 */ printf("\n%7d%19d", 1, 1); /* fi(1) = 1 */ temp = f0; for (n = 2; n <= LIMIT; ++n) { f1 += temp; temp = f0;f0 = f1; printf("\n%7d%19d%19.16f",n, f1, (double) f1 / (double) f0); } printf("\n\n"); }

  48. Prime Number: example #include <stdio.h> /* print all primes less than LIMIT */ #define LIMIT 1000 main(){ int cnt = 0, j, k; for (k = 2; k < LIMIT; ++k) { j = 2; while (k % j != 0) ++j; if (j == k) { ++cnt; /* a prime has been found */ if (cnt % 6 == 1) printf("\n"); printf("%12d", k); } } printf("\n\nthere are %d prime numbers less than %d\n", cnt, LIMIT); }

  49. SWITCH statement : multiple choice • switch onst_expr(label): must have integer valued constants (type char is included), expressions formed solely from constants • Evaluate the expression. • Execute the case with label constant matching the value. If no match, execute the default, or if no default, terminate. • Terminate the switch when a break stmt is encountered, or by "falling off the end". switch ( expression ) { case const_expr : statement1 case const_expr : statement2 default: statement3 }

  50. Switch: example switch (c) { case 'a': ++a_cnt; break; case 'b': ++b_cnt; break; case 'c': ++c_cnt; /* what happen if no break?*/ case 'd': case 'D': ++dD_cnt; break; default: ++other_cnt; /* break is not necessary */ }

More Related