c/c++读取规律的.dat文件并保存到结构体中(以齿轮参数为例)

// 齿轮选择.cpp : Defines the entry point for the console application.

#include<iostream> 
#include<cstdio> 
#include<cmath>
#include<string.h> 
using namespace std; 

const int Max=10;//字符串最大长度 

class Gear;//类声明 
void setData(Gear &s);//设置对象s的数据 
void count(Gear &s);//计算对象s的总分,平均分 
void sort(Gear S[],int N);//把长度为N的对象数组S,按平均分排序 
double getAverage(Gear S[],int N);//计算全班的平均分 
void print(Gear &s);//打印信息 
int search(Gear S[],int N,char *n);//从长度为M的对象数组中,查找学号n的位置 
class Gear 
{
public:
	char number[Max]; //序号
	char name[Max];	//名称
	double power; //功率
	double speed;	//转速
	double ratio; //传动比
	double moshu;//模数 
	double chishu; //齿数 
//是否热处理 
}; 

class data_ 
{
public:
	char a[10][10];
	char b[10][10];
}; 

float chars2float(char str[])
{
	float number0=0,number1=0;
	int flag=0;
	for(int i=0;i<10;i++) 
	{ 
		if(str[i]=='.')
			flag=1;
		else if(str[i]>='0'&&str[i]<='9')
		{
			if(flag==0)
			{
				number0=10*number0+str[i]-48; 
				//cout<<number0<<endl;
			}
			else
			{
			 	number1=number1+pow(0.1,flag)*(str[i]-48);
			 //	cout<<number1<<endl;
			 	flag++;
			}
		}
		else break;
	} 
	return number0+number1;
}
int main() //系统集成
{ 
	
	Gear canshu[10];
	data_ here[10];
	FILE *fp;
	fp=fopen("齿轮数据0.dat","r");
	int j=0,jj=0,n=0,nn=0;
	int m=0;
	int row[10]={0},column[10][10]={0};
for(m=0;m<10;m++)
{
	n=0;nn=0;
	for(j=0;j<10;j++)
	{
		for(jj=0;jj<10;jj++)
		{
			fscanf(fp,"%c",&(here[m].a[j][jj]));
			if(here[m].a[j][jj]==',')
			{
				column[m][n]=nn;
				n++;
				nn=0;
				break;			 
			}
			else if(here[m].a[j][jj]==';')
			{
				column[m][n]=nn;
				row[m]=n+1;
				n=0;
				nn=0;
				j=10;
				jj=10;
				break;
			}
			else if(here[m].a[j][jj]=='!')
			{
				column[m][n]=nn;
				row[m]=n+1;
				m=10;
				j=10;
				break;
			}
			else if(here[m].a[j][jj]=='\n')//还包含一个换行符'\n'
			{
				//cout<<endl;
			}
			else  
			{
				here[m].b[n][nn]=here[m].a[j][jj];
				//cout<<"m="<<m<<"   "<<"n="<<n<<"   "<<"nn="<<nn<<"   "<<here[m].b[n][nn]<<endl;			
				nn++;
				//cout<<"j="<<j<<"   "<<"jj="<<jj<<"   "<<here[m].a[j][jj]<<endl;			
			}
		}		
		//cout<<here[m].b[n]<<endl;	
	}
	//cout<<here[m].b<<endl;	
}

for(m=0;m<10;m++)
{
	//cout<<endl<<row[m];
	for(j=0;j<row[m];j++)
	{
		//cout<<"   "<<column[m][j]<<"  ";
		for(jj=0;jj<column[m][j];jj++)
		{	
			//cout<<chars2float(here[m].b[j])<<endl;
			//cout<<endl<<"m="<<m<<"   "<<"j="<<j<<"   "<<"jj="<<jj<<"   "<<here[m].b[j][jj]<<endl;			
		}		
	}	
}

for(m=0;m<10;m++)
{
	for(j=0;j<row[m];j++)
	{
		cout<<chars2float(here[m].b[j])<<endl;		
	}
	cout<<endl<<endl;	
}


for(m=0;m<10;m++)
{
		canshu[m].power=chars2float(here[m].b[0]);
		canshu[m].speed=chars2float(here[m].b[1]);
		canshu[m].ratio=chars2float(here[m].b[2]);
		canshu[m].moshu=chars2float(here[m].b[3]);
		canshu[m].chishu=chars2float(here[m].b[4]);
		cout<<endl;			
}
	double power0=0; //功率
	double speed0=0;	//转速
	double ratio0=0; //传动比
	
	/*char ssss[10][10]={"12.54","7","54.23"};
	cout<<endl<<chars2float(ssss[1]);*/
	cin>>power0>>speed0>>ratio0;
	for(m=0;m<10;m++)
{
	if(power0==canshu[m].power)
		if(speed0==canshu[m].speed)
			if(ratio0==canshu[m].ratio)
			{
				cout<<"模数m="<<canshu[m].moshu<<endl;
				cout<<"齿数z="<<canshu[m].chishu<<endl;
				break;
			}
			else  ;
		else ;
	else if(m==9) cout<<"文件中不存在!"	;		
}
	

	return 0; 
} 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值