目录
Git(分布式版本控制系统) + VS Code C语言学习笔记(1)
1、GIT
1.1 GIT介绍
https://baike.baidu.com/item/GIT:百度百科
1.2 GIT 安装过程
1. 下载完成后,双击下载好的软件开始安装,出现如下对话框
2. 点击Next,使用默认选项不用修改,Git Bash Here和Git GUI Here可以方便的在任意目录下打开git
3. 点击next,选择默认编辑器,我选择的是Visual Studio Code。
4. 继续next,配置PATH环境
5. 继续next,默认选项
点击Install开始安装,安装完成后点击Finish即可。
在桌面下右击,可以看到右键菜单中有Git GUI Here和Git Bash Here两个选项
2、VS Code
安装完Git后打开VS Code,打开终端,选择Git bash终端
若没有这个选项的话,选择Select Default Shell选项,找到Git bash终端然后选中,关闭VS Code重新打开
若当前终端变为类似linux虚拟机终端说明配置成功
代码练习
推荐一个刷基础算法题的网站:https://exercism.io/
注册登陆完后,加入C的练习中
还需要配置好相应的环境,点击任意练习下载配件
选择windows
进入网址下载,最好下载到默认环境,我的是C:\Exercism
下载完后添加PATH变量,注销电脑后生效
题目:
Introduction
An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.
For example:
- 9 is an Armstrong number, because
9 = 9^1 = 9
- 10 is not an Armstrong number, because
10 != 1^2 + 0^2 = 1
- 153 is an Armstrong number, because:
153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
- 154 is not an Armstrong number, because:
154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190
Write some code to determine whether a number is an Armstrong number.
在练习页面找到
1.拷贝代码,并用VS Code打开代码下载到的文件夹
exercism download --exercise=armstrong --track=c
2.实现代码
armstrong_numbers.c
#include "armstrong_numbers.h"
#include<math.h>
bool is_armstrong_number(int candidate)
{
int digit = 0;
double sum = 0;
int tempValue = candidate;
if(0 == candidate) return true;
int number = log10(candidate) + 1;
for (int i = 0; i < number; i++)
{
digit = tempValue % 10;
tempValue /= 10;
sum += pow(digit, number);
}
return sum == candidate;
}
armstrong_numbers.h
#ifndef ARMSTRONG_NUMBERS
#define ARMSTRONG_NUMBERS
#include <stdbool.h>
bool is_armstrong_number(int candidate);
#endif
test_armstrong_numbers.c
注意:测试代码时需要手动将TEST_IGNORE()函数注释掉
#include "vendor/unity.h"
#include "../src/armstrong_numbers.h"
void setUp(void)
{
}
void tearDown(void)
{
}
static void test_zero_is_an_armstrong_number(void)
{
TEST_ASSERT_TRUE(is_armstrong_number(0));
}
static void test_single_digit_numbers_are_armstrong_numbers(void)
{
// TEST_IGNORE(); // delete this line to run test 测试代码时该函数要手动注释掉
TEST_ASSERT_TRUE(is_armstrong_number(5));
}
static void test_there_are_no_two_digit_armstrong_numbers(void)
{
// TEST_IGNORE();
TEST_ASSERT_FALSE(is_armstrong_number(10));
}
static void test_three_digit_number_that_is_an_armstrong_number(void)
{
// TEST_IGNORE();
TEST_ASSERT_TRUE(is_armstrong_number(153));
}
static void test_three_digit_number_that_is_not_an_armstrong_number(void)
{
// TEST_IGNORE();
TEST_ASSERT_FALSE(is_armstrong_number(100));
}
static void test_four_digit_number_that_is_an_armstrong_number(void)
{
// TEST_IGNORE();
TEST_ASSERT_TRUE(is_armstrong_number(9474));
}
static void test_four_digit_number_that_is_not_an_armstrong_number(void)
{
// TEST_IGNORE();
TEST_ASSERT_FALSE(is_armstrong_number(9475));
}
static void test_seven_digit_number_that_is_an_armstrong_number(void)
{
// TEST_IGNORE();
TEST_ASSERT_TRUE(is_armstrong_number(9926315));
}
static void test_seven_digit_number_that_is_not_an_armstrong_number(void)
{
// TEST_IGNORE();
TEST_ASSERT_FALSE(is_armstrong_number(9926314));
}
int main(void)
{
UnityBegin("test/test_armstrong_numbers.c");
RUN_TEST(test_zero_is_an_armstrong_number);
RUN_TEST(test_single_digit_numbers_are_armstrong_numbers);
RUN_TEST(test_there_are_no_two_digit_armstrong_numbers);
RUN_TEST(test_three_digit_number_that_is_an_armstrong_number);
RUN_TEST(test_three_digit_number_that_is_not_an_armstrong_number);
RUN_TEST(test_four_digit_number_that_is_an_armstrong_number);
RUN_TEST(test_four_digit_number_that_is_not_an_armstrong_number);
RUN_TEST(test_seven_digit_number_that_is_an_armstrong_number);
RUN_TEST(test_seven_digit_number_that_is_not_an_armstrong_number);
return UnityEnd();
}
3.运行测试
CC=gcc make test
4.上传代码
exercism submit src/*
刷新一下网页会发现你的解决方案已经上传成功!
如有错误请指出