c8051f系列单片机 C程序编写

c8051f系列单片机 C程序编写
c8051f系列单片机 C程序编写

6

C8051F020 C Programming

6.0 Introduction

Initialization and Startup 6.1 Register

Definitions,

Code

Basic C program Structure

6.2 Programming Memory Models

Overriding the default memory model, Bit-valued data,

Special Function Registers, Locating Variables at absolute

addresses

6.3 C Language Control Structures

Relational Operators, Logical Operators, Bitwise Logical

Operators, Compound Operators, Making Choices (if..else,

switch .. case), Repetition(for loop, while loop), Waiting for

Events, Early Exits

6.4 Functions

Standard functions - Initializing System Clock, Memory Model

Used for a Function

6.5 Interrupt

Functions

Timer 3 Interrupt Service Routine, Disabling Interrupts before

Initialization, Timer 3 Interrupt Initialization, Register Banks 6.6 Reentrant

functions

6.7 Pointers

A Generic Pointer in Keil TM C, Memory Specific Pointers

6.8 Summary of Data Types

Questions

6.9 Tutorial

2 Chapter 6 C8051F020 C Programming 6.0 Introduction

This chapter introduces the Keil TM C compiler for the Cygnal C8051F020

board. We assume some familiarity with the C programming language to

the level covered by most first courses in the C language.

Experienced C programmers who have little experience with the C8051F020 architecture should become familiar with the system. The

differences in programming the C8051F020 in C compared to a standard

C program are almost all related to architectural issues. These

explanations will have little meaning to those without an understanding of

the C8051F020 chip.

The Keil TM C compiler provided with the Cygnal C8051F020 board does

not come with a floating point library and so the floating point variables

and functions should not be used. However if you require floating point

variables a full license for the Keil TM C compiler can be purchased.

6.1 Register Definitions, Initialization and Startup

Code

C is a high level programming language that is portable across many

hardware architectures. This means that architecture specific features

such as register definitions, initialization and start up code must be made

available to your program via the use of libraries and include files.

For the 8051 chip you need to include the file reg51.h or using the

Cygnal C8051F020-TB development board include the file c8051f020.h:

#include

Or

#include

These files contain all the definitions of the C8051F020 registers. The

standard initialization and startup procedures for the C8051F020 are

contained in startup.a51. This file is included in your project and will be

assembled together with the compiled output of your C program. For

custom applications, this start up file might need modification.

Chapter 6 C8051F020 C Programming 3

Basic C program structure

The following is the basic C program structure; all the programs you will write will have this basic structure.

Note: All variables must be declared at the start of a code block, you cannot declare variables among the program statements.

You can test this program in the Cygnal IDE connected to the C8051F020 development board. You won’t see anything happening on the board, but you can step through the program using the debugger.

6.2 Programming Memory Models

The C8051F020 processor has 126 Bytes of directly addressable internal memory and up to 64 Kbytes of externally addressable space. The Keil TM C compiler has two main C programming memory models, SMALL and LARGE which are related to these two types of memory. In the SMALL memory model the default storage location is the 126 Bytes of internal memory while in the LARGE memory model the default storage location is the externally addressed memory.

//------------------------------------------------- // Basic blank C program that does nothing // other than disable the watch dog timer

//------------------------------------------------- // Includes

//------------------------------------------------- #include // SFR declarations

void main (void) {

// disable watchdog timer WDTCN = 0xde; WDTCN = 0xad;

while(1); // Stops program terminating and

// restarting

}

//-------------------------------------------------

4 Chapter 6 C8051F020 C Programming

The default memory model required is selected using the pragma compiler control directive :

Any variable declared in this file (such as the variable X above) will be stored in the internal memory of the C8051F020.

The choice of which memory model to use depends on the program, the anticipated stack size and the size of data. If the stack and the data cannot fit in the 128 Bytes of internal memory then the default memory model should be LARGE, otherwise SMALL should be used.

Yet another memory model is the COMPACT memory model. This memory model is not discussed in this chapter. More information on the compact model can be found in the document Cx51 Compiler User’s Guide for Keil TM Software .

You can test the different memory models with the Cygnal IDE connected to the C8051F020-TB development board. Look at the symbol view after downloading your program and see in which memory addresses the compiler has stored your variables.

Overriding the default memory model

The default memory model can be overridden with the use of Keil TM C programming language extensions that tell the compiler to place the variables in another location. The two main available language extensions are data and xdata :

The integer variable X and character variable Initial are stored in the internal memory while the integer variable Y and character variable SInitial are stored in the external memory overriding any default memory model.

#pragma small int X; int data X;

char data Initial; int xdata Y;

char data SInitial;

Chapter 6 C8051F020 C Programming 5 Constant variables can be stored in the read-only code section of the

C8051F020 using the code language extension:

const char code CR=0xDE;

In general, access to the internal memory is the fastest, so frequently

used data should be stored here while less frequently used data should

be stored on the external memory.

The memory storage related language extensions, bdata, and associated data types bit, sbit, sfr and sfr16 will be discussed in the

following sections. Additional memory storage language extensions

including, pdata and idata, are not discussed in this chapter; refer to the

document Cx51 Compiler User’s Guide for Keil TM Software for

information on this.

Bit-valued Data

Bit-valued data and bit-addressable data must be stored in the bit-

addressable memory space on the C8051F020 (0x20 to 0x2F). This

means that bit- valued data and bit-addressable data must be labelled as

such using the bit, sbit and bdata.

Bit-addressable data must be identified with the bdata language

extension:

int bdata X;

The integer variable X declared above is bit-addressable.

Any bit valued data must be given the bit data type, this is not a standard

C data type:

bit flag;

The bit-valued data flag is declared as above.

6 Chapter 6 C8051F020 C Programming

The sbit data type is used to declare variables that access a particular bit field of a previously declared bit-addressable variable.

X7flag declared above is a variable that references bit 7 of the integer variable X .

You cannot declare a bit pointer or an array of bits.

The bit valued data segment is 16 bytes or 128 bits in size, so this limits the amount of bit-valued data that a program can use.

Special Function Registers

As can be seen in the include files c8051f020.h or reg51.h , the special function registers are declared as a sfr data type in Keil TM C. The value in the declaration specifies the memory location of the register:

Extensions of the 8051 often have the low byte of a 16 bit register preceding the high byte. In this scenario it is possible to declare a 16 bit special function register, sfr16, giving the address of the low byte:

The memory location of the register used in the declaration must be a constant rather than a variable or expression.

bdata X;

sbit X7flag = X^7; /* bit 7 of X*/ /* BYTE Register */sfr P0 = 0x80; sfr P1 = 0x90; sfr16 TMR3RL = 0x92;// Timer3 reload value sfr16 TMR3 = 0x94;// Timer3 counter

Chapter 6 C8051F020 C Programming 7

Locating Variables at absolute addresses

Variables can be located at a specific memory location using the _at_ language extension:

The above statement locates the integer X at the memory location 0x40. The _at_ language extension can not be used to locate bit addressable data. 6.3 C language control structures

C language is a structured programming language that provides sequence , selection and repetition language constructs to control the flow of a program.

