字符设备 - The most important !

字符设备是Linux设备驱动中最基础、最重要的概念之一。它是用户空间程序与硬件设备交互的重要桥梁。通过本文,你将深入了解字符设备的基础知识、实现方式、典型应用场景,以及关键代码实例。


一、什么是字符设备

1.1 字符设备的定义

字符设备(Character Device)是指以字符流的形式进行数据读写的设备,数据传输通常是逐字节进行的。这种设备以线性方式提供访问接口,不支持随机访问。简单来说,字符设备允许用户程序通过顺序读取或写入操作来与设备交互。

1.2 字符设备的典型例子

字符设备常见于需要逐字节处理数据的场景。以下是一些典型例子:

类别示例设备节点描述
终端设备/dev/tty提供用户与系统交互的终端接口
串口设备/dev/ttyS0通信设备,支持串行传输
键盘设备/dev/input/event*提供键盘输入的字符流
鼠标设备/dev/input/mouse*处理鼠标输入事件
音频设备/dev/snd/*支持音频输入输出的设备
视频设备/dev/video*用于摄像头、视频捕获等功能
内存设备/dev/mem提供对物理内存的直接访问
随机数设备/dev/random提供随机数流供加密或其他应用使用
null设备/dev/null吞掉所有写入数据,永远返回EOF
零设备/dev/zero提供连续的零字节流
伪终端设备/dev/pts/*支持虚拟终端功能

它们都挂载在/dev目录下,用户可以通过文件操作接口对这些设备进行操作,例如catecho等命令。
在这里插入图片描述

1.3 字符设备的特点

为了更好地理解字符设备,可以总结其特点:

特性描述
线性读写数据以顺序的方式读取或写入
无缓存机制通常不使用复杂的缓存机制,直接与设备交互
适用于实时数据流例如串口通信,需要快速、实时处理数据流

与块设备相比,字符设备更适合于流式数据的处理,例如键盘输入和传感器数据读取。


二、字符设备的底层工作原理

字符设备通过Linux的设备文件(Device File)与用户空间程序进行交互。设备文件是Linux中用户与硬件交互的抽象概念,通过特殊的文件节点表现,用户可以像操作普通文件一样操作设备。

2.1 主设备号与次设备号

Linux中,每个设备通过主设备号和次设备号唯一标识:

  • 主设备号:标识设备类别。例如,所有串口设备可能共享同一个主设备号。
  • 次设备号:标识同一类设备中的具体设备实例。
    在这里插入图片描述
示例

/dev/ttyS0为例:

ls -l /dev/ttyS0
crw-rw---- 1 root dialout 4, 64 ...
字段含义
``表示字符设备
``主设备号
``次设备号

主设备号和次设备号的组合可以在内核中定位具体的驱动程序和设备。

2.2 文件操作接口

字符设备的行为由内核中的struct file_operations结构定义,用户对设备文件的每次操作(如读、写、打开等)都会触发相应的驱动代码。

操作描述
open打开设备文件,准备与设备交互
read从设备读取数据,将数据传递到用户空间
write将数据从用户空间传递到设备进行写入
release关闭设备文件,释放资源

三、字符设备驱动的实现步骤

字符设备驱动是实现设备功能的关键部分。以下是实现一个简单字符设备驱动的详细步骤:

3.1 环境准备

在开始编写驱动之前,需要准备以下开发环境:

  • 操作系统:Linux(推荐Ubuntu或其他常用发行版)。
  • 工具链:包括gccmakeinsmodrmmod等工具。
  • Linux内核源码:方便参考内核中的示例代码或实现细节。

3.2 实现步骤

1. 定义设备数据结构

字符设备的核心数据结构包括主设备号、次设备号以及设备功能的实现逻辑。以下是初始化数据结构的简单例子:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>

#define DEVICE_NAME "my_char_device"

static int major;
static struct cdev my_cdev;

在这里插入图片描述

2. 实现文件操作接口

文件操作接口是字符设备的核心功能,负责处理用户对设备的操作请求。以下是典型接口的实现:

static int my_open(struct inode *inode, struct file *file) {
    printk(KERN_INFO "Device opened\n");
    return 0;
}

static ssize_t my_read(struct file *file, char __user *buffer, size_t len, loff_t *offset) {
    printk(KERN_INFO "Read from device\n");
    return 0; // 假设没有实际数据
}

static ssize_t my_write(struct file *file, const char __user *buffer, size_t len, loff_t *offset) {
    printk(KERN_INFO "Write to device\n");
    return len; // 假设写成功
}

static int my_release(struct inode *inode, struct file *file) {
    printk(KERN_INFO "Device closed\n");
    return 0;
}

static struct file_operations fops = {
    .open = my_open,
    .read = my_read,
    .write = my_write,
    .release = my_release,
};
3. 注册字符设备

设备注册是字符设备驱动加载到内核的重要步骤。以下是注册代码示例:

static int __init my_device_init(void) {
    int ret;

    // 动态分配主设备号
    ret = alloc_chrdev_region(&major, 0, 1, DEVICE_NAME);
    if (ret < 0) {
        printk(KERN_ERR "Failed to allocate major number\n");
        return ret;
    }

    // 初始化cdev结构
    cdev_init(&my_cdev, &fops);
    my_cdev.owner = THIS_MODULE;

    // 添加设备到系统
    ret = cdev_add(&my_cdev, MKDEV(major, 0), 1);
    if (ret < 0) {
        unregister_chrdev_region(MKDEV(major, 0), 1);
        printk(KERN_ERR "Failed to add cdev\n");
        return ret;
    }

    printk(KERN_INFO "Device registered with major number %d\n", major);
    return 0;
}

static void __exit my_device_exit(void) {
    cdev_del(&my_cdev);
    unregister_chrdev_region(MKDEV(major, 0), 1);
    printk(KERN_INFO "Device unregistered\n");
}

module_init(my_device_init);
module_exit(my_device_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");


四、字符设备的测试与验证

字符设备的开发完成后,需要进行测试和验证,以确保驱动的正确性和功能的实现。以下是详细的测试步骤:

4.1 编译驱动模块

编写Makefile用于编译模块,内容如下:

obj-m += my_char_device.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

在驱动代码目录下运行以下命令进行编译:

make

4.2 加载驱动模块

使用以下命令加载驱动模块到内核中:

sudo insmod my_char_device.ko

4.3 验证模块加载

运行以下命令检查模块是否加载成功:

lsmod | grep my_char_device

或者查看内核日志,确认设备注册是否成功:

dmesg | tail

4.4 创建设备文件

根据驱动注册的主设备号,使用mknod命令创建设备文件。例如,如果主设备号是240:

sudo mknod /dev/my_device c 240 0
sudo chmod 666 /dev/my_device

4.5 测试设备功能

  1. 写入数据测试

    使用echo命令写入数据:

    echo "Hello, Device" > /dev/my_device
    
  2. 读取数据测试

    使用cat命令读取数据:

    cat /dev/my_device
    
  3. 验证日志输出

    查看内核日志,确保printk信息与预期一致:

    dmesg | tail
    

4.6 卸载驱动模块

在测试完成后,使用以下命令卸载驱动模块:

sudo rmmod my_char_device

同时删除设备文件:

sudo rm /dev/my_device

五、字符设备的高级功能

字符设备驱动不仅可以处理基本的读写操作,还支持更高级的功能,如异步I/O、多设备管理等。

5.1 异步I/O

异步I/O允许用户程序在等待I/O完成的同时执行其他任务。字符设备驱动可以通过支持pollselect系统调用来实现异步I/O功能。

5.2 并发访问控制

当多个进程同时访问字符设备时,需要确保并发访问的安全性。可以使用内核提供的同步机制,如互斥锁(mutex)或信号量(semaphore),来避免竞争条件。

以下是使用互斥锁的示例代码:

#include <linux/mutex.h>

static DEFINE_MUTEX(my_device_mutex);

static int my_open(struct inode *inode, struct file *file) {
    if (!mutex_trylock(&my_device_mutex)) {
        printk(KERN_INFO "Device is busy\n");
        return -EBUSY;
    }
    printk(KERN_INFO "Device opened\n");
    return 0;
}

static int my_release(struct inode *inode, struct file *file) {
    mutex_unlock(&my_device_mutex);
    printk(KERN_INFO "Device closed\n");
    return 0;
}

六、常见问题与解决方案

在字符设备驱动开发过程中,可能会遇到一些常见问题,以下是总结与解决方案:

问题类型原因解决方案
设备文件不可用主设备号或次设备号错误检查dmesg日志,确保号匹配正确
模块加载失败依赖的内核符号未定义确保内核版本匹配,检查modinfo依赖项
并发访问冲突缺少同步机制使用互斥锁或信号量保护关键代码区域
无法访问设备数据未正确实现read/write接口检查用户空间与内核空间数据传递逻辑

七、总结

通过本文,我们详细解析了Linux字符设备的概念、底层原理、驱动开发步骤以及测试方法。从设备文件的交互接口到高级功能的实现,字符设备驱动开发涵盖了Linux内核模块开发的核心知识点。希望本文能帮助你全面掌握字符设备驱动的开发技术。如果你有任何问题或建议,欢迎留言讨论!"}]}

[mysqld] # 设置mysql的安装目录,即你解压缩安装包的位置=# 设置mysql的安装目录,即你解压缩安装包的位置 # Adjust sizes as needed, experiment to find the optimal values.=# Adjust sizes as needed, experiment to find the optimal values. # The server defaults are faster for transactions and fast SELECTs.=# The server defaults are faster for transactions and fast SELECTs. #绑定IPv4和3306端口=#绑定IPv4和3306端口 # basedir=..... # Remove leading # to turn on a very important data integrity option: logging=# Remove leading # to turn on a very important data integrity option: logging # Remove leading # and set to the amount of RAM for the most important data=# Remove leading # and set to the amount of RAM for the most important data # Remove leading # to set options mainly useful for reporting servers.=# Remove leading # to set options mainly useful for reporting servers. #设置字符集为utf8=#设置字符集为utf8 # These are commonly set, remove the # and set as required.=# These are commonly set, remove the # and set as required. # join_buffer_size=128M #需要注意到的是这里是斜杠"/" 而不是Windows里面的反斜杠'\"=#需要注意到的是这里是斜杠"/" 而不是Windows里面的反斜杠'\" # 允许最大连接数=# 允许最大连接数 #开启查询缓存=#开启查询缓存 basedir=C:\Program Files (x86)\Beyond\\mysql-5.7.21-winx64 bind-address=0.0.0.0 # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.=# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # changes to the binary log between backups.=# changes to the binary log between backups. character_set_server=utf8 # datadir=..... datadir=C:\Program Files (x86)\Beyond\mysql-5.7.21-winx64\data explicit_defaults_for_timestamp=true innodb_buffer_pool_size=1024M # log_bin=# log_bin loose-default-character-set=utf8 max_connections=100 # 设置mysql数据库的数据的存放目录=# 设置mysql数据库的数据的存放目录 # port=..... # server_id=..... port=3306 # read_rnd_buffer_size=2M # sort_buffer_size=2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [WinMySQLadmin] Server=C:\Program Files (x86)\Beyond\mysql-5.7.21-winx64\bin\mysqld.exe [client] loose-default-character-set=utf8 #设置客户端字符集=#设置客户端字符集 port=3306
07-23
请查看以下的C++代码的编写要求,请根据代码要求开始编写代码 PURPOSE: This file is a proforma for the EEET2246 Laboratory Code Submission/Test 1. This file defines the assessment task which is worth 10% of course in total - there is no other documentation. At the BASIC FUNCTIONAL REQUIREMENTS level, your goal is to write a program that takes two numbers from the command line and perform and arithmetic operations with them. Additionally your program must be able to take three command line arguments where if the last argument is 'a' an addition is performed, and if 's' then subtraction is performed with the first two arguments. At the FUNCTIONAL REQUIREMENTS level you will be required to extend on the functionality so that the third argument can also be 'm' for multiplication,'d' for division and 'p' for exponential operations, using the first two arguments as the operands. Additionally, at this level basic error detection and handling will be required. The functionality of this lab is relatively simple: + - / * and "raised to the power of" The emphasis in this lab is to achieve the BASIC FUNCTIONALITY REQUIREMENTS first. Once you a basic program functioning then you should attempt the FUNCTIONALITY REQUIREMENTS and develop your code so that it can handle a full range of error detection and handling. ___________________________________________________________________________________________ ___ GENERAL SPECIFICATIONS (mostly common to all three EEET2246 Laboratory Code Submissions): G1. You must rename your file to lab1_1234567.cpp, where 1234567 is your student number. Your filename MUST NEVER EVER contain any spaces. _under_score_is_Fine. You do not need to include the 's' in front of your student number. Canvas will rename your submission by adding a -1, -2 etc. if you resubmit your solution file - This is acceptable. G2. Edit the name/email address string in the main() function to your student number, student email and student name. The format of the student ID line is CSV (Comma Separated Variables) with NO SPACES- student_id,student_email,student_name When the program is run without any operands i.e. simply the name of the executable such as: lab1_1234567.exe the program MUST print student ID string in Comma Separated Values (CSV) format with no spaces. For example the following text should be outputted to the console updated with your student details: "1234567,s1234567@student.rmit.edu.au,FirstName_LastName" G3. All outputs are a single error character or a numerical number, as specified by the FUNCTIONAL REQURMENTS, followed by a linefeed ( endl or \n). G4. DO NOT add more than what is specified to the expected console output. Do NOT add additional information, text or comments to the output console that are not defined within the SPECIFICATIONS/FUNCTIONAL REQURMENTS. G5. DO NOT use 'cin', system("pause"), getchar(), gets(), etc. type functions. Do NOT ask for user input from the keyboard. All input MUST be specified on the command line separated by blank spaces (i.e. use the argv and argc input parameters). G6. DO NOT use the characters: * / \ : ^ ? in your command line arguments as your user input. These are special character and may not be processed as expected, potentially resulting in undefined behaviour of your program. G7. All input MUST be specified on the command line separated by blank spaces (i.e. use the argc and argv[] input parameters). All input and output is case sensitive unless specified. G8. You should use the Integrated Debugging Environment (IDE) to change input arguments during the development process. G9. When your code exits the 'main()' function using the 'return' command, you MUST use zero as the return value. This requirement is for exiting the 'main()' function ONLY. A return value other than zero will indicate that something went wrong to the Autotester and no marks will be awarded. G10. User-defined functions and/or class declarations must be written before the 'main()' function. This is a requirement of the Autotester and failure to do so will result in your code scoring 0% as it will not be compiled correctly by the Autotester. Do NOT put any functions/class definitions after the 'main()' function or modify the comments and blank lines at the end of this file. G11. You MUST run this file as part of a Project - No other *.cpp or *.h files should be added to your solution. G12. You are not permitted to add any other #includes statements to your solution. The only libraries permitted to be used are the ones predefined in this file. G13. Under no circumstances is your code solution to contain any go_to labels - Please note that the '_' has been added to this description so that this file does not flag the Autotester. Code that contains go_to label like syntax will score 0% and will be treated as code that does not compile. G14. Under no circumstances is your code solution to contain any exit_(0) type functions. Please note that the '_' has been added to this description so that this file does not flag the Autotester. Your solution must always exit with a return 0; in main(). Code that contains exit_(0); label like syntax will score 0% and will be treated as code that does not compile. G15. Under no circumstances is your code solution to contain an infinite loop constructs within it. For example usage of while(1), for(int i; ; i++) or anything similar is not permitted. Code that contains an infinite loop will result in a score of 0% for your assessment submission and will be treated as code that does not compile. G16. Under no circumstances is your code solution to contain any S_l_e_e_p() or D_e_l_a_y() like statements - Please note that the '_' has been added to this description so that this file does not flag the Autotester. You can use such statements during your development, however you must remove delays or sleeps from your code prior to submission. This is important, as the Autotester will only give your solution a limited number of seconds to complete (i.e. return 0 in main()). Failure for your code to complete the required operation/s within the allotted execution window will result in the Autotester scoring your code 0 marks for that test. To test if your code will execute in the allotted execution window, check that it completes within a similar time frame as the provided sample binary. G17. Under no circumstances is your code solution to contain any characters from the extended ASCII character set or International typeset characters. Although such characters may compile under a normal system, they will result in your code potentially not compiling under the Autotester environment. Therefore, please ensure that you only use characters: a ... z, A ... Z, 0 ... 9 as your variable and function names or within any literal strings defined within your code. Literal strings can contain '.', '_', '-', and other basic symbols. G18. All output to console should be directed to the standard console (stdout) via cout. Do not use cerr or clog to print to the console. G19. The file you submit must compile without issues as a self contained *.cpp file. Code that does not compile will be graded as a non-negotiable zero mark. G20. All binary numbers within this document have the prefix 0b. This notation is not C++ compliant (depending on the C++ version), however is used to avoid confusion between decimal, hexadecimal and binary number formats within the description and specification provided in this document. For example the number 10 in decimal could be written as 0xA in hexadecimal or 0b1010 in binary. It can equally be written with leading zeroes such as: 0x0A or 0b00001010. For output to the console screen you should only ever display the numerical characters only and omit the 0x or 0b prefixes (unless it is specifically requested). ___________________________________________________________________________________________ ___ BASIC FUNCTIONAL REQUIREMENTS (doing these alone will only get you to approximately 40%): M1. For situation where NO command line arguments are passed to your program: M1.1 Your program must display your correct student details in the format: "3939723,s3939723@student.rmit.edu.au,Yang_Yang" M2. For situation where TWO command line arguments are passed to your program: M2.1 Your program must perform an addition operation, taking the first two arguments as the operands and display only the result to the console with a new line character. Example1: lab1_1234567.exe 10 2 which should calculate 10 + 2 = 12, i.e. the last (and only) line on the console will be: 12 M3. For situations where THREE command line arguments are passed to your program: M3.1 If the third argument is 'a', your program must perform an addition operation, taking the first two arguments as the operands and display only the result to the console with a new line character. M3.2 If the third argument is 's', your program must perform a subtraction operation, taking the first two arguments as the operands and display only the result to the console with a new line character. The second input argument should be subtracted from the first input argument. M4. For situations where less than TWO or more than THREE command line arguments are passed to your program, your program must display the character 'P' to the console with a new line character. M5. For specifications M1 to M4 inclusive: M5.1 Program must return 0 under all situations at exit. M5.2 Program must be able to handle integer arguments. M5.3 Program must be able to handle floating point arguments. M5.4 Program must be able to handle one integer and one floating point argument in any order. Example2: lab1_1234567.exe 10 2 s which should calculate 10 - 2 = 8, i.e. the last (and only) line on the console will be: 8 Example3: lab1_1234567.exe 10 2 which should calculate 10 + 2 = 12, i.e. the last (and only) line on the console will be: 12 Example4: lab1_1234567.exe 10 4 a which should calculate 10 + 4 = 14, i.e. the last (and only) line on the console will be: 14 ___________________________________________________________________________________________ ___ FUNCTIONAL REQUIREMENTS (to get over approximately 50%): E1. For situations where THREE command line arguments (other than 'a' or 's') are passed to your program: E1.1 If the third argument is 'm', your program must perform a multiplication operation, taking the first two arguments as the operands and display only the result to the console with a new line character. E1.2 If the third argument is 'd', your program must perform a division operation, taking the first two arguments as the operands and display only the result to the console with a new line character. E1.3 If the third argument is 'p', your program must perform an exponential operation, taking the first argument as the base operand and the second as the exponent operand. The result must be display to the console with a new line character. Hint: Consider using the pow() function, which has the definition: double pow(double base, double exponent); Example5: lab1_1234567.exe 10 2 d which should calculate 10 / 2 = 5, i.e. the last (and only) line on the console will be: 5 Example6: lab1_1234567.exe 10 2 p which should calculate 10 to power of 2 = 100, i.e. the last (and only) line on the console will be: 100 NOTE1: DO NOT use the character ^ in your command line arguments as your user input. Question: Why don't we use characters such as + - * / ^ ? to determine the operation? Answer: Arguments passed via the command line are processed by the operating system before being passed to your program. During this process, special characters such as + - * / ^ ? are stripped from the input argument stream. Therefore, the input characters: + - * / ^ ? will not be tested for by the autotester. See sections G6 and E7. NOTE2: the pow() and powl() function/s only work correctly for given arguments. Hence, your code should output and error if there is a domain error or undefined subset of values. For example, if the result does not produce a real number you code should handle this as an error. This means that if the base is negative you can't accept and exponent between (but not including) -1 and 1. If you get this then, output a MURPHY's LAW error: "Y" and return 0; NOTE3: zero to the power of zero is also undefined, and should also be treated MURPHY's LAW error. So return "Y" and return 0; In Visual Studio, the 0 to the power of 0 will return 1, so you will need to catch this situation manually, else your code will likely calculate the value as 1. ___ REQUIRED ERROR HANDLING (to get over approximately 70%): The following text lists errors you must detect and a priority of testing. NB: order of testing is important as each test is slight more difficult than the previous test. All outputs should either be numerical or upper-case single characters (followed by a new line). Note that case is important: In C, 'V' is not the same as 'v'. (No quotes are required on the output). E2. Valid operator input: If the third input argument is not a valid operation selection, the output shall be 'V'. Valid operators are ONLY (case sensitive): a addition s subtraction m multiplication d division p exponentiation i.e. to the power of: 2 to the power of 3 = 8 (base exponent p) E3. Basic invalid number detection (Required): Valid numbers are all numbers that the "average Engineering graduate" in Australia would consider valid. Therefore if the first two arguments are not valid decimal numbers, the output shall be 'X'. For example: -130 is valid +100 is valid 1.3 is valid 3 is valid 0.3 is valid .3 is valid ABC123 is not valid 1.3.4 is not valid 123abc is not valid ___ ERROR HANDLING (not marked by the autotester): E4. Intermediate invalid number detection (NOT TESTED BY AUTOTESTER - for your consideration only): If the first two arguments are not valid decimal numbers, the output shall be 'X'. Using comma punctuated numbers and scientific formatted numbers are considered valid. For example: 0000.111 is valid 3,000 is valid - NB: atof() will read this as '3' not as 3000 1,000.9 is valid - NB: atof() will read this as '1' not as 1000.9 1.23e2 is valid 2E2 is valid -3e-0.5 is not valid (an integer must follow after the e or E for floating point number to be valid) 2E2.1 is not valid e-1 is not valid .e3 is not valid E5. Advanced invalid number detection (NOT TESTED BY AUTOTESTER - for your consideration only): If the first two arguments are not valid decimal numbers, the output shall be 'X'. 1.3e-1 is valid 1,00.0 is valid - NB: if the comma is not removed atof() will read this as '1' not as 100 +212+21-2 is not valid - NB: mathematical operation on a number of numbers, not ONE number 5/2 is not valid - NB: mathematical operation on a number of numbers, not ONE number HINT: consider the function atof(), which has the definition: double atof (const char* str); Checking the user input for multiple operators (i.e. + or -) is quite a difficult task. One method may involve writing a 'for' loop which steps through the input argv[] counting the number of operators. This process could also be used to count for decimal points and the like. The multiple operator check should be considered an advanced task and developed once the rest of the code is operational. E6. Input number range checking: All input numbers must be between (and including) +2^16 (65536) or -2^16 (-65536). If the operand is out of range i.e. too small or too big, the output shall be 'R'. LARGE NUMBERS: is 1.2e+999 acceptable input ? what happens if you enter such a number ? try and see. Hint: #INF error - where and when does it come up ? SMALL NUMBERS: is 1.2e-999 acceptable input ? what happens if you enter such a number ? try and see. Test it by writing your own test program. E7. ERROR checks which will NOT be performed are: E7.1 Input characters such as: *.* or / or \ or : or any of these characters: * / ^ ? will not be tested for. E7.2 Range check: some computer systems accept numbers of size 9999e999999 while others flag and infinity error. An infinity error becomes an invalid input Therefore: input for valid numbers will only be tested to the maximum 9.9e99 (Note: 9.9e99 is out of range and your program should output 'R') E8. Division by zero should produce output 'M' E9. Error precedence: If multiple errors occur during a program execution event, your program should only display one error code followed by a newline character and then exit (using a return 0; statement). In general, the precedence of the error reported to the console should be displayed in the order that they appear within this proforma. However to clarify the exact order or precedence for the error characters, the precedence of the displayed error code should occur in this order: 'P' - Incorrect number of input command line arguments (see M4) 'X' - Invalid numerical command line argument 'V' - Invalid third input argument 'R' - operand (command line argument) value out of range 'M' - Division by zero 'Y' - MURPHY'S LAW (undefined error) Therefore if an invalid numerical command line argument and an invalid operation argument are passed to the program, the first error code should be displayed to the console, which in this case would be 'X'. Displaying 'V' or 'Y' would be result in a loss of marks. E10. ANYTHING ELSE THAT CAN GO WRONG (MURPHY'S LAW TEST): If there are any other kinds of errors not covered here, the output shall be 'Y'. Rhetorical question: What for example are the error codes that the Power function returns ? If this happens then the output shall be 'Y'. See section E1.3, NOTE2. ___________________________________________________________________________________________ ___ HINTS: - Use debug mode and a breakpoint at the return statement prior to program finish in main. - What string conversion routines, do you know how to convert strings to number? Look carefully as they will be needed to convert a command line parameter to a number and also check for errors. - ERROR CHECKING: The basic programming rules are simple (as covered in lectures): 1) check that the input is valid. 2) check that the output is valid. 3) if any library function returns an error code USE IT !!! CHECK FOR IT !!! - Most conversion routines do have inbuilt error checking - USE IT !!! That means: test for the error condition and take some action if the error is true. If that means more than 50% of your code is error checking, then that's the way it has to be. ____________________________________________________________________________________________ */ // These are the libraries you are allowed to use to write your solution. Do not add any // additional libraries as the auto-tester will be locked down to the following: #include <iostream> #include <cstdlib> #include <time.h> #include <math.h> #include <errno.h> // leave this one in please, it is required by the Autotester! // Do NOT Add or remove any #include statements to this project!! // All library functions required should be covered by the above // include list. Do not add a *.h file for this project as all your // code should be included in this file. using namespace std; const double MAXRANGE = pow(2.0, 16.0); // 65536 const double MINRANGE = -pow(2.0, 16.0); // All functions to be defined below and above main() - NO exceptions !!! Do NOT // define function below main() as your code will fail to compile in the auto-tester. // WRITE ANY USER DEFINED FUNCTIONS HERE (optional) // all function definitions and prototypes to be defined above this line - NO exceptions !!! int main(int argc, char *argv[]) { // ALL CODE (excluding variable declarations) MUST come after the following 'if' statement if (argc == 1) { // When run with just the program name (no parameters) your code MUST print // student ID string in CSV format. i.e. // "studentNumber,student_email,student_name" // eg: "3939723,s3939723@student.rmit.edu.au,Yang_Yang" // No parameters on command line just the program name // Edit string below: eg: "studentNumber,student_email,student_name" cout << "3939723,s3939723@student.rmit.edu.au,Yang_Yang" << endl; // Failure of your program to do this cout statement correctly will result in a // flat 10% marks penalty! Check this outputs correctly when no arguments are // passed to your program before you submit your file! Do it as your last test! // The convention is to return Zero to signal NO ERRORS (please do not change it). return 0; } //--- START YOUR CODE HERE. // The convention is to return Zero to signal NO ERRORS (please do not change it). // If you change it the AutoTester will assume you have made some major error. return 0; } // No code to be placed below this line - all functions to be defined above main() function. // End of file.
08-16
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值