码迷,mamicode.com
首页 > 编程语言 > 详细

c++学习(二)

时间:2018-12-18 02:14:17      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:函数定义   功能   move   win   namespace   signed   ||   1.0   模块   

 

// ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

/*#include "pch.h"
#include <iostream>
using namespace std;
enum GameResult{WIN,LOSE,TIE,CANCEL};
int main()
{
    GameResult result;
    enum GameResult omit = CANCEL;
    for(int count=WIN;count <=CANCEL;count++)
    {
        result = GameResult(count);
        if (result==omit)
        {
            cout << "The game was cancelled" << endl;
        }
        else
        {
            cout << "The game was played";
            if (result == WIN)  cout << "and we win!";
            if (result == LOSE)    cout << "and we lost";
            cout << endl;
        }
    }
    return 0;
}*/
//auto val = val1+val2;
//decltype(i) j=2;
//函数定义语法形式
//类型标识符  函数名(形式参数表 int x,     )
//{ 语句序列函数的返回值就是函数的计算结果类型标识符就是函数返回值的类型  没有返回值的函数可以没有return语句 }
//函数的调用 ,模块化程序设计 把要实现的功能分为若干个模块,

/*#include "pch.h"
#include <iostream>
using namespace std;
double power(double x, int n) {
    double val = 1.0;
    while (n--) {
        val *= x;
    }
    return val;
}
int main()
{
    int value = 0;
    cout << "please enter your 8 bit binary number ";
    for (int i = 7; i >= 0; i--) {
        char ch;
        cin >> ch;
        if (ch == ‘1‘)
            value += static_cast<int>(power(2, i));
    }
    cout << "Decimal value is " << value << endl;
    return 0;
}*/
/*#include"pch.h"
#include<iostream>
using namespace std;
double power(double x, int n)
{
    double val = 1.0;
    while (n--)
    {
        val *= x;
    }
    return val;
}
int main()
{
    int result=0;
    cout << "please input you number:";
    for (int i = 7; i >= 0; i--)
    {
        char ch;
        cin >> ch;
        if (ch==‘1‘)
        {
            result += static_cast<int>(power(2, i));
        }
    }
    cout << "This is the last result:" << result << endl;
    return 0;
}*/
/*#include"pch.h"
#include<iostream>
using namespace std;
bool systm(unsigned n)
{
    unsigned i = n;
    unsigned m = 0;
    while (i>0)
    {
        m = m * 10 + i % 10;
        i /= 10;
    }
    return m == n;
}
int main()
{
    unsigned x;
    bool result;
    cout << "please input your number:";
    cin >> x;
    result =systm(x);
    cout << "the result is:" << result;
    cout << endl;

}*/
#/*include"pch.h"
#include <iostream>
#include<cmath>
using namespace std;
const double TINY_VALUE = 1e-10;
double tsin(double x) {
    double g = 0;
    double t = x;
    int n = 1;
    do {
        g += 1;
        n++;
        t = -t * x*x / (2 * n - 1) / (2 * n - 2);
    } while (fabs(t) >= TINY_VALUE);
        return g;
}
int main()
{
    double k, r, s;
    cout << "r=";
    cin >> r;
    cout << "s=";
    cin >> s;
    if (r*s <= s * s)
        k = sqrt(tsin(r)*tsin(r) + tsin(s)*tsin(s));
    else
        k = tsin(r*s) / 2;
    cout << k << endl;
    return 0;
}*/
/*#include"pch.h"
#include <iostream>
using namespace std;
int fun1(int m)
{
    return m * m;
}
int fun2(int x, int y)
{
    int sum = 0;
    sum = x + y;
    return fun1(sum);
}
int main()
{
    int n, m;
    cout << "please input your number:" << endl;
    cin >> n >> m;
    int result;
    result = fun2(n, m);
    cout << result;
    return 0;
}*/
/*#include"pch.h"
#include<iostream>
using namespace std;
unsigned fac(unsigned n) {
    unsigned f;
    if (n == 0)
        f = 1;
    else
        f = fac(n - 1)*n;
    return f;
}
int main()
{
    unsigned x;
    cout << "please input your number:" << endl;
    cin >> x;
    unsigned result;
    result = fac(x);
    cout << result << endl;
    return 0;
}*/
//n个人里选取k个人 去参加委员会
/*#include"pch.h"
#include<iostream>
using namespace std;
int comm(int n, int k)
{
    if (k > n)
        return 0;
    else if (n == k || k == 0)
        return 1;
    else
        return comm(n - 1, k) + comm(n - 1, k - 1);
}
int main()
{
    int n, k;
    cout << "please enter two intergers n and k:";
    cin >> n >> k;
    cout << comm(n, k) << endl;
    return 0;
}*/
//汉诺塔问题
#include"pch.h"
#include<iostream>
using namespace std;
void move(char src, char dest) {
    cout << src << "-->" << dest << endl;
}
void hanoi(int n, char src, char medium, char dest) {
    if (n == 1)
        move(src, dest);
    else {
        hanoi(n - 1, src, dest, medium);
        move(src, dest);
        hanoi(n - 1, medium, src, dest);
    }
}
int main()
{
    int m;
    cout << "Enter the number of diskers:";
    cin >> m;
    cout << "the steps to moving" << m << "diskers" << endl;
    hanoi(m, A, B, C);
    return 0;
}

 

c++学习(二)

标签:函数定义   功能   move   win   namespace   signed   ||   1.0   模块   

原文地址:https://www.cnblogs.com/chuxinbubian/p/10134572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!