The sequence in which the program statements execute is one after another within a code block. Selection of different code blocks is determined by evaluating if and else if statements (as well as switch-case statements) while repetition is determined by the evaluation of for loop or while loop constructs.

Relational Operators

Relational operators compare data and the outcome is either True or False. The if statements, for loops and while loops can make use of C relational operators. These are summarized in Table 6.1.

Operator Description

==

Equal to != Not Equal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to

Table 6.1 Relational Operators

int X _at_ 0x40;

8 Chapter 6 C8051F020 C Programming

Logical Operators

Logical operators operate on Boolean data (True and False) and the

outcome is also Boolean. The logical operators are summarized in Table

6.2.

Operator Description

AND

&& Logical

OR

|| Logical

! Logical

NOT

Table 6.2 Logical Operators

Bitwise Logical Operators

As well as the Logical operators that operate on integer or character

data, the C language also has bitwise logical operators. These are summarized in Table 6.3.

Operator Description

AND

& Bitwise

OR

| Bitwise

NOT

~ Bitwise

XOR

^ Bitwise

Table 6.3 Bit valued logical operators

Bitwise logical operators operate on each bit of the variables individually.

Example:

X = 0x40 | 0x21;

The above statement will assign the value 0x61 to the variable X.

0000

0x40 0100

0x21 0010 0001 bitwise logical OR

0x61 0110

0001

Chapter 6 C8051F020 C Programming 9

Compound Operators

C language provides short cut bitwise operators acting on a single variable similar to the +=, -=, /= and *= operators. These are summarized in Tables 6.4 and 6.5.

Operator

Description

Example

Equivalent

+=

Add to variable

X += 2 X=X + 2 -= Subtract from

variable X -= 1 X=X - 1 /= Divide variable X /= 2 X=X / 2 *= Multiply variable

X *= 4

X=X * 4

Table 6.4 Compound Arithmetic Operators

Operator Description Example Equivalent

&=

Bitwise And with variable

X &= 0x00FF X=X & 0x00FF |= Bitwise Or with variable X |= 0x0080

X=X | 0x0080 ^=

Bitwise XOR with variable

X ^= 0x07A0

X=X | 0x07A0

Table 6.5 Compound Bitwise Operators

Example: Initialising Crossbar and GPIO ports

We can initialize the crossbar and GPIO ports using the C bitwise

operators.

//-- Configures the Crossbar and GPIO ports XBR2 = 0x40; //-- Enable Crossbar and weak

// pull-ups (globally)

P1MDOUT |= 0x40;//-- Enable P1.6 (LED) as push-

// pull output

10 Chapter 6 C8051F020 C Programming

Making Choices

Figure 6.1 Flow chart for selection

Choices are made in the C language using an if else statement.

When the Condition is evaluated as True the first block is executed and if the Condition evaluates as being False the second block is executed. More conditions can be created using a sequence of if and else if statements.

In some situations, when there is a list of integer or character choices a switch-case statement can be used.

if (x > 10) { y=y+1; } else

{ y=y-1; } if (x > 10)

{ y=y+1; } else if (x > 0)

{ y=y-1; } else

{ y=y-2; }

Chapter 6 C8051F020 C Programming 11

When the variable x in the switch statement matches one of the case statements, that block is executed. Only when the break statement is reached does the flow of control break out of the switch statement. The default block is executed when there are no matches with any of the case statements.

If the break statements are missing from the switch-case statement then the flow will continue within the switch-case block until a break statement or the end of the switch-case block is reached.

Repetition

Numeric repetition of a code block for a fixed set of times is achieved using a for loop construct.

switch (x) {

case 5:

y =

y+2; break; case 4: case 3: y =

y+1; break; case 2: case 1: y =

y-1; break; default:

y =y-2; break; } int i; int sum=0; for( i = 0; i<10; i++) { sum = sum + i; }

12 Chapter 6 C8051F020 C Programming

Figure 6.2 Flow chart for a for loop

When the looping required is not determined by a fixed number of counts but more complex conditions we normally use the while loop construct to control the process.

Figure 6.3 Flow chart for a while loop

The while loop repeats the loop while the condition specified is true.

Chapter 6 C8051F020 C Programming 13

Waiting for events

We can use a while loop to wait for the crystal oscillator valid flag to be set.

Early Exits

When executing a code block or a loop, sometimes it is necessary to exit the current code block. The C language provides several mechanisms to do this.

The break statement will move the flow of control outside the end of the current loop.

The continue statement skips the remaining code in the current loop, but continues from the start of the code block of the loop (after incrementing and checking that the loop should not terminate)

//--wait till XTLVLD pin is set while ( !(OSCXCN & 0x80) ); int i;

int sum=0;

for( i = 0; i<10; i++) {

sum = sum + i;

if (sum > 25) break; } int i;

int sum=0;

for( i = 0; i<10; i++) {

if (i == 5) continue; sum = sum + i; }

14 Chapter 6 C8051F020 C Programming

6.4 Functions

Functions in C are declared using the return data type, the data type of the parameters and the body of the function.

Standard functions in Keil TM C are not re-entrant and so should not be called recursively. This is the case as parameters and local variables are stored in a standard location for all calls to a particular function. This means that recursive calls will corrupt the data passed as arguments to the function as well as the local variables.

A stack, starting straight after the last data stored in internal memory is used to keep track of function calls, but only the return address is stored on the stack, so conserving space. You can see the operation of the stack in the Cygnal IDE.

Test the functions using the Cygnal IDE connected to the c8051f020 development board. You will notice that sometimes the compiler optimizations will result in some variables sharing the same memory address!

Standard Function - Initialising System Clock

We can write a C function to initialize the system clock.

int square (int x){

return x*x; }

Chapter 6 C8051F020 C Programming 15

Memory Model Used for a Function

The memory model used for a function can override the default memory model with the use of the small , compact or large keywords.

6.5 Interrupt Functions

The basic 8051 has 5 possible interrupts which are listed in Table 6.6.

int square (int x)large {

return x*x; }

