C语言打印空心菱形
#include<stdio.h>
void printStars();
void main(){
int totalLevel = 5;
printStars(totalLevel);
getchar();
}
void printStars(int totalLevel){
int i,j,k;
for(i=0;i<=totalLevel;i++){
for(k=totalLevel-i;k>0;k--){
printf(" ");
}
for(j=0;j<2*i-1;j++){
if(i>0 && i<=totalLevel){
if(j==0 || j==2*i-2){
printf("*");
}else{
printf(" ");
}
}else{
printf("*");
}
}
printf("\n");
}
for(i=0;i<=totalLevel-1;i++){
for(k=0;k<=i;k++){
printf(" ");
}
for(j=0;j<(2*(totalLevel-i-1)-1);j++){
if(i>=0 && i<totalLevel-1){
if(j==0 || j==(2*(totalLevel-i-1)-2)){
printf("*");
}else{
printf(" ");
}
}else{
printf("*");
}
}
printf("\n");
}
}
C语言中,char数组的赋值问题。
之所以,char a[10]; a=“hello”;错误。个人理解: 就是在声明一个10个字符的空间的数组的时候,a就变成了指向这个空间的指针,之后执行a=“hello”;相当于是一个赋值操作,但是字符数组是不能赋给一个常量的,(指针地址视为常量)。 但是语法char a[10]="hello"符合C语言语法,理由简单来说就是在开辟空间的时候,做了对应字符位置上的赋值操作,视为a[0]='a’等等,且此时的=号并不理解为赋值操作,应该视为初始化操作。
练习
编程创建一个 Box 结构体,在其中定义三个成员表示一个立方体的长、宽和高,长宽高可以通过控制台输入。 定义一个函数获取立方体的体积(volume)。 创建一个结构体,打印给定尺寸的立方体的体积。
#include <stdio.h>
struct Box{
double length;
double weight;
double height;
double volume;
};
double getVolume(struct Box box){
box.volume = box.length*box.weight*box.height;
return box.volume;
}
void main(){
double result;
struct Box box;
scanf("%lf",&box.length);
scanf("%lf",&box.weight);
scanf("%lf",&box.height);
result = getVolume(box);
printf("体积为=%.2f",result);
getchar();
getchar();
}
注意:结构体作为函数的形参的时候,默认传入的是值传递。可以修改为“地址传递”。
景区门票例子(结构体,地址传递)
#include <stdio.h>
#include <string.h>
struct Visitor{
char name[10];
int age;
double payment;
};
void pay(struct Visitor *visitor){
if((*visitor).age>18){
(*visitor).payment = 20.0;
}else{
(*visitor).payment = 0;
}
}
void main(){
struct Visitor visitor;
while (1)
{
printf("输入名字=");
scanf("%s", visitor.name);
if (!strcmp(visitor.name,"n"))
{
break;
}
printf("输入年龄=");
scanf("%d", &visitor.age);
pay(&visitor);
printf("应该付=%.2f\n",visitor.payment);
}
printf("程序结束");
getchar();
getchar();
}
家庭收支软件实现
非模块化版本实现(直接使用主函数实现,维护性差)
#include <stdio.h>
#include <string.h>
void main(){
int operation;
char details[3000]="-----------------------当前收支明细记录-----------------------\n收支\t收支金额\t\t账户金额\t\t说明";
double balance=1000.0;
char temp[300]="";
double money;
char note[30]="";
char choice = ' ';
int loop=1;
int flag=0;
do
{
printf("\n\n--------------------家庭收支记账软件--------------------");
printf("\n 1 收支明细");
printf("\n 2 登记收入");
printf("\n 3 登记支出");
printf("\n 4 退出");
printf("\n请选择(1-4): ");
scanf("%d",&operation);
getchar();
switch (operation)
{
case 1:
if(flag){
printf("\n%s",details);
}else{
printf("没有任何记录!");
}
break;
case 2:
printf("\n本次收入的金额:");
scanf("%lf",&money);
balance+=money;
getchar();
printf("\n本次收入的说明:");
scanf("%s",¬e);
getchar();
sprintf(temp,"\n收入\t%.2f\t\t\t%.2f\t\t\t%s",money,balance,note);
strcat(details,temp);
flag=1;
break;
case 3:
printf("\n本次支出的金额:");
scanf("%lf",&money);
getchar();
if(money>balance){
printf("您的余额不足");
break;
}else{
balance-=money;
}
printf("\n本次支出的说明:");
scanf("%s",¬e);
getchar();
sprintf(temp,"\n收入\t%.2f\t\t\t%.2f\t\t\t%s",money,balance,note);
strcat(details,temp);
flag=1;
break;
case 4:
do
{
printf("\n确认是否退出(Y/N):");
scanf("%c",&choice);
getchar();
if(choice=='y' || choice=='n'){
break;
}
printf("您的输入有误,请重新输入!");
} while (1);
if (choice=='y')
{
loop=0;
}
default:
continue;
}
} while (loop);
printf("您已经退出软件~~");
getchar();
}
家庭收支软件实现,模块化编程(将功能分开,模块化)
#include <stdio.h>
#include <string.h>
struct MyFamilyAccount{
int flag;
char details[3000];
double balance;
};
int operation;
char temp[300] = "";
double money;
char note[30] = "";
char choice = ' ';
int loop = 1;
void pay(struct MyFamilyAccount *myFamilyAccount){
printf("\n本次支出的金额:");
scanf("%lf", &money);
getchar();
if (money > (*myFamilyAccount).balance)
{
printf("您的余额不足");
return;
}
else
{
(*myFamilyAccount).balance -= money;
}
printf("\n本次支出的说明:");
scanf("%s", ¬e);
getchar();
sprintf(temp, "\n收入\t%.2f\t\t\t%.2f\t\t\t%s", money, (*myFamilyAccount).balance, note);
strcat((*myFamilyAccount).details, temp);
(*myFamilyAccount).flag = 1;
}
void income(struct MyFamilyAccount *myFamilyAccount){
printf("\n本次收入的金额:");
scanf("%lf", &money);
(*myFamilyAccount).balance += money;
getchar();
printf("\n本次收入的说明:");
scanf("%s", ¬e);
getchar();
sprintf(temp, "\n收入\t%.2f\t\t\t%.2f\t\t\t%s", money, (*myFamilyAccount).balance, note);
strcat((*myFamilyAccount).details, temp);
(*myFamilyAccount).flag = 1;
}
void showDetails(struct MyFamilyAccount *myFamilyAccount){
if ((*myFamilyAccount).flag)
{
printf("\n%s", (*myFamilyAccount).details);
}
else
{
printf("没有任何记录!");
}
}
void MyExit(){
do
{
printf("\n确认是否退出(Y/N):");
scanf("%c", &choice);
getchar();
if (choice == 'y' || choice == 'n')
{
break;
}
printf("您的输入有误,请重新输入!");
} while (1);
if (choice == 'y')
{
loop = 0;
}
}
void mainMenu(struct MyFamilyAccount *myFamilyAccount){
do
{
printf("\n\n--------------------家庭收支记账软件--------------------");
printf("\n 1 收支明细");
printf("\n 2 登记收入");
printf("\n 3 登记支出");
printf("\n 4 退出");
printf("\n请选择(1-4): ");
scanf("%d",&operation);
getchar();
switch (operation)
{
case 1:
showDetails(myFamilyAccount);
break;
case 2:
income(myFamilyAccount);
break;
case 3:
pay(myFamilyAccount);
break;
case 4:
MyExit();
default:
continue;
}
} while (loop);
printf("您已经退出软件~~");
getchar();
}
void main(){
struct MyFamilyAccount myFamilyAccount;
myFamilyAccount.flag=0;
myFamilyAccount.balance=1000.0;
memset(myFamilyAccount.details,3000,0);
strcpy(myFamilyAccount.details,"-----------------------当前收支明细记录-----------------------\n收支\t收支金额\t\t账户金额\t\t说明");
mainMenu(&myFamilyAccount);
}
客户管理系统
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
struct Consumer{
int id;
char name[20];
char gender[2];
int age;
char phone[30];
char email[50];
};
int loop=1;
struct Consumer consumers[100];
int ConsumerNum = 0;
int findConsumerIndexById(int id){
for(int i=0;i<ConsumerNum;i++){
if(consumers[i].id==id){
return i;
}
}
return -1;
}
void UpdateConsumerInfo(){
int ConsumerId;
int index;
char key=' ';
int flag=1;
do
{
printf("\n---------------修改客户---------------");
printf("\n请选择待修改客户编号(-1退出):");
scanf("%d", &ConsumerId);
getchar();
if (ConsumerId == -1)
{
return;
}
index = findConsumerIndexById(ConsumerId);
if (index!=-1)
{
flag=0;
}else{
printf("\n\n没有找到对应的客户,请重新输入!");
}
} while (flag);
char temp[10] = {'\0'};
int inttemp=0;
printf("\n姓名(%s):", consumers[index].name);
int i = 0;
while((key = getchar()) != '\n'){
consumers[index].name[i++] = key;
}
printf("\n性别(%s):", consumers[index].gender);
i = 0;
while((key = getchar()) != '\n'){
consumers[index].gender[i++] = key;
}
printf("\n年龄(%d):", consumers[index].age);
i = 0;
if((key = getchar()) != '\n'){
consumers[index].age = 0;
consumers[index].age = key - '0';
while((key = getchar()) != '\n'){
consumers[index].age = 10 * consumers[index].age + key - '0';
}
}
printf("\n电话(%s):", consumers[index].phone);
i = 0;
while((key = getchar()) != '\n'){
consumers[index].phone[i++] = key;
}
printf("\n邮箱(%s):", consumers[index].email);
i = 0;
while((key = getchar()) != '\n'){
consumers[index].email[i++] = key;
}
printf("\n---------------修改完成---------------");
}
void delConsumer(){
int ConsumerId;
int index;
char key=' ';
int flag=1;
do
{
printf("\n---------------删除客户---------------");
printf("\n请选择待删除客户编号(-1退出):");
scanf("%d", &ConsumerId);
getchar();
if (ConsumerId == -1)
{
return;
}
index = findConsumerIndexById(ConsumerId);
if (index!=-1)
{
flag=0;
}else{
printf("\n\n没有找到对应的客户,请重新输入!");
}
} while (flag);
printf("\n确认是否删除(Y/N):");
scanf("%c",&key);
getchar();
if(key=='y'){
for (int i = index; i < ConsumerNum; i++)
{
consumers[i] = consumers[i + 1];
}
printf("\n---------------删除完成---------------");
ConsumerNum--;
}else{
printf("\n---------------删除取消---------------");
return;
}
}
void addConsumer(){
printf("\n---------------添加客户---------------");
consumers[ConsumerNum].id=ConsumerNum+1;
printf("\n姓名:");
scanf("%s",&consumers[ConsumerNum].name);
getchar();
printf("\n性别:");
scanf("%s",&consumers[ConsumerNum].gender);
getchar();
printf("\n年龄:");
scanf("%d",&consumers[ConsumerNum].age);
getchar();
printf("\n电话:");
scanf("%s",&consumers[ConsumerNum].phone);
getchar();
printf("\n邮箱:");
scanf("%s",&consumers[ConsumerNum].email);
getchar();
ConsumerNum++;
printf("\n---------------添加完成---------------");
}
void MyExit(){
char operition=' ';
int flag=1;
do
{
printf("\n确认是否退出(Y/N):");
scanf("%c",&operition);
getchar();
if(operition=='y'){
loop=0;
return;
}else if(operition=='n'){
flag=0;
}else{
printf("输入错误,请重新输入");
}
} while (flag);
}
void getConsumerInfo(struct Consumer *consumer){
printf("\n%d\t%s\t%s\t%d\t%s\t%s",(*consumer).id,(*consumer).name,(*consumer).gender,(*consumer).age,(*consumer).phone,(*consumer).email);
}
void showConsumerInfo(){
int i;
printf("\n--------------------客户列表---------------------");
printf("\n编号\t姓名\t性别\t年龄\t电话\t邮箱");
for(i=0;i<ConsumerNum;i++){
getConsumerInfo(&consumers[i]);
}
printf("\n\n--------------------客户列表完成---------------");
}
void mainMenu(){
int choice;
do
{
printf("\n\n--------------客户信息管理软件--------------");
printf("\n 1 添加客户");
printf("\n 2 修改客户");
printf("\n 3 删除客户");
printf("\n 4 客户列表");
printf("\n 5 退 出");
printf("\n 请选择(1-5):");
scanf("%d",&choice);
getchar();
switch (choice)
{
case 1:
addConsumer();
break;
case 2:
UpdateConsumerInfo();
break;
case 3:
delConsumer();
break;
case 4:
showConsumerInfo();
break;
case 5:
MyExit();
default:
break;
}
} while (loop);
printf("你已退出软件");
getchar();
}
void main(){
mainMenu();
}
|