100分悬赏!! 请问C语言程式高手一些有关於条件判断的问题!!

作者&投稿:针狄 (若有异议请与网页底部的电邮联系)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct _student
{
char name[16];
int sign;
int val;
} Student;

Student* readFile(FILE* pf, size_t* cnt)
{
size_t n = 8, i = 0;
Student* ps = (Student*)malloc(n * sizeof(Student));
*cnt = 0;
if(ps != 0)
{
while(fscanf(pf, "%s %d %d", ps[i].name, &ps[i].sign, &ps[i].val) != -1)
{
if(++i == n)
{
Student* tmp;
n <<= 1;
if((tmp = (Student*)realloc(ps, n * sizeof(Student))) != 0)
ps = tmp;
else
{
free(ps);
ps = 0;
break;
}
}
}
ps = (Student*)realloc(ps, i * sizeof(Student));
*cnt = i;
}
return ps;
}

void realse(Student* ps)
{
free(ps);
}

int lfind(Student* ps, size_t cnt, Student* tofind)
{
int i = 0;
for( ; i < cnt; i++)
if(strcmp(ps[i].name, tofind->name) == 0)
return i;
return -1;
}

void modify(Student* s1, const Student* s2)
{
int val1 = s1->val, val2 = s2->val;
if(s1->sign == 2) val1 = -val1;
if(s2->sign == 2) val2 = -val2;
val1 += val2;
s1->val = abs(val1);
s1->sign = val1 >=0 ? 1 : 2;
}

void display(Student* ps)
{
printf("%s %d %d\n", ps->name, ps->sign, ps->val);
}

void displayAll(Student* ps, size_t cnt)
{
size_t i = 0;
for( ; i < cnt; i++)
display(ps + i);
}

int main(void)
{
char fname[20];
FILE* pf;
Student* ps;
size_t cnt;
printf("请输入文件名(*.txt)\t");
scanf("%s", fname);
pf = fopen(fname, "r");
if(pf == 0)
printf("文件打开失败\n");
else
{
ps = readFile(pf, &cnt);
fclose(pf);
if(ps != 0)
{
Student tmp;
int i, score, choice;
while(1)
{
printf("请输入姓名 加减分代码 分数\n");
scanf("%s %d %d", tmp.name, &tmp.sign, &tmp.val);
i = lfind(ps, cnt, &tmp);
if(i == -1)
printf("没有找到%s,请重新输入\n", tmp.name);
else
{
modify(ps + i, &tmp);
printf("是否继续?1.是 2.否\n");
scanf("%d", &choice);
if(choice != 1)
break;
}
}
displayAll(ps, cnt);
printf("请输入标准分数\n");
scanf("%d", &score);
for(i = 0; i < cnt; i++)
{
if(ps[i].sign == 2 || (ps[i].sign == 1 && ps[i].val < score))
display(ps + i);
}
realse(ps);
}
else
{
printf("内存不足\n");
}
}
return 0;
}

运行结果

请输入文件名(*.txt) 1.txt
请输入姓名 加减分代码 分数
Bub 1 50
是否继续?1.是 2.否
1
请输入姓名 加减分代码 分数
Ann 2 5
是否继续?1.是 2.否
2
Ryu 1 10
Ken 2 5
Ann 1 10
Bub 1 30
请输入标准分数
30
Ryu 1 10
Ken 2 5
Ann 1 10

