华为上机-要求编程实现上述高精度的十进制加法。要求实现函数: void add (const char *num1, const char *num2, char *result)
2014-10-06 10:52
549 查看
/* 要求编程实现上述高精度的十进制加法。要求实现函数: void add (const char *num1, const char *num2, char *result) 【输入】num1:字符串形式操作数1,如果操作数为负,则num1[0]为符号位'-' num2:字符串形式操作数2,如果操作数为负,则num2[0]为符号位'-' 【输出】result:保存加法计算结果字符串,如果结果为负,则result[0]为符号位。 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 1000 void add(const char *num1, const char *num2, char *result); void two_positive(const char *positive1, const char *positive2, char *result); void sub(const char *bigger, const char *smaller, char *result); void two_positive_negtive(const char *positive, const char *negtive, char *result); void reverse(char *src); int main (int argc, char **argv) { char s1 = "\0"; char s2 = "\0"; char *dst = nullptr; printf("输入操作数1:\n"); scanf("%s", s1); printf("输入操作数2:\n"); scanf("%s", s2); int len = 0; len = strlen(s1) > strlen(s2)? strlen(s1):strlen(s2); dst = (char *)malloc(sizeof(char)*(len+2)); //最高位的进位和最后的'\0' memset(dst, 0, sizeof(char)*(len+2)); add(s1, s2, dst); printf("result:%s\n", dst); free(dst); dst = nullptr; system("pause"); return 0; } void add(const char *num1, const char *num2, char *result) { if (nullptr == num1 || nullptr == num2) { printf("输入错误\n"); return; } if (nullptr == result) { printf("输出错误\n"); return; } if ('-' != num1[0] && '-' != num2[0]) //两个都是正数 { two_positive(num1, num2, result); reverse(result); } else if ('-' == num1[0] && '-' == num2[0]) //两个都是负数 { result[0] = '-'; two_positive(num1+1, num2+1, result+1); reverse(result+1); } else if ('-' == num1[0] && '-' != num2[0]) //num1负,num2正 { two_positive_negtive(num2, num1, result); } else if ('-' != num1[0] && '-' == num2[0]) //num1正,num2负 { two_positive_negtive(num1, num2, result); } } void two_positive(const char *positive1, const char *positive2, char *result) { int sum = 0, carry = 0; int temp1 = 0, temp2 = 0; int end1 = 0, end2 = 0; int count = 0; end1 = strlen(positive1)-1; end2 = strlen(positive2)-1; while (end1 >= 0 || end2 >= 0) { if (end1 >= 0) { temp1 = positive1[end1]-'0'; } else { temp1 = 0; } if (end2 >= 0) { temp2 = positive2[end2]-'0'; } else { temp2 = 0; } sum = (temp1+temp2+carry)%10; carry = (temp1+temp2+carry)/10; result[count++] = sum+'0'; end1 --; end2 --; if (end1 < 0 && end2 < 0) { if (0 != carry) { result[count++] = carry+'0'; } else { } } } result[count] = '\0'; } void sub(const char *bigger, const char *smaller, char *result) { int temp1 = 0, temp2 = 0; int diff = 0, borrow = 0; int count = 0; int t1 = strlen(bigger)-1; int t2 = strlen(smaller)-1; while (t1 >= 0 || t2 >= 0) { if (t1 >= 0) { temp1 = bigger[t1]-'0'; } else { temp1 = 0; } if (t2 >= 0) { temp2 = smaller[t2]-'0'; } else { temp2 = 0; } diff = temp1-temp2-borrow; if (diff >= 0) { borrow = 0; } else { diff += 10; borrow = 1; } result[count++] = diff+'0'; t1 --; t2 --; } while ('0' == result[count-1] && 1 != count ) { count --; } result[count] = '\0'; } void two_positive_negtive(const char *positive, const char *negtive, char *result) { int len1 = strlen(positive); int len2 = strlen(negtive+1); if (len1 > len2) { sub(positive, negtive+1, result); } else if (len1 == len2) { if (strcmp(positive, negtive+1) >= 0) { sub(positive, negtive+1, result); } else { result[0] = '-'; sub(negtive+1, positive, result+1); } } else if (len1 < len2) { result[0] = '-'; sub(negtive+1, positive, result+1); } if ('-' == result[0]) { reverse(result+1); } else { reverse(result); } } void reverse(char *src) { int len = strlen(src); char temp = '\0'; for (int i = 0; i < (len+1)/2; i ++) { temp = *(src+i); *(src+i) = *(src+len-1-i); *(src+len-1-i) = temp; } }
相关文章推荐
- 编程:用java 语言实现,输入一个数,就相应地输出的几维数组!||用1、2、2、3、4、5这六个数字,用java写一个函数,打印出所有不同的排列,要求:"4"不能在第三位,"3"与"5"不能相连
- C语言 编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数。
- 2010年华为上机笔试二(高精度整数加法)
- 【c语言】编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数
- 编写一个函数reverse_string(char * string)(递归实现) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中 的字符串操作函数。
- 要求实现函数: void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr);
- 2013华为实习生上机考试试题(一)华为机试 请设计函数int getTest(char input[]) ,检测输入字符串中是否包含连续的或者离散的test,test只出现一次
- 【C语言】编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。要求:不能使用C函数库中的字符串操作函数。
- //4. 编写一个函数reverse_string(char * string)(递归实现) //实现:将参数字符串中的字符反向排列。 //要求:不能使用C函数库中的字符串操作函数。
- java蓝桥杯试题高精度加法函数实现
- 编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数。
- 实现一个数值加法函数,返回两个数字的和。要求能进行参数验证,若参数不是数字或为空则抛出异常。
- 编写一个函数reverse_string(char * string) 实现:将参数字符串中的字符反向排列。 要求:不能使用C函数库中的字符串操作函数
- 编写一个函数reverse_string(char * string)(递归实现)实现:将参数字符串中的字符反向排列。要求:不能使用C函数库中的字符串操作函数。
- 编程实现函数 int my_atoi(const char *str)
- 华为上机题:高精度整数加法
- 发个小程序玩,实现任意长度十进制整数加法。传说中的什么复试C语言题
- 5-16: 输入10个数,要求编写一个排序函数,能够实现按绝对值从大到小排序。在主函数中输入10个数,输出排序后的10个数
- 深入浅出Dll(介绍函数导出、类导出、钓子dll、不同语言混合编程方法、插件等的实现方法)
- 串口编程 —— 实现 GPS 过程中自己用到的函数 COMMTIMEOUTS,SetCommMask,SetCommTimeouts