void Init_Clock(void){

OSCXCN = 0x67; //-- 0110 0111b

//-- External Osc Freq Control Bits (XFCN2-0) set // to 111 because crystal frequency > 6.7 MHz //-- Crystal Oscillator Mode (XOSCMD2-0) set to // 110

//-- wait till XTLVLD pin is set while ( !(OSCXCN & 0x80) );

OSCICN = 0x88; //-- 1000 1000b

//-- Bit 2 : Internal Osc. disabled (IOSCEN = 0) //-- Bit 3 : Uses External Oscillator as System // Clock (CLKSL = 1)

//-- Bit 7 : Missing Clock Detector Enabled // (MSCLKE = 1) }

16 Chapter 6 C8051F020 C Programming

Interrupt No.

Description

Address

0 External INT 0 0x0003 1 Timer/ Counter 0 0x000B 2 External INT 1 0x0013 3

Timer/ Counter 1

0x001B

4 Serial Port

0x0023

Table 6.6 8051 Interrupts

The Cx51 has extended these to 32 interrupts to handle additional

interrupts provided by manufacturers who have extended the 8051 like the Cygnal C8051F020. The 22 interrupts implemented in C8051F020 are discussed in detail in Chapter 11.

An interrupt function is declared using the interrupt key word followed by the required interrupt number.

Interrupt functions must not take any parameters and not return any parameters. Interrupt functions will be called automatically when the interrupt is generated, they should not be called in normal program code, this will generate a compiler error.

Timer 3 Interrupt Service Routine

We can write a timer 3 Interrupt service routing that changes the state of an LED depending on whether a switch is pressed

int count;

void timer1_ISR (void) interrupt 3 {

count++; }

Chapter 6 C8051F020 C Programming 17

Disabling Interrupts before Initialization

Before using interrupts (such as the timer interrupts) they should be initialized. Before initialization interrupts should be disabled so that there is no chance that the interrupt service routine is called before initialization is complete.

When initialization has been completed the interrupts can be enabled. EA = 0;

//--disable global interrupts

EA = 1;

//--enable global interrupts

//--This routine changes the state of the LED // whenever Timer3 overflows.

void Timer3_ISR (void) interrupt 14 {

unsigned char P3_input;

TMR3CN &= ~(0x80); //-- clear TF3

P3_input = ~P3;

if (P3_input & 0x80) //-- if bit 7 is set, { // then switch is pressed LED_count++;

if ( (LED_count % 10) == 0)

{ //-- do every 10th count LED = ~LED; //-- change state of LED LED_count = 0; } } }

18 Chapter 6 C8051F020 C Programming

Timer 3 Interrupt Initialization

We can put the timer 3 initialization statements within a C function Register Banks

Normally a function uses the default set of registers. However there are 4 sets of registers available in the C8051F020. The register bank that is currently in use can be changed for a particular function via the using Keil TM C language extension.

The register bank specified by the using statement ranges from 0 to 3. The register bank can be specified for normal functions, but are more appropriate for interrupt functions. When no register bank is specified in an interrupt function the state of the registers must be stored on the stack before the interrupt service routine is called. If a new register bank is specified then only the old register bank number needs to be copied to the stack significantly improving the speed of the interrupt service routine.

int count;

void timer1 (void) interrupt 3 using 1 {

count++; } //-- Configure Timer3 to auto-reload and generate //-- an interrupt at interval specified by //-- using SYSCLK/12 as its time base. void Init_Timer3 (unsigned int counts) {

TMR3CN = 0x00; //-- Stop Timer3; Clear TF3; //-- use SYSCLK/12 as timebase

TMR3RL = -counts; //-- Init reload values

TMR3 = 0xffff; //-- set to reload immediately EIE2 |= 0x01; //-- enable Timer3 interrupts TMR3CN |= 0x04; //-- start Timer3 by setting

// TR3 (TMR3CN.2) to 1

}

Chapter 6 C8051F020 C Programming 19

6.6 Reentrant functions

Normal Keil TM C functions are not re-entrant. A function must be declared

as re-entrant to be able to be called recursively or to be called simultaneously by two or more processes. This capability is often required in real-time applications or in situations when interrupt code and non-interrupt code need to share a function.

A re-entrant function stores the local variables and parameters on a simulated stack. The default position of the simulated stack is at the end of internal memory (0xFF). The starting positions of the simulated stack are initialized in startup.a51 file.

The simulated stack makes use of indirect addressing; this means that when you use the debugger and watch the values of the variables they will contain the address of the memory location where the variables are stored. You can view the internal RAM (address 0xff and below) to see the parameters and local variable placed on the simulated stack.

6.7 Pointers

Pointers in C are a data type that stores the memory addresses. In standard C the data type of the variable stored at that memory address must also be declared:

A Generic Pointer in Keil TM C

Since there are different types of memory on the C8051F020 processor there are different types of pointers. These are generic pointers and memory specific pointers . In standard C language we need to declare the correct data type that the pointer points to. In Keil TM C we also need to be

int fact (int X) reentrant {

if ( X==1) { return 1; }

else { return X*fact(X-1); } } int * X;

20 Chapter 6 C8051F020 C Programming

mindful of which memory model we are pointing to when we are using memory-specific pointers. Generic pointers remove this restriction, but are less efficient as the compiler needs to store what memory model is being pointed to. This means that a generic pointer takes 3 bytes of storage - 1 byte to store the type of memory model that is pointed to and two bytes to store the address. You may also explicitly specify the memory location that the generic pointer is stored in, to override the default memory model.

Memory Specific Pointers

A memory specific pointer points to a specific type of memory. This type of pointer is efficient as the compiler does not need to store the type of memory that is being pointed to. The data type of the variable stored at the memory location must be specified.

You may also specify the memory location that the memory-specific pointer is stored in, to override the default memory model.

int * Y; char * ls; long * ptr; int * xdata Y; char * idata ls; long * data ptr;

int data * xdata Y;

char xdata * idata ls; long idata * data ptr;

int xdata * Y; char data * ls; long idata * ptr;

C语言简易计算器的实现

目录 一.课程设计目的 (1) 二.设计环境 (1) 三.设计内容 (1) 四.设计说明 (2) 五.设计程序流程图 (2) 六.调试 (4) (1)错误原因分析一 (4) (2)语法错误 (5) (3)逻辑错误 (5) 七. 调试结果图 (6) 八. 结论与心得体会 (7) 九.附录 (8) 具体代码实现 (8) 十.参考文献 (18)

一.课程设计目的 1.通过一个学期的学习,我认为要学号C语言程序这门课程,不仅要认真阅读课本知识,更重要的是要通过上机实践来巩固我 们的知识,特别是学计算机专业的,我们更应该注重这一环节, 只有这样我们才能成为一个合格的计算机人才。通过这一个课程 设计,进一步来巩固所学的语句,如:循环,和分支结构的运用。还要熟悉四则运算和函数的算法。 2.通过这次课程设计扩展自己的知识面,课本上的东西是远 远不够的,可以通过上网或去图书馆查资料等方式得到一些新的 知识, 3.通过课程设计,加深对课程化设计思想的理解,能进行一 个系统功能分析,并设计一个合理的模块化结构,提高程序开发 能力。 二.设计环境 1.硬件:一台完整的电脑,包括键盘、鼠标,最小硬盘空间1GHz 2.软件:安装有Microsoft visual c++6.0 三.设计内容 以简易计算器为例,通过对简单应用软件计算器的设计,编制、调试,实现

简单的加,减,乘,除等运算,以学习应用MFC库类编写对话框的原理,加深对C++类的学习及应用。 (1)定义一个结构体类型数组,输入0~9及+、--、*等符号的信息,将其信息存入文件中; (2)输入简单的加减乘除算术计算式,并在屏幕上显示计算结果; (3)画出部分模块的流程图; (4)编写代码; (5)程序分析与调试。 四.设计说明 1)包含的功能有:加、减、乘、除运算,开方、平方等功能。 (2)计算器上数字0—9为一个控件数组,加、减、乘、除为一个控件数组,其余为单一的控件。 (3)输入的原始数据、运算中间数据和结果都显示在窗口顶部的同一个标签中。 (4)计算功能基本上是用系统内部函数。 (5)程序可以能自动判断输入数据的正确性,保证不出现多于一个小数点、以0开头等不正常现象。 (6)“CE”按钮可以清除所有已输入的数据从头计算 五.设计程序流程图

C8051F021单片机实验指导

提要:实验项目 1、单片机的IO编程 实验1 IO开关量输入实验 实验2 IO输出驱动继电器(或光电隔离器)实验 实验3 IO输入/输出------半导体温度传感器DS18B20实验2、单片机的中断系统 实验1 外部外部中断----脉冲计数实验 3、单片机的定时器/计数器 实验1 计数器实验 实验2 秒时钟发生器实验 4、单片机的串口特点和编程 实验1 P C机串口通讯实验 实验2 R S485通讯实验 5、存储器 实验1 RAM存储器读写实验 6、PWM发生器 实验1 PWM发生器(模拟)实验 实验1 PWM发生器(内部)实验 7、WDG看门狗 实验1 外扩WDG(MAX813)实验 实验2 WDG(内部)实验

8、SPI总线 实验1 SPI(模拟)实验-----TLC2543 AD转换实验 实验2 SPI(模拟)实验-----TLV5616 DA转换实验 9、I2C总线 实验1 I2C(模拟)实验-----AT24C01读写实验 实验2 I2C(内部)实验-----AT24C01读写实验 10、综合实验 实验1 HD7279LED数码管显示实验 实验2 HD7279键盘实验 实验3 外部中断---电机转速显示实验 11、步进电机正反转实验 12、TFT液晶显示彩色条纹实验 13、16X16LED点阵显示汉字实验 一、单片机的IO编程 实验1 IO开关量输入实验 目的:学习单片机读取IO引脚状态的的方法。 内容:编程读取IO引脚状态。 设备:EL-EMCU-I试验箱、EXP-C8051F021 CPU板。 编程:首先要把相关的引脚设置在IO的输入状态,然后写一个循环,不停地检测引脚的状态。 步骤: 1、将CPU板正确安放在CPU接口插座上, 2、连线:用导线将试验箱上MCU部分的IO1--- IO8分别连接到SWITCH 的8个拨码开关的K1---K8的输出端子K1---K8上,连接好仿真器。

c语言程序设计课程计算器设计报告

课程设计说明书 题目计算器程序设计 起讫日期 2006 年 7月 3日至 2006 年 8月 6日 所在院系软件学院 专业机械+软件班级 04-2 学生姓名偶偶哦学号 指导教师 2006年 8 月日

摘要 当今社会是信息社会,科技经济高速发展的社会!为了更方便人们的工作生活和加速人们处理信息的速度,计算器应运而生。由于它体积小巧,携带方便,价格便宜,构造简单等诸多的优点成为人们生活中的必备品! 随着科技的发展计算器的种类变得更多,功能变得更强大,体积变得更小!电脑的出现改变人们的生活习惯,很多事情都可以电脑来完成!电脑的更大一个优点就是可以通过软件的应用无限的延伸电脑功能的外延!下面我们将用我们学习的c语言编写一个简易的计算器程序!实现简单的初步的计算功能! 本程序的编写基础是Tubro 汉化版,它在tubro c的原有基础上实现了多汉字的支持方便了我们的使用。生成的程序可移植性强兼容性好稳定!现在只实现了加、减、乘、除、求幂、求模,求平方根,求Sin,求Cos,求Log10,以及一个时钟原代码。这个系统是基于软件发展的生命周期来研制的,它可以直接输入数学表达式,不需要任何转换,就可以直接输出数学四则运算的结果。但是,每次只能运算一个表达式。不能运算多个表达式。在程序里面在添加一组选择函数即可。本论文主要介绍了本课题的开发背景,开发的过程和所要完成的功能。重点的说明了系统设计思想,设计的步骤、难点技术和解决方案。 关键词:C语言 Tubro c 汉化版计算器时钟

目录 第一章综述 (1) 1.1 课题的现实意义 (1) 1.2 软件环境 (1) 1.3 硬件环境 (1) 第二章系统设计流程图 (2) 2.1 系统流程图 (2) 2.2 主要功能表 (2) 第三章系统分析和设计 (3) 3.1 图形的绘制和输出 (3) 3.2 文本的输出显示 (3) 3.3 计算函数的调用 (4) 3.4 程序的运行和退出 (5) 第四章系统测试 (6) 4.1 系统测试 (6) 4.2 调试 (6) 4.3 错误原因分析一 (6) 4.4 错误原因分析二 (6) 第五章用户使用说明书 (8)

C语言课程设计 简单计算器程序

课程设计名称:C语言课程设计课程设计题目:简单计算器程序

目录 第1章需求分析 (1) 1.1设计要求 (1) 1.2任务 (1) 第2章总体设计 (2) 2.1设计简介及设计方案论述 (2) 2.2功能模块层次图 (2) 第3章详细设计 (3) 3.3由(后缀)逆波兰表达式计算中缀表达式原理 (8) 3.3.1算法描述 (8) 第4章调试分析 (10) 4.1程序设计中所遇到的错误及犯错的原因 (10) 4.2错误的解决方法 (10) 第5章用户手册 (11) 总结 (15) 参考文献 (16) 附录(程序清单) (17)

第1章需求分析 1.1 设计要求 (1)用 C 语言数据结构实现程序设计; (2)利用结构体、栈、进行相关信息处理; (2)系统的各个功能模块要求用函数的形式实现; (4)界面简单,可操作性高。 1.2任务 (1)定义一个结构体类型数组,输入0~9 及+、--、*等符号的信息,将其信息存储起来; (2)输入简单的加减乘除算术计算式,并在屏幕上显示逆波兰(后缀式)表达式和计算结果; (3)编写代码; (4)程序分析与调试。 说明: 本课程设计将实现一个简单计算器。在功能上尽量模仿windows 的计算器。系统界面不做牵制要求。该程序能实现标准型中+、-、*、/、(、)、.、的混合运算表达式(一般意义上的中缀表达式),将其转换成逆序波兰表达式(后缀表达式)并计算输出结果。在进行运算后可以选择继续运算或者结束当前运算。即时准确地获得需要的计算的结果,充分降低了数字计算的难度和节约了时间,对人们的生活有一定的帮助。

第2章 总体设计 2.1设计简介及设计方案论述 逆波兰表达式又叫做后缀表达式。在通常的表达式中,二元运算符总是置于与之相 关的两个运算对象之间,所以,这种表示法也称为中缀表达式。波兰逻辑学家 J.Lukasiewicz 于 1929 年提出了另一种表示表达式的方法。按此方法,每一运算符都置 于其运算对象之后,故称为后缀表达式。 后缀表达式的优点是显而易见的, 编译器在处理时候按照从左至右的顺序读取逆波 兰表达式,遇到运算对象直接压入堆栈,遇到运算符就从堆栈提取后进的两个对象进行计算,这个过程正好符合了计算机计算的原理。后缀表达式比前缀表达式更加易于转换,并且它的最左面一定为数字,这一点在实 际编程的时候就会体会到它的好处了。 逆波兰表达式有一个更大的优点,就是拆括号,根据运算符的级别将中缀表达式转 换成逆波兰表达式后,运算顺序就已经替代了运算符的级别,这样也避免了括号提高运 算级别的特殊处理。 2.2功能模块层次图 将算术表达式转化为逆波兰表达式 计算逆波兰表达式的值 简单计算器 表 达 式 格 式 转 换 系统 求 值 计 算 系 统

C语言实现计算器功能

实验一多功能计算器 一、问题描述 设计一个多功能计算器,可以完成基本的计算。 设计要求: 1、具备整型数据、浮点型数据的算术(加、减、乘、除)运算功能。依次输入第一个运算数、运算符(+,-,*,/)、第二个运算数,然后输出结果。结果可以作为下一个运算的第一运算数。按‘C’清屏,按‘R’返回菜单。 例如:输入:2 + 5 输出:7 2、实现单运算符表达式计算的功能。输入的操作数可以包含整数或浮点数。输入表达式如下: 例如:输入:2+5 输出:7 二、算法说明 1.数据结构说明(可以图示说明,也可以文字说明) 本程序主要根据选择菜单编写了六个自定义函数,用于在main()函数中调用,在main()中,用一个字符变量num1来记录下菜单选项的标号,根据num1的值来决定调用哪个函数。 程序要完成的功能及所要用到的函数如下:

下面就是整个程序的流程图:

2.算法说明(即函数说明) void suanshuyunsuan() //做算术运算时调用的函数 void suanshuyunsuan2() //选择继续做算术运算调用的函数,将上次运算的结果做为下次算术运算的第一个操作数//判断算术运算就是否继续 void panduan() //判断算术运算就是否继续 void biaodashiyunsuan() //单运算符表达式实现函数 void qingping() //清除屏幕 void fanhuicaidan() //显示菜单 三、测试结果(这部分需文字与图示结合) 1.第一组测试用例 (1)测试输入: 测试目的:测试算术运算的功能 结果输出:

(2)再一次输入:1测试目的:测试算术运算就是否能继续 结果输出: (3)这时输入:0 测试目的:退出算术运算 结果输出:

大学计算机c语言计算器源代码

C++语言编写。。 #include #include #include using namespace std; const double pi = 3.14159265; const double e = 2.718281828459; const int SIZE = 1000; typedef struct node//为了处理符号而建立的链表(如: 1+(-2)) { char data; node *next; }node; typedef struct stack_num//存储数的栈 { double *top; double *base; }stack_num; typedef struct stack_char//存储运算符号的栈 { char *top;

char *base; }stack_char; stack_num S_num;//定义 stack_char S_char;//定义 char fu[18] = {'\n', ')', '+', '-', '*', '/', '%', '^', 'Q', 'L', 'C', 'S', 'T', 'c', 's', 't', '('}; int compare[1000];//表现出各运算符号的优先级 double shu[1000];//存储"数"的数组 double dai_result;//运算的结果,是为了处理M运算(简介函数里有M的定义) int biao = 0;//和dia_result一样,为了处理M运算 char line[SIZE];//输入的所要计算的表达式 void init()//初始化 { compare[fu[0]] = -2;//用数字的大小表现出符号的优先级 compare[fu[1]] = -1; compare[fu[2]] = 2; compare[fu[3]] = 2; compare[fu[4]] = 4; compare[fu[5]] = 4; compare[fu[6]] = 4;

