Programming Specification
1. Define variable return_code to record the function's status.
1 | int return_code = 0; |
2. Define the exit flag: exit_flag, which is used by goto. This flag is defined at the end of function body. The content of exit_flag includes free memory and return the status of function.
1 2 3 4 5 6 7 8 9 10 11 | exit_flag: if (m_a) free(m_a); if (m_b) free(m_b); if (m_c) free(m_c); if (m_d) free(m_d); return return_code; |
3. Check the array formal parameters of function.
1 2 3 4 5 6 7 8 9 | //check if (NULL == a) { return_code = -1; goto exit_flag; } if (NULL == b) { return_code = -1; goto exit_flag; } |
4. Allocate memory
1 2 3 4 5 6 7 | // allocate memory m_a = ( float *) malloc(n * sizeof ( float )); if (!m_a){ printf( "Failed to allocate memory! \n" ); return_code = -1; goto exit_flag; } |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | #define IN #define OUT int solve_tridiagonal_equation_thomas( IN int n, IN float a[], IN float b[], IN float c[], IN float d[], OUT float x[] ) { int return_code = 0; //check if (NULL == a) { return_code = -1; goto exit_flag; } if (NULL == b) { return_code = -1; goto exit_flag; } if (NULL == c) { return_code = -1; goto exit_flag; } if (NULL == d) { return_code = -1; goto exit_flag; } if (NULL == x) { return_code = -1; goto exit_flag; } int i = 0; float tmp = 0; float *m_a, *m_b, *m_c, *m_d; // allocate memory m_a = ( float *) malloc(n * sizeof ( float )); if (!m_a){ printf( "Failed to allocate memory! \n" ); return_code = -1; goto exit_flag; } m_b = ( float *) malloc(n * sizeof ( float )); if (!m_b){ printf( "Failed to allocate memory! \n" ); return_code = -1; goto exit_flag; } m_c = ( float *) malloc(n * sizeof ( float )); if (!m_c){ printf( "Failed to allocate memory! \n" ); return_code = -1; goto exit_flag; } m_d = ( float *) malloc(n * sizeof ( float )); if (!m_d){ printf( "Failed to allocate memory! \n" ); return_code = -1; goto exit_flag; } // diagonal dominant validation and copy data bool cond1 = (abs(b[0]) > abs(c[0])) && (abs(c[0]) > 0); bool cond2 = (abs(b[n-1]) > abs(a[n-1])) && (abs(a[n-1]) > 0); if (!(cond1 && cond2)) { printf( "Matrix is Invalid! \n" ); return_code = -2; goto exit_flag; } for (i = 1; i < n-1; ++i) { if (abs(b[i]) < abs(c[i]) + abs(c[i])) { printf( "Matrix is NOT diagonal dominant! \n" ); return_code = -2; goto exit_flag; } else { m_a[i] = a[i]; m_b[i] = b[i]; m_c[i] = c[i]; m_d[i] = d[i]; } } memcpy(m_a, a, n * sizeof ( float )); // forward elimination for (i = 1; i < n; ++i) { tmp = m_a[i] / m_b[i-1]; m_b[i] = m_b[i] - tmp * m_c[i-1]; m_d[i] = m_d[i] - tmp * m_d[i-1]; } // backward substitution x[n-1] = m_d[n-1] / m_b[n-1]; for (i = n-2; i >= 0; --i) { x[i] = (m_d[i] - m_c[i] * x[i+1]) / m_b[i]; } // free memory and exit exit_flag: if (m_a) free(m_a); if (m_b) free(m_b); if (m_c) free(m_c); if (m_d) free(m_d); return return_code; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