前言

老师在疫情停课期间给了我们一个关于数据结构的作业,虽说不难,但也比较耗费时间,因为它很繁琐,下面是一些我在写的时候的心得吧。代码部分在截止日期前会自动隐藏

介绍

Data are related to all aspects of a computer program. This homework is designed to practice the following tasks related to data:

  • Data storage and description:
    • Variables (place holders)
    • constants
    • Different data types.
  • Data flow: Input and output (I/O)
    • Input: Tell the program the data needed for its computation.
    • Output: Let the program show the computation results and talk to a user.
  • Data usage: compute some data and get some results.
    • Simple expressions
    • Simple functions
      • function prototypes and definitions.

The above picture is found from the internet [1], which shows the view of input and output (I/O) on a computer. It is interesting that the view of I/O can also be applied to other concepts, like a software application, a C program, or a C function.

大致意思就是:

设置变量、常量、设计函数、进行输入输出、进行一些简单计算。

这些都比较基础了,就是操作的次数多,略显繁琐。分为7个任务,具体任务可以看:https://files.hoyue.fun/myc/hmk2_cs_eie_110_2021_Fall.pdf

之后是我觉得有些要注意的地方。


注意点

输入字符

当以字符方式输入的时候,要特别注意输入前是否有其他符号,因为scanf是不会忽略回车符的。

  • scanf()在读取数字时会跳过空格、制表符和换行符!

  • %c只能输出或输入一个字符,%s输出的是一串字符。

  • scanf遇到 回车(enter),空格,TAB 就会结束一次输入,空格不会接收,scanf在一次输入结束后,不会舍弃最后的回车符(即回车符会残留在数据缓冲区中)

例如:

#include <stdio.h>
int main()
{
char c1,c2;
scanf("%c %c",&c1,&c2); //这里有一个空格
printf("%d %d\n",c1,c2);//输出为ASCII码值
scanf("%c%c",&c1,&c2); //这里没有空格
printf("%d %d\n",c1,c2);
return 0;
}

 我们输入a b,正确输出则是第二行的97 98,错误输出则为第四行的 10 97

我们对照ASCII码值表,可以看出10对应的正是换行符,二97正是a。

说明换行符被读入了,空格没有。

scanf单字符输入时规定只接收一个字符,所以第一次输入a b 时 ,第一个scanf("%c %c")之间有一个空格,所以在输入字符a之后,我们可以输入空格,enter,,scanf都会自动忽略它(那个空格会读取停止字符并释放掉),所以第一次输入正常,但它却把回车符也作为字符对待的。在我们输入完b之后按回车(Enter),这个回车符是放在缓冲区的,并且不会舍弃最后的回车符,此时的数据缓存区中还残存着一个回车符,

第二次调用scanf("%c%c",&c1,&c2);是从缓冲区中取两个字符,首先把第一次调用scanf("%c%c",&c1,&c2);后输入的回车当作输入字符赋值给c1 ,之后把a赋值给了c2

因此我们在遇到上一行有回车符,且下一行需要读入字符时,我们应该先加上一个空格。

例如:

#include <stdio.h>
int main(){
char c1,c2;
scanf("%c %c",&c1,&c2); //这里有一个空格
printf("%d %d\n",c1,c2);
scanf(" %c %c",&c1,&c2); //这里也有了空格
printf("%d %d\n",c1,c2);
return 0;
}

这样的输出结果就对了。


注意unsigned数据结构

作业中有让我们定义unsigned的类型,其实就和普通的一样,如下表转化:

unsigned int %u
unsigned long %lu
unsigned long long %llu

使用unsigned定义有可能会导致溢出,一般别用。

定义函数时的类型

当我们在自定义函数时,需要给这个函数一个类型,这个类型取决于返回值的类型

例如:

int test()

 表示一个函数test,其返回值的类型为int,其他类型以此类推,这些函数最后都需要return xxx。

若函数不需要返回值,则类型为void

例如:

void test()

 该函数就不需要返回,也不应该出现return xxx;除非是return 0;结束。


作业

这里是我的作业代码展示,本代码将在2021.10.16之后自动公开,在此之前将隐藏。