AT89C51单片机C实现简易计算器

AT89C51单片机简易计算器的设计 一、总体设计 根据功能和指标要求,本系统选用MCS-51系列单片机为主控机。通过扩展必要的外围接口电路,实现对计算器的设计。具体设计如下:(1)由于要设计的是简单的计算器,可以进行四则运算,为了得到较好的显示效果,采用LCD 显示数据和结果。 (2)另外键盘包括数字键(0~9)、符号键(+、-、×、÷)、清除键和等号键,故只需要16 个按键即可,设计中采用集成的计算键盘。 (3)执行过程:开机显示零,等待键入数值,当键入数字,通过LCD显示出来,当键入+、-、*、/运算符,计算器在内部执行数值转换和存储,并等待再次键入数值,当再键入数值后将显示键入的数值,按等号就会在LCD上输出运算结果。 (4)错误提示:当计算器执行过程中有错误时,会在LCD上显示相应的提示,如:当输入的数值或计算得到的结果大于计算器的表示范围时,计算器会在LCD上提示溢出;当除数为0时,计算器会在LCD 上提示错误。 系统模块图:

二、硬件设计 (一)、总体硬件设计 本设计选用AT89C51单片机为主控单元。显示部分:采用LCD 静态显示。按键部分:采用4*4键盘;利用MM74C922为4*4的键盘扫描IC,读取输入的键值。 总体设计效果如下图:

