⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blinky.c

📁 using keil to subtract 2 "string number" (MCB2378 ARM)
💻 C
字号:
/******************************************************************************/
/* BLINKY.C: LED Flasher                                                      */
/******************************************************************************/
/* This file is part of the uVision/ARM development tools.                    */
/* Copyright (c) 2005-2006 Keil Software. All rights reserved.                */
/* This software may only be used under the terms of a valid, current,        */
/* end user licence from KEIL for a compatible version of KEIL software       */
/* development tools. Nothing else gives you the right to use this software.  */
/******************************************************************************/
                  
#include <stdio.h>
#include <LPC23xx.H>                    /* LPC23xx definitions                */
#include "LCD.h"                        /* Graphic LCD function prototypes    */


/* Function that initializes LEDs                                             */
void LED_Init(void) {
  PINSEL10 = 0;                         /* Disable ETM interface, enable LEDs */
  FIO2DIR  = 0x000000FF;                /* P2.0..7 defined as Outputs         */
  FIO2MASK = 0x00000000;
}

/* Function that turns on requested LED                                       */
void LED_On (unsigned int num) {
  FIO2SET = (1 << num);
}

/* Function that turns off requested LED                                      */
void LED_Off (unsigned int num) {
  FIO2CLR = (1 << num);
}

/* Function that outputs value to LEDs                                        */
void LED_Out(unsigned int value) {
  FIO2CLR = 0xFF;                       /* Turn off all LEDs                  */
  FIO2SET = (value & 0xFF);             /* Turn on requested LEDs             */
}


/* Function for displaying bargraph on the LCD display                        */
void Disp_Bargraph(int pos_x, int pos_y, int value) {
  int i;

  set_cursor (pos_x, pos_y);
  for (i = 0; i < 16; i++)  {
    if (value > 5)  {
      lcd_putchar (0x05);
      value -= 5;
    }  else  {
      lcd_putchar (value);
      value = 0;
    }
  }
}

/* Import external IRQ handlers from IRQ.c file                               */
extern __irq void T0_IRQHandler  (void);
extern __irq void ADC_IRQHandler (void);

/* Import external functions from Serial.c file                               */
extern       void init_serial    (void);

/* Import external variables from IRQ.c file                                  */
extern short AD_last;
extern unsigned char clock_1s;

char* input_number()
{
	char ch;
	char* str = (char*)malloc(0);
	char* str1;
	int i = 0;
	int n=0;

	do
	{
		n++;
		ch = getkey();
		sendchar(ch);
		str1 = (char*)malloc(n+1);
		for(i = 0; i <n-1; i++)
		{
			str1[i] = str[i];
		}
		str1[n-1] = ch;
		str = str1;
	}while(ch != 13);
	str[n] = 'e';
	return str;
}

void output_number(char* str)
{
	int i;

	for(i = 0; str[i] != 'e'; i++)
	{
		sendchar(str[i]);
	}
}

int getlen(char* str)
{
	int i;

	for(i = 0; str[i] != 'e'; i++);
	return i;
}

char* sum2number(char* num1, char* num2)
{
	int n1, n2, nmax, nmin, i, j, remember = 0;
	char* nummax;
	char* nummin;
	char* numsum;
	char chmax, chmin;
	int x1, x2, x;

	n1 = getlen(num1);
	n2 = getlen(num2);
	if(n1 > n2)
	{
		nmax = n1;
		nummax = num1;
		nmin = n2;
		nummin = num2;
	}
	else
	{
		nmax = n2;
		nummax = num2;
		nmin = n1;
		nummin = num1;
	}

	numsum = (char*)malloc(nmax+2);
	numsum[nmax+1] = 'e';

	for(i = nmax - 1 , j = nmin - 1; j>=0; i--, j--)
	{
		chmax = nummax[i];
		chmin = nummin[j];
		x1 = chmax - '0';
		x2 = chmin - '0';
		x = x1 + x2 + remember;
		numsum[i+1] = x%10 + '0';
		if(x/10 == 0)
		{
			remember = 0;
		}
		else
		{
			remember = 1;
		}
	}
	for( ; i>=0; i--)
	{
		chmax = nummax[i];
		x1 = chmax - '0';
		x = x1 + remember;
		numsum[i+1] = x%10 + '0';
		if(x/10 == 0)
		{
			remember = 0;
		}
		else
		{
			remember = 1;
		}
	}
	if(remember == 1)
	{
		numsum[0] = 1;
	}
	else
	{
		numsum++;
	}
	return numsum;
}

char* subtract2number(char* num1, char* num2)
{
	int n1, n2, nmax, nmin, i, j, remember = 0;
	char* nummax;
	char* nummin;
	char* numSubtract;
	char chmax, chmin;
	int x1, x2, x;

	n1 = getlen(num1);
	n2 = getlen(num2);
	if(n1 > n2)
	{
		nmax = n1;
		nummax = num1;
		nmin = n2;
		nummin = num2;
	}
	else
	{
		nmax = n2;
		nummax = num2;
		nmin = n1;
		nummin = num1;
	}

	numSubtract = (char*)malloc(nmax+1);
	numSubtract[nmax] = 'e';

	for(i = nmax - 1 , j = nmin - 1; j>=0; i--, j--)
	{
		chmax = nummax[i];
		chmin = nummin[j];
		x1 = chmax - '0';
		x2 = chmin - '0';
		if(x1 < x2 + remember)
		{
			x = x2 + remember - x1;
			remember = 1;
		}
		else
		{
			x = x1 - (x2 + remember);
			remember = 0;
		}
		numSubtract[i] = x + '0';
	}
	for( ; i>=0; i--)
	{
		chmax = nummax[i];
		x1 = chmax - '0';
		if(x1 < remember)
		{
			x = remember - x1;
			remember = 1;
		}
		else
		{
			x = x1 - remember;
			remember = 0;
		}
		numSubtract[i] = x + '0';
	}
	return numSubtract;
}

void sendstring(char str[])
{
    int n, i;

	n = strlen(str);
	for(i = 0; i < n; i++)
	{
		sendchar(str[i]);
	}
}

int main (void) {
  char* num1;
  char* num2;
  char* numsum;
  char ch;
  
  init_serial(); 
  lcd_init();
  while(1)
  {
    sendstring("Nhap so thu nhat:");
	num1 = input_number();
	sendchar(10);
  	sendchar(13);

	sendstring("Nhap so thu hai :");
  	num2 = input_number();
	sendchar(10);
  	sendchar(13);

	//num1 = "1234e";
	//num2 = "123e";

	sendstring("Hieu hai so la:");
  	numsum = subtract2number(num1, num2);
  	//output_number(numsum);
	sendstring(numsum);
	sendchar(10);
  	sendchar(13);
  }  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -