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的更多相关文章

  1. Getting started with the basics of programming exercises_5

    1.编写函数,把由十六进制数字组成的字符串转换为对应的整型值 编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整型值.字符串中允许包含的数字包括:0~9 ...

  2. Getting started with the basics of programming exercises_4

    1.编写一个删除C语言程序中所有的注释语句的程序.要正确处理带引号的字符串与字符串常量,C语言中程序注释不允许嵌套. #include<stdio.h> void rcomment(int ...

  3. Getting started with the basics of programming exercises_3

    1.编写一个程序删除每个输入行末尾的空格及制表符并删除完全是空白符的行 #include<stdio.h> #define MAXLINE 1000 // maximum input li ...

  4. Getting started with the basics of programming exercises_1

    1.编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替 使用if 结构: #include<stdio.h> #define NONBLANK 'a'; // repal ...

  5. Beginning C# Programming with Unity

    Welcome to the wonderful world of programming! In this book you’ll learn the basics of programming u ...

  6. C语言学习书籍推荐《Practical C++ Programming》下载

    下载链接 :点我 C++ is a powerful, highly flexible, and adaptable programming language that allows software ...

  7. 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? ...

  8. LINQ Query Expressions

    https://msdn.microsoft.com/en-us/library/bb397676(v=vs.100).aspx Language-Integrated Query (LINQ) is ...

  9. 【译】微软的Python入门教程(一)

    Getting started with Python(Python入门) Overview 概述 The series of videos on Channel 9 is designed to h ...

随机推荐

  1. Boost Asio教程集合

    http://zh.highscore.de/cpp/boost/ 第七章 https://mmoaay.gitbooks.io/boost-asio-cpp-network-programming- ...

  2. python基础--文件相关操作

    文件操作方式的补充: “+”表示的是可以同时读写某个文件 r+:可读可写 w+:可读可写 a+:可读可写 x:只写模式[不可读:不存在则创建,存在则报错] x+:可读可写 文件内的光标移动: 1.re ...

  3. 关于Ajax提交后无法刷新iframe页面的问题

    原因及解决方法: 要把刷新代码写进ajax的代码块里面,而不是放在后面

  4. Total Number of Unicorn Companies: 188

    https://www.cbinsights.com/research-unicorn-companies   Total Number of Unicorn Companies: 188 Total ...

  5. roc.m

    function [tpr,fpr,thresholds] = roc(targets,outputs) %ROC Receiver operating characteristic. % % The ...

  6. excel怎么并排查看两个工作表

    excel怎么并排查看两个工作表 excel怎么并排查看两个工作表?excel打开一个窗口想要同时查看两个工作表中的数据,类似于word中的分栏效果,该怎么实现呢?EXCEL是一个使用最多的办公软件, ...

  7. 拦截导弹 (最长上升子序列LIS)

    #include <iostream> #include <stdio.h> #include <algorithm> using namespace std; ] ...

  8. 7 种 Javascript 常用设计模式学习笔记

    7 种 Javascript 常用设计模式学习笔记 由于 JS 或者前端的场景限制,并不是 23 种设计模式都常用. 有的是没有使用场景,有的模式使用场景非常少,所以只是列举 7 个常见的模式 本文的 ...

  9. SpringBoot Actuator监控【转】

    springboot actuator 监控 springboot1.5和springboot2.0 的actuator在启动日志上的差异就很大了. springboot1.5在启动时会打印很多/XX ...

  10. VUE中的style 样式处理的Scope (<style scope>)

    在VUE组件中,为了让样式私有化,不对全局造成污染,可以在style标签上添加scoped属性以表示它的只属于当下的模块. 但是这样的话,就会导致无法修改其他第三方组件样式,或者是内嵌的样式,解决方案 ...