(二)、键盘接口电路 计算器输入数字和其他功能按键要用到很多按键,如果采用独立按键的方式,在这种情况下,编程会很简单,但是会占用大量的I/O 口资源,因此在很多情况下都不采用这种方式,而是采用矩阵键盘的方案。矩阵键盘采用四条I/O 线作为行线,四条I/O 线作为列线组成键盘,在行线和列线的每个交叉点上设置一个按键。这样键盘上按键的个数就为4×4个。这种行列式键盘结构能有效地提高单片机系统中I/O 口的利用率。 矩阵键盘的工作原理: 计算器的键盘布局如图2所示:一般有16个键组成,在单片机中正好可以用一个P口实现16个按键功能,这种形式在单片机系统中也最常用。 图 2 矩阵键盘布局图 矩阵键盘内部电路图如图3所示:

C8051Fxx单片机开发工具及调试技术

关于开发工具及调试的技术问答 1、问:C8051FXX系列单片机的开发工具是不是串行适配器(PC机串口和JTAG协议转换适配)相同而目标版不同? 答:开发套件中串行适配器(核心部件)是通用的,只是目标版不同。只要您购买一套开发套件,就可以开发全系列单片机,只要将集成开发环境软件升级就可以了。为了加快您的开发进程,您只要购买带有相关型号单片机的目标板就可以了。 2、问:C8051F单片机是怎样调试用户系统的? 答:C8051F单片机是用开发套件来调试用户系统的。单片机开发套件包括开发软件IDE(集成开发环境),ML-EC3至PC机USB口和单片机JTAG接口的协议转换模块和一个目标板(板上有 C8051FMCU)。IDE中集成编译器,汇编器和连接器,支持汇编语言和C语言(第三方支持);ML-EC3是从USB口到JTAG协议的转换模块。 目标板上带有一块相应的C8051FMCU和一些简单的外围电路构成一个最小单片机系统,并将所有引脚连接到插座。C8051F单片机内集成了一个以JTAG协议为基础的调试电路,这样在调试您的系统时,不需要专用仿真芯片、目标仿真头及目标RAM等。您在IDE上编译生成程序代码后,通过ML-EC3(连接到计算机USB口和JTAG接口)将代码下载到用户系统板的C8051FMCU的Flash存储器中,然后您就可以调试您的目标系了。 3、问:可以用KeiluVision2IDE调试全系列C8051F单片机应用系统吗?

