C++基本ウィジェット-プログラマーへの第一歩
"Student.h"
"Test.cpp"
#pragma once
#include <assert.h>
void Swap1(int* num1,int* num2)
{
int tmp = *num1;
*num1 = *num2;
*num2 = tmp;
}
void Swap2(int* num1,int* num2)
{
*num1 = (*num1)*(*num2);
*num2 = (*num1)/(*num2);
*num1 = (*num1)/(*num2);
}
void Swap3(int* num1,int* num2)
{
*num1 = (*num1)+(*num2);
*num2 = (*num1)-(*num2);
*num1 = (*num1)-(*num2);
}
void Swap4(int* num1,int* num2)
{
*num1 = (*num1)^(*num2);
*num2 = (*num1)^(*num2);
*num1 = (*num1)^(*num2);
}
void Swap5 (int& num1,int& num2)
{
int tmp = num1;
num1 = num2;
num2 = tmp;
}
void PrimeNumber1()
{
int i = 0;
int j = 0;
for (i = 100;i <= 200;i++)
{
for (j = 2;j < i;j++)
{
if (i%j == 0)
{
break;
}
}
if (i == j)
{
cout<<i<<" ";
}
}
cout<<endl;
}
void PrimeNumber2()
{
int i = 0;
int j = 0;
for (i = 101;i <= 200;i += 2)
{
for (j = 3;j < i;j+=2)
{
if (i % j == 0)
{
break;
}
}
if (j == i)
{
cout<<i<<" ";
}
}
cout<<endl;
}
void MultiplicationTable()
{
int i = 0;
int j = 0;
for (i = 1;i <= 9;i++)
{
for (j = 1;j <= i;j++)
{
cout<<i<<" * "<<j<<" = "<<i*j<<" ";
}
cout<<endl;
}
}
bool IsLeapYear(int year)
{
assert(year > 0);
if (((year % 4 == 0)&&(year % 100 != 0)) || (year % 400 == 0))
{
return true;
}
else
{
return false;
}
}
char* _strchr(const char* str,const char c)
{
assert(str);
while (*str != '\0')
{
if (*str == c)
{
return (char* )str;
}
str++;
}
return NULL;
}
int BinarySystem1(unsigned int num)
{
int count = 0;
while (num)
{
if (num % 2 == 1)
{
count++;
}
num = num/2;
}
return count;
}
int BinarySystem2(unsigned int num)
{
int count = 0;
while (num)
{
if ((num & 1) == 1)
{
count++;
}
num = num>>1;
}
return count;
}
int BinarySystem3(unsigned int num)
{
int count = 0;
do
{
count++;// 1
num = num & (num-1);
} while (num);
return count;
}
bool IsSame(int* arr1,int size1,int* arr2,int size2)
{
assert(arr1);
assert(arr2);
int i = 0;
int j = 0;
for (i = 0;i < size1;i++)
{
for (j = 0;j < size2;j++)
{
if (arr1[i] == arr2[j])
{
return true;
}
}
}
return false;
}
void SwapArr(int* arr1,int* arr2,int size)
{
assert(arr1);
assert(arr2);
int i = 0;
int tmp = 0;
for (i = 0;i < size;i++)
{
tmp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = tmp;
}
}
void UnEven_Even(unsigned int num)
{
int arr1[16] = {0};
int arr2[16] = {0};
int i = 0;
int j = 0;
for (i = 0;i < 32;i += 2)
{
//
arr1[j] = ((num>>i)&1);
j++;
}
j = 0;
for (i = 1;i < 32; i += 2)
{
//
arr2[j] = ((num>>i)&1);
j++;
}
for (i = 15;i >= 0;i--)
{
cout<<arr1[i]<<" ";
}
cout<<endl;
for (i = 15;i >= 0;i--)
{
cout<<arr2[i]<<" ";
}
cout<<endl;
}
"Test.cpp"
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include "student.h"
void Test1()
{
//
int n1 = 5;
int n2 = 8;
Swap1(&n1,&n2);
cout<<"n1 = "<<n1<<endl<<"n2 = "<<n2<<endl;
int n3 = 6;
int n4 = 10;
Swap2(&n3,&n4);
cout<<"n3 = "<<n3<<endl<<"n4 = "<<n4<<endl;
int n5 = 15;
int n6 = 20;
Swap3(&n5,&n6);
cout<<"n5 = "<<n5<<endl<<"n6 = "<<n6<<endl;
int n7 = 65;
int n8 = 38;
Swap4(&n7,&n8);
cout<<"n7 = "<<n7<<endl<<"n8 = "<<n8<<endl;
int n9 = 32;
int n10 = 23;
Swap5(n9,n10);
cout<<"n9 = "<<n9<<endl<<"n10 = "<<n10<<endl;
}
void Test2()
{
// 1 , 1 , 20 ,
int money = 0;
int total = 0;
int empty = 0;
cin>>money;
empty = money;
while(empty >= 2)
{
total = total + empty;
empty = (empty%2) + (empty/2);
}
cout<<" "<<total<<" "<<endl;
}
void Test3()
{
// 100-200
//PrimeNumber1();
PrimeNumber2();
}
void Test4()
{
//
MultiplicationTable();
}
void Test5()
{
//
int year = 0;
cin>>year;
cout<<IsLeapYear(year)<<endl;
}
void Test6()
{
// strchr()
char* p = "hello";
char c = 'e';
cout<<_strchr(p,c)<<endl;
}
void Test7()
{
// 1
unsigned int num = 0;
cin>>num;
cout<<BinarySystem1(num)<<endl;
cout<<BinarySystem2(num)<<endl;
cout<<BinarySystem3(num)<<endl;
}
void Test8()
{
//
int arr1[] = {1,2,3,4,5,6};
int arr2[] = {8};
int size1 = sizeof(arr1)/sizeof(arr1[0]);
int size2 = sizeof(arr2)/sizeof(arr2[0]);
cout<<IsSame(arr1,size1,arr2,size2)<<endl;
}
void Test9()
{
// A B
int arr1[] = {1,2,3,4,5};
int arr2[] = {1,3,5,7,9};
int size1 = sizeof(arr1)/sizeof(arr1[0]);
int size2 = sizeof(arr2)/sizeof(arr2[0]);
SwapArr(arr1,arr2,size1);
int i = 0;
for (i = 0;i < size1;i++)
{
cout<<arr1[i]<<" ";
}
cout<<endl;
for (i = 0;i < size2;i++)
{
cout<<arr2[i]<<" ";
}
}
void Test10()
{
// ,
unsigned int num = 0;
cin>>num;
UnEven_Even(num);
}
int main()
{
//Test1();
//Test2();
//Test3();
//Test4();
//Test5();
//Test6();
//Test7();
//Test8();
//Test9();
//Test10();
system("pause");
return 0;
}