1. C語言編程 選擇結構程序設計
最終結果——m=3
switch
(a%3)
→a為16,a%3為1
→
執行
case
1
→
m初值為0,m++為1。注意,這里case1
並沒有break,所以會繼續向下執行完整個switch
(a%3)
→
執行switch
(b%2)
→
b為21,b%2為1
→
執行default
→
m為1,m++為2
→
注意這里依然會繼續執行case0
→
m++為3,break跳出switch
(b%2)
→switch
(a%3)語句結束
→
執行printf,此時m為3。
#include<stdio.h>
voidmain(){
inta=16,b=21,m=0;
switch(a%3){
case0:m++;break;
case1:m++;
switch(b%2){
default:m++;
case0:m++;break;
}
}
printf("m=%d
",m);
}
運行結果
2. c語言選擇結構中進行編程的大致步驟有哪些
摘要 首先明確要解決的問題;
3. C語言編程,2選1,急急
太麻煩了,這個你可以參照個下.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define N 58
char wj[100]; //存儲當前文件
int k=0; //判斷當前是否打開文件
struct stu_info //用結構體存儲每個學生的基本信息
{
char xh[15];
char xm[9];
char xb[3];
char mz[5];
char zzmm[5];
int nl;
struct stu_info *next;
};
void input(struct stu_info *head) //輸入模塊,雒東祥負責
{
struct stu_info *p,*q; //p,q分別指向當前節點和下一個節點
q=head;
while(q->next!=NULL) //遍歷到最後
q=q->next;
printf("請輸入要錄入學生的信息(學號,姓名,性別,民族,政治面貌,年齡)\n");
p=(struct stu_info *)malloc(sizeof(struct stu_info));
printf("請錄入:");
scanf("%s%s%s%s%s%d",p->xh,p->xm,p->xb,p->mz,p->zzmm,&p->nl);
q->next=p;
q=p;
printf("錄入成功!\n");
printf("按「1」繼續錄入!\n");
q->next=NULL;
}
void save(struct stu_info *head) //保存模塊 劉建宏負責
{
FILE *fp;
struct stu_info *p=head->next;
fp=fopen(wj,"wb");
if(fp==NULL)
printf("文件不能建立,信息不能正常保存!\n");
else
{
while(p!=NULL)
{
fprintf(fp,"%15s%9s%3s%5s%5%4d\n",p->xh,p->xm,p->xb,p->mz,p->zzmm,p->nl);
p=p->next;
}
fclose(fp);
printf("保存成功!\n");
}
}
void open(struct stu_info *head) //打開模塊 牛強強負責
{
FILE *fp;
struct stu_info *p,*q=head;
printf("請輸入你要打開的班級文件名\n");
scanf("%s",wj);
strcat(wj,".dat");
fp=fopen(wj,"rb");
if(fp==NULL)
printf("文件信息不能正常打開!\n");
else
{
while(!feof(fp))
{
p=(struct stu_info *)malloc(sizeof(struct stu_info));
fscanf(fp,"%s%s%s%s%s%d\n",p->xh,p->xm,p->xb,p->mz,p->zzmm,&p->nl);
q->next=p;
q=p;
}
q->next=NULL;
fclose(fp);
printf("打開成功!\n");
k=1;
}
}
struct stu_info * search(struct stu_info *head) //查詢模塊 楊嘉瑛負責
{
struct stu_info *p,*q=head;
char dcxh[15];
scanf("%s",dcxh);
p=head->next;
while((p!=NULL)&&(strcmp(p->xh,dcxh)!=0))
{
q=p;
p=p->next;
}
if(p==NULL)
printf("沒有這個學生!\n");
else
{
printf("該學生的信息為:\n");
printf("學號 姓名 性別 民族 政治面貌 年齡\n");
printf("%15s%9s%5s%5s%9s%4d\n",p->xh,p->xm,p->xb,p->mz,p->zzmm,p->nl);
}
return q;
}
void modify(struct stu_info *head) //修改模塊 馬濤負責
{ int i;
struct stu_info *p;
printf("請輸入要修改學生的學號:\n");
p=search(head);
p=p->next;
if(p!=NULL)
{
printf("按「1」鍵確定修改\n");
scanf("%d",&i);
if(i==1)
{
printf("請輸入要修改學生的新信息(學號,姓名,性別,民族,政治面貌,年齡)\n");
scanf("%s%s%s%s%s%d",p->xh,p->xm,p->xb,p->mz,p->zzmm,&p->nl);
printf("你已修改!\n");
}
}
printf("按「1」鍵繼續修改!\n");
}
void delet(struct stu_info *head) //刪除模塊 楊嘉瑛負責
{
int i;
struct stu_info *p,*q;
printf("請輸入要刪除學生的學號:\n");
q=search(head);
p=q->next;
if(p!=NULL)
{
printf("按「1」鍵確定刪除該學生!\n");
scanf("%d",&i);
if(i==1)
{
q->next=p->next;
free(p);
printf("該學生已被刪除!\n");
}
}
printf("按「1」鍵繼續刪除!\n");
}
void list(struct stu_info *head) //按學號列表 蓋仲德負責
{
int i=1;
struct stu_info *p,*q,*r;
p=head->next;
head->next=NULL;
while(p!=NULL)
{
q=head;
p=head->next;
while((r!=NULL)&&(strcmp(p->xh,r->xh)<0))
{
q=r;
r=r->next;
}
q->next=p;
p=p->next;
if(r==NULL)
q->next->next=NULL;
else
q->next->next=r;
}
p=head->next;
printf(" ********班級基本信息********\n");
printf("序號 學號 姓名 性別 民族 政治面貌 年齡\n");
while(p!=NULL)
{
printf("%3d%15s%9s%5s%5s%9s%5d\n",i++,p->xh,p->xm,p->xb,p->mz,p->zzmm,p->nl);
p=p->next;
if(i%22==0)
system("pause");
}
}
void jiami(char *p) //加密模塊 秦亞妮負責
{
while(*p!='\0')
{
*p+=100;
p++;
}
}
void jiemi(char *p) //解密模塊 秦亞妮負責
{
while(*p!='\0')
{
*p-=100;
p++;
}
}
void repass() //修改密碼模塊 蓋仲德負責
{
FILE *fp;
int i=3;
char mm[2][19];
fp=fopen("pass.ini","rb");
if(fp==NULL)
printf("無法修改!\n");
else
{
fscanf(fp,"%s",mm[0]);
fclose(fp);
jiemi(mm[0]);
while(i--)
{
printf("請輸入舊密碼:\n");
scanf("%s",mm[1]);
if(strcmp(mm[0],mm[1])==0||strcmp(mm[1],"guest")==0)
{
printf("請輸入新密碼:\n");
scanf("%s",mm[0]);
printf("再輸入一次確認:\n");
scanf("%s",mm[1]);
if(strcmp(mm[1],mm[0])==0)
{
fp=fopen("pass.ini","wb");
if(fp==NULL)
printf("修改失敗,請重試!\n");
else
{
jiami(mm[0]);
fprintf(fp,"%19s",mm[0]);
fclose(fp);
printf("修改成功!\n");
break;
}
}
else
printf("你輸入不一致!\n");
}
else
printf("舊密碼錯誤,請重新輸入:\n");
}
if(i==0)
printf("你無權修改!\n");
}
}
int test() //密碼測試模塊 蓋仲德負責
{
FILE *fp;
char mm[2][19];
int i=3;
printf("**************歡迎使用!*************\n");
fp=fopen("pass.ini","rb");
if(fp==NULL)
{
printf("你第一次使用!\n");
do
{
printf("請設置密碼!\n");
printf("請輸入你要設置的密碼(不包含空格):");
scanf("%s",mm[0]);
printf("請再次輸入你的密碼:");
scanf("%s",mm[1]);
if(strcmp(mm[1],mm[0])==0)
{
fp=fopen("pass.ini","wb");
if(fp==NULL)
printf("此環境不支持!\n");
else
{
jiami(mm[0]);
fprintf(fp,"%19s",mm[0]);
fclose(fp);
printf("設置成功!\n");
return 1;
}
}
printf("你兩次輸入不一致,請重試!\n");
}while(strcmp(mm[0],mm[1])!=0);
}
else
{
fscanf(fp,"%s",mm[0]);
fclose(fp);
jiemi(mm[0]);
while(i--)
{
printf("請輸入密碼:\n");
scanf("%s",mm[1]);
if(strcmp(mm[0],mm[1])==0||strcmp(mm[1],"guest")==0)
return 1;
printf("你輸入錯誤,請重新輸入!\n");
}
printf("你無權使用此系統!\n");
return 0;
}
return 0;
}
void newwj(struct stu_info *head) //新建模塊 劉建宏負責
{
FILE *fp;
printf("請輸入要新建的班級名:");
scanf("%s",wj);
strcat(wj,".dat");
fp=fopen(wj,"rb");
if(fp==NULL)
{
printf("新建成功!\n");
k=1;
}
else
{
fclose(fp);
printf("新建失敗!(可能此文件已存在。)請重新新建\n");
}
}
void nuew(int i,struct stu_info *head) //菜單模塊 牛強強負責
{
if(k==0)
switch(i)
{
case 9: newwj(head); break;
case 2: open(head);break;
case 10:repass();break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8: printf("請先新建或打開!\n");break;
case 1: break;
case 0: break;
case 11: break;
default: printf("你輸入錯誤!請重輸:\n");
}
else
switch(i)
{
case 3: input(head);break;
case 4: save(head);break;
case 5: printf("請輸入要查找的學生學號:\n");search(head);
printf("按「1」鍵繼續查詢!\n");break;
case 6: modify(head);break;
case 7: delet(head);break;
case 8: list(head);break;
case 10: repass(); break;
case 2:
case 9: printf("請先關閉當前文件!\n");break;
case 1: break;
case 0: break;
case 11:break;
default: printf("你輸入錯誤!請重輸:\n");
}
}
void main() //主函數 蓋仲德負責
{
struct stu_info *head,*p,*q;
int str,ch=4;
int i,count=0;
head=(struct stu_info *)malloc(sizeof(struct stu_info));
head->next=NULL;
p=head->next;
i=test();
if(i)
{
while(ch!=0)
{
while(ch!=11 && ch!=0)
{
if((count++%5)==0)
{
printf("操作提示:\n");
printf("2-打開 3-錄入 4-保存 5-查詢 6-修改 7-刪除 8-列出 \n9-新建 10-修改密碼 11-關閉 0-退出 1-重復\n");
}
str=ch;
scanf("%d",&ch);
if(ch==1)
ch=str;
nuew(ch,head);
}
if(str!=4)
{
printf("是否要保存你的操作?按「1」鍵保存!\n");
scanf("%d",&i);
if(i==1)
{
save(head);
str=4;
}
}
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
head->next=NULL;
k=0;
if(ch==11)
{
printf("文件已關閉!\n");
ch=4;
}
}
if(str!=4)
{
printf("是否要保存你的操作?按「1」鍵保存!\n");
scanf("%d",&i);
if(i==1)
save(head);
}
}
printf("****************謝謝使用!****************\n");
}
4. 用C語言編程:用選擇法對10個整數排序,10個整數用scanf函數輸入
1、打開visual C++ 6.0,准備一個空白的c語言文件,引入頭文件,在main函數中定義變數和數組:
5. C語言程序設計,為什麼選A
單引號是用來表示單個字元的 裡面只能有一個字元
如果是字元串需要用雙引號
B是常量字元串
C是整型常量
D是浮點型常量
6. c語言選擇結構中進行編程的大致步驟有哪些
摘要 程序設計離不開演算法,演算法指導程序設計,演算法是程序的靈魂。因此程序設計的大致步驟如下。
7. 用c語言編程,應該選擇哪個
C++目前用的最多的
8. 急求c語言選擇編程程序
親,在么?是要程序代碼,還是寫實驗步驟?
第一個:
float x=0.0,y=0.0;
printf("請輸入X的值:");
scanf("%f",&x);
if (x<=0)
{
y=abs(pow(x,3)-1);
}
else
{
y=sqrt(pow(x,3)+1);
}
printf("所求的Y值為%f",y);
第二個
float x=0.0,rate=0.0,y=0.0;
printf("請輸入貸款");
scanf("%f",&x);
if (x>=10000)
{
rate=0.05;
}
if (5000<=x&&x<10000)
{
rate=0.04;
}
if (1000<=x&&x<5000)
{
rate=0.03;
}
if (x<1000)
{
rate=0;
}
y=x*rate;
printf("稅金為%f",y);
9. VS2015中想用C語言編程怎麼選擇
解決這個問題的方法如下:
1、首先打開vs2015,進入主界面。