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 ...
随机推荐
- db link的查看创建与删除 1
1.查看dblink select owner,object_name from dba_objects where object_type='DATABASE LINK'; 或者 select * ...
- Struts_添加客户练习
1.修改CustomerAction,实现ModelDriven接口 2.修改配置文件 3.修改表单提交地址
- 洛谷P1508 Likecloud-吃、吃、吃 [2017年4月计划 动态规划10]
P1508 Likecloud-吃.吃.吃 题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一 ...
- bzoj2049: [Sdoi2008]Cave 洞穴探测
bzoj2049: [Sdoi2008]Cave 洞穴探测 给n个点,每次连接两个点或切断一条边,保证是树结构,多次询问两个点是否联通 Lct裸题 //Achen #include<algori ...
- 手机端点击键盘无法获取keyCode值的部分时隐藏键盘并执行事件
用计时器监视window.innerHeight高度改变来判断.触发键盘其他地方也有事件反应 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- 使用新版本5+SDK创建最简Android原生工程(Android studio)http://ask.dcloud.net.cn/article/13232
1 使用Android Studio创建一个工程 2 删除原生工程中Java目录下系统默认创建的源代码 3 复制SDK->libs->lib.5plus.base-release.aar文 ...
- 【JZOJ5088】【GDOI2017第四轮模拟day2】最小边权和 排序+动态规划
题面 有一张n个点m条边的有向图,每条边有一个互不相同的边权w,有q个询问,要求你从点a经过不超过c条边到点b,要求经过的边权递增并和尽量小,求出最小的边权和,如果没有合法方案则输出-1. 对于100 ...
- python基础知识--标志位的设定
在单层循环的退出中,使用break即能退出,那么多层循环呢?机智的人们使用flag标识符的方式,例如: exit_flag = False for i in range(10): if i <5 ...
- 电影的微信小程序
最近,工作没有那么忙,学习了一下小程序开发,感觉上手比较简单. 在项目中学习是最好的方式,于是就自己模仿豆瓣电影开发一款微信小程序版的豆瓣电影 准备工作: 数据来源:豆瓣电影API 功能: 电影榜单列 ...
- SQLServer —— 数据类型的转换
一.使用convert函数实现强制转换 例如我们现在有如下一张学员成绩表: 现在想查询学号等于100003的学员总成绩,并按照要求打印出来,我们可以这样实现: 结果报错,因为最后一句字符串不能和数值相 ...