最近在学习ios开发,学Objective-c时顺带复习了一下C语言基础,把自己写的一些代码记录于此。
1,输入几月几日,计算是2016年的第几天。
#include
int main(int argc ,char * argv[]){
int month,day,days=0;
printf("输入月");
scanf("%d",&month);
printf("输入日");
scanf("%d",&day);
switch(month-1){
case 11:days+=30;
case 10:days+=31;
case 9:days+=30;
case 8:days+=31;
case 7:days+=31;
case 6:days+=30;
case 5:days+=31;
case 4:days+=30;
case 3:days+=31;
case 2:days+=29;
case 1:days+=31;
default:days+=day;break;
}
printf("这一天是2016年的第%d天\n",days);
return 0;
}
2,写一个函数,返回输入整数的每位数的数字之和。#include
int function1(int number){
int result=0;
while (number!=0) {
result+=number%10;
number/=10;
}
return result;
}
int main(int argc, const char * argv[]) {
int a;
printf("输入一个整数:\n");
scanf("%d",&a);
printf("%d 各位数之和为 %d\n",a,function1(a));
return 0;
}
3,满足abc+cba=1333 的数字a b c 共有多少种组合?
#include
int main(int argc, const char * argv[]) {
int a,b,c;
int i=0;
printf("\nabc+cba=1333\n\n");
for (int x=100; x<=999; x++) {
a=x/100;
b=x/10%10;
c=x%10;
if(c*100+b*10+a+x==1333){
printf("a=%d,b=%d,c=%d\n",a,b,c);
i++;
}
}
printf("共%d个组合\n",i);
return 0;
}
4,输出如下图形
// *
// ***
//*****
// ***
// *
#include
int main(int argc, const char * argv[]) {
int x,a,b;
for ( x=1;x<=5; x++) {
if (x<=3) {
a=2*x-1;
b=3-x;
}
else{
a=11-2*x;
b=x-3;
}
for (int i=0; i
#include
int main(int argc, const char * argv[]) {
int q=0;
for(int a=12;a<=98;a++){
for (int b=12; b<=a; b++) {
if ((a%11!=0)&&(b%11!=0)) {
int x=a%10*10+a/10;
int y=b%10*10+b/10;
if (a * b == x * y) {
printf("%dx%d=%dx%d\n",a,b,x,y);
q++;
}
}
}
}
printf("共%d对\n",q);
return 0;
}
#include
int main(int argc, const char * argv[]) {
int sum=0;
for (int a=0; a<=3; a++) {
for (int b=0; b<=3; b++) {
for (int c=0; c<=6; c++) {
if (a+b+c==8) {
sum++;
printf("第%d种组合:\t红球%d个,白球%d个,蓝球%d个\n",sum,a,b,c);
}
}
}
}
printf("共%d种组合\n",sum);
return 0;
}
7,打印9 9乘法表
#include
int main(int argc, const char * argv[]) {
for (int a=1; a<=9; a++) {
for (int b=1; b<=a; b++) {
printf("%d x %d = %d \t",b,a,a*b);
}
printf("\n");
}
return 0;
}
// A
// ABA
// ABCBA
// ABCDCDA
// ABCDEDCBA
#include
int main(int argc, const char * argv[]) {
for (int i=1; i<=5; i++) {
for (int j=0; j<5-i; j++) {
printf(" ");
}
char ch='A';
for (int k=0; k<2*i-1; k++) {
printf("%c",ch);
if (k<(2*i-1)/2) {
ch++;
}
else {
ch--;
}
}
printf("\n");
}
return 0;
}
// *
// * *
// * *
// * *
// * *
// * *
// *
#include
#include
#include
int main(int argc, const char * argv[]) {
int N=7;
for (int i=0; i
10,输出一个整数的二进制表示如 输入6 输出 110
#include
int main(int argc, const char * argv[]) {
int result[32]={0};
int number;
printf("输入一个正整数:\n");
scanf("%d",&number);
printf("%d的二进制为:\n",number);
for (int i=0;number>0; i++) {
result[i]=number%2;
number/=2;
}
int flag=0;
for (int i=31; i>=0; i--) {
if (result[i]!=0) {
flag=1;
}
if (flag==1) {
printf("%d",result[i]);
}
}
return 0;
}
#include
int main(int argc, const char * argv[]) {
char String [1000];
printf("输入一个字符串\n");
scanf("%s",String);
for (int i=0; String[i]!='\0'; i++) {
if (String[i]>='a'&&String[i]<='z') {
String[i]+=3;
if (String[i]>'z') {
String[i]=String[i]-26;
}
}
if (String[i]>='A'&&String[i]<='Z') {
String[i]+=3;
if (String[i]>'Z') {
String[i]=String[i]-26;
}
}
}
printf("加密之后\n%s",String);
return 0;
}
#include
int main(int argc, const char * argv[]) {
char number[50]={0};
double result=0;
unsigned long length;
int L;
printf("输入一个数(如12.34或231)\n");
scanf("%s",number);
length=strlen(number);
for (L=0; number[L]!='.'&&L
13,找出一个二维数组中的“鞍点”,即该位置上的元素在该行中最大,在该列中最小(也可能没有“鞍点”)
#include
int main(int argc, const char * argv[]) {
int n[5][5]={
8,3,4,9,6,
6,4,5,7,6,
5,2,3,4,5,
8,5,6,17,8,
8,6,7,8,9
};
for (int i=0; i<5; i++) {
int max=n[i][0];
for (int j=0 ; j<5; j++) {
if (n[i][j]>=max) {
max =n[i][j];
}
}
for (int a=0; a<5; a++) {
if (n[i][a]==max) {
int min=n[i][a];
int flag=1;
for (int b=0; b<5; b++) {
if (n[b][a]
int IntArryMax(int numbers[],int length){//返回整数数组中的最大值-为工具函数
int max=numbers[0];
for(int i=0;imax){
max=numbers[i];
}
}
return max;
}
void SortIntArry(int source[],int length,int isMAxFirst){//用冒泡法排序整型数组-为第二种方法的工具函数
for(int i=0;isource[j+1]) {
int temp=source[j];
source[j]=source[j+1];
source[j+1]=temp;
}
}
}
}
}
int function(int numbers[],int length){//第一种方法:返回整数数组中数字的最大重复次数
const int n=IntArryMax(numbers, length);
int mark[n];
for (int i=0; i
void function4(char * source){
int flag=1;
for (int i=0;source[i]!='\0'; i++) {
if (flag==1&&(source[i]>='a'&&source[i]<='z')){
source[i]-=32;
flag=0;
}
if (!(source[i]>='a'&&source[i]<='z')&&!(source[i]>='A'&&source[i]<='Z')&&source[i]!='\'') {
flag=1;
}
}
}
#include
int main(int argc, const char * argv[]) {
char *string="The People Republic of china,is the best\ncontry all over the world!";
char *longestWord=string;
int length=0;
char *curentWord=string;
int tempLength=0;
int flag=1;
for (int i=0; ilength) {
longestWord=curentWord;
length++;
}
}else{
flag=1;
}
}
printf("最长的单词");
for (int i=0; i
#include
int main(int argc, const char * argv[]) {
int people[13]={0};
int i=0,j=0;
int count=0;
int *p=people;
while (i!=12) {
if(*p==0){
count++;
if (count==3) {
*p=1;
i++;
count=0;
}
}
p++;
j++;
if (j>12) {
p=people;
j=0;
}
}
for (int i=0; i<13; i++) {
if (people[i]==0) {
printf("第%d个人是叛徒 ",i+1);
break;
}
}
printf("\nHello, World!\n");
return 0;
}
main.c
#include
#include "ATM.h"
int main(int argc, const char * argv[]) {
init();
return 0;
}
#ifndef ATM_h
#define ATM_h
#include
#include
#include
typedef struct _account{
char * name;
char * account;
char * passwd;
long money;
}Account;
void init();
int LoginSystem(char *account,char * passwd);
void displayBalance(int ID);
int getMoney(int ID);
int putMoney(int ID);
int changePasswd(int ID);
int giveMoney2(int ID);
#endif /* ATM_h */
#include "ATM.h"
Account accounts[4] = {
{"name1", "1001", "123456", 10000},
{"name2", "1002", "123123", 20000},
{"name3", "1003", "123456", 30000},
{"name4", "1004", "111111", 40000}
};
int ATMmoney=1000000;
int LoginSystem(char *account,char * passwd){
for (int i=0; i<4; i++) {
if (strcmp(accounts[i].account, account)==0&&strcmp(accounts[i].passwd, passwd)==0) {
return i;
}
}
return -1;
}
void displayBalance(int ID){
printf("账户:%s\t用户名:%s\n账户余额:%ld\n",accounts[ID].account,accounts[ID].name,accounts[ID].money);
}
int getMoney(int ID){
int money=0;
printf("请输入取款金额:");
scanf("%d",&money);
if (money>ATMmoney) {
printf("ATM机余额不足!\n");
return 0;
} else if (money>accounts[ID].money) {
printf("账户余额不足!\n");
return 0;
} else if(money%100!=0){
printf("请取整百金额!\n");
return 0;
} else {
accounts[ID].money-=money;
ATMmoney-=money;
printf("取款成功!!\n");
}
return 1;
}
int putMoney(int ID){
int money=0;
printf("请输入存款金额:");
scanf("%d",&money);
accounts[ID].money+=money;
ATMmoney+=money;
printf("存款成功!!\n");
return 1;
}
int changePasswd(int ID){
char passwd[20];
char * passwdnew=malloc(sizeof(char)*20);
char passwdnew2[20];
printf("请输入旧密码:\n");
scanf("%s",passwd);
if (strcmp(accounts[ID].passwd, passwd)==0) {
printf("请输入新密码:\n");
scanf("%s",passwdnew);
printf("确认新密码:\n");
scanf("%s",passwdnew2);
if (strcmp(passwdnew, passwdnew2)==0) {
accounts[ID].passwd=passwdnew;
printf("修改成功!!");
}
}
else printf("密码错误!!\n");
return 1;
}
int giveMoney2(int ID){
int money;
int targetID;
long targetIDmoney;
printf("请输入目标账户:\n");
char targetAccount[20];
scanf("%s",targetAccount);
for (targetID=0; targetID<4; targetID++) {
if (strcmp(accounts[targetID].account, targetAccount)==0) {
targetIDmoney=accounts[targetID].money;
break;
}
}
printf("输入转账金额:\n");
scanf("%d",&money);
printf("目标账户:%s\t目标用户名:%s\n转账金额:%d\n确认?(yes/no)\n",accounts[targetID].account,accounts[targetID].name,money);
char order[3];
scanf("%s",order);
if (strcmp(order, "yes")==0) {
accounts[targetID].money+=money;
accounts[ID].money-=money;
printf("转账成功!!\n");
}else printf("操作已取消。\n");
return 1;
}
void mypause(){
printf("按回车键继续。。。。\n");
getchar();
getchar();
}
void init(){
while (1) {
char account[20];
char passwd[20];
int userID=-1;
printf("===天地·银行===\n==欢迎使用ATM机==\n\n");
printf("\t请登录\n");
printf("用户名:");
scanf("%s",account);
printf("密码:");
scanf("%s",passwd);
userID=LoginSystem(account,passwd);
if (userID!=-1) {
int order;
printf("登录成功!\n");
while (userID!=-1) {
printf("1、显示当前余额\n2、取款\n3、存款\n4、转账\n5、修改密码\n6、退出\n");
scanf("%d",&order);
switch (order) {
case 1:displayBalance(userID);mypause();
break;
case 2:getMoney(userID);mypause();
break;
case 3:putMoney(userID);mypause();
break;
case 4:giveMoney2(userID);mypause();
break;
case 5:changePasswd(userID);mypause();
break;
case 6:printf("欢迎再次使用!!\n");userID=-1;
break;
default:
break;
}
}
}
}
}