有限小数用c语言,分数化为有限小数或无限循环小数(c实现)

问题描述:

将分数转化为小数,相信很多人都会吧.那么,这里给定一个分数N/D,N为分子,D为分母(N,D均为整数),试编程求出N/D的小数形式,当然如果这个小数为无限循环小数,则把循环的部分用括号括起来,接着循环的部分则省略不写。比如:

1/3    =0.(3)

22/5=4.4

1/7    =0.(142857)

2/2    =1.0

3/8    =0.375

45/56    =0.803(571428)

输入为两个正整数N,D(1 <= N,D <= 100000),输出为对应的小数(为了对齐格式,请一行最多输出76个字符)。

样例输入:

1 3

22 5

1 7

对应输出:

0.(3)

4.4

0.(142857)

============

code=================== /** *Copyright (C) aprin at Xiamen University *2005-04-23 */ #include #include #define LEN_SHANG sizeof(struct node_shang) #define LEN_YUSHU sizeof(struct node_yushu) struct node_shang {/*商结点*/   char data;   struct node_shang *next; } *shang_head=0, *shang_tail=0; struct node_yushu {/*余数结点*/   long data;   struct node_yushu *next; } *yushu_head=0, *yushu_tail=0; int shang_empty(void) {/*判断商串是否空*/   return shang_head==0; } int yushu_empty(void) {/*判断余数串是否空*/   return yushu_head==0; } struct node_shang *new_shang_node(char ch) {/*新建商的结点*/   struct node_shang *ptr= (struct node_shang *) malloc(LEN_SHANG);   ptr->data=ch;   ptr->next=0;   return ptr; } struct node_yushu *new_yushu_node(long a) {/*新建余数结点*/   struct node_yushu *ptr= (struct node_yushu *) malloc(LEN_YUSHU);   ptr->data= a;   ptr->next=0;   return ptr; } void insert_shang(char ch) {/*插入商字符串的结点*/   struct node_shang *newptr= new_shang_node(ch);   if(shang_empty())     shang_head=shang_tail=newptr;   else {     shang_tail->next= newptr;     shang_tail= newptr;   } } void insert_yushu(long a) {/*插入余数结点*/   struct node_yushu *newptr= new_yushu_node(a);   if(yushu_empty())     yushu_head=yushu_tail=newptr;   else {     yushu_tail->next= newptr;     yushu_tail= newptr;   } } char *longinttostr(long a, char *str) {/*将长整型转化为字符串*/   char temp;   int i, j;   i=0;   if(a==0) {/*a=0时特别处理*/     str[0]='0';     i++;   } else {     while(a!=0) {       str[i]=(a%10)+'0';       a=a/10;       i++;     }   }   str[i]='/0';   for(j=0; j<=(i-1)/2; j++) {/*倒置*/     temp= str[j];     str[j]= str[i-1-j];     str[i-1-j]= temp;   }   return str;/*返回长度*/ } long found_xunhuan(void) {/*通过余数是否相等判断是否出现循环节,若出现返回出现位置的指针ind(相对小数点的偏移量),若无反回-1*/   struct node_yushu *i;   long ind;   for(i=yushu_head, ind=0; i->next!=0; i=i->next, ind++)     if(yushu_tail->data==i->data)       return ind;   if(i->next==0)     return -1; } void div(long d, long n) {/*d是被除数,n是除数*/   long yushu, shang_zhenshu, temp, i, len_temp;   char str[7];   struct node_shang *j, *new_node;   /*计算整数部分*/   shang_zhenshu= d/n;   d=d%n;   insert_yushu(d);/*余数保存到余数链表*/   longinttostr(shang_zhenshu, str);   i=0;   while(str[i]!='/0') {/*商保存到商链表*/     insert_shang(str[i]);     i++;   }   insert_shang('.');   if(d==0) {/*恰好整除的情况*/     insert_shang('0');     return;   }   while((d!=0)&&((temp=found_xunhuan())==-1)) {/*当除尽或发现循环节时停止*/     d=d*10;/*进位*/     insert_shang((d/n)+'0');     insert_yushu(d%n);     d=d%n;   }   /*除法已完成*/   if(temp!=-1) {/*发现循环节*/     j=shang_head;     while(j->data!='.')/*找到小数点的位置*/       j=j->next;     for(i=0;inext;/*找到循环节开始的前一位*/     new_node= new_shang_node('(');     new_node->next=j->next;     j->next= new_node;     new_node= new_shang_node(')');     shang_tail->next= new_node;     shang_tail= new_node;   } } void output(void) {   struct node_shang *i;   long temp;   i=shang_head;   temp=0;   while(i->next!=0) {     putchar(i->data);     i=i->next;     temp++;     if((temp%76)==0)/*每行输出76个字符*/       putchar('/n');   }   putchar(shang_tail->data);   putchar('/n'); } int main(void) {   long d, n;   scanf("%ld %ld", &d, &n);   while((d<1)||(d>100000)||(n<1)||(n>100000)) {     printf("Input is wrong! Please inpute again!(1<=d, n<=100000)/n");     scanf("%ld %ld", &d, &n);   }   div(d, n);   output();   return 0; }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/257935.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