答:可以。但必须安装动态链接库。该驱动程序可以在我公司的网站上下载。 4、问:如何将绝对目标代码转换成hex格式文件? 答:第一,可以使用KEILC,在编译时直接生成。 第二,使用OH51(DOS命令)将目标文件转换成hex格式的文件。 Oh5Linputfile〔hexfile〕 第三,在IDE环境中生成HEX文件 ﹙1﹚.在TOOL菜单中选择ADDREMOVEUSERTOOL ﹙2﹚.点击ADD按钮,增加一个MENUTEXT(名称可以任何输入) ﹙3﹚.在TOOLS里选择OH51.EXE文件(此文件在KEIL的BIN目录中有,必须将此文件放在项目所在目录) ﹙4﹚.在ARGUMENTS里输入文件名或项目名 ﹙5﹚.在DIROF里输入HEX文件的保存目录 5、问:Silabs IDE中带有4K代码限制版的KeilC51,那么如何将无限制版的KeilC51嵌入到Silabs IDE中呢? 答:先将您完全版的KeilC51安装到您的PC机中;然后进入Silabs IDE界面,选择Project-﹥TOOLChainIntegration弹出对话框,单击Browse按钮,更换A51.EXE和BL51.EXE的路径(换成“Keil安装目录”/c51/bin)即可。 6、问:程序代码加密后,IDE不能与目标系统连接怎么办?

C语言制作简单计算器

C语言制作简单计算器 一、项目介绍 我们要用c语言做一个简单的计算器,进行加、减、乘、除操作。本程序涉及的所有数学知识都很简单,但输入过程会增加复杂性。我们需要检查输入,确保用户没有要求计算机完成不可能的任务。还必须允许用户一次输入一个计算式,例如:32.4+32 或者9*3.2 项目效果图 编写这个程序的步骤如下: ?获得用户要求计算机执行计算所需的输入。 ?检查输入,确保输入可以理解。 ?执行计算。 ?显示结果。 三、解决方案 1.步骤1

获得用户输入是很简单的,可以使用printf()和scanf()。下面是读取用户输入的程序代码: #includeint main(){ double number1=0.0; //定义第一个操作值 double number2=0.0; //定义第二个操作值 char operation=0; //operation必须是'+''-''*''/'或'%' printf("\nEnter the calculation\n"); scanf("%lf%c%lf",&number1,&operation,&number2); return0; } 2.步骤2 接着,检查输入是否正确。最明显的检查是要执行的操作是否有效。有效的操作有+、-、*、/和%,所以需要检查输入的操作是否是其中的一个。 还需要检查第二个数字,如果操作是/或者%,第二个数字就不能是0。如果右操作数是0,这些操作就是无效的。这些操作都可以用if语句来完成,switch语句则为此提供了一种更好的方式,因此它比一系列if语句更容易理解。 switch(operation) { case'+': printf("=%lf\n",number1+number2); break; case'-': printf("=%lf\n",number1-number2); break; case'*': printf("=%lf\n",number1*number2); break; case'/': if(number2==0) printf("\n\n\aDavision by zero error!\n"); else printf("=%lf\n",number1/number2); break;

设计一个简单计算器的C语言课程设计报告

C语言课程设计报告题目:设计一个简单计算器 目录 1. 设计目的 2. 内容

3. 总体设计(有流程图) 4. 源程序编写(附上了运行图) 5. 执行结果 6. 心得体会 一、设计目的 设计一个简单计算器,在功能上功能尽量模拟windows 操作系统中的计算器,系统界面不做强制要求。 全面熟悉、掌握C语言基本知识,掌握C程序设计中的顺序、分支、循环三种结构及数组、函数、指针和文件的操作,把编程和实际结合起来,增强对不同的问题运用和灵活选择合适的数据结构以及算法描述的本领,熟悉编制和调试程序的技巧,掌握分析结果的若干有效方法,进一步提高上机动手能力,培养使用计算机解决实际问题的能力,规范编程思想,为以后在专业

课程中应用计算机系统解决计算、分析、实验和设计等学习环节打下较扎实的基础。 二、内容 1、程序设计的一般步骤 a、确定数据结构 b、确定算法 C、编程 d、调试 e、总结资料 2、基本要求 a .设计正确,方案合理,能实现相应功能。 b .界面友好,使用方便。 c .程序精炼,结构清晰。 d .设计报告含程序设计说明,用户使用说明,源程序清单及程序框图。 e .上机演示。

三、总体设计(程序设计组成框图、流程图)

四、源程序编与 #in clude #in clude #in clude double jisua n( char a[]) { int i=1,j,k,m,cnt=0,t1=0,t2=0,t3=0; char nibo[50],zha n2[50]; double x,n, l,z=0,zha n3[20]; typedef struct { double d1; int d2; }dd; typedef struct {

C语言课程设计--计算器(图形界面)

扬州大 学 题目一个简易计算器的设计与实现 班级 学号 姓名 指导教师 成绩 老师评语: 扬州大学信息工程学院 2010 年6 月25

目录 一、程序设计目的: (1) 二、程序设计内容: (1) 三、课程设计所补充的内容:补充的函数或算法…………3,4 四、系统总体设计 (4) 五、系统详细设计………………………………………5,6,7,8 六、运行结果………………………………………………8,9,10 七、系统调试…………………………………………8,9,10,11 八、课程设计体会总结………………………………8,9,10,11

1 课程设计目的 (1).课程设计是一项综合性实践环节,是对平时实验的一个补充,课程设计内容包括课程的主要理论知识,但由于C语言对初学者较难掌握,因而对一个完整的C语言程序不适合平时实验。通过课程设计可以达到综合设计C语言程序的目的。 (2)通过本课程设计,可以培养独立思考,综合运用所学有关相应知识的能力,能更好地使用C语言的知识,更好地了解C语言的好处和其可用性!掌握基本的程序设计过程和技巧,掌握基本的分析问题和利用计算机求解问题的能力,具备初步的高级程序设计能力。为后续各门计算机课程的学习和毕业设计打下坚实基础! (3)通过本程序训练程序设计的基本技能,掌握字符串的表示方法和字符串函数的功能、自学掌握四则运算的算法及WIN-TC的图形操作的基本知识、键盘上特殊键的获取及图形方式下光标的显示。 2 课程设计内容 目的:本课程的课程设计要求学生模拟实现一个简单计算器,要求(1)能够实现四则运算,并能支持优先级运算。(2)三角与反三角运算:如sinx,cosx等。(3)指数对数运算:如log(x),lnx,e的x次方等。(4)其他运算:如X!,x 的累加等。(4)不允许调用库函数,使用自行开发的程序实现常用函数运算。(5)进一步考虑计算器外观设计,用可视化界面给出计算器外观、功能按键及输入界面。 使用说明:执行加法运算,'—'表示执行减法运算,表示执行乘法运算,'/'表示除法运算.‘Q’表示退出计算器‘C’表示清零,’=’表示得出结果'^'表示执行x的y次方,'e'表示执行e的x次方操作,'&'表示执行累加操作.,你可以可以用键盘上的上下左右键对光标进行移动,当光标移动到计算器按键上时,按ENTER即可执行该键的操作!最后按“=”则可得出结果。 3 课题设计的补充知识 本程序通过int specialkey(void)和#include来实现对屏幕的操作,通过调用int arrow()函数,int specialkey(void)和#include来实现对光标的操作。计算机图形采用Turbo C 2.0绘图程序制作。因此涉及C的图形程序设计知识。此外,由于不允许调用库函数,则要自行开发程序实现sinx,cosx,e的x次方函数运算,则可以根据幂级数的展开式来设计实现其运算的算法,而x的阶乘和x的累加则可用for语句来实现。 最后,不得不说说四则运算的算法,有两种方法可以实现:(1)利用堆栈实现四则运算(2)还可以用递归整数的四则运算。 sinx函数 #include

