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 ...
随机推荐
- Leetcode96.Unique Binary Search Trees不同的二叉搜索树
给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...
- 举例分析private的作用【c/c++学习】
抛砖引玉: c++中private的用处 我知道我们可以用 public 中的值,把private中的数据给提出来,但是还是搞不懂private该怎么用,或者说在一个具体程序中,private有什么用 ...
- ScrollView 实现子视图滑动到顶部时固定不动
这个,个人建议使用自己写的布局使用view的gon或者visble的方法,使用design包中的控件来的话,局限性很大 方法有倆 (1)自定义ScrollView 重写ScrollView 的 com ...
- vue学习之组件(component)(二)
自定义事件 父组件使用 prop 传递数据给子组件.但子组件怎么跟父组件通信呢?这个时候 Vue 的自定义事件系统就派得上用场了. 1. 使用 v-on 绑定自定义事件 每个vue实例都实现了事件接口 ...
- 利用 awk 将当前的链接按端口汇总倒排序
写了一行命令,利用 awk 将当前的链接按端口汇总倒排序 :) netstat -ano | awk /tcp.*:1[15].*:[1-5]/'{print $4}' | awk -F ':' ' ...
- 微信小程序之上拉加载更多
loadmore 加载更多(分页加载) 当用户打开一个页面时,假设后台数据量庞大时,一次性地返回所有数据给客户端,页面的打开速度就会有所下降,而且用户只看上面的内容而不需要看后面的内容时,也浪费用户流 ...
- 2017年浙工大迎新赛热身赛 A 毕业设计选题 【结构体排序】
时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 又到了一年一度,大四老学长们毕业设计选题的时候, ...
- Ajax系列之二:核心对象XMLHttpRquest
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhanghongjie0302/article/details/31432939 ...
- Vue 实现展开折叠效果
Vue 实现展开折叠效果 效果参见:https://segmentfault.com/q/1010000011359250/a-1020000011360185 上述链接中,大佬给除了解决方法,再次进 ...
- Leetcode874.Walking Robot Simulation模拟行走的机器人
机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方.该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x <= 9:向 ...