編譯編程題
首先scanf_s是錯誤的,有兩處。
另外你的程序演算法有問題,會出現死循環(用f10單步執行看看你就知道了),自己再檢查一下吧
這類問題用鏈表來做很簡單的,下面我用的是雙向循環鏈表來實現的,可以參考一下
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define NULL 0
struct List
{
int data;
char name[15];
struct List *next;
struct List *prior;
}List;
struct List * InitList(int n)
{//建立一個雙向循環鏈表,以head為頭結點,數據從1開始至n,返回head指針
struct List *head,*p,*q;
int i;
head=(struct List *)malloc(sizeof(struct List));
head->data=1;
printf("請輸入第1個小孩的名字:");
scanf("%s",head->name);
head->next=head;
head->prior=head;
p=head;
for(i=2;i<=n;i++)
{
q=(struct List *)malloc(sizeof(struct List));
q->data=i;
printf("請輸入第%d個小孩的名字:",i);
scanf("%s",q->name);
p->next=q;
q->prior=p;
q->next=head;
head->prior=q;
p=p->next;
}
return head;
}
void GetList(struct List *head,int w,int s)
{
struct List *p,*q;
p=head;
int i;
for(i=1;i<w;i++,p=p->next);
for(;p->next!=p;i++,p=p->next)
if((i-w+1)%s==0)
{
q=p->prior;
p->next->prior=p->prior;
p->prior->next=p->next;
printf("%s\n",p->name);
free(p);
p=q;
}
printf("%s\n",p->name);
}
void main()
{
int n,w,s;
struct List *head;
do
{
system("cls");
printf("請輸入總小孩人數:");
scanf("%d",&n);
if(n>0)head=InitList(n); //可重復運行直至n<=0停止
else exit(0);
printf("請輸入最先報數的小孩的位置及間隔:");
scanf("%d,%d",&w,&s);
GetList(head,w,s);
system("pause");
}while(1);
}
2. 編譯原理編程題
#include <stdio.h>
int identf(char *str);
int isKey(char *str);
int x_strcmp(char *si, char *di);
int main()
{
char buffer[256];
printf("Enter a string: ");
gets(buffer);
if(identf(buffer))
puts("合法的標識符");
else
puts("非法的標識符");
return 0;
}
/**************************************************************************/
int identf(char *str)
{
int flag=0;
char *p=str;
/*判斷第一個字元是否符合*/
if((*str>='a' && *str<='z') || (*str>='A' && *str<='Z') || *str=='_')
{
flag=1;
str++;
}
/*從第二個字元開始遍歷判斷*/
while(*str && flag)
{
if((*str>='a' && *str<='z') || (*str>='A' && *str<='Z') || *str=='_'
|| (*str>='0' && *str<='9'))
{
flag=1;
str++;
}
else
flag=0;
}
/*判斷是不是關鍵字*/
if(flag)
flag=isKey(p);
return flag;
}
/*****************************是不是關鍵字*******************************/
int isKey(char *str)
{
char *keyword[]={"auto", "break", "case", "char", "const", "continue", "default",
"do", "double", "else", "enum", "extern", "float", "for", "goto",
"if", "int", "long", "register", "return", "short", "signed",
"sizeof", "static", "struct", "switch", "typedef", "union", "unsigned",
"void", "volatile", "while"};
int i, flag=1;
for(i=0; i<32; i++)
{
if(x_strcmp(str, keyword[i])==0)
{
flag=0;
break;
}
}
return flag;
}
/*****************************字元串比較函數*******************************/
int x_strcmp(char *si, char *di)
{
while(*si && *di && *si==*di)
{
si++;
di++;
}
return *si-*di;
}
3. C語言編程題
long fun(int k)
{
if(i<2)
return 1L;
return k*fun(k-1);
}
或:
#include "stdio.h"
main()
{
double h,c;
//printf("Input h ");
scanf("%lf",&h);
c=5.0/9*(h-32);
printf("c=%lf",c);
}
(3)編譯編程題擴展閱讀:
C語言包含的各種控制語句僅有9種,關鍵字也只有32 個,程序的編寫要求不嚴格且以小寫字母為主,對許多不必要的部分進行了精簡。實際上,語句構成與硬體有關聯的較少,且C語言本身不提供與硬體相關的輸入輸出、文件管理等功能,如需此類功能,需要通過配合編譯系統所支持的各類庫進行編程,故c語言擁有非常簡潔的編譯系統。
4. c語言編程題
#include <stdio.h>
#include <math.h>
int main()
{
double x,y;
scanf("%lf",&x);
if(x<=-2)
y=-pow(exp(1),2*x+1)+3;
else if(x<=3)
y=2*x-1;
else
y=2*log10(3*x+5)-11;
printf("%lf ",y);
return 0;
}
5. C語言編程題目
#include<stdio.h>
#include<assert.h>
#define N 3
float fee(int x)
{
double money;
assert(x>=0);
if(x<=300)
{
money=0.6*x;
}
else if(x<=600)
{
money=0.6*300+0.5*(x-300);
}
else if(x<=1000)
{
money=0.6*300+0.5*300+0.4*(x-600);
}
else
{
money=0.6*300+0.5*300+0.4*400+0.3*(x-1000);
}
return (float)money;
}
int main()
{
int a[3],i,n=N;
printf("請輸入%d個客戶的用電量:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("fee(%d)=%0.1f\n",a[i],fee(a[i]));
}
return 0;
}
//該程序經過VC++6.0成功編譯執行,沒有任何問題,最後祝樓主學習進步。
6. C語言編程題,用gcc編譯器
#include<stdio.h>
voidmain(){
inti,j,m,n,num=0,s=0;
scanf("%d%d",&m,&n);
if(m<1||n>500||m>n)
printf("輸入數據錯誤");
else{
for(i=m;i<=n;i++){
for(j=2;j<=i/2;j++)
if(i%j==0)break;
if(j>i/2&&i>1)
{num++;s+=i;}
}
printf("%d%d",num,s);
}
}
//運行示例:
7. 5道簡單的java編程題(高分懸賞)
很詳細的幫你寫下,呵呵,所以要給分哦!
1、
(1)源程序如下:
public class One {
public static void main(String[] args) {
String name = "張三";
int age = 23;
char sex = '男';
String myclass = "某某專業2班";
System.out.println("姓名:" + name);
System.out.println("姓名:" + age);
System.out.println("姓名:" + sex);
System.out.println("姓名:" + myclass);
}
}
(2)
編寫完程序的後綴名是.java,如本題,文件名就是One.java。
開始\運行\cmd,進入「命令提示符窗口」,然後用javac編譯器編譯.java文件,語句:javac One.java。
(3)
編譯成功後,生成的文件名後綴是.class,叫做位元組碼文件。再用java解釋器來運行改程序,語句:java One
2、編寫程序,輸出1到100間的所有偶數
(1)for語句
public class Two1 {
public static void main(String[] args) {
for(int i=2;i<=100;i+=2)
System.out.println(i);
}
}
(2)while語句
public class Two2 {
public static void main(String[] args) {
int i = 2;
while (i <= 100) {
System.out.println(i);
i += 2;
}
}
}
(3)do…while語句
public class Two3 {
public static void main(String[] args) {
int i = 2;
do {
System.out.println(i);
i += 2;
}while(i<=100);
}
}
3、編寫程序,從10個數當中找出最大值。
(1)for循環
import java.util.*;
public class Three1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int i = 0; i < 10; i++) {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
}
System.out.println("最大值:" + max);
}
}
(2)while語句
import java.util.*;
public class Three2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
while (i < 10) {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}
System.out.println("最大值:" + max);
}
}
(3)do…while語句
import java.util.*;
public class Three3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
do {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}while(i<10);
System.out.println("最大值:" + max);
}
}
4、編寫程序,計算從1到100之間的奇數之和。
(1)for循環
public class Four1 {
public static void main(String[] args) {
int sum=0;
for(int i = 1;i<=100;i+=2){
sum+=i;
}
System.out.println("1~100間奇數和:" + sum);
}
}
(2)while語句
public class Four2 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
sum += i;
i += 2;
}
System.out.println("1~100間奇數和:" + sum);
}
}
(3)do…while語句
public class Four3 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i;
i += 2;
} while (i <= 100);
System.out.println("1~100間奇數和:" + sum);
}
}
5、
(1)什麼是類的繼承?什麼是父類?什麼是子類?舉例說明。
繼承:是面向對象軟體技術當中的一個概念。如果一個類A繼承自另一個類B,就把這個A稱為"B的子類",而把B稱為"A的父類"。繼承可以使得子類具有父類的各種屬性和方法,而不需要再次編寫相同的代碼。在令子類繼承父類的同時,可以重新定義某些屬性,並重寫某些方法,即覆蓋父類的原有屬性和方法,使其獲得與父類不同的功能。另外,為子類追加新的屬性和方法也是常見的做法。繼承需要關鍵字extends。舉例:
class A{}
class B extends A{}
//成員我就不寫了,本例中,A是父類,B是子類。
(2)編寫一個繼承的程序。
class Person {
public String name;
public int age;
public char sex;
public Person(String n, int a, char s) {
name = n;
age = a;
sex = s;
}
public void output1() {
System.out.println("姓名:" + name + "\n年齡:" + age + "\n性別:" + sex);
}
}
class StudentPerson extends Person {
String school, department, subject, myclass;
public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s);
school = sc;
department = d;
subject = su;
myclass = m;
}
public void output2() {
super.output1();
System.out.println("學校:" + school + "\n系別:" + department + "\n專業:"
+ subject + "\n班級:" + myclass);
}
}
public class Five2 {
public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大學", "某某系別",
" 某專業", "某某班級", " 張三", 23, '男');
StudentPersonDemo.output2();
}
}
8. 幾道C語言編程題
多給些分吧,累死我了:(
給,都已經編譯運行確認了:
1.
下標法:
#include<conio.h>
#include<stdio.h>
int main()
{
int a[11]={0,1,2,3,4,5,6,7,8,9,10};
int N;
int i,j,temp;
printf("Please input N: \n");
scanf("%d",&N);
for(i=0;i<N;i++)
{
temp=a[10];
for(j=10;j>=1;j--)
{
a[j]=a[j-1];
}
a[0]=temp;
}
printf("After Move: \n");
for(i=0;i<11;i++) printf("%d ",a[i]);
getch();
return 1;
}
指針法:
#include<conio.h>
#include<stdio.h>
int main()
{
int a[11]={0,1,2,3,4,5,6,7,8,9,10};
int N;
int i,j,temp;
printf("Please input N: \n");
scanf("%d",&N);
for(i=0;i<N;i++)
{
temp=*(a+10);
for(j=10;j>=1;j--)
{
*(a+j)=*(a+j-1);
}
*a=temp;
}
printf("After Move: \n");
for(i=0;i<11;i++) printf("%d ",*(a+i));
getch();
return 1;
}
2.
數組法:
#include<conio.h>
#include<stdio.h>
int main()
{
int a[20]={0};
int i,j,sum=0;
printf("請輸入20個數: \n");
for(i=0;i<20;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",&a[i]);
}
printf("輸入的數為: \n");
for(i=0;i<20;i++)
{
printf("%d ",a[i]);
if((i+1)%5==0) printf("\n");
}
for(i=0;i<20;i++)
if(a[i]%2==0) sum+=a[i];
printf("\n偶數的和為: %d",sum);
getch();
return 1;
}
指針法:
#include<conio.h>
#include<stdio.h>
int main()
{
int a[20]={0};
int i,j,sum=0;
printf("請輸入20個數: \n");
for(i=0;i<20;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",a+i);
}
printf("輸入的數為: \n");
for(i=0;i<20;i++)
{
printf("%d ",*(a+i));
if((i+1)%5==0) printf("\n");
}
for(i=0;i<20;i++)
if(*(a+i)%2==0) sum+=*(a+i);
printf("\n偶數的和為: %d",sum);
getch();
return 1;
}
3.
指針法:#include<conio.h>
#include<stdio.h>
int main()
{
int a[15]={0};
int i,num;
printf("請輸入15個數: \n");
for(i=0;i<15;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",a+i);
}
printf("要找的數為: \n");
scanf("%d",&num);
for(i=0;i<15;i++)
if(*(a+i)==num)
{
printf("該數是數組中的元素的第%d個元素的值.\n",i+1);
break;
}
if(i==15)
printf("無此數.");
getch();
return 1;
}
數組法:
#include<conio.h>
#include<stdio.h>
int main()
{
int a[15]={0};
int i,num;
printf("請輸入15個數: \n");
for(i=0;i<15;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",&a[i]);
}
printf("要找的數為: \n");
scanf("%d",&num);
for(i=0;i<15;i++)
if(a[i]==num)
{
printf("該數是數組中的元素的第%d個元素的值.\n",i+1);
break;
}
if(i==15)
printf("無此數.");
getch();
return 1;
}
4.
數組法:
#include<conio.h>
#include<stdio.h>
int main()
{
int a[11]={0};
int i,num;
int p=0;
printf("請輸入10個數: \n");
for(i=0;i<10;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",&a[i]);
}
printf("要插入的數為: \n");
scanf("%d",&num);
while(num>a[p]&&p<10)
p=p+1;
for(i=9;i>=p-1;i--)
a[i+1]=a[i];
a[p]=num;
printf("Result: \n");
for(i=0;i<11;i++)
printf("%d ",a[i]);
getch();
return 1;
}
指針法:
#include<conio.h>
#include<stdio.h>
int main()
{
int a[11]={0};
int i,num;
int p=0;
printf("請輸入10個數: \n");
for(i=0;i<10;i++)
{
printf("%d: ",i+1);
fflush(stdin);
scanf("%d",a+i);
}
printf("要插入的數為: \n");
scanf("%d",&num);
while(num>*(a+p)&&p<10)
p=p+1;
for(i=9;i>=p-1;i--)
*(a+i+1)=*(a+i);
*(a+p)=num;
printf("Result: \n");
for(i=0;i<11;i++)
printf("%d ",*(a+i));
getch();
return 1;
}
9. C語言編程題,DEVC編譯器
#include<stdio.h>
#include<string.h>
intmain()
{
intn,i,j;
charout='a';
scanf("%d",&n);
for(i=0;i<n-1;++i){
for(j=0;j<i;++j){
printf("");
}
printf("%c",out);
out++;
if(out>'h')out='a';
for(j=0;j<(n-1-i)*2+1;++j){
printf("");
}
printf("%c",out);
out++;
if(out>'h')out='a';
printf(" ");
}
for(j=0;j<=i;++j){
printf("");
}
printf("%c",out);
return0;
}
/*
ab↙
cd↙
ef↙
gh↙
a↙
*/
10. 編譯預處理編程題 :用條件編譯實現從鍵盤上輸入一行電文,可以任意選擇兩種輸出
如果定義了 宏名 ENCODE 則編譯後的程序輸出編了碼的。
如果沒有定義這個宏名 則編譯後的程序 按原句輸出。
#include<stdio.h>
// if you have the line, it will output encoded
#define ENCODE
int main()
{
char s[100];
int i;
printf("input one line string:\n");
gets(s);
#ifdef ENCODE
for (i=0;i<strlen(s);i++){
if (s[i]>='a' && s[i]<='z') s[i]=(s[i]-'a'+3) % 26 + 'a';
else if (s[i]>='A' && s[i]<='Z') s[i]=(s[i]-'A'+3) % 26 + 'A';
}
#endif
printf("%s\n",s);
return 0;
}
例如:
input one line string:
ABCD abcd 123 XYZ xyz -- 輸入
DEFG defg 123 ABC abc -- 輸出