【c++】 递归应用:根据原料清单计算总价
来源:互联网 发布:python 字符串 split 编辑:程序博客网 时间:2024/04/20 09:59
日志:利用C++递归解决实际问题:给定产品清单,计算零件总价。
1.问题及算法思路:见图片。
2.其中遇到的问题总结:
1).按行读取文件内容:
C语言使用fgets( )按行读取文件,判断是否读完不用!feof做判断,因为今天遇到了问题,最后一行读完后仍然重复读取了最后一行部分数据。这里采用while (fgets(StrLine,25,fp)!=NULL)来读取全部数据
2).以空格为界分割字符串
3).List<string>::iterator index
这里index相当于指针的功能,返回类型为string,在本例中要将其返回值转化成 *char[] 作为函数实参。转化过程分两步,首先由string转化成const char,再转化成*char
步骤一:使用(*index).c_str()
步骤二:使用const_cast<char *> 取消其const属性。因为.c_str()返回类型为const
完整方法:const_cast<char *> ((*index).c_str())
4). switch()函数不能以字符数组作为参数
变通:但可以利用atoi()将字符串转化为整形作为参数
代码:
#include<stdio.h>#include<stdlib.h>#include<string.h>#include <list>#include <iostream>double calcPartCost(char id1[6],char date1[10][3][6]);double clacAssemblyCost(char id2[6],char date2[10][3][6]);int NumbyId(char id3[6]); using namespace std;int main() { /*-------将txt文本中数据读取到数组中--------*/ char filename[] ="C:\\Users\\leigang\\Desktop\\2.txt"; //文件名 FILE *fp; char StrLine[25]; //每行最大读取的字符数 char alldate[10][3][6]; int num1=0; int num2=0; if((fp = fopen(filename,"r")) == NULL) //判断文件是否存在及可读 { printf("error!"); return -1; } while (fgets(StrLine,25,fp)!=NULL) { //读取一行 printf("%s\n", StrLine); char * p; p = strtok (StrLine," "); num2=0; //printf("%s\n", p); while(p!=NULL) { strcpy(alldate[num1][num2],p); p = strtok(NULL," "); num2++; //printf("%s\n", p); } num1++; } fclose(fp); //关闭文件 printf("---测试---\n"); printf("%s\n", alldate[9][2]); /*-----递归算法开始-------*/ double totalCost =0.0; int i; for (i=0;i<10;i++) { totalCost=totalCost+calcPartCost(alldate[i][0],alldate); //依次将零件(部件)的编号传入函数 //printf("test: %.2f\n", c); } printf("total: %.2f\n", totalCost); system("PAUSE"); return 0; }double calcPartCost(char id1[6],char date1[10][3][6]){ double c=0; int m=NumbyId(id1); if(id1[4]!='0') //零件 c=atof( date1[m][2]); else c=clacAssemblyCost(id1,date1); return c; }double clacAssemblyCost(char id2[6],char date2[10][3][6]){ list<string> Assembly1; list<string> Assembly2; list<string> Assembly3; list<string>::iterator index; Assembly1.push_back("00011"); Assembly1.push_back("00012"); Assembly2.push_back("00021"); Assembly2.push_back("00022"); Assembly2.push_back("00010"); Assembly3.push_back("00031"); Assembly3.push_back("00032"); Assembly3.push_back("00033"); Assembly3.push_back("00020"); double d=0; if(id2[3]=='1') { for(index = Assembly1.begin(); index!= Assembly1.end(); ++index) d=d+ calcPartCost(const_cast<char *> ((*index).c_str()),date2); } if(id2[3]=='2') { for(index = Assembly2.begin(); index!= Assembly2.end(); ++index) d=d+ calcPartCost(const_cast<char *> ((*index).c_str()),date2); } if(id2[3]=='3') { for(index = Assembly3.begin(); index!= Assembly3.end(); ++index) d=d+ calcPartCost(const_cast<char *> ((*index).c_str()),date2); } return d;}int NumbyId(char id3[6]){ int m; switch(atoi(id3)) { case 10: m=0; break; case 11: m=1; break; case 12: m=2; break; case 20: m=3; break; case 21: m=4; break; case 22: m=5; break; case 30: m=6; break; case 31: m=7; break; case 32: m=8; break; case 33: m=9; break; } return m; }
代码中使用的 2.txt内容:
00010 组合键1 0
00011 零件1 10.00
00012 零件2 21.50
00020 组合键2 0
00021 零件3 10.00
00022 零件4 21.50
00030 组合键3 0
00031 零件5 5.00
00032 零件6 8.50
00033 零件7 10.80
0 0
- 【c++】 递归应用:根据原料清单计算总价
- 计算总价
- AngularJS—实现根据数量、单价计算总价效果
- 计算蔬菜总价
- AngularJS计算总价
- 1.计算蔬菜总价
- vue购物车总价计算
- 购物车加减计算总价
- 分组计算[根据日期][C#]
- C根据日期计算星期
- 总价
- JavaScript由单价、数量计算总价
- android 单价乘以数量计算总价 乘法
- JQuery动态增加删除表格,计算总价
- C 递归和倒序计算
- 根据儒略日计算实际时间 c#+mysql
- C语言根据经纬度计算距离
- 根据生日计算星座(Objective-C)
- Coder-Strike 2014 - Finals (online edition, Div. 2)
- Longest Palindromic Substring-----最长回文子串
- 第六周作业1——利用哈夫曼编码英文字母表
- 全文检索程序coreseek安装配置及注意的问题(平台ubuntu12.04)
- android 关于读取SD卡或者U盘的一些方法
- 【c++】 递归应用:根据原料清单计算总价
- Androidannotation使用之@Rest获取资源及用户登录验证(一)
- 快速幂运算
- 感知器代码
- 修改linux ,root用户名
- 舵机详解
- 编译器
- 访问PCI配置空间方法一(通过I/O端口直接读取)
- Android学习----异常(3)---java.lang.IllegalArgumentException: column '_id' does not exist