目标
- 适应Linux环境
- 更深入的思考
- 解决一些C的问题
推荐阅读
- 基本的C1~7,9,10页
- Absolute Beginner’s Guide to C的1~5,9,11~17章
- Programming in C 1~6章
评判原则:
能力,正确性,设计,代码风格
简单命令
cd: change directory
ls: list
mkdir:make a directory
Hello,C
准备:
cs50在线IDE
你要做:
0.注册一个edX账号,进入
1.在命令行中输入update50
更新到最新
2.创建文件目录结构:
~/workspace
|--pset1
--hello.c
--hello.txt
基本流程
1.编辑pset1文件夹中的hello.c,记得保存
#include<stdio.h>
int main(void)
{
printf("hello, world\n");
}
2.make hello编译
3.运行编译后的文件
./hello #.指的当前目录下
4.你会看到以下内容:
hello, world
这时候你回到文件夹会看到除了hello.c和hello.txt,多了一个hello(带* 意味着可执行),这就是编译后的文件
尽管如此,当你运行make的时候,如果看到一些错误信息,说明有bug(如果觉得终端窗口小可以拉动它顶部的边框来增加行高),如果你看到一些像expected declaration的东西,也许你犯了语法错误,也可能一些字符输入错误,彻底的检查一下,当我们学习编程的时候很容易错过细节,认真对照你的代码,在你做出改动之后一定要保存,并且重新编译
CS50 Check
让我们看一下你的程序写的是否正确,在CS50 IDE里面用check50
命令来检查
介绍一个重命名(移动)文件的命令
mv source destination
source:当前文件的名字
destination:想修改的的名字
for instance
mv Hello.c hello.c
假设你的文件名字是hello.c,然后执行下面的代码:
check50 2015.fall.pset1.hello hello.c
如果你的程序是正确的,你会看到:
:) hello.c exists
:) hello.c compiles
:) prints "hello, world\n"
:)说明你的程序通过的检查,你也发现了输出底部的URL,那只是为了工作人员的方便(当然你也可以参观)
程序错误:
:( hello.c exists
\ expected hello.c to exist
:| hello.c compiles
\ can't check until a frown turns upside down
:| prints "hello, world\n"
\ can't check until a frown turns upside down
因为check50没找到hello.c的文件所在,所以会有一个难过脸,其他两行由于依赖于hello.c会产生:|,所以他们不会运行
如果出现以下错误:
:) hello.c exists
:) hello.c compiles
:( prints "hello, world\n"
\ expected output, but not "hello, world"
说明字符串没有像预期那样输出,检查你的字符串
实际上要知道check50
只是一个在提交作业之前让你检查你的作业的正确性的命令,一旦你提交了,cs50的工作人员会使用check50去评估你提交的正确性
Library
什么是库
用于开发软件的子程序集合,库不是独立程序,他们是向其他程序提供服务的代码
例如printf(),他是c标准库的一部分
在课程中会用到的其他库:
input/output(stdio) I/O
string 字符串
math 数学库
cs50 课程用
cryptography 加解密
如何使用
我们在编译后会生成一个object文件(*.o)
编写的库,会把文件分类成header(*.h)
头文件和implementation(*.c)
实现文件
头文件指向库的资源(函数、变量、结构、类型定义…),这些通常称为接口(interface)
.water
实现
如何计算在淋浴时用了多少的水,假定你的淋浴头每分钟会消耗掉1.5gallon的水(1gallon=128盎司,所以是1.5*128=192盎司的水),一瓶矿泉水大约16盎司,之前的数值可以更直观的转换为12瓶(192÷16),当我们淋浴时间为10分钟时,就相当于用了120瓶矿泉水
在~/workspace/pset1
目录下写一个water.c
的文件
输入沐浴的时间就可以得到所耗费的瓶数
代码
#include <stdio.h>
#include <cs50.h>
int main(){
int minutes; //define minutes
do{
printf("please input your num:\n");
minutes = GetInt();
}
while(minutes<=0);
int bottles = minutes*(1.5*128/16); //translate bottle
printf("minutes:%d\n",minutes);
printf("bottles:%d\n",bottles);
}
.mario
实现
模拟超级玛丽1-1中最后的棋子前的那个“半金字塔”
写一个mario.c,它使用”#”符号来构建那个半金字塔,输入你的要盖的高度,返回由#构成的半金字塔,输入范围是23以内的正整数
代码
#include <stdio.h>
#include <cs50.h>
int main(){
char str = '#';
int i;
int row;
int spa;
int height;
printf("height: ");
height=GetInt();// accept nums
while(height>23||height<0){
printf("retry: ");
height=GetInt();
}
for(row=0;row<height;row++){ //set rows length
for(spa=height;spa>row+1;spa--){ //set space length --large to small
printf(" ");
}
for(i=0;i<row+2;i++){ //print #
printf("%c",str);
}
printf("\n");
}
}
.greedy
贪婪算法( Greedy algorithms)
是一种在每一步选择中都采取在当前状态下最好或最优(即最有利)的选择,从而希望导致结果是最好或最优的算法 比如在旅行推销员问题中,如果旅行员每次都选择最近的城市,那这就是一种贪心算法
贪心算法在有最优子结构的问题中尤为有效
最优子结构的意思是局部最优解能决定全局最优解
简单地说,问题能够分解成子问题来解决,子问题的最优解能递推到最终问题的最优解
细节
1.建立数学模型来描述问题
2.把求解的问题分成若干个子问题
3.对每一子问题求解,得到子问题的局部最优解
4.把子问题的解局部最优解合成原来解问题的一个解
实现
写一个greedy.c的程序,首先询问用户需要找的钱数,然后换成尽可能少的硬币数量(从大到小取整钱),使用GetFloat()接收用户输入并输出的所兑换的硬币个数到答案中,假设只有四种硬币可兑换:
25(quarter) 10(dime) 5(nickel) 1(penny)
TODO:
1.接受用户输入
- 合理的输入值(非负数)
- 接受输入值的类型
- 使用GetInt()和GetFloat()
round(x) 对x进行四舍五入
2.尽量换成最大的硬币(最小数量)
3.保持追踪
- 每种硬币兑换多少个
- 总共的数量
4.打印结果
代码
#include <stdio.h>
#include <math.h>
#include <cs50.h>
int main(){
int curr, quarter, dime, nickel, penny;
float nums;
do { printf ("Please input your dollar:\n");
nums = GetFloat(); // get dollar
//printf("%f", nums);
}
while(nums<=0);
curr = round(nums*100); //use round() if not, will !=420
// printf("your have %d penny\n", curr);
quarter = curr/25; //41/25=1 16
dime = (curr%25)/10;//16/10=1 6
nickel = ((curr%25)%10)/5;//6/5=1 1
penny = (((curr%25)%10)%5);
// printf("%dquarter, %ddime, %dnickel, %dpenny\n", quarter, dime, nickel, penny);
printf("%d\n", quarter+dime+nickel+penny);
// printf ("quarter:%d\ndime:%d\nnickel:%d\npenny:%d\n", quarter, dime, nickel, penny);
}