你该把前端外包出来了

2019独角兽企业重金招聘Python工程师标准>>> 移动热潮慢慢褪去&#xff0c;大的几个app已经霸占了所有的人桌面&#xff0c;而微信却变得越来越重要。微信里面&#xff0c;提倡H5的应用&#xff0c;H5应用开发成本低、上线快、易调整、跨平台等诸多优势&#xff0c;…

R 统计学工具部署和使用

由于公司内部对于市场数据分析的需求&#xff0c;要求引入R统计工具&#xff0c;并集成到报表工具中。对于R的介绍&#xff0c;大家请百度一下&#xff0c;当然&#xff0c;最好能去看官方的说明 https://www.r-project.org/ 下面简单介绍一下R工具的安装和数据分析工具Spotfir…

USACO Dual Palindromes

输出N个大于s的满足条件的数&#xff0c; 对于满足条件的数的定义是其2-10进制表示中&#xff0c;至少有两种表示为回文串。。还是暴力&#xff1a; /*ID: m1500293LANG: CPROG: dualpal */ #include <cstdio> #include <cstring> #include <algorithm>using…

c语言库函数fgets,C语言 标准I/O库函数 fgets 使用心得

char *fgets(char *s, int n, FILE *stream);参数说明&#xff1a;s --指定存放所读取的数据的位置n -- 指定所读取数据的最大长度(这个最大长度包括了字符串结束符 \0所占据的存储空间&#xff0c;因此&#xff0c;实际最大读取的有效字符数是 n - 1)stream --数据源&#xff…

Android下创建一个输入法

输入法是一种可以让用户输入文字的控件。Android提供了一套可扩展的输入法框架&#xff0c;使得应用程序可以让用户选择各种类型的输入法&#xff0c;比如基于触屏的键盘输入或者基于语音。当安装了特定输入法之后&#xff0c;用户即可在系统设置中选择个输入法&#xff0c;并在…

linux awk f,linux的awk详情(上)

一丶awk介绍AWK是一种处理文本文件的语言&#xff0c;是一个强大的文本分析工具&#xff0c;可以报告生成器&#xff0c;格式化文本输出1.常用语法awk [options] ‘program’ varvalue file…awk [options] -f programfile varvalue file…awk [options] BEGIN{ action;… } pa…

C#的async和await

C# 5.0中引入了async 和 await。这两个关键字可以让你更方便的写出异步代码。 看个例子&#xff1a; public class MyClass {public MyClass(){DisplayValue(); //这里不会阻塞System.Diagnostics.Debug.WriteLine("MyClass() End.");}public Task<double> Get…

eclipse创建android工程,在eclipse创建android 工程

1.在工具栏选择"New".在弹出对话框里&#xff0c;开打android文件夹&#xff0c;选择"android application Project"&#xff0c;选择“Next”.2.Application Name: 应用程序名称。Projetc Name: 工程名称。Packet Name: 包名称. 注意&#xff0c;包名称…

SQL select查询原理--查询语句执行原则转

1.单表查询&#xff1a;根据WHERE条件过滤表中的记录&#xff0c;形成中间表&#xff08;这个中间表对用户是不可见的&#xff09;&#xff1b;然后根据SELECT的选择列选择相应的列进行返回最终结果。 1)简单的单表查询 SELECT 字段 FROM 表名 WHERE 条件表达式 那它们是按什么…