c8051f系列单片机选型表

C8051F单片机选型表 Number MIPS (peak) Flash Memory (bytes) RAM (bytes) Ext Mem I/F Digital Port I/O Pins Serial Buses Timers (16- bit) PCA Chnls Internal Osc ADC1 ADC2 D C8051F005 25 32KB 2304 - 32 UART, SMBus, SPI 4 5 ±20%12-bit, 8ch., 100ksps - 1 2 C8051F015 25 32KB 2304 - 32 UART, SMBus, SPI 4 5 ±20%10-bit, 8ch., 100ksps - 1 2 C8051F020 25 64KB 4352 Y 64 2 UARTs, SMBus, SPI 5 5 ±20% 12-bit, 8ch., 100ksps 8-bit, 8ch., 500ksps 1 2 C8051F021 25 64KB 4352 Y 32 2 UARTs, SMBus, SPI 5 5 ±20% 12-bit, 8ch., 100ksps 8-bit, 8ch., 500ksps 1 2 C8051F022 25 64KB 4352 Y 64 2 UARTs, SMBus, SPI 5 5 ±20% 10-bit, 8ch., 100ksps 8-bit, 8ch., 500ksps 1 2 C8051F023 25 64KB 4352 Y 32 2 UARTs, SMBus, SPI 5 5 ±20% 10-bit, 8ch., 100ksps 8-bit, 8ch., 500ksps 1 2 C8051F040 25 64KB 4352 Y 64 CAN2.0B, 2 UARTs, SMBus, SPI 5 6 ±2% 12-bit, 13ch., 100ksps 8-bit, 8ch., 500ksps 1 2 C8051F060 25 64KB 4352 Y 59 CAN2.0B, 2 UARTs, SMBus, SPI 5 6 ±2% 16-bit, 2ch., 1Msps 10-bit, 8ch., 200ksps 1 2 C8051F064 25 64KB 4352 Y 59 2 UARTs, SMBus, SPI 5 6 ±2% 16-bit, 2ch., 1Msps - - C8051F120 100 128KB 8448 Y 64 2 UARTs, SMBus, SPI 5 6 ±2% 12-bit, 8ch., 100ksps 8-bit, 8ch., 500ksps 1 2 C8051F124 50 128KB 8448 Y 64 2 UARTs, SMBus, SPI 5 6 ±2% 12-bit, 8ch., 100ksps 8-bit, 8ch., 500ksps 1 2 C8051F126 50 128KB 8448 Y 64 2 UARTs, SMBus, SPI 5 6 ±2% 10-bit, 8ch., 100ksps 8-bit, 8ch., 500ksps 1 2 C8051F130 100 128KB 8448 Y 64 2 UARTs, SMBus, SPI 5 6 ±2% 10-bit, 8ch., 100ksps - - C8051F206 25 8KB 1280 - 32 UART, SPI 3 - ±20%12-bit, 32ch., 100ksps - - C8051F230 25 8KB 256 - 32 UART, SPI 3 - ±20%- - -C8051F236 25 8KB 1280 - 32 UART, SPI 3 - ±20%- - - C8051F300 25 8KB 256 - 8 UART, SMBus 3 3 ±2%8-bit, 8ch., 500ksps - - C8051F304 25 4KB 256 - 8 UART, SMBus 3 3 ±20%- - -C8051F305 25 2KB 256 - 8 UART, SMBus 3 3 ±20%- - - C8051F310 25 16KB 1280 - 29 UART, SMBus, SPI 4 5 ±2%10-bit, 21ch., 200ksps - - C8051F314 25 8KB 1280 - 29 UART, SMBus, SPI 4 5 ±2%- -C8051F315 25 8KB 1280 - 25 UART, SMBus, SPI 4 5 ±2%- - C8051F320 25 16KB 2304 - 25 USB 2.0, UART, SMBus, SPI 4 5 ±1.5% 10-bit, 17ch., 200ksps - - C8051F326 25 16KB 1536 - 15 USB 2.0, UART, SMBus, SPI 2 - ±1.5%- - - C8051F327 25 16KB 1536 - 15 USB 2.0, UART, SMBus, SPI 2 - ±1.5%- - - C8051F330 25 8KB 768 - 17 UART, SMBus, SPI 4 3 ±2%10-bit, 16ch., 200ksps - 1 1

(完整word版)C语言简易计算器课程设计.doc

C语言课程设计报告书题目:简易计算器 院系: 班级: 学号: 姓名:

摘要 该软件为用户提供在windows 系统上的简易计算器,能进行简单的加、减、 乘、除四则运算与混合运算。目的是为了巩固和加深 C 语言课程的基本知识的理 解和掌握,利用 C语言进行基本的软件设计,掌握 C 语言的编程和程序调试 的基本技能,掌握书写程序设计说明文档的能力,提高运用 C 语言解决实际问 题的能力。 关键词: C; 简易计算器 ; 程序 ; 基本算法;混合运算 SUMMARY the software provides a simple calculator in Windows system for users ,can be a simple add,subtract,multiply,divide.The purpose is to consolidate and deepen the basic knowledge of C language courses to understand and master, The use of C language for software design basic, To master the basic skills of C language programming and program debugging, Master the writing program design documentation ability,improve the ability to use C language to solve practical problems.

C8051F系列单片机的发展和应用

C8051F系列单片机的发展和应用 摘要:C8051F系列单片机的推出,使单片机进入SoC ( System on Chip )时代。C8051F系列单片机功能强大,能够作为嵌入式系统的主控制器,具有上手快(全兼容8051指令集)、研发快(开发工具易用,可缩短研发周期)和见效快(调试手段灵活)等特点,使得C8051F系列单片机得到广泛的应用。本文首先对C8051F 系列单片机做了大概的介绍,之后详细分析说明了它的原理机制,介绍了C8051F 系列单片机的特点,阐述了它的发展和应用。 关键词:C8051F SoC 发展应用 前言 在嵌入式系统低端的单片机领域, 80C51系列一直扮演着一个重要角色,近年来,由于80C51的速度低(每一条指令至少需要12个时钟周期) ,功耗高(几毫安到几十毫安) ,功能少(不能直接处理模拟信号)等等, 80C51系列单片机似乎已经走道了尽头,然而当前CYGNAL公司推出的C8051F系列单片机又将80C51兼容单片机推上了8位机的先进行列,使80C51系列从MCU时代进入到了SoC ( System on Chip )时代。 SoC是随着半导体生产技术的不断发展而产生的新概念,它是集成度越来越高和对嵌入式控制技术可靠性越来越高的产物[1]。SoC是指片上系统或系统级芯片, SoC的完整定义为:在同一个芯片上集成了控制部件(微处理器,存储器)和执行部件( I/O接口,微型开关,微机械) ,能够自成体系,独立工作的芯片。因此, C8051F系列单片机功能强大,能够作为嵌入式系统的主控制器。本文主要介绍了C8051F系列单片机的特点,以及它的发展和应用。 一 C8051F系列单片机简介 C8051F系列单片机是完全集成的混合信号系统级芯片,具有与8051兼容的CIP-51微控制器内核,采用流水线结构,单周期指令运

