1일차 정리
쉬프트+F5하면 디버그모드를 빠져나옴
★ " , \ , % 등과 같은 기호들을 표기하기 위해 \를 앞에 붙여준다.
★\n --> std::endl;
★char a, printf("%s", a); --> std::cout<< a;
★int a, scanf("%d", &a) --> std::coin >> a;
★함수가 전달될 때 앞에서부터 채워진다.
★using namespace std;
★함수에서 초기값 지정이 가능해졌다. C++
================
정의
독립적인 기능을 가지는 작은 프로그램.
★C는 만들 때 "*.c" 처럼 확장자를 "c"로 나타내야 된다.
C++은 상관없음.
수정모드로 계속 들어가지면 "insert"를 누름
#----> 함수
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
int a = 10;
int * pa;
pa = &a;
printf("%d", *pa);//★★*은 주소값을 따라감.
return 0;
-------------실행결과
10
-----------------------
C++
표준출력함수
stdio.h ->iostream(input/output stream)
printf->cout (씨아웃)
stdd:: cout <<"출력 대상";//1개 입력시
stdd:: cout <<"출력 대상"<<"출력 대상";// 2개 입력시
=============C++ 예제1
#include "stdafx.h"
#include <iostream>
void main () {
std::cout << "Hello";
std::cout << " World" << std::endl;
std::cout << 10 << ", " << 20 << std::endl;
}
----------------실행결과
Hello World
10, 20
계속하려면 아무 키나 누르십시오 . . .
-------------------
@--------> 예제 2
#include "stdafx.h"
#include <iostream>
void main () {
int su1=10, sum=0;
float su2= 10.5;
char arr[10]={"Hellow"};
std::cout << "su1 = " << su1 << std::endl;
std::cout << "su2 = " << su2 << std::endl;
std::cout << "su1 + su2 = "<< su1+su2 << std::endl;
std::cout << arr << std::endl;
}
----------실행결과
su1 = 10
su2 = 10.5
su1 + su2 = 20.5
Hellow
계속하려면 아무 키나 누르십시오 . . .
--------------------
@------>예제 3
#include "stdafx.h"
#include <iostream>//<iostream.h>로 해도됨.
void main () {
std::cout<<"[안내]"<<std::endl;
std::cout<<"인터넷 변경하세요"<<std::endl;
std::cout<<"통신사 관계없이"<<std::endl;
std::cout<< "\\현금\\ 등 40만원+"<<std::endl;
std::cout<<"할인 37%!!"<<std::endl;
std::cout<<"<최/대> \"2천~9천\""<<std::endl;
std::cout<<"가능 통화버튼 눌러주세요.\n";
}
-------실행결과
[안내]
인터넷 변경하세요
통신사 관계없이
\현금\ 등 40만원+
할인 37%!!
<최/대> "2천~9천"
가능 통화버튼 눌러주세요.
계속하려면 아무 키나 누르십시오 . . .
---------------
@--------->예제 4
#include "stdafx.h"
#include <iostream>//<iostream.h>로 해도됨.
void main () {
int num1, num2, sum;
std::cout<<"정수 2개를 입력하세요..."<<std::endl;
std::cin >> num1;
std::cin >> num2;
sum = num1 + num2;
std::cout << "두 수의 합은 " << sum << "입니다. " <<std::endl;
}
--------------실행결과
정수 2개를 입력하세요...
1 2
두 수의 합은 3입니다.
계속하려면 아무 키나 누르십시오 . . .
--------------------
@-------> 예제 5
#include "stdafx.h"
#include <iostream>//<iostream.h>로 해도됨.
void main () {
char name1[30];
std::cout << "이름을 입력하세요\n";
std::cin >> name1; //scanf("%s",name1);
std::cout << "나의 이름은 "<< name1 <<" 입니다."<<std::endl;
}
--------실행결과
이름을 입력하세요
김아무개
나의 이름은 김아무개 입니다.
계속하려면 아무 키나 누르십시오 . . .
---------------
@-------->입 출력 예제 6
#include "stdafx.h"
#include <iostream>//<iostream.h>로 해도됨.
int main(int argc, char* argv[])
{
char name[20];
int age;
char phone[20]; // 010-1234-5678
std::cout << "이름을 입력하세요 " << std::endl;
std::cin >> name;
std::cout << "나이를 입력하세요 " << std::endl;
std::cin >> age;
std::cout << "전화번호를 입력하세요 " << std::endl;
std::cin >> phone;
std::cout << "이름 : " << name << std::endl;
std::cout << "나이 : " << age << std::endl;
std::cout << "전화번호 : " << phone << std::endl;
}
--------실행결과
-이름을 입력하세요
김덕호
나이를 입력하세요
20
전화번호를 입력하세요
123-1234-1234
이름 : 김덕호
나이 : 20
전화번호 : 123-1234-1234
계속하려면 아무 키나 누르십시오 . . .
-------------------
@--------->예제
#include "stdafx.h"
#include <iostream>
using namespace std;// std라는 이름공간을 사용하겠다!!로 std::를 사용안해도 됨.
void Func (int n = 10){
int i, sum=0;
for(i=0;i<=n;i++)
sum += i;
cout << sum << endl;
}
void main () {
Func();
Func(100);
}
---------실행결과
55
5050
계속하려면 아무 키나 누르십시오 . . .
------
@------------>default 매개변수 호출 예제
#include "stdafx.h"
#include <iostream>
using namespace std;
void FuncOne (int num=7){
cout << num +1 << endl;
}
void FuncTwo (int num1=5, int num2=7){
cout << num1+num2<< endl;
}
void main(int argc, char* argv[])
{
FuncOne();
FuncTwo();
FuncTwo(2);
}
----------실행결과
8
12
9
계속하려면 아무 키나 누르십시오 . . .
--------------------
@-------->반지름 입력문제
#include "stdafx.h"
#include <iostream>
using namespace std;
int banji(int r = 1){
int result;
result = r * r * 3.14;
return result;
}
void main()
{
int a;
cout << "반지름 입력해보쇼\n";
cin >> a;
cout << banji(a);
}
---------실행결과
데이터 입력해보쇼
20
1256계속하려면 아무 키나 누르십시오 . . .
--------------------
@---->1부터 입력값까지 출력
#include "stdafx.h"
#include <iostream>
using namespace std;
int func(int r = 1){
int sum=0, i;
for(i=1;i <= r;i++){
sum += i;
}
return sum;
}
void main()
{
int a;
cout << "1부터 입력값까지 출력\n";
cin >> a;
cout << func(a);
}
---------------실행결과
1부터 입력값까지 출력
10
55계속하려면 아무 키나 누르십시오 . . .
--------------------------
@---------> 구구단
#include "stdafx.h"
#include <iostream>
using namespace std;
int func(int r = 9){
int sum=0, i;
for(i=1;i <= 9;i++){
cout << r <<" * " <<i <<" = " << r*i<<endl;
}
return sum;
}
void main()
{
int a;
cout << "구구단 출력\n";
cin >> a;
cout << func(a);
}
------------실행결과
구구단 출력
2
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
0계속하려면 아무 키나 누르십시오 . . .
------------------