LHL算法入门经典 5.2.3 高精度运算类bign

5.2.3 高精度运算类bign

在这里插入图片描述

#include<stdio.h>
#include<string.h>
#include<math.h>
const int maxn=1000;
struct bign
{
	int len,s[maxn];
	bign()
	{memset(s,0,sizeof(s));len=1;}//构造函数
 
	bign operator =(const char* num)//运算符重载
	{
		len=strlen(num);
		for(int i=0;i<len;i++)
		s[i]=num[len-i-1]-'0';
		return *this;
	}
 
	bign operator =(int num)//运算符重载
	{
		char s[maxn];
		sprintf(s,"%d",num);
		*this=s;
		return this;
	}
 
	bign(int num){*this=num;}//构造函数重载
	bign(const char* num){*this=num;}
 
	string str() const
	{
		string res="";
		for(int i=0;i<len;i++)
		res=(char)(s[i]+'0')+res;//字符串用加号连接
		if(res=="")
		res="0";
		return res;
	}
 
	bign operator + (const bign& b) const//重载运算符 +
	{
		bign c;//两数之和
		c.len=0;
		for(int i=0,g=0;g||i<max(len,b.len);i++)
		{
			int x=g;
			if(i<len)x+=s[i];//大数每位相加
			if(i<b.len)x+=b.s[i];
			c.s[c.len++]=x%10;//每位不超过9
			g=x/10;//进位
		}
		return c;
	}
 
	bool operator < (const bign& b) const//重载运算符 <
	{
		if(len!=b.len)return len<b.len;//位数不同,长度比较结果与大小比较结果一致,true或false
		for(int i=len-1;i>=0;i--)
			if(s[i]!=b.s[i])return s[i]<b.s[i];//位数相同,结果与每位数字的比较结果一致
		return false;
	}
 
};
 
 
istream& operator >> (istream &in,bign& x)
{
	string s;
	in>>s;
	x=s.c_str();//c_str()是string类中的一个函数,返回当前字符串的首地址
	return in;
}
 
ostream& operator >> (ostream &in,bign& x)
{
	out<<x.str();
	return out;
}
int main()
{
	
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
详细设计: 1. 框图 ``` +----------------+ +------------------------+ | | | | | 用户操作界面 |<--------->| 服务器处理 | | | | | +----------------+ +------------------------+ ``` 2. 程序实现 (1)添加学生信息 ``` void add_student_info(char* file_name, char* student_id, char* student_name, float student_score) { FILE* fp = fopen(file_name, "a"); fprintf(fp, "%s\t%s\t%.2f\n", student_id, student_name, student_score); fclose(fp); } ``` (2)修改学生信息 ``` void modify_student_info(char* file_name, char* student_id, char* student_name, float student_score) { FILE* fp = fopen(file_name, "r"); FILE* tmp_fp = fopen("tmp.txt", "w"); char buf[1024]; int found = 0; while (fgets(buf, sizeof(buf), fp) != NULL) { char id[10], name[20]; float score; sscanf(buf, "%s\t%s\t%f", id, name, &score); if (strcmp(id, student_id) == 0) { fprintf(tmp_fp, "%s\t%s\t%.2f\n", student_id, student_name, student_score); found = 1; } else { fprintf(tmp_fp, "%s", buf); } } fclose(fp); fclose(tmp_fp); if (found) { remove(file_name); rename("tmp.txt", file_name); } else { remove("tmp.txt"); } } ``` (3)删除学生信息 ``` void delete_student_info(char* file_name, char* student_id) { FILE* fp = fopen(file_name, "r"); FILE* tmp_fp = fopen("tmp.txt", "w"); char buf[1024]; int found = 0; while (fgets(buf, sizeof(buf), fp) != NULL) { char id[10]; sscanf(buf, "%s", id); if (strcmp(id, student_id) == 0) { found = 1; } else { fprintf(tmp_fp, "%s", buf); } } fclose(fp); fclose(tmp_fp); if (found) { remove(file_name); rename("tmp.txt", file_name); } else { remove("tmp.txt"); } } ``` (4)查询学生信息 ``` void query_student_info(char* file_name, char* student_id) { FILE* fp = fopen(file_name, "r"); char buf[1024]; int found = 0; while (fgets(buf, sizeof(buf), fp) != NULL) { char id[10], name[20]; float score; sscanf(buf, "%s\t%s\t%f", id, name, &score); if (strcmp(id, student_id) == 0) { printf("%s\t%s\t%.2f\n", id, name, score); found = 1; break; } } fclose(fp); if (!found) { printf("Student info not found!\n"); } } ``` (5)显示所有学生信息 ``` void display_all_student_info(char* file_name) { FILE* fp = fopen(file_name, "r"); char buf[1024]; while (fgets(buf, sizeof(buf), fp) != NULL) { char id[10], name[20]; float score; sscanf(buf, "%s\t%s\t%f", id, name, &score); printf("%s\t%s\t%.2f\n", id, name, score); } fclose(fp); } ``` 3. 核心步骤图 (1)添加学生信息 ![add_student_info](https://i.imgur.com/0xZ8JYf.png) (2)修改学生信息 ![modify_student_info](https://i.imgur.com/8fO8mY0.png) (3)删除学生信息 ![delete_student_info](https://i.imgur.com/FXiw3xq.png) (4)查询学生信息 ![query_student_info](https://i.imgur.com/1RUK2gX.png) (5)显示所有学生信息 ![display_all_student_info](https://i.imgur.com/SOJ8LhL.png)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值