分类 C/C++ 下的文章

#include <stdio.h>
#include <string.h>
char * s_gets(char * st, int n);
#define MAXTITL   40
#define MAXAUTL   40
#define MAXBKS   100              //最大书籍数

struct book {                     //建立book结构声明
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

int main(void)
{
    struct book library[MAXBKS]; //book 类型结构的数组
    int count = 0;
    int index;
    
    printf("Please enter the book title.\n");
    printf("Press [enter] at the start of a line to stop.\n");
    while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL
           && library[count].title[0] != '\0')

//library[count].title[0] != '\0'判断字符串中的首字符是否为空字符
//按下 Enter 键相当于输入了一个字符串
    {
        printf("Now enter the author.\n");
        s_gets(library[count].author, MAXAUTL);
        printf("Now enter the value.\n");
        scanf("%f", &library[count++].value);
        while (getchar() != '\n')
            continue;          //清理输入行
        if (count < MAXBKS)
            printf("Enter the next title.\n");
    }
    
    if (count > 0)
    {
        printf("Here is the list of your books:\n");
        for (index = 0; index < count; index++)
            printf("%s by %s: $%.2f\n", library[index].title,
                   library[index].author, library[index].value);
    }
    else
    	printf("No books? Too bad.\n");
    
    return 0;
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    char * find;
    
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');   // 查找换行符
        if (find)                  //如果地址不是NULL
            *find = '\0';          // 在此处放置一个空字符
        else
            while (getchar() != '\n')
                continue;          // 处理输入行中剩余的字符
    }
    return ret_val;
}

--> #include <stdio.h> #include <string.h> char * s_gets(char * st, int n); #define MAXTITL 40 #define MAXAUTL 40 #define MAXBKS 100 //最大书籍数 struct book { ...

/*char *s_gets(char *st, int n)是为了获取字符st中的n个元素,
并将其结果返回为s_gets的字符指针。功能类似于fgets函数,
但它解决了fgets函数会读取'\n'的问题。*/

#include <stdio.h>
#include <string.h>

char * s_gets(char *st, int n);
int main()
{
    
    char name[20];
    printf("输入姓名\n");
    s_gets(name,20);
    printf("姓名为:%s\n",name);

    return 0;
}

char * s_gets(char *st, int n)
{
    char * ret_val;
    char * find;
                                     //表示从标准输入流中读取n-1个元素到字符st中,再将
    ret_val = fgets(st,n,stdin);     //再将指针ret_val指向指针st               
    if (ret_val)                      //if语句为了避免fgets函数返回值为NULL的情况
    {
        find  = strchr(st, '\n');      //查找换行符
        if (find)
            *find =  '\0';             //替换为空字符
        else
            while (getchar() != '\n')  //清除IO缓冲区 
                  continue;
    }
    return ret_val;
}

--> /*char *s_gets(char *st, int n)是为了获取字符st中的n个元素,并将其结果返回为s_gets的字符指针。功能类似于fgets函数,但它解决了fgets函数会读取'\n'的问题。*/#include <stdio.h>#include <string.h>char * s_gets(char *st, int n);int main(){ ...

一
ifndef是 if not define 的缩写,一种宏定义。它是预处理功能中三种
(宏定义,文件包含和条件编译)中的第三种--条件编译

#ifndef

#define X

...

#endif


c语言在对程序进行编译时,会先根据预处理命令进行预处理,C语言编译系统包括预处理,

编译和链接等部分。

#ifndef X  //先测试是否被宏定义

#define X 

程序段1//如果X没有被宏定义过,定义X,并编译程序段1;

#else

程序段2 //如果X已经定义过了则编译程序段2 的语句,忽视程序段1.

#endif


ifndef和endif要一起使用,不能存在丢失。

假如你有一个C源文件,它包含了多个头文件,比如头文件A和头文件B,头文件B又包含了头文件A,
则最终该源文件包含了两次头文件A。
如果你在头文件A里定义了结构体或者类类型
那么问题来了,编译时会报大量的重复定义错误。
例如要编写头文件  eatline.h

//在头文件开头写上两行:

#ifndef EATLINE_H_      //一般是文件名的大写

#define EATLINE_H_      

//头文件结尾写上一行:

#endif

这样一个工程文件里同时包含两个test.h时,就不会出现重定义的错误了。
当第一次包含test.h时,由于没有定义_TEST_H,条件为真,
这样就会包含(执行)#ifndef _TEST_H和
#endif之间的代码,当第二次包含test.h时前面一次已经定义了_TEST_H,条件为假,
#ifndef _TEST_H和#endif之间的代码也就不会再次被包含,
这样就避免了重定义了.

