Create a simple c language project file project for vs2010

Foreword:

Because the teacher learning data today asked us to write a project document, but it seems that we have not learned what a project document is, so I wrote this article to briefly describe what I am about a project document and how to write a qualified project document. understand. (If there are mistakes, please forgive me QAQ)

According to my experience in writing keil5, a qualified project file is suitable for transplantation (modular programming, file classification, and applicable to all trial and error situations), easy to understand (comments or instructions), easy to use (with quick opening Way).

  1. Easy to use (shortcut way to open)

vs2010 and other programming software have a feature - there is a shortcut that can be opened directly

As shown in the figure: in each created project, there are files of the type Microsoft Visual..., which is the shortcut to open each project file, but it should be noted that the file cannot be easily moved .

Shifting will cause the shortcut to not find its original location, and the file will be opened incorrectly.

b. Simple and easy to understand (comments or explanations)

The description can be written under the most basic file, and the content can be written as follows (please ignore my mentally handicapped behavior)

The next step is annotations. Annotations are a good habit, and it is good to develop them.

#include <stdio.h>
#include <stdlib.h>        //该文件的声明可以使用exit
#include "My_math.h"
#include <math.h>

int main()
{
    int n=-1;        //N为阶乘的结果,n为阶乘数
    float p;
    while(n == -1)
    {
    printf("请输入一个正整数n\n:");
    scanf("%f",&p);
    n=Jiecheng(p);            //阶乘函数的调用    该阶乘函数是在函数内部有输入提示
    }
    printf("%d\n",n);

    int N1,N;        //x是e的次方数,N1是阶乘,N是记录p的值的
    float x,sum=0;    //sum是结果。
    double N2=0.0;    //N2是x的n次方。
A:  printf("计算e的x次方,请输入x以及需阶次n(x,n):\n");
    scanf("%f,%f",&x,&p);
    N=p;        //记录p,最后打印用
    for(;p>=0;p--)        //从后到前计算
    {
        N1=Jiecheng(p);        //N1是阶乘
        if(N1==-1)
        {
            goto A;        //如果有误,重新输入
        }
        N2=pow(x,p);    //N2是x的n次方值
        sum=sum+N2/N1;    //结果加起来
    }
    printf("e^%.2f的%d阶泰勒展开的结果为%f",x,N,sum);    //
}

ps: Please forgive my immature programming techniques for using goto, a function that easily destroys programs

c. Suitable for transplantation ( modular programming, file classification, and applicable to all trial and error situations )

Applicable to all trial and error situations

A major feature of the data structure is that all unsatisfied inputs require the programmer to think in advance and improve or prompt the input error and allow the user to re-enter.

For example, the main function above and the My_math file below are the programs after solving the interference of numbers including decimals, negative numbers, and 0

#include <stdio.h>
#include <stdlib.h>

int Jiecheng(float p)
{
    int n=0;
    n=(int)p;
    if(n<0)        //判断输入是否小于0
    {
        printf("输入数据错误(<0)!重新输入\n");
        return -1;
    }
    else if(n!=p)        //判断是否为小数
    {
        printf("输入数据出错(float)!重新输入\n");
        return -1;
    }
    else if(n==1||n==0)        //1和0都是正确的
    {
        return 1;
    }
    else 
    {
        return(n*Jiecheng(n-1));            //循环调用阶乘函数
    }
}

file classification

In keil5, we will store files in Hardware (most functions), user (main function) or system (other) files, as follows

However, vs2010 is not so particular (after all, in the learning stage, there is no need for more than 5 files to encapsulate functions)

We put it all in one file, as follows

在这里,自创的文件都可以放在此处,理论上来说,只要是h文件,在添加时需要添加到头文件列表,而cpp文件放在源文件即可,如下

模块化编程

右击头文件,选择新建新文件,点击h文件,写一个合格的头文件是工程文件的重中之重(注意创建时,名字要与cpp文件相同,毕竟你也不想以后被人诟病你这文件怎么找不到头文件吧)

#ifndef __MY_MATH_H
#define __MY_MATH_H

int Jiecheng(float p);        //阶乘函数的声明

#endif

格式如上,翻译一下就懂了。就是为了避免一些小问题才写的ifndef……define……endif

注意后者写名称时要大写,前面两个下划线,后面一个下划线即可

在写完头文件之后,在主函数文件开头用include声明我要使用我的文件啦(具体看”注释“标题下的主函数。)(此处记得要用”“引用而非<>引用)

为什么要这么做呢?我们分析一下。

如果一个文件下,我们要调用很多很多函数,如果全部在主文件之中声明,首先是臃肿,其次是不方便知晓你的文件中有什么函数,在移植程序时很很很麻烦。使用头文件可以让编程更简洁,更加艺术。

工程文件私信可发捏,弘扬开源精神,从大一做起~

Guess you like

Origin blog.csdn.net/ChiShangying/article/details/129325805