#include
<stdio.h>
#include
<string.h>
#include
<stdlib.h>
typedef
struct
_student
{
char
name[16];
int
sign;
int
val;
}
Student;
Student*
readFile(FILE*
pf,
size_t*
cnt)
{
size_t
n
=
8,
i
=
0;
Student*
ps
=
(Student*)malloc(n
*
sizeof(Student));
*cnt
=
0;
if(ps
!=
0)
{
while(fscanf(pf,
"%s
%d
%d",
ps[i].name,
&ps[i].sign,
&ps[i].val)
!=
-1)
{
if(++i
==
n)
{
Student*
tmp;
n
<<=
1;
if((tmp
=
(Student*)realloc(ps,
n
*
sizeof(Student)))
!=
0)
ps
=
tmp;
else
{
free(ps);
ps
=
0;
break;
}
}
}
ps
=
(Student*)realloc(ps,
i
*
sizeof(Student));
*cnt
=
i;
}
return
ps;
}
void
realse(Student*
ps)
{
free(ps);
}
int
lfind(Student*
ps,
size_t
cnt,
Student*
tofind)
{
int
i
=
0;
for(
;
i
<
cnt;
i++)
if(strcmp(ps[i].name,
tofind->name)
==
0)
return
i;
return
-1;
}
void
modify(Student*
s1,
const
Student*
s2)
{
int
val1
=
s1->val,
val2
=
s2->val;
if(s1->sign
==
2)
val1
=
-val1;
if(s2->sign
==
2)
val2
=
-val2;
val1
+=
val2;
s1->val
=
abs(val1);
s1->sign
=
val1
>=0
?
1
:
2;
}
void
display(Student*
ps)
{
printf("%s
%d
%d\n",
ps->name,
ps->sign,
ps->val);
}
void
displayAll(Student*
ps,
size_t
cnt)
{
size_t
i
=
0;
for(
;
i
<
cnt;
i++)
display(ps
+
i);
}
int
main(void)
{
char
fname[20];
FILE*
pf;
Student*
ps;
size_t
cnt;
printf("请输入文件名(*.txt)\t");
scanf("%s",
fname);
pf
=
fopen(fname,
"r");
if(pf
==
0)
printf("文件打失败\n");
else
{
ps
=
readFile(pf,
&cnt);
fclose(pf);
if(ps
!=
0)
{
Student
tmp;
int
i,
score,
choice;
while(1)
{
printf("请输入姓名
加减代码
数\n");
scanf("%s
%d
%d",
tmp.name,
&tmp.sign,
&tmp.val);
i
=
lfind(ps,
cnt,
&tmp);
if(i
==
-1)
printf("没找%s,请重新输入\n",
tmp.name);
else
{
modify(ps
+
i,
&tmp);
printf("否继续?1.
2.否\n");
scanf("%d",
&choice);
if(choice
!=
1)
break;
}
}
displayAll(ps,
cnt);
printf("请输入标准数\n");
scanf("%d",
&score);
for(i
=
0;
i
<
cnt;
i++)
{
if(ps[i].sign
==
2
||
(ps[i].sign
==
1
&&
ps[i].val
<
score))
display(ps
+
i);
}
realse(ps);
}
else
{
printf("内存足\n");
}
}
return
0;
}
运行结
请输入文件名(*.txt)
1.txt
请输入姓名
加减代码

Bub
1
50
否继续?1.
2.否
1
请输入姓名
加减代码

Ann
2
5
否继续?1.
2.否
2
Ryu
1
10
Ken
2
5
Ann
1
10
Bub
1
30
请输入标准数
30
Ryu
1
10
Ken
2
5
Ann
1
10

这个程序本身就没错,恐怕是你一时没想通而已。

C语言程序题 在线等 100分悬赏~

参考一下这个吧,我原来写的,有些错误
我的程序先生成一个素数数组,分解质因数时依次从这个数组里面取元素
但我的程序只限于分解比较小的数,而且会出现程序异常的错误

unknown software exception(0xc0000094),位置为0x004011be
unknown software exception(0xc0000094),位置为0x004011be
unknown software exception(0xc0000027),位置为0x7c80e0b9


#include
#include
#define LB 2
#define UB 100

void create_prime_array(int *p){
int k,i,j;
for(j=LB;j<=UB;j++){
k=sqrt(j);
for(i=2;i<=k;i++) /*i as divisor*/
if(j%i==0) break;
if(i>k) {*p=j;p++;} /*save prime j to an element of array which
pointer p pointed*/
}/*for*/
}

void prime_factor(int a, int *p){
for(;a!=0;p++){
while(1){
if(a%*p==0) {printf("%d",*p);a=a/(*p);}
if(a%*p!=0) break;
}/*repeat*/
}/*for*/
}

void main(){
int x,a[50]={0};
puts("Enter an integer >2");
scanf("%d",&x);
create_prime_array(a);
prime_factor(x,a);
}

clude "string.h"

//考虑到用数据库文件保存注册信息的话要使用access创建文件并且还要配置数据源,所以我的方法是采用将注册信息保存到文件
//下面是完整的程序:

//登陆检测函数
int login(char *name,char *password)
{
char info[10000];
char *p=info;
FILE *file=fopen("user","r");
int size;
if(file)
{
size=fread(info,1,10000,file);
while(size!=(int)p-(int)info)
{
if(!strcmp(p,name)&&!strcmp(p+strlen(p)+1,password))
{
fclose(file);
return 1;
}
p+=strlen(p)+1;
p+=strlen(p)+1;
}
}
fclose(file);
return 0;
}


//添加注册信息入文件
void save(char *name,char *password)
{
FILE *file=fopen("user","a");
fwrite(name,1,strlen(name)+1,file);
fwrite(password,1,strlen(password)+1,file);
fclose(file);
}

#define PASSWORD "12345" //这里指定你要允许通过的密码,比如12345,将引号里的数字改为你想要的即可
int main()
{
char password[100];
char name[100],c[100],password1[100];
tag1: printf("press 1 to register, or 2 to login
");//输入1为注册,输入2为登陆
while(1)
{
gets(c);
if('1'==c[0])
{
printf("please enter your name
");//输入姓名
gets(name);
tag2: printf("please enter your password
");//输入密码
gets(password);
printf("please enter your password again
");
gets(password1);
if(strcmp(password,password1))
{
//两次密码不一致,重输
printf("the password you entered is different from the first one,please try again!
");
goto tag2;
}
printf("register is completed!
");//注册成功
//下面实现将注册信息加入文件保存
save(name,password);
goto tag1;
}
else if('2'==c[0])
{
tag3: printf("please enter your name:
");
gets(name);
printf("please enter your password:
");
gets(password);
if(login(name,password))//如果验证通过,则
{
printf("login successfully!
");

//这里添加成功登陆后要执行的代码
}
else
{
printf("your name or password doesn't exist!
");//否则重输
goto tag3;
}

}
else
{
printf("invalid input!press 1 to register, or 2 to login
");//输入非法,重输
goto tag1;
}

}


return 0;
}

饿,写了我两个小时啊,大哥,分一定要给我啊~~~~~~