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 ...
随机推荐
- NKOJ1469 通向自由的钥匙
P1469通向自由的钥匙 时间限制 : 10000 MS 空间限制 : 65536 KB 问题描述 通向自由的钥匙被放n个房间里,这n个房间由n-1条走廊连接.但是每个房间里都有特别 的保护魔 ...
- Go开发 之 Go如何引用github包
- cf round 482D Kuro and GCD and XOR and SUM
题意: 开始有个空集合,现在有两种操作: $(1,x)$:给集合加一个数$x$,$x \leq 10^5$; $(2,x,k,s)$:在集合中找一个$a$,满足$a \leq s-x$,而且$k|gc ...
- jmeter 之 https 请求
本文为自己学习总结,转载需说明出处. jmeter发送https请求需要在巨jmeter中导入被测试网站的安全证书. 一.通过浏览器登录对应的网站导出安全证书.下面操作以360浏览器为例: 二.把导出 ...
- mac pro 1.5T内存是如何实现的
苹果发布全新Mac Pro:28核1.5T内存 预计售价10万+ 看到这样的新闻标题是不是很震撼,甚至怀疑人生,64位机怎么就可以1.5T内存了,自己的系统盘都没那么大 而且我们知道windows下的 ...
- PHP正则表达式判断身份
//身份证验证 $id_card = ''; $preg_card="/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d ...
- 逐行粒度的vuex源码分析
vuex源码分析 了解vuex 什么是vuex vuex是一个为vue进行统一状态管理的状态管理器,主要分为state, getters, mutations, actions几个部分,vue组件基于 ...
- 【水滴石穿】MyFirstRNDemo
比较简单的项目 //index.js /** @format */ import {AppRegistry} from 'react-native'; //默认创建的类 import App from ...
- golang的包规则
1.包声明:文件所在目录,有利于源码整体迁移. 2.包导入:4种方式. ---------------------------------------------------------------- ...
- startActivity 流程图