Getting started with the basics of programming exercises_2
1、编写简单power函数
#include<stdio.h>
int power(int m, int n);
// test power function
int main(void)
{
int i; for(i = 0; i < 10; ++i)
{
printf("%d %d %d\n", i, power(2, i), power(-3, i));
}
return 0;
}
// power: raise base to n-th power; n >= 0
int power(int base, int n)
{
int i, p; p = 1;
for(i = 1; i <= n; ++i)
p = p * base;
return p;
}
其中power函数如果直接使用形参n的话,可以使程序更简洁
int power(int base, int n)
{
int p; for(p = 1; n > 0; --n)
p = p * base;
return p;
}
2、读入一组文本行,并把最长的文本行打印出来
简单写下算法框架: while(还有未处理的行) if(改行比已处理的最长行还要长) 保存改行为最长行 保存改行的长度 打印最长的行 |
程序如下:
#include<stdio.h>
#define MAXLINE 1000 // maxinmum input line length
int getline(char line[], int maxline);
void copy(char to[], char from[]);
// print the longest input line
int main(void)
{
int len; // current line length
int max; // maximum length seen so far
char line[MAXLINE]; // current input file
char longest[MAXLINE]; // longest line saved here max = 0;
while((len = getline(line, MAXLINE)) > 0)
{
if(len > max)
{
max =len;
copy(longest, line);
}
}
if(max > 0) // there was a line
printf("%s", longest);
return 0;
}
/*
getline: read a line into s, return length
读入文本行时返回改行的长度, 而在遇到文件结束符时返回0. 由于0不是有效的行长度,因此可以作为标志文件结束的返回值。
每一行至少包括一个字符,只包含换行符的行,其长度为1.
*/
int getline(char s[], int lim)
{
int c, i;
for(i = 0; i < lim -1 && (c = getchar()) != EOF && c != '\n'; ++i) // 检查是否溢出,因为程序无法预知输入行长度
s[i] = c;
if(c == '\n')
{
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
// copy: copy 'from' into 'to': assume to is big enough
void copy(char to[], char from[])
{
int i; i = 0;
while((to[i] = from[i]) != '\0')
++i;
}
3、打印任意长度的输入行的长度,并尽可能多地打印文本
#include<stdio.h>
#define MAXLINE 1000 // maximum input line size
int getline(char line[], int maxline);
void copy(char to[], char from[]);
// print longest input line
int main(void)
{
int len; // current line length
int max; // maximum length seen so far
char line[MAXLINE]; // current input line
char longest[MAXLINE]; // longest line saved here max = 0;
while((len = getline(line, MAXLINE)) > 0)
{
printf("%d %s", len, line);
if(len > max)
{
max = len;
copy(longest, line);
}
}
if(max > 0) // there was a line
printf("%s", longest);
return 0;
}
// copy: copy 'from' into 'to'; assume to is big enough
void copy(char to[], char from[])
{
int i; i = 0;
while((to[i] = from[i]) != '\0')
++i;
}
// getline: read a line into s, return length
int getline(char s[], int lim)
{
int c, i, j; // i记录字符串长度, j记录被复制到字符串s中的字符的个数 j = 0;
for(i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
if(i < lim-2)
{
s[j] = c; // line still in boundaries
++j;
}
if(c == '\n')
{
s[j] = c;
++j;
++i;
}
s[j] = '\0';
return i;
}
Getting started with the basics of programming exercises_2的更多相关文章
- Getting started with the basics of programming exercises_5
1.编写函数,把由十六进制数字组成的字符串转换为对应的整型值 编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整型值.字符串中允许包含的数字包括:0~9 ...
- Getting started with the basics of programming exercises_4
1.编写一个删除C语言程序中所有的注释语句的程序.要正确处理带引号的字符串与字符串常量,C语言中程序注释不允许嵌套. #include<stdio.h> void rcomment(int ...
- Getting started with the basics of programming exercises_3
1.编写一个程序删除每个输入行末尾的空格及制表符并删除完全是空白符的行 #include<stdio.h> #define MAXLINE 1000 // maximum input li ...
- Getting started with the basics of programming exercises_1
1.编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替 使用if 结构: #include<stdio.h> #define NONBLANK 'a'; // repal ...
- Beginning C# Programming with Unity
Welcome to the wonderful world of programming! In this book you’ll learn the basics of programming u ...
- C语言学习书籍推荐《Practical C++ Programming》下载
下载链接 :点我 C++ is a powerful, highly flexible, and adaptable programming language that allows software ...
- How do I learn machine learning?
https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644 How Can I Learn X? ...
- LINQ Query Expressions
https://msdn.microsoft.com/en-us/library/bb397676(v=vs.100).aspx Language-Integrated Query (LINQ) is ...
- 【译】微软的Python入门教程(一)
Getting started with Python(Python入门) Overview 概述 The series of videos on Channel 9 is designed to h ...
随机推荐
- MySQL数据库的全局锁和表锁
1.概念 数据库锁设计的初衷是处理并发问题.作为多用户共享的资源,当出现并发访问的时候,数据库需要合理地控制资源的访问规则.而锁就是用来实现这些访问规则的重要数据结构. 2.锁的分类 根据加锁的范围, ...
- BZOJ2069: [POI2004]ZAW
2069: [POI2004]ZAW Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 303 Solved: 138[Submit][Status][D ...
- 【Ruby】与ruby相关的内容
本博文是为了记录Ruby相关的可学习资源,以便日常使用 在线学习Rails的网站版本 Ruby on Rails教程(Rails 5) Ruby的中文社区 Ruby China
- Js 克隆
1.浅表克隆 调用concate() 或者slice() 方法,可以创建数组的浅表副本,在浅表副本中,如果原始数组的元素是复杂数据类型,则元素值指向对象的引用而非对象本身, 与原始数组一样,浅表副本的 ...
- 隐马尔可夫模型及Viterbi算法
隐马尔可夫模型(HMM,hidden Markov model)是可用于标注问题的统计学模型,描述由隐藏的马尔可夫链随机生成观测序列的过程,属于生成模型.HMM模型主要用于语音识别,自然语言处理,生物 ...
- Listview的条目item内的点击响应事件
还是这张图 这里的历史列表就是一个ListView,抛开该界面中ScrollView或者RecycleView与该ListView会有冲突,所谓的冲突,说白了就是父控件与子控件两者间的关系冲突,该冲突 ...
- myeclipse10 java builder path libraries 添加tomcat
Error: The import javax.servlet cannot be resolved The import javax.servlet.http.HttpServlet ...
- 【JZOJ5088】【GDOI2017第四轮模拟day2】最小边权和 排序+动态规划
题面 有一张n个点m条边的有向图,每条边有一个互不相同的边权w,有q个询问,要求你从点a经过不超过c条边到点b,要求经过的边权递增并和尽量小,求出最小的边权和,如果没有合法方案则输出-1. 对于100 ...
- 使用FastJson对实体类和Json还有JSONObject之间的转换
1. 实体类或集合转JSON串 String jsonString = JSONObject.toJSONString(实体类); 2.JSON串转JSONObject JSONObject json ...
- Oracle企业管理框架
oracle管理服务器 是一个基于java的web构件,该构件是dba用来监视和控制oracle企业框架内各个受管理目标的实际界面 oracle储存库 已收集到并与受管理目标有关的配置和监视信息被存储 ...