--> 一ifndef是 if not define 的缩写,一种宏定义。它是预处理功能中三种(宏定义,文件包含和条件编译)中的第三种--条件编译#ifndef#define X...#endif二c语言在对程序进行编译时,会先根据预处理命令进行预处理,C语言编译系统包括预处理,编译和链接等部分。#ifndef X  //先测试是否被宏定义#define X 程序段1//如果X没有被宏定义过,定义X...

[label]进制转换[/label][alert]编写进制转换函数来接受两个参数,且第二个参数在 2~10 范围内,然后以第二个参数中指定的进制打印第一个参数的数值[/alert]

#include <stdio.h>
void to_base_n (int x, int base);//转换进制函数
int main()
{
    int number, b, count;
    printf("输入数字\n");
    while(scanf("%d",&number) == 1) //检测输入是否为数字
    {
        printf("输入 2~10 中的一个数字\n");
        while((count = scanf("%d",&b)) == 1 && (b < 2 || b > 10)) //检测输入是否为数字且在范围内
        {
            printf("输入需要在 2~10 范围内: ");
        }
        if (count != 1)
        break;
        printf("Base %d equivalent: ", b);
        to_base_n(number, b);
        putchar('\n');
        printf("输入一个数字 (q to quit):\n");
    }
    printf("Done!\n");
    return 0;
 }
 
void to_base_n (int x, int base)
{
    int r;
 
    r = x % base; //除以进制数求余
    if (x >= base)
    to_base_n(x / base, base); //递归
    putchar('0' + r);
    return;
}

--> [label]进制转换[/label][alert]编写进制转换函数来接受两个参数,且第二个参数在 2~10 范围内,然后以第二个参数中指定的进制打印第一个参数的数值[/alert]#include <stdio.h>void to_base_n (int x, int base);//转换进制函数 int main() { int number, b, count; ...

[label]前言[/label][alert] 混合使用 getchar() 和 scanf() 时,如果在调用 getchar() 之前,scanf() 在输入行留下一个换行符,会导致一些问题

编写程序时,要认真设计用户界面。事先预料一些用户可能会犯的错误,然后设计程序妥善处理这些错误情况[/alert]

#include <stdio.h>

char get_choice(void);       //菜单选择,获取获取用户输入
char get_first(void);        //处理换行符等,使其返回下一个非空白字符而不仅仅是下一个字符
int get_int(void);           //处理混合字符和数值输入
void count(void);

int main(void)
{
    int choice;
    void count(void);

    while ( (choice = get_choice()) != 'q')
    {
        switch (choice)
        {
            case 'a' : printf("Buy low, sell high.\n");
            break;
            case 'b' : putchar('\a'); /* ANSI */
            break;
            case 'c' : count();
            break;
            default : printf("Program error!\n");
            break;
        }
    }
    printf("Bye.\n");

    return 0;
}

void count(void)
{
    int n,i;

    printf("Count how far? Enter an integer:\n");
    n = get_int();
    for (i = 1; i <= n; i++)
    printf("%d\n", i);
    while ( getchar() != '\n')
    continue;
}

char get_choice(void)
{
    int ch;

    printf("Enter the letter of your choice:\n");
    printf("a. advice b. bell\n");
    printf("c. count q. quit\n");
    ch = get_first();
    while ( (ch < 'a' || ch > 'c') && ch != 'q')
    {
        printf("Please respond with a, b, c, or q.\n");
        ch = get_first();
    }

    return ch;
}

char get_first(void)
{
    int ch;

    ch = getchar();
    while (getchar() != '\n')
    continue;

    return ch;
}

int get_int(void)
{
    int input;
    char ch;

    while (scanf("%d", &input) != 1)
    {
        while ((ch = getchar()) != '\n')
        putchar(ch);                                //处理错误输出
        printf(" is not an integer.\nPlease enter an ");
        printf("integer value, such as 25, -178, or 3: ");
    }

    return input;
}

--> [label]前言[/label][alert] 混合使用 getchar() 和 scanf() 时,如果在调用 getchar() 之前,scanf() 在输入行留下一个换行符,会导致一些问题编写程序时,要认真设计用户界面。事先预料一些用户可能会犯的错误,然后设计程序妥善处理这些错误情况[/alert]#include <stdio.h>char get_choic...