C语言简单计算器的编写课程设计

2013-2014学年第二学期 《高级语言程序设计》 课程设计报告 题目:简单计算器 专业:数字媒体技术 班级:13级1班 姓名:邢洪波 指导教 师:王辉 成绩: 计算机与信息工程系 二0一四年二月二十日 1设计内容及要求 利用本学期所学的《C语言程序设计》课程,运用相关知识,查阅相关资料,编写C语言程序,设计一个简单计算器,要求编写的简单计算器能够模拟windows系统的计算器,用户能够用键盘输入相关数据,能够进行简单的加、减、乘、除运算,并且在程序运行过程中能够正常的退出程序。? 在计算器程序运行中,输入数据时如果遇到输入错误的情况,能够能过键盘上的退格键进行删除,并且重新输入正确的数据。在数据输入完成后,

如果需要放弃本次计算操作,可以利用程序中设置好的按键进行清零,并为下一次运算作准备。 运用相关知识,查阅相关资料,编写一个简单的计算器,能够实现简单的基本的加减乘除运算,在计算器工作时用户能利用键盘或鼠标进行相应操作。程序基本功能要求实现完整,并有简单的验证。程序还须有符合标准的 程序设计报告。

2概要设计 系统流程图 系统流程图主要功能表 主要功能表

3设计过程或程序代码 #include<> #include<> #include<> /*malloc的头文件代表在内存中开辟存储空间*/ double jisuan(char a[]) { int i=1,j,k,m,cnt=0,t1=0,t2=0,t3=0; /*定义整型变量i,j,k,m,n,cnt,t1,t2,t3*/ char nibo[50],zhan2[50]; /*定义字符型数组*/ double x,n,l,z=0,zhan3[50]; /*定义双精度变量x,n,l,z,zhan3[50]*/ typedef struct /*表示可以用con定义结构体变量*/ { double d1; int d2; }dd; typedef struct { dd data[50]; int top; }zhan1; zhan1 *shu; /*定义指针变量*/ shu=(zhan1 *)malloc(sizeof(zhan1)); shu->top=0; while(a[i]!='\0') /*外循环是用while语句*/ {

C语言编写的计算器源代码

C语言计算器源代码 #include /*DOS接口函数*/ #include /*数学函数的定义*/ #include /*屏幕操作函数*/ #include /*I/O函数*/ #include /*库函数*/ #include /*变量长度参数表*/ #include /*图形函数*/ #include /*字符串函数*/ #include /*字符操作函数*/ #define UP 0x48 /*光标上移键*/ #define DOWN 0x50 /*光标下移键*/ #define LEFT 0x4b /*光标左移键*/ #define RIGHT 0x4d /*光标右移键*/ #define ENTER 0x0d /*回车键*/ void *rar; /*全局变量,保存光标图象*/ struct palettetype palette; /*使用调色板信息*/ int GraphDriver; /* 图形设备驱动*/ int GraphMode; /* 图形模式值*/ int ErrorCode; /* 错误代码*/ int MaxColors; /* 可用颜色的最大数值*/ int MaxX, MaxY; /* 屏幕的最大分辨率*/ double AspectRatio; /* 屏幕的像素比*/ void drawboder(void); /*画边框函数*/ void initialize(void); /*初始化函数*/ void computer(void); /*计算器计算函数*/ void changetextstyle(int font, int direction, int charsize); /*改变文本样式函数*/ void mwindow(char *header); /*窗口函数*/ int specialkey(void) ; /*获取特殊键函数*/ int arrow(); /*设置箭头光标函数*/ /*主函数*/ int main() { initialize();/* 设置系统进入图形模式*/ computer(); /*运行计算器*/ closegraph();/*系统关闭图形模式返回文本模式*/ return(0); /*结束程序*/ } /* 设置系统进入图形模式*/ void initialize(void) { int xasp, yasp; /* 用于读x和y方向纵横比*/

C语言_程序设计_计算器

C语言_程序设计_计算器 课程设计说明书 题目多功能计算器起讫日期 2006 年 7月 3日至 2006 年 8月 6日 所在院系 专业班级 学生姓名学号 指导教师 2006年 8 月 1 日 摘要 当今社会,随着人们物质生活的不断提高,电子产品已经走进家家户户,无论是生活和学习还是娱乐和消遣几乎样样都离不开电子产品,计算器可谓是我们最亲密的电子伙伴之一。 随着科技的发展,人们对计算要求的精确度越来越高,各种不同功能的计算器已经悄悄走进我们的生活和学习中。对于学生来说一个功能齐全的计算器对我们的学习有着莫大的帮助。借着C语言程序设计的机会,我决定自己动手来研究一下计算器。在练习巩固C语言学习的同时又可以从中体会到计算器的强大功能,然而由于学习的内容和时间有限,至今为止我只能实现四则运算加、减、乘、除、平方、立方和开方的一些简单功能,并且在计算器的里面加了一个漂亮的时钟,用来方便我们的学习。在今后的学习中,我会继续研究,争取进一步来完善它的功能。 本系统采用Turbo C语言开发,生成可执行文件在任何系统下都可以执行,具有很好的数据处理能力和稳定性。本论文主要介绍了该课题的开发背景以及过程和

所要完成的功能。并且重点的说明了系统设计思想,设计的步骤、难点技术和解决方案。由于“初来乍到”,这个程序设计还有一些漏洞和缺陷,希望您多多提出~关键字:Turbo c c语言计算器程序四则数值运算 目录 第一章综述...........................................................................1 1(1 课题的现实意义...............................................................1 1(2 软件环境........................................................................1 1(3 硬件环境 (1) 第二章系统设计流程图............................................................2 2(1 系统流程图.....................................................................2 2(2 主要功能表.....................................................................2 第三章系统分析和设计............................................................3 3(1 图形的绘制和输出............................................................3 3(2 文本的输出显示...............................................................3 3(3 计算函数的调用...............................................................4 3(4 程序的运行和退出 (5) 第四章系统测试.....................................................................6 4(1 缺少变量定义,定义位置不正确..........................................6 4(2 语法错误........................................................................6 4(3 注释的位置.....................................................................6 4(4 逻辑错误 (6) 第五章用户使用说明书……………………………………………………8 5(1 运行Turbo C程序进入计算器界面…………………………………8 5(2 计算器的使

相关文档
最新文档