android导航二级分类,Android实现腾讯新闻的新闻类别导航效果

效果图如下所示&#xff1a;1、在Adapter中加入如下代码private int clickTemp -1;//TODO 被选择的item下标/** * TODO 传入下标&#xff0c;设置被选择的item * * param position */public void setSelection(int position) {clickTemp position;}2、在Adapter的getView方法…

Linux下访问window挂载的磁盘

点击window挂在的磁盘,如下图左侧"文档". 出现如下错误: Error mounting /dev/sda3 .... Command-line mount -t "ntfs" -o "uhelperudisks2 修复办法: sudo ntfsfix /dev/sda6 参考资料: 1. win8安装ubuntu后不能访问windows其他磁盘转载于:https:…

linux遍历目录源代码

<pre code_snippet_id"1622396" snippet_file_name"blog_20160324_1_744516" name"code" class"cpp">遍历目录获取整个目录的占用空间: uint64_t dir_space(char *path) {int ret 0;uint64_t space 0;char cur_dir[PATH_MAX …

android studio 手动安装gradle,Android Studio 如何安装Gradle?

今天新下载安装了Android Studio 2.1&#xff0c;启动并新建第一个项目&#xff0c;结果卡在 gradle 上。网上搜原因&#xff0c;得到这个网页&#xff1a;http://blog.csdn.net/maxsky/article/details/50204093。说是要自己下载 gradle的压缩包&#xff0c;查了项目目录下的 …

Label 表达式绑定

Text<%#"总金额为: "Convert.ToString(Convert.ToDecimal(TextBox1.Text)*Convert.ToInt32(TextBox2.Text)%> Page_Load { Page.DataBind(); }转载于:https://www.cnblogs.com/handsomer/p/4150386.html

(转)如果知道dll文件是面向32位系统还是面向64位系统的?

本文为转载文章&#xff0c;原文地址&#xff1a;http://www.cnblogs.com/qguohog/archive/2011/09/13/2174897.html&#xff0c;仅仅是记录供后续使用&#xff0c;如有侵权请通知删除。 在发布dll时&#xff0c;可以选择编译为x86模式、x64模式以及Any Cpu模式等。那么对于已经…

Spring Roo 简介

一直以来&#xff0c;Java/Spring开发被认为是笨重的代表&#xff0c;无法快速生成项目原型和骨架。所以&#xff0c;Spring推出了Spring Roo这个项目&#xff0c;帮助我们快速生成项目原型。本文参考自Spring Roo的官方文档&#xff0c;如果熟悉英文的话可以直接看原文档&…

双缓冲 android,Android 的 SurfaceView 双缓冲应用

075 int index 0;本文引用地址&#xff1a;http://www.eepw.com.cn/article/201610/305442.htm076 try {077 index field.getInt(R.drawable.class);078 } catch (IllegalArgumentException e) {079 // TODO Auto-generated catch block080 e.printStackTrace();081 } catch …

Windows—JDK安装与环境变量配置

本文介绍JDK的安装与环境变量配置。 工具/原料 JDK1.8.0_65WIN7 32bitjdk-8u65-windows-i586.exe方法/步骤 安装JDK 选择安装目录 安装过程中会出现两次 安装提示 。第一次是安装 jdk &#xff0c;第二次是安装 jre 。建议两个都安装在同一个java文件夹中的不同文件夹中。&…

典型案例道出“服务台”的价值

引 言&#xff1a;作为运营管理着庞大IT系统的CIO&#xff0c;相信您或多或少都尝试过&#xff0c;或正建有IT服务台&#xff08;或帮助台&#xff09;&#xff0c;然而您可能依然面临服务效率低下&#xff0c;用户满意度欠佳的 困扰。这其中的原因&#xff0c;多半就在于您的服…

数据的艺术

数据的艺术概念:数据 --程序操作的对象&#xff0c;用于描述客观事物。数据的特点:a. 可以输入到计算机b. 可以被计算机程序处理*数据是一个抽象的概念&#xff0c;将其进行分类得到程序设计语言中的类型。数据元素 -组成数据的基本单位a. 数据项:一个数据元素由若干数据项组成…