/*
Name: ;
Class: ;
Student ID: ;
Homework: #2 Data;
仅供参考!!!
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
char name[80];//task 3.10. The name is defined as a global variable.
//task2: define constant.
#define PI 3.1415 //task 2.1
#define GREETING "Thanks for your cooperation!" //task 2.2
//task6: function declarations
double add(double m,double n);//task 6.1
double minus(double m,double n);//task 6.2
double times(double m,double n);//task 6.3
double divide(double m,double n);//task 6.4
int modular(int m,int n);//task 6.5
double circle_area(unsigned long long r);//task 6.6
double rect_area(float s);//task 6.7
//task7: declarate test function, then call the test function in the main function.
void test();
int main()
{
    //task3: place holders
    char x11,x12;//task 3.1
    int x21,x22;//task 3.2
    long x31,x32;//task 3.3
    long long x41,x42;//task 3.4
    unsigned int x51,x52;//task 3.5
    unsigned long x61,x62;//task 3.6
    unsigned long long x71,x72;//task 3.7
    float y11,y12;//task 3.8
    double y21,y22;//task 3.9
    //PS:The name has defined as a global variable.
    //task4: input and output
    //Preface
    printf("Hello, thank you for running my program. Now could you tell me your name?\n");
    scanf("%s",name);
    //task 4.1 input and output char
    printf("OK, thank you %s. Now could you input two characters x11 and x12?\n",name);
    scanf(" %c%c",&x11,&x12);//Because I intput a '\n' in font of the sentence, I blank first input to 'eat' Carriage return
    printf("Thank you %s,\nyou input characters is x11:%c, and x12:%c; Size is %llu byte.\n",name,x11,x12,sizeof(x12));
    //task 4.2 intput and output int
    printf("OK, thank you %s. Now could you input two int numbers x21 and x22?\n",name);
    scanf("%d%d",&x21,&x22);
    printf("Thank you %s,\nyou input number is x21:%d, and x22:%d; Size is %llu byte.\n",name,x21,x22,sizeof(x22));
    //task 4.3 intput and output long
    printf("OK, thank you %s. Now could you input two long int numbers x31 and x32?\n",name);
    scanf("%ld%ld",&x31,&x32);
    printf("Thank you %s,\nyou input number is x31:%ld, and x32:%ld; Size is %llu byte.\n",name,x31,x32,sizeof(x32));
    //task 4.4 intput and output long long
    printf("OK, thank you %s. Now could you input two long long int numbers x41 and x42?\n",name);
    scanf("%lld%lld",&x41,&x42);
    printf("Thank you %s,\nyou input number is x41:%lld, and x42:%lld; Size is %llu byte.\n",name,x41,x42,sizeof(x42));
    //task 4.5 intput and output unsigned int
    printf("OK, thank you %s. Now could you input two unsigned int numbers x51 and x52?\n",name);
    scanf("%u%u",&x51,&x52);
    printf("Thank you %s,\nyou input number is x51:%u, and x52:%u; Size is %llu byte.\n",name,x51,x52,sizeof(x52));
    //task 4.6 intput and output unsigned long
    printf("OK, thank you %s. Now could you input two unsigned long int numbers x61 and x62?\n",name);
    scanf("%lu%lu",&x61,&x62);
    printf("Thank you %s,\nyou input number is x61:%lu, and x62:%lu; Size is %llu byte.\n",name,x61,x62,sizeof(x62));
    //task 4.7 intput and output unsigned long long
    printf("OK, thank you %s. Now could you input two unsigned long long int numbers x71 and x72?\n",name);
    scanf("%llu%llu",&x71,&x72);
    printf("Thank you %s,\nyou input number is x71:%llu, and x72:%llu; Size is %llu byte.\n",name,x71,x72,sizeof(x72));
    //task 4.8 input and output float
    printf("OK, thank you %s. Now could you input two float numbers y11 and y12?\n",name);
    scanf("%f%f",&y11,&y12);
    printf("Thank you %s,\nyou input number is y11:%f, and y12:%f; Size is %llu byte.\n",name,y11,y12,sizeof(y12));
    //task 4.9 input and output double
    printf("OK, thank you %s. Now could you input two double numbers y21 and y22?\n",name);
    scanf("%lf%lf",&y21,&y22);
    printf("Thank you %s,\nyou input number is y21:%lf, and y22:%lf; Size is %llu byte.\n",name,y21,y22,sizeof(y22));
    //task 4.10 output name and greeting
    printf("OK, thank you %s.That's all! I'm sorry for making you work so hard. %s\n",name,GREETING);
    //end of task4

    //The main content of task5-6 is below the main function.

    //task7: test function
    test();
    return 0;
    //code end;
}
//task5: function definitions
double add(double m,double n)
{
    return m+n;//task 5.1
}
double minus(double m,double n)
{
    return m-n;//task 5.2
}
double times(double m,double n)
{
    return m*n;//task 5.3
}
double divide(double m,double n)
{
    return m/n;//task 5.4
}
int modular(int m,int n)
{
    return m%n;//task 5.5
}
double circle_area(unsigned long long r)
{
    return PI*r*r;//Possible result is decimal, return double. Task 5.6
}
double rect_area(float s)
{
    return s*s;//Possible result is decimal, return double. Task 5.7
}
void test()
{
    double m,n;//test add, minus, times, divide
    int a,b;//test mod
    unsigned long long r;//test circle_area
    float s;//test rect_area
    printf("All right %s. Now I need you help my test my functions.\n",name);
    //task 7.1-7.4
    printf("%s, please input two double numbers m,n.\n",name);
    scanf("%lf%lf",&m,&n);
    printf("OK %s, please help me check m+n=%lf ; m-n=%lf ; m*n=%lf ; m/n=%lf\n",name,add(m,n),minus(m,n),times(m,n),divide(m,n));
    //task 7.5
    printf("Thank you %s, let's move on. Please input two int numbers a,b.\n",name);
    scanf("%d%d",&a,&b);
    printf("OK %s, please help me check a mod b = %d\n",name,modular(a,b));
    //task 7.6
    printf("Thank you %s, let's move on. Please input one unsigned long long number r as radius, I will calculate the area of the circle.\n",name);
    scanf("%llu",&r);
    printf("OK %s, please help me check the area of the circle is %lf\n",name,circle_area(r));
    //task 7.7
    printf("Thank you %s, next is the last one. Please input one float number s as side length of the square. I will calculate the area of the square\n",name);
    scanf("%f",&s);
    printf("OK %s, please help me check the area of the square is %lf\n",name,rect_area(s));
    printf("Thank you %s, all the tests are over. Now this program will also be over, thank you for your company and see you next time!\n",name);
}

这里的一切都有始有终,却能容纳所有的不期而遇和久别重逢。
最后更新